-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathmodels.py
100 lines (72 loc) · 2.51 KB
/
models.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
"""Models for unit testing sandman"""
from flask.ext.admin.contrib.sqla import ModelView
from sandman.model import register, Model, activate
from sandman.model.models import db
class ArtistAdminView(ModelView):
pass
class SomeModel(db.Model):
__tablename__ = 'some_model'
id = db.Column(db.Integer, primary_key=True)
value = db.Column(db.String)
class Artist(Model):
"""Model mapped to the "Artist" table"""
__tablename__ = 'Artist'
__view__ = ArtistAdminView
class MediaType(Model):
"""Model mapped to the "MediaType" table"""
__tablename__ = 'MediaType'
def __str__(self):
"""Return string representation of *self*."""
return self.Name
class Track(Model):
"""Model mapped to the "Artist" table"""
__tablename__ = 'Track'
@staticmethod
# pylint: disable=invalid-name
def validate_PUT(resource=None):
"""Return False if request should not be processed.
:param resource: resource related to current request
:type resource: :class:`sandman.model.Model` or None
"""
if int(resource.TrackId) == 999:
return False
return True
class Album(Model):
"""Model mapped to the "Album" table
Only supports HTTP methods specified.
"""
__tablename__ = 'Album'
__methods__ = ('POST', 'PATCH', 'DELETE', 'PUT', 'GET')
__top_level_json_name__ = 'Albums'
def __str__(self):
"""Return string representation of *self*."""
return self.Title
class Playlist(Model):
"""Model mapped to the "Playlist" table
Only supports HTTP methods specified.
"""
__tablename__ = 'Playlist'
__methods__ = ('POST', 'PATCH')
class Style(Model):
"""Model mapped to the "Genre" table
Has a custom endpoint ("styles" rather than the default, "genres").
Only supports HTTP methods specified.
Has a custom validator for the GET method.
"""
__tablename__ = 'Genre'
__endpoint__ = 'styles'
__methods__ = ('GET', 'DELETE')
@staticmethod
# pylint: disable=invalid-name
def validate_GET(resource=None):
"""Return False if the request should not be processed.
:param resource: resource related to current request
:type resource: :class:`sandman.model.Model` or None
"""
if isinstance(resource, list):
return True
elif resource and resource.GenreId == 1:
return False
return True
register((Artist, Album, Playlist, Track, MediaType, Style, SomeModel))
activate(browser=True)