Skip to content

Commit e0ddfb0

Browse files
authored
Merge pull request #4 from brionmario/develop
Implement Application and Sessions routes
2 parents 7ae90c0 + 7965cd6 commit e0ddfb0

24 files changed

+653
-65
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "venv/bin/python3.7"
3+
}

CONTRIBUTING.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ The scope should be the name of the npm package affected (as perceived by the pe
2323

2424
The following is the list of supported scopes:
2525

26-
- **config**
26+
- **core**
2727
- **common**
28+
- **app**
29+
- **tests**
30+
- **models**
31+
- **views**
32+
- **manager**
2833
- **vcs**
29-
- **core**
30-
- **routes**
34+
- **deps**
3135
- **migrations**
36+
- **config**

app/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,26 @@ def create_app(config_name):
1717
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # disabling sqlalchemy event system
1818

1919
CONFIG[config_name].init_app(app)
20+
2021
root = CONFIG[config_name].APPLICATION_ROOT
2122

23+
# flask migrate doesn't recognize the tables without this import
24+
from app.models import Application, Genre, ApplicationType, Session, Questionnaire
25+
2226
# Set up extensions
2327
db.init_app(app)
2428

2529
# Create app blueprints
26-
from .main import main as main_blueprint
30+
from app.routes.v1 import main as main_blueprint
2731
app.register_blueprint(main_blueprint, url_prefix=root + '/')
2832

29-
from .applications import applications as applications_blueprint
30-
app.register_blueprint(applications_blueprint, url_prefix=root + '/applications')
33+
from app.routes.v1 import application as application_blueprint
34+
app.register_blueprint(application_blueprint, url_prefix=root + '/application')
35+
36+
from app.routes.v1 import session as session_blueprint
37+
app.register_blueprint(session_blueprint, url_prefix=root + '/session')
38+
39+
from app.routes.v1 import questionnaire as questionnaire_blueprint
40+
app.register_blueprint(questionnaire_blueprint, url_prefix=root + '/questionnaire')
3141

3242
return app

app/applications/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/applications/views.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/main/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/models/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
"""
55

66
from .application import * # noqa
7+
from .application_type import * # noqa
8+
from .genre import * # noqa
9+
from .session import * # noqa
10+
from .questionnaire import * # noqa

app/models/application.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
1+
from marshmallow import fields, validate
2+
from .application_type import ApplicationTypeSchema
3+
from .genre import GenreSchema
14
from .. import db, ma
25

36

47
class Application(db.Model):
5-
__tablename__ = 'applications'
8+
__tablename__ = 'application'
9+
610
id = db.Column(db.Integer, primary_key=True)
7-
name = db.Column(db.String(250), nullable=False)
11+
name = db.Column(db.String(100), nullable=False)
12+
identifier = db.Column(db.String(100), nullable=False)
13+
developer = db.Column(db.String(100), nullable=False)
14+
type_id = db.Column(db.Integer, db.ForeignKey('application_type.id', use_alter=True, name='fk_type_id'), nullable=False)
15+
description = db.Column(db.String(250), nullable=False)
816
creation_date = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), nullable=False)
9-
genre = db.Column(db.String(250), nullable=False)
17+
genre_id = db.Column(db.Integer, db.ForeignKey('genre.id', use_alter=True, name='fk_genre_id'), nullable=False)
18+
sessions = db.relationship('Session', backref='app', lazy='dynamic')
1019

11-
def __init__(self, name, genre):
20+
def __init__(self, name, identifier, developer, type, description, genre):
1221
self.name = name
22+
self.identifier = identifier
23+
self.developer = developer
24+
self.type = type
25+
self.description = description
1326
self.genre = genre
1427

28+
def __repr__(self):
29+
return '<Application %r>' % self.id
30+
1531

1632
class ApplicationSchema(ma.Schema):
17-
class Meta:
18-
fields = ('id', 'name', 'creation_date', 'genre')
33+
id = fields.Integer(dump_only=True)
34+
name = fields.String(required=True, validate=validate.Length(1, 100))
35+
identifier = fields.String()
36+
developer = fields.String(required=True, validate=validate.Length(1, 100))
37+
type = fields.Nested(ApplicationTypeSchema, dump_only=True)
38+
description = fields.String(required=True, validate=validate.Length(1, 250))
39+
creation_date = fields.DateTime()
40+
genre = fields.Nested(GenreSchema, dump_only=True)

app/models/application_type.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import json
3+
from marshmallow import fields, validate
4+
from .. import db, ma
5+
6+
APPLICATION_TYPE_META_FILE_PATH = 'meta/application_type.meta.json'
7+
8+
9+
class ApplicationType(db.Model):
10+
__tablename__ = 'application_type'
11+
12+
id = db.Column(db.Integer, primary_key=True)
13+
name = db.Column(db.String(50), nullable=False)
14+
display_name = db.Column(db.String(100), nullable=False)
15+
display_name_full = db.Column(db.String(250), nullable=False)
16+
applications = db.relationship('Application', backref='type', lazy='dynamic')
17+
18+
def __init__(self, name, display_name, display_name_full):
19+
self.name = name
20+
self.display_name = display_name
21+
self.display_name_full = display_name_full
22+
23+
@classmethod
24+
def seed(cls):
25+
if cls.is_table_empty(cls):
26+
if os.path.exists(APPLICATION_TYPE_META_FILE_PATH):
27+
with open(APPLICATION_TYPE_META_FILE_PATH) as app_type_meta_json:
28+
data = json.load(app_type_meta_json)
29+
for item in data['types']:
30+
app_type = ApplicationType(name=item['name'], display_name=item['display_name'], display_name_full=item['display_name_full'])
31+
app_type.save()
32+
print("Adding application type metadata: {}".format(app_type))
33+
else:
34+
# TODO: Add exception
35+
print("Couldn't locate meta file")
36+
else:
37+
print('Table is already filled')
38+
39+
def save(self):
40+
db.session.add(self)
41+
db.session.commit()
42+
43+
def is_table_empty(self):
44+
if not self.query.all():
45+
return True
46+
return False
47+
48+
def __repr__(self):
49+
return '<ApplicationType %r>' % self.id
50+
51+
52+
class ApplicationTypeSchema(ma.Schema):
53+
id = fields.Integer()
54+
name = fields.String(required=True)
55+
display_name = fields.String(required=True)
56+
display_name_full = fields.String(required=True)

app/models/genre.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import json
3+
from marshmallow import fields
4+
from .. import db, ma
5+
6+
GENRE_META_FILE_PATH = 'meta/genre.meta.json'
7+
8+
9+
class Genre(db.Model):
10+
__tablename__ = 'genre'
11+
12+
id = db.Column(db.Integer, primary_key=True)
13+
name = db.Column(db.String(100), nullable=False)
14+
display_name = db.Column(db.String(250), nullable=False)
15+
applications = db.relationship('Application', backref='genre', lazy='dynamic')
16+
17+
def __init__(self, name, display_name):
18+
self.name = name
19+
self.display_name = display_name
20+
21+
@classmethod
22+
def seed(cls):
23+
if cls.is_table_empty(cls):
24+
if os.path.exists(GENRE_META_FILE_PATH):
25+
with open(GENRE_META_FILE_PATH) as genre_meta_json:
26+
data = json.load(genre_meta_json)
27+
for item in data['genre']:
28+
genre = Genre(name=item['name'], display_name=item['display_name'])
29+
genre.save()
30+
print("Adding genre metadata: {}".format(genre))
31+
else:
32+
# TODO: Add exception
33+
print("Couldn't locate meta file")
34+
else:
35+
print('Table is already filled')
36+
37+
def save(self):
38+
db.session.add(self)
39+
db.session.commit()
40+
41+
def is_table_empty(self):
42+
if not self.query.all():
43+
return True
44+
return False
45+
46+
def __repr__(self):
47+
return '<Genre %r>' % self.id
48+
49+
50+
class GenreSchema(ma.Schema):
51+
id = fields.Integer()
52+
name = fields.String(required=True)
53+
display_name = fields.String(required=True)

0 commit comments

Comments
 (0)