Skip to content

Commit 4cec396

Browse files
authored
Merge pull request #134 from spaceshipearth/mission_serializer
mission: add basic serializer
2 parents 5da3c89 + e8a02d8 commit 4cec396

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ redis==2.10.6
5151
requests==2.21.0
5252
rsa==4.0
5353
sendgrid==6.0.4
54+
serpy==0.3.1
5455
six==1.12.0
5556
SQLAlchemy==1.3.4
5657
structlog==19.1.0
57-
-e git+https://github.com/spaceshipearth/tasktiger.git@exceptions_as_context#egg=tasktiger
58+
-e git+https://github.com/spaceshipearth/tasktiger.git@2f159cec740acc8035da136dfa4253723a40886d#egg=tasktiger
5859
typed-ast==1.3.1
5960
urllib3==1.24.2
6061
webassets==0.12.1

spaceship/goals.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
import serpy
23

34
GOALS_BY_CATEGORY = {
45
"diet": {
@@ -153,6 +154,14 @@
153154
}
154155
}
155156

157+
class GoalSerializer(serpy.DictSerializer):
158+
name = serpy.StrField()
159+
icon = serpy.StrField()
160+
alt = serpy.StrField()
161+
short_description = serpy.StrField()
162+
description = serpy.StrField()
163+
category = serpy.StrField()
164+
156165
# let goals know their category
157166
for category_name, category_info in GOALS_BY_CATEGORY.items():
158167
category_info["name"] = category_name

spaceship/models/base.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11

2+
import serpy
23
from sqlalchemy.exc import DatabaseError
34

45
from flask_sqlalchemy.model import Model
56

67
from spaceship.db import db
78

89
class Base(Model):
10+
class Serializer(serpy.Serializer):
11+
id = serpy.IntField()
12+
13+
def serialize(self):
14+
return self.Serializer(self).data
15+
916
@property
1017
def session(self):
1118
return db.session

spaceship/models/goal.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
import pendulum
3+
import serpy
34

45
from spaceship.db import db
6+
from spaceship.goals import GOALS_BY_CATEGORY, GoalSerializer
57
from spaceship.models.custom_fields import PendulumDateTimeField
68
from sqlalchemy.ext.hybrid import hybrid_property
79

@@ -16,6 +18,16 @@ class Goal(db.Model):
1618
visible_after = db.Column(PendulumDateTimeField(), nullable=True)
1719
hidden_after = db.Column(PendulumDateTimeField(), nullable=True)
1820

21+
class Serializer(db.Model.Serializer):
22+
short_description = serpy.StrField()
23+
category = serpy.StrField()
24+
actual_goal = GoalSerializer()
25+
26+
@property
27+
def actual_goal(self):
28+
cat_goals = GOALS_BY_CATEGORY[self.category]
29+
return [g for g in cat_goals['goals'] if g['name'] == self.short_description][0]
30+
1931
@hybrid_property
2032
def is_visible(self):
2133
now = pendulum.now('UTC')

spaceship/models/mission.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import pendulum
2+
import serpy
23
from sqlalchemy.ext.hybrid import hybrid_property
34
from sqlalchemy.ext.associationproxy import association_proxy
45
import uuid
56

67
from spaceship.db import db
7-
from spaceship.models import MissionGoal
8+
from spaceship.models import MissionGoal, Goal
89
from spaceship.models.custom_fields import PendulumDateTimeField
910

1011
class Mission(db.Model):
@@ -27,6 +28,14 @@ class Mission(db.Model):
2728
created_at = db.Column(PendulumDateTimeField(), default=lambda: pendulum.now('UTC'))
2829
deleted_at = db.Column(PendulumDateTimeField(), nullable=True)
2930

31+
class Serializer(db.Model.Serializer):
32+
uuid = serpy.StrField()
33+
title = serpy.StrField()
34+
short_description = serpy.StrField()
35+
duration_in_weeks = serpy.IntField()
36+
37+
goals = Goal.Serializer(many=True)
38+
3039
@property
3140
def is_deleted(self):
3241
return self.deleted_at != None

0 commit comments

Comments
 (0)