-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmodels.py
155 lines (115 loc) · 3.66 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
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
from datetime import datetime
import json
import os
import re
import time
from uuid import uuid4
from passlib.hash import bcrypt
import settings
import utils
class ModelClass(object):
_id = None
created = None
updated = None
def __init__(self, *args, **kwargs):
"""
Accepts either a dictionary as the first argument
or a series of keyword arguments.
"""
if args:
if isinstance(args[0], dict):
for k,v in args[0].items():
setattr(self, k,v)
for k,v in kwargs.items():
setattr(self, k, v)
if not self._id:
self._id = str(uuid4())
now = time.mktime(datetime.now().timetuple())
for field in ['created', 'updated']:
if not getattr(self, field):
setattr(self, field, now)
def __getitem__(self, key):
return getattr(self, key)
def __str__(self):
return self.__unicode__()
def __repr__(self):
return self.__unicode__()
def to_dict(self):
return dict(self.__dict__)
def to_json(self):
return json.dumps(self.to_dict())
def commit_to_db(self, collection):
self.updated = time.mktime(datetime.now().timetuple())
collection = utils.connect(collection)
result = collection.save(self.to_dict())
return result
class User(ModelClass):
name = None
email = None
sessions_voted_for = []
sessions_pitched = []
login_hash = None
password = None
fingerprint = None
def __unicode__(self):
return self.name
def auth_user(self, possible_password):
return bcrypt.verify(possible_password, self.login_hash)
def save(self, test=False):
if not self.login_hash:
if not self.password:
raise ValueError("Password must not be null.")
if self.password == "password":
raise ValueError("Seriously. Seriously?")
self.login_hash = bcrypt.encrypt(self.password)
self.password = None
if not test:
self.commit_to_db('user')
return self
def update_records(self):
votes = utils.connect('vote')
sessions = utils.connect('session')
self.sessions_voted_for = [x['session'] for x in list(votes.find({"user": self._id}))]
self.sessions_pitched = [x['_id'] for x in list(sessions.find({"user": self._id}))]
self.save()
@staticmethod
def tally():
collection = utils.connect('user')
users = list(collection.find({}))
for user_dict in users:
u = User(user_dict)
u.update_records()
print("Updated %s users." % len(users))
class Session(ModelClass):
title = None
description = None
user = None
accepted = False
votes = 0
def __unicode__(self):
return self.title
def save(self, test=False):
if not test:
self.commit_to_db('session')
return self
def update_records(self):
votes = utils.connect('vote')
self.votes = votes.find({"session": self._id}).count()
self.save()
@staticmethod
def tally():
collection = utils.connect('session')
sessions = list(collection.find({}))
for session_dict in sessions:
s = Session(session_dict)
s.update_records()
print("Updated %s sessions." % len(sessions))
class Vote(ModelClass):
user = None
session = None
def __unicode__(self):
return "%s,%s" % (self.user, self.session)
def save(self, test=False):
if not test:
self.commit_to_db('vote')
return self