forked from rxuriguera/plays-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.py
executable file
·114 lines (82 loc) · 2.94 KB
/
schemas.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
from datetime import timedelta
from marshmallow import fields, post_load
from . import ma
from .models import Channel, Performer, Song, PlayByChannel, PlayBySong
class PlaysBaseSchema(ma.Schema):
@post_load
def make(self, data):
if self.Meta.model:
return self.Meta.model(**data)
else:
return None
class Meta:
model = None
class ChannelSchema(PlaysBaseSchema):
name = fields.String(required=True)
class Meta:
model = Channel
fields = ('name',)
class PerformerSchema(PlaysBaseSchema):
name = fields.String(required=True)
class Meta:
model = Performer
fields = ('name',)
class SongSchema(PlaysBaseSchema):
title = fields.String(required=True)
performer = fields.String(required=True)
class Meta:
model = Song
fields = ('title', 'performer')
class PlaySchema(PlaysBaseSchema):
title = fields.String(required=True)
performer = fields.String(required=True)
start = fields.DateTime(required=True)
end = fields.DateTime(required=True)
channel = fields.String(required=True)
class PlayByChannelSchema(PlaySchema):
class Meta:
model = PlayByChannel
fields = ('channel', 'start', 'end', 'title', 'performer')
class PlayBySongSchema(PlaySchema):
class Meta:
model = PlayBySong
fields = ('title', 'performer', 'start', 'end', 'channel')
class GetRequestSchema(ma.Schema):
title = fields.Str(required=True)
performer = fields.Str(required=True)
channel = fields.Str(required=True)
start = fields.DateTime(required=True)
end = fields.DateTime()
limit = fields.Integer()
@post_load
def make(self, data):
if 'end' not in data:
data['end'] = data['start'] + timedelta(days=7)
if 'limit' not in data:
data['limit'] = 40
return data
def load(self, data, many=None, partial=None, required=None):
# Foir the sake of clarity, allow to specify which fields are required
# instead of the ones that are not
if required:
fld = set(GetRequestSchema.Meta.fields)
req = set(required)
partial = fld.difference(req)
return super(GetRequestSchema, self).load(data=data, many=many, partial=partial)
class Meta:
fields = ('title', 'performer', 'channel', 'start', 'end', 'limit')
class ChannelsParameterSchema(ma.Schema):
channels = fields.List(fields.Str(), required=True)
@post_load
def make(self, data):
return data['channels']
class Meta:
fields = ('channels', )
channel_schema = ChannelSchema()
performer_schema = PerformerSchema()
song_schema = SongSchema()
play_by_channel_schema = PlayByChannelSchema()
play_by_song_schema = PlayBySongSchema()
request_schema = GetRequestSchema()
channels_param_schema = ChannelsParameterSchema()