-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
81 lines (61 loc) · 2.61 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Schema for the Application and VolunteeringReport models."""
from marshmallow import validate, pre_load, ValidationError, validates
from marshmallow_enum import EnumField
from innopoints.extensions import ma
from innopoints.models import Application, ApplicationStatus, VolunteeringReport, Feedback
# pylint: disable=missing-docstring
class ApplicationSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Application
load_instance = True
ordered = True
include_relationships = True
status = EnumField(ApplicationStatus)
applicant = ma.Nested('AccountSchema', only=('full_name', 'email'))
feedback = ma.Nested('FeedbackSchema', exclude=('time',))
reports = ma.Nested('VolunteeringReportSchema', many=True)
telegram_username = ma.Str(data_key='telegram')
class ActivityProjectSchema(ma.SQLAlchemyAutoSchema):
'''An intermediate schema for VolunteeringReportSchema to get the project name.'''
class Meta:
model = Application
load_instance = True
fields = ('project', 'name')
include_relationships = True
project = ma.Nested('ProjectSchema', only=('name', 'id'))
class ApplicationActivitySchema(ma.SQLAlchemyAutoSchema):
'''An intermediate schema for VolunteeringReportSchema to get the activity name.'''
class Meta:
model = Application
load_instance = True
fields = ('activity',)
include_relationships = True
activity = ma.Nested('ActivityProjectSchema')
class VolunteeringReportSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = VolunteeringReport
load_instance = True
ordered = True
include_fk = True
include_relationships = True
reporter_email = ma.Str(dump_only=True)
application_id = ma.Int(dump_only=True)
application = ma.Nested('ApplicationActivitySchema', data_key='application_on')
rating = ma.Int(validate=validate.Range(min=1, max=5))
class FeedbackSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Feedback
load_instance = True
ordered = True
include_relationships = True
exclude = ('transaction',)
@pre_load
def wrap_competences(self, data, **_kwargs):
"""Turn a flat list of competence IDs into a list of objects."""
data['competences'] = [{'id': comp_id} for comp_id in data['competences']]
return data
@validates('competences')
def ensure_competence_amount(self, competences):
if len(competences) not in range(1, 4):
raise ValidationError('Must include 1-3 competences.')
answers = ma.List(ma.Str(), required=True)