-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdatabase.py
More file actions
321 lines (282 loc) · 9.18 KB
/
database.py
File metadata and controls
321 lines (282 loc) · 9.18 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from mongoengine import (
Document,
EmbeddedDocument,
StringField,
BooleanField,
DateTimeField,
ReferenceField,
IntField,
ListField,
EmbeddedDocumentField,
)
from math import exp
import pendulum
from flask import flash
from config import TZ, ENABLE_ACHIEVEMENTS
class AchievementDocument(Document):
""" Wrapper class to attach achievements to documents.
On every save, each function which has been decorated as an achievement
will be called. This way, possible achievements can be evaluated.
"""
@classmethod
def achievement_function(cls, f):
cls.achievement_functions = getattr(cls, 'achievement_functions', []) + [f]
return f
def save(self, *args, **kwargs):
if ENABLE_ACHIEVEMENTS and type(self).objects.count() > 0:
# most (all?) achievements only work if there are any instances
# in the database
for f in getattr(self, 'achievement_functions', []):
f(self)
return super().save(*args, **kwargs)
meta = {
'abstract': True,
}
class Achievement(EmbeddedDocument):
title = StringField()
description = StringField()
key = StringField()
date = DateTimeField(default=pendulum.now)
validUntil = DateTimeField(default=lambda: pendulum.now().add(days=7))
class AchievementDescriptions(Document):
key = StringField()
title = StringField()
validDays = IntField()
descriptions = ListField(StringField())
class Transaction(AchievementDocument):
date = DateTimeField(default=pendulum.now)
description = StringField(null=True)
diff = IntField()
user = ReferenceField('User', null=True)
def __str__(self):
return self.description
@staticmethod
def aggregateDaily(objectSelector):
groupStage = {
'$group': {
'_id': {
'$dateToString': {
'format': '%Y-%m-%d',
'date': '$date',
}
},
'total': {
'$sum': '$diff',
},
}
}
sortStage = {
'$sort': {
'_id': 1,
},
}
return objectSelector.aggregate(groupStage, sortStage)
@staticmethod
def dailyTransactions():
return list(Transaction.aggregateDaily(Transaction.objects))
@staticmethod
def dailyExpenses():
return list(Transaction.aggregateDaily(Transaction.objects(user=None)))
class Consumption(AchievementDocument):
date = DateTimeField(default=pendulum.now)
units = IntField(default=1)
price_per_unit = IntField()
user = ReferenceField('User')
def __str__(self):
return '{}\'s consumtion of {} units, {} each'.format(
self.user.name,
self.units,
self.price_per_unit,
)
@staticmethod
def dailyConsumptions():
groupStage = {
'$group': {
'_id': {
'$dateToString': {
'format': '%Y-%m-%d',
'date': '$date'
},
},
'total': {
'$sum': {
'$multiply': ['$units', '$price_per_unit'],
}
}
}
}
sortStage = {
'$sort': {
'_id': 1
}
}
return list(Consumption.objects.aggregate(groupStage, sortStage))
class Service(AchievementDocument):
date = DateTimeField()
service_count = IntField(default=1)
user = ReferenceField('User')
master = BooleanField(default=True)
cleaned = BooleanField(default=False)
cleaning_program = BooleanField(default=False)
decalcify_program = BooleanField(default=False)
@staticmethod
def current():
return (Service
.objects(date__lte=pendulum.now(TZ), master=True)
.order_by('-date')
.first()
)
@staticmethod
def upcoming():
return list(Service.objects.aggregate(
{
'$match': {
'date': {
'$gte': pendulum.now(TZ),
},
'master': {
'$eq': True,
},
},
},
{
'$group': {
'_id': {
'$dateToString': {
'date': '$date',
'format': '%Y%V',
}
},
'user': {
'$first': '$user'
}
}
},
{
'$sort': {
'_id': 1,
},
}
))
class User(Document):
username = StringField(required=True, unique=True)
name = StringField()
email = StringField()
active = BooleanField(default=True)
vip = BooleanField(default=False)
admin = BooleanField(default=False)
achievements = ListField(EmbeddedDocumentField(Achievement))
@property
def is_authenticated(self):
return True
def get_id(self):
return self.username
@property
def is_anonymous(self):
return False
@property
def is_active(self):
return self.active
@property
def balance(self):
return self.payments - self.consume
@property
def payments(self):
return self.backref(
{'$sum': '$diff'},
Transaction
)
@property
def consume(self):
return self.backref(
{'$sum': {'$multiply': ['$units', '$price_per_unit']}},
Consumption
)
def consumption_list(self):
match = {'$match': {'user': self.id}}
# id will be an integer representing YYYYmmdd
group_id = {
'$dateToString': {
'date': '$date',
'format': '%Y%m%d',
}
}
consume_pipeline = [
match,
{
'$group': {
'_id': group_id,
'diff': {
'$sum': {'$multiply': [-1, '$units', '$price_per_unit']},
},
},
},
]
transaction_pipeline = [
match,
{'$group': {
'_id': group_id,
'diff': {
'$sum': '$diff'
}}},
]
cs = list(Consumption.objects.aggregate(*consume_pipeline))
ts = list(Transaction.objects.aggregate(*transaction_pipeline))
sorted_result = sorted(cs + ts, key=lambda t: t['_id'])
return [{'amount': t['diff'],
'date': pendulum.from_format(t['_id'], 'YYYYMMDD').to_date_string()}
for t in sorted_result]
def backref(self, field, Reference, default=0):
""" Apply aggregations on Documents referencing the User document.
"""
pipeline = [
{
'$group': {
'_id': '$user',
'f': field,
},
},
]
result = list(Reference.objects(user=self).aggregate(*pipeline))
if len(result):
return result[0]['f']
else:
return default
@property
def last_service(self):
""" Return the latest service object, where the given user did the
master service.
"""
return (Service.objects(user=self.id, master=True, cleaned=True)
.order_by('-date').first())
@property
def score(self):
if self.vip:
return 100
services = 0
consumptions = 1
latestService = Service.objects.order_by('-date').first()
latest = latestService.date if latestService else pendulum.now()
for s in Service.objects(user=self):
timediff = latest - s.date
services += s.service_count * exp(-timediff.days / 365)
for c in Consumption.objects(user=self):
timediff = latest - c.date
units = c.units or 0
consumptions += units * exp(-timediff.days / 365)
return services**3 / consumptions
def delete(self, *args, **kwargs):
assert(self.username != 'DELETED_USERS')
try:
guest_user = User.objects.get(username='DELETED_USERS')
except:
flash('No `DELETED_USERS` user found. Skipping Delete.')
return
Transaction.objects(user=self).update(user=guest_user)
Consumption.objects(user=self).update(user=guest_user)
Service.objects(user=self).update(user=guest_user)
super().delete(*args, **kwargs)
def get_uids():
return [(str(u.id), u.name) for u in User.objects.order_by('name')]
def __str__(self):
return 'User `{}`'.format(self.username)