-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbClasses.py
146 lines (121 loc) · 4.57 KB
/
dbClasses.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
from datetime import datetime
from datetime import timedelta
import urllib
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.api import mail
class AppUser(db.Model):
"""Stores extra User data for validation of events and features"""
id = db.UserProperty(auto_current_user=True)
verified = db.BooleanProperty(indexed=False)
banned = db.BooleanProperty(indexed=False)
premium = db.BooleanProperty(indexed=False)
goodEventsCount = db.IntegerProperty(indexed=False)
badEventsCount = db.IntegerProperty(indexed=False)
@staticmethod
def registerUser():
"""Constructor for the AppUser class"""
id=users.get_current_user()
key=id.email().split('@')[0]
user = AppUser(key_name=key)
user.id = id
user.verified=False
user.banned=False
user.premium=False
user.goodEventsCount=0
user.badEventsCount=0
user.put()
return user
@staticmethod
def getUser():
"""Gets the AppUser object of the current user. If they're not registered, this registers them"""
user = users.get_current_user()
userList = db.GqlQuery("SELECT * FROM AppUser WHERE id = :1 LIMIT 1",
user).fetch(1)
if userList == []: # Wasn't found
return AppUser.registerUser()
return userList[0]
@property
def getUserLink(self):
return """/User/"""+str(self.key().name())
@staticmethod
def getUserFromKey(key):
""" Retrieves an AppUser object from the """
#get(Key(key))
#return None if no user found
def banUser(self):
"""Initiates the banhammer"""
#ensure they're supposed to be here
if self.badEventsCount >= 3 and not self.banned:
self.banned=True
self.put()
message = mail.EmailMessage(
sender="Friends with Food Admin <[email protected]>",
subject="Your account has been flagged to be banned")
message.to = self.id.email()
message.cc = "[email protected]"
message.body = """
Dear %s:
Your account on Friends with Food has been flagged for
banning after being reported for several consecutive fake
events. Your account has been banned from creating new events.
If you feel that this is in error, please reply-all to this
message with an explanation.
Thanks,
The Friends with Food Team
""" % self.id.nickname()
message.send()
def promoteUser(self):
"""Users are rewarded with verified user status whenever they pay up or
make 3 good Events"""
#ensure they're supposed to be here and haven't been here before
if self.goodEventsCount >= 3 and not self.verified:
self.verifiedUser=True
self.put()
message = mail.EmailMessage(
sender="Friends with Food Admin <[email protected]>",
subject="Your account has been verified!")
message.to = self.id.email()
message.cc = "[email protected]"
message.body = """
Dear %s:
Your account on Friends with Food has been verified! Because you've
shown us so many good events, we've upgraded your account. Now, you'll
get notified of free food on campus ASAP! You'll also be able to verify
events so that everyone knows they're legit.
*With great power comes great responsibility*
Thanks,
The Friends with Food Team
""" % self.id.nickname()
message.send()
class Event(db.Model):
"""Models an individual Event item, describing the entirety of the event."""
creator = db.ReferenceProperty(AppUser, required=True)
host = db.StringProperty(indexed=False)
name = db.StringProperty(indexed=False, required=True)
location = db.StringProperty(indexed=False, required=True)
description = db.StringProperty(multiline=True)
dateStart = db.DateTimeProperty(required=True)
dateEnd = db.DateTimeProperty()
lastUpdated = db.DateTimeProperty(auto_now=True)
verified = db.BooleanProperty()
attending = db.IntegerProperty(indexed=False)
def verify(self):
if self.verified:
return
self.verified=True
#push
def getXMLFormat(self):
return """<Event name='""" + self.name + """'>
<location>""" + self.location + """</location>
<start>""" + self.startTime.isoformat()[:16] + """</start>
<end>""" + (self.dateEnd.isoformat()[:16] if dateEnd is not None else "") + """</end>
<host>""" + self.host + """</host>
<creator>""" + self.creator.id.nickname() + """</creator>
<attending>""" + (str)(self.attending) + """</attending>
<description>""" + self.description + """</description>
<verified>""" + ("1" if self.verified else "0") + """</verified>
<key>""" + self.key() + """</key>
</Event>"""
def getEventLink(self):
return """/Event/"""+str(self.key()) #Update to actual page.