-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataModels.py
187 lines (154 loc) · 6.84 KB
/
dataModels.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
# dataModels.py: Definitions of GAE Data Store models.
##
# © 2012 Steven Casagrande ([email protected]) and
# Christopher E. Granade ([email protected]).
# This file is a part of the ThingPool Server project.
# Licensed under the AGPL version 3.
##
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
## TODO ########################################################################
# Change all instances of "id" in __api__ methods to "uri". This has not been
# done yet, as it depends on a final decision about API paths.
## IMPORTS #####################################################################
## GAE API ##
from google.appengine.ext import db
from google.appengine.ext.db import polymodel
## PYTHON STANDARD LIBRARY ##
import json
## THINGPOOL MODULES ##
import security as s
from index import *
## DATA MODELS #################################################################
class Person(db.Model):
user_account = db.UserProperty(required=True) # Used to store User account
permissions = db.IntegerProperty(required=True) # Six levels of permissions: Admin, manager, kiosk, user, requested, banned
keycard = db.StringProperty(required=False) # TODO: Make sure this is unique
@property
def uri(self):
app.url_for('user', user_id=self.key().id())
@property
def can_query_user(self):
# TODO: check that this is how you query a property of Person, or if
# we need to run a query here.
# TODO: check if is a GAE admin, in which case all other checks are bypassed.
# Consider adding that check as a decorator.
return self.permissions >= s.USER_STATUS_USER
@property
def can_access_admin(self):
# Some admin functions disabled for managers that aren't admins,
# but they should still have access to the console.
return self.permissions >= s.USER_STATUS_MANAGER
@staticmethod
def get_person(user=None, keycard=None):
if user is not None and keycard is None:
user = Person.all().filter("user_account = ",user)
elif user is None and keycard is not None:
user = Person.all().filter('keycard = ', keycard)
else:
user = users.get_current_user()
return user.get()
def __api__(self):
return {
'id': self.key().id(),
'keycard': self.keycard,
'nickname': self.user_account.nickname(),
'g_account': self.user_account.email(),
'role': s.USER_STATUS_DESCRIPTIONS[self.permissions]
}
class Category(db.Model):
name = db.StringProperty(required=True)
category_parent = db.SelfReferenceProperty()
# We don't use the GAE built-in parent/child relation support here,
# as that association is permenant, and we want user reconfigurable
# categories.
@property
def uri(self):
# This is not yet implemented on the API side.
app.url_for('category', category_id=self.key().id())
def __api__(self):
return {
'id': self.key().id(),
'name': self.name,
'parent': self.category_parent.key().id() # FIXME: make this a URI.
}
class Item(polymodel.PolyModel):
name = db.StringProperty(required=True) # Primary name of item (book title, product number, etc)
name2 = db.StringProperty() # Secondary name to search on (eg author name, type of equipment)
content = db.StringProperty(multiline=True) # Storage of supplimentary details (eg brief description/specs)
category = db.ReferenceProperty(Category, required=True) # Which type/category item is (book, electronics, tool, etc)
store_location = db.StringProperty() # Where should the Thing be stored?
@property
def uri(self):
app.url_for('item', item_id=self.key().id())
def __api__(self):
data = {
'id': self.key().id(),
'name': self.name,
'category': self.category.uri
}
if self.name2 is not None:
data['subname'] = self.name2
if self.content is not None:
data['content'] = self.content
if self.store_location is not None:
data['store_location'] = self.store_location
return data
@property
def is_checked_out(self):
q = CheckoutTransaction.all().filter('item =', self).filter('checkin_date =', None)
return q.get() is not None
@property
def is_requested(self):
q = RequestTransaction.all().filter('item =', self).filter('resolved_date =', None)
return q.get() is not None
class BookItem(Item):
isbn = db.StringProperty(required=True) # Must be a string to support checksum-10 digit "X".
author = db.StringProperty(required=True)
def __api__(self):
data = super(BookItem, self).__api__()
data['isbn'] = self.isbn
data['author'] = self.author
return data
class CheckoutTransaction(db.Model):
item = db.ReferenceProperty(Item, required=True)
holder = db.ReferenceProperty(Person, required=True) # Who currently has this item checked out
checkout_date = db.DateTimeProperty(auto_now_add=True) # Record checkout date
checkin_date = db.DateTimeProperty(auto_now_add=False)
def __api__(self):
data = {
'item': self.item.uri,
'holder': self.holder.uri,
'checkout_date': self.checkout_date
}
if self.checkin_date is not None:
data['checkin_date'] = self.checkin_date
return data
class RequestTransaction(db.Model):
item = db.ReferenceProperty(Item, required=True)
requestor = db.ReferenceProperty(Person, required=True) # If requested, by who
request_date = db.DateTimeProperty(auto_now_add=True) # Record request date
resolved_date = db.DateTimeProperty(auto_now_add=False)
def __api__(self):
data = {
'item': self.item.uri,
'requestor': self.requestor.uri,
'request_date': self.request_date
}
if self.resolved_date is not None:
data['resolved_date'] = self.resolved_date
return data