-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_model.py
78 lines (61 loc) · 2.04 KB
/
base_model.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
"""Base application database model class.
Provides the base for application models.
Defines the base required fields.
Defines the base required functions of application object classes to be synced.
Usage:
See app_model.py
License:
The MIT License (MIT), see LICENSE.txt for more details.
Copyright:
Copyright (c) 2014 Steven Tucker and Gavin Kromhout.
"""
from six import iterkeys
from schematics.models import Model
from schematics.types import LongType, BooleanType
SELECT = 'SELECT'
INSERT = 'INSERT'
INTO = 'INTO'
VALUES = 'VALUES'
FROM = 'FROM'
WHERE = 'WHERE'
SEP = ',\n '
class BaseAppModel(Model):
"""Base application model class.
Application model classes must inherit from this class."""
# Required fields.
# Do not override in subclasses.
rowid = LongType()
originClientId = LongType()
originClientObjectId = LongType()
lastUpdatedByClientId = LongType()
ownerUserId = LongType()
lastSync = LongType()
deleted = BooleanType(default=0)
def columns(self):
columns = self.keys()
columns.remove('rowid')
columns.insert(0, 'id')
return columns
def select_by_id(self):
return '\n'.join([SELECT,
' ' + SEP.join(self.columns()),
FROM,
' ' + self.__class__.__name__,
WHERE,
' ' + 'id = %s'])
def select_by_id_params(self):
return self.rowid,
def insert(self):
table = self.__class__.__name__
columns = self.keys()
columns.remove('rowid')
values = ('%s' for _ in xrange(len(columns)))
return '\n'.join([INSERT + ' ' + INTO + ' ' + table + ' (',
' ' + SEP.join(columns),
')',
VALUES + ' ( ',
' ' + ', '.join(values),
')'])
def insert_params(self):
return tuple(
self.get(k) for k in iterkeys(self._fields) if k is not 'rowid')