Activities: database model init
This commit is contained in:
parent
f4f8dc9ea2
commit
3f0b3844d4
2
Makefile
2
Makefile
@ -29,7 +29,7 @@ lint-react:
|
|||||||
$(NPM) lint
|
$(NPM) lint
|
||||||
|
|
||||||
migrate-db:
|
migrate-db:
|
||||||
$(FLASK) db migrate
|
$(FLASK) db migrate --directory $(MIGRATIONS)
|
||||||
|
|
||||||
serve-python:
|
serve-python:
|
||||||
$(FLASK) run --with-threads -h $(HOST) -p $(API_PORT)
|
$(FLASK) run --with-threads -h $(HOST) -p $(API_PORT)
|
||||||
|
@ -4,6 +4,7 @@ CLIENT_PORT = 3000
|
|||||||
|
|
||||||
export REACT_APP_API_URL = http://$(HOST):$(API_PORT)
|
export REACT_APP_API_URL = http://$(HOST):$(API_PORT)
|
||||||
export FLASK_APP = $(PWD)/mpwo_api/server.py
|
export FLASK_APP = $(PWD)/mpwo_api/server.py
|
||||||
|
export FLASK_DEBUG = 1
|
||||||
export TEST_URL = http://$(HOST):$(CLIENT_PORT)
|
export TEST_URL = http://$(HOST):$(CLIENT_PORT)
|
||||||
export REQUIREMENTS = $(PWD)/mpwo_api/requirements.txt
|
export REQUIREMENTS = $(PWD)/mpwo_api/requirements.txt
|
||||||
export DATABASE_URL = postgres://mpwo:mpwo@$(HOST):5432/mpwo
|
export DATABASE_URL = postgres://mpwo:mpwo@$(HOST):5432/mpwo
|
||||||
|
56
mpwo_api/migrations/versions/b7cfe0c17708_.py
Normal file
56
mpwo_api/migrations/versions/b7cfe0c17708_.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: b7cfe0c17708
|
||||||
|
Revises: 9741fc7834da
|
||||||
|
Create Date: 2018-01-21 17:24:52.587814
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'b7cfe0c17708'
|
||||||
|
down_revision = '9741fc7834da'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('sports',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('label', sa.String(length=50), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('label')
|
||||||
|
)
|
||||||
|
op.create_table('activities',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('sport_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('gpx', sa.String(length=255), nullable=True),
|
||||||
|
sa.Column('creation_date', sa.DateTime(), nullable=True),
|
||||||
|
sa.Column('modification_date', sa.DateTime(), nullable=True),
|
||||||
|
sa.Column('activity_date', sa.DateTime(), nullable=False),
|
||||||
|
sa.Column('duration', sa.Interval(), nullable=False),
|
||||||
|
sa.Column('pauses', sa.Interval(), nullable=True),
|
||||||
|
sa.Column('moving', sa.Interval(), nullable=True),
|
||||||
|
sa.Column('distance', sa.Numeric(precision=5, scale=2), nullable=True),
|
||||||
|
sa.Column('min_alt', sa.Numeric(precision=5, scale=2), nullable=True),
|
||||||
|
sa.Column('max_alt', sa.Numeric(precision=5, scale=2), nullable=True),
|
||||||
|
sa.Column('descent', sa.Numeric(precision=5, scale=2), nullable=True),
|
||||||
|
sa.Column('ascent', sa.Numeric(precision=5, scale=2), nullable=True),
|
||||||
|
sa.Column('max_speed', sa.Numeric(precision=5, scale=2), nullable=True),
|
||||||
|
sa.Column('ave_speed', sa.Numeric(precision=5, scale=2), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['sport_id'], ['sports.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('activities')
|
||||||
|
op.drop_table('sports')
|
||||||
|
# ### end Alembic commands ###
|
@ -25,9 +25,11 @@ migrate.init_app(app, db)
|
|||||||
|
|
||||||
from .users.auth import auth_blueprint # noqa
|
from .users.auth import auth_blueprint # noqa
|
||||||
from .users.users import users_blueprint # noqa
|
from .users.users import users_blueprint # noqa
|
||||||
|
from .activities.activities import activities_blueprint # noqa
|
||||||
|
|
||||||
app.register_blueprint(users_blueprint, url_prefix='/api')
|
app.register_blueprint(users_blueprint, url_prefix='/api')
|
||||||
app.register_blueprint(auth_blueprint, url_prefix='/api')
|
app.register_blueprint(auth_blueprint, url_prefix='/api')
|
||||||
|
app.register_blueprint(activities_blueprint, url_prefix='/api')
|
||||||
|
|
||||||
if app.debug:
|
if app.debug:
|
||||||
logging.getLogger('sqlalchemy').setLevel(logging.WARNING)
|
logging.getLogger('sqlalchemy').setLevel(logging.WARNING)
|
||||||
|
0
mpwo_api/mpwo_api/activities/__init__.py
Normal file
0
mpwo_api/mpwo_api/activities/__init__.py
Normal file
29
mpwo_api/mpwo_api/activities/activities.py
Normal file
29
mpwo_api/mpwo_api/activities/activities.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from flask import Blueprint, jsonify
|
||||||
|
|
||||||
|
from ..users.utils import authenticate
|
||||||
|
from .models import Activity
|
||||||
|
|
||||||
|
activities_blueprint = Blueprint('activities', __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@activities_blueprint.route('/activities', methods=['GET'])
|
||||||
|
@authenticate
|
||||||
|
def get_activities(user_id):
|
||||||
|
"""Get all activities"""
|
||||||
|
activities = Activity.query.all()
|
||||||
|
activities_list = []
|
||||||
|
for activity in activities:
|
||||||
|
activity_object = {
|
||||||
|
'id': activity.id,
|
||||||
|
'user_id': activity.user_id,
|
||||||
|
'sport_id': activity.sport_id,
|
||||||
|
'creation_date': activity.creation_date
|
||||||
|
}
|
||||||
|
activities_list.append(activity_object)
|
||||||
|
response_object = {
|
||||||
|
'status': 'success',
|
||||||
|
'data': {
|
||||||
|
'activities': activities_list
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return jsonify(response_object), 200
|
57
mpwo_api/mpwo_api/activities/models.py
Normal file
57
mpwo_api/mpwo_api/activities/models.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
|
from mpwo_api import db
|
||||||
|
|
||||||
|
|
||||||
|
class Sport(db.Model):
|
||||||
|
__tablename__ = "sports"
|
||||||
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
|
label = db.Column(db.String(50), unique=True, nullable=False)
|
||||||
|
activities = db.relationship('Activity',
|
||||||
|
lazy=True,
|
||||||
|
backref=db.backref('sports', lazy='joined'))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.label
|
||||||
|
|
||||||
|
def __init__(self, label):
|
||||||
|
self.label = label
|
||||||
|
|
||||||
|
|
||||||
|
class Activity(db.Model):
|
||||||
|
__tablename__ = "activities"
|
||||||
|
id = db.Column(
|
||||||
|
db.Integer,
|
||||||
|
primary_key=True,
|
||||||
|
autoincrement=True)
|
||||||
|
user_id = db.Column(
|
||||||
|
db.Integer,
|
||||||
|
db.ForeignKey('users.id'),
|
||||||
|
nullable=False)
|
||||||
|
sport_id = db.Column(
|
||||||
|
db.Integer,
|
||||||
|
db.ForeignKey('sports.id'),
|
||||||
|
nullable=False)
|
||||||
|
gpx = db.Column(db.String(255), nullable=True)
|
||||||
|
creation_date = db.Column(db.DateTime, default=datetime.datetime.now)
|
||||||
|
modification_date = db.Column(db.DateTime, onupdate=datetime.datetime.now)
|
||||||
|
activity_date = db.Column(db.DateTime, nullable=False)
|
||||||
|
duration = db.Column(db.Interval, nullable=False)
|
||||||
|
pauses = db.Column(db.Interval, nullable=True)
|
||||||
|
moving = db.Column(db.Interval, nullable=True)
|
||||||
|
distance = db.Column(db.Numeric(5, 2), nullable=True)
|
||||||
|
min_alt = db.Column(db.Numeric(5, 2), nullable=True)
|
||||||
|
max_alt = db.Column(db.Numeric(5, 2), nullable=True)
|
||||||
|
descent = db.Column(db.Numeric(5, 2), nullable=True)
|
||||||
|
ascent = db.Column(db.Numeric(5, 2), nullable=True)
|
||||||
|
max_speed = db.Column(db.Numeric(5, 2), nullable=True)
|
||||||
|
ave_speed = db.Column(db.Numeric(5, 2), nullable=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.sport.label + \
|
||||||
|
" - " + self.activity_date.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
def __init__(self, user_id, sport_id, activity_date):
|
||||||
|
self.user_id = user_id
|
||||||
|
self.sport_id = sport_id
|
||||||
|
self.activity_date = activity_date
|
@ -19,6 +19,9 @@ class User(db.Model):
|
|||||||
location = db.Column(db.String(80), nullable=True)
|
location = db.Column(db.String(80), nullable=True)
|
||||||
bio = db.Column(db.String(200), nullable=True)
|
bio = db.Column(db.String(200), nullable=True)
|
||||||
picture = db.Column(db.String(255), nullable=True)
|
picture = db.Column(db.String(255), nullable=True)
|
||||||
|
activities = db.relationship('Activity',
|
||||||
|
lazy=True,
|
||||||
|
backref=db.backref('users', lazy='joined'))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<User %r>' % self.username
|
return '<User %r>' % self.username
|
||||||
|
Loading…
Reference in New Issue
Block a user