-
Notifications
You must be signed in to change notification settings - Fork 1
/
life_planner.py
350 lines (311 loc) · 12.9 KB
/
life_planner.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#all our imports
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import random
from google.appengine.dist import use_library
import logging
import decimal
import datetime
import re
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
#all our classes
class Messages(db.Model): #all messages are stored using this class
user = db.StringProperty(required = False)
content = db.StringProperty(required = False, multiline = True)
calendar = db.StringProperty(required = False)
money = db.StringProperty(required = False)
flashcards = db.StringProperty(required = False)
AM = db.StringProperty(required = False)
PM = db.StringProperty(required = False)
class FavSite(db.Model):
user = db.StringProperty(required = False)
site = db.StringProperty(required = False)
class CalendarEvents(db.Model): #calendar messages specially stored as this. Allows us to associate entries with their time.
date = db.StringProperty(required = False)
user = db.StringProperty(required = False)
hours = db.StringProperty(required = False)
minutes = db.StringProperty(required = False)
events = db.StringProperty(required = False)
AM = db.StringProperty(required = False)
PM = db.StringProperty(required = False)
class Settings(db.Model): #used for the settings
user = db.StringProperty(required = False)
savings = db.StringProperty(required = False)
monthly_income = db.StringProperty(required = False)
monthly_expenses = db.StringProperty(required = False)
def message_creator(self): #yay, condensing repetative code!
content = self.request.get('content')
cal_tag = self.request.get('cal_tag')
mon_tag = self.request.get('mon_tag')
fla_tag = self.request.get('fla_tag')
AM = self.request.get('AM')
PM = self.request.get('PM')
message = Messages(user=str(users.get_current_user()), content=content, calendar=cal_tag, money=mon_tag, flashcards=fla_tag, AM=AM, PM=PM)
message.put()
if message.calendar:
c = re.search(r'\d+', message.content)
content = message.content
if c:
if message.content.__contains__(':'):
i = message.content.index(':')
hour = message.content[0:i]
minute = message.content[i+1:i+3].replace(',','').replace('.','').replace('!','').replace(';','').replace('?','')
hour_search = re.search(r'\d+', hour)
minute_search = re.search(r'\d+', minute)
if hour_search and minute_search:
if int(hour_search.group(0)) <= 24:
if int(hour_search.group(0)) > 12:
hour_san = str(int(hour_search.group(0)) - 12)
else:
hour_san = hour_search.group(0)
if int(minute_search.group(0)) < 60:
minute_san = minute_search.group(0)
else:
hour_san = "Unknown"
minute_san = "Time"
else:
hour_san = "Unknown"
minute_san = "Time"
else:
hour_san = "Hour"
minute_san = "Minute"
content = "Please enter time following the format on the left."
AM = message.AM
PM = message.PM
calendar = CalendarEvents(date=str(datetime.date.today()), user=str(users.get_current_user()), hours=hour_san, minutes=minute_san, events=content, AM=AM, PM=PM)
calendar.put()
#all our handlers
class SettingHandler(webapp.RequestHandler): #handles the settings page
def get(self):
template_values = {}
valid_messages = []
messages = Messages.all()
for message in messages:
if message.content == '' and message.calendar == '' and message.money == '' and message.flashcards == '':
message.delete() #if a message has no content at all, it is deleted, as extra blank messages were being generated on post
if message.user == str(users.get_current_user()):
valid_messages.append(message)
template_values['messages'] = valid_messages #passes only the user's messages to the template
path = os.path.join(os.path.dirname(__file__), 'settings.html')
self.response.out.write(template.render(path, template_values))
def post(self):
message_creator(self)
savings = self.request.get('savings').replace(',','') #allows for commas when entering the settings values
monthly_income = self.request.get('monthly_income').replace(',','')
monthly_expenses = self.request.get('monthly_expenses').replace(',','')
if savings.isdigit() and monthly_income.isdigit() and monthly_expenses.isdigit():
sav = savings
mon_in = monthly_income
mon_ex = monthly_expenses
user = users.get_current_user()
settings = Settings(user=str(user), savings=sav, monthly_income=mon_in, monthly_expenses=mon_ex)
settings.put()
else:
pass
cal_tag_on = self.request.get('cal_tag_on') #the following are used to toggle the settings
if cal_tag_on:
messages = Messages.get(cal_tag_on)
if messages:
messages.calendar = None;
messages.put()
cal_tag_off = self.request.get('cal_tag_off')
if cal_tag_off:
messages = Messages.get(cal_tag_off)
if messages:
messages.calendar = 'on';
messages.put()
mon_tag_on = self.request.get('mon_tag_on')
if mon_tag_on:
messages = Messages.get(mon_tag_on)
if messages:
messages.money = None;
messages.put()
mon_tag_off = self.request.get('mon_tag_off')
if mon_tag_off:
messages = Messages.get(mon_tag_off)
if messages:
messages.money = "on";
messages.put()
fla_tag_on = self.request.get('fla_tag_on')
if fla_tag_on:
messages = Messages.get(fla_tag_on)
if messages:
messages.flashcards = None;
messages.put()
fla_tag_off = self.request.get('fla_tag_off')
if fla_tag_off:
messages = Messages.get(fla_tag_off)
if messages:
messages.flashcards = "on";
messages.put()
message_to_delete = self.request.get('delete') #allows user to delete messages
if message_to_delete:
messages = Messages.get(message_to_delete)
if messages:
messages.delete()
messages = Messages.all() #deletes messages that were generated on the post of the page, which have no content
for message in messages:
if message.content == '' and message.calendar == '' and message.money == '' and message.flashcards == '':
message.delete()
self.get()
class InstructionHandler(webapp.RequestHandler): #the handler for the instructions page
def get(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'instructions.html')
self.response.out.write(template.render(path, template_values))
def post(self):
message_creator(self)
self.get()
class ContentHandler(webapp.RequestHandler): #the handler for the home page
def get(self):
user = users.get_current_user()
template_values = {'username': user.email()}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
def post(self):
message_creator(self)
self.get()
class ContactHandler(webapp.RequestHandler): #the handler for the Contact Us page
def get(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'contact_us.html')
self.response.out.write(template.render(path, template_values))
def post(self):
message_creator(self)
self.get()
class LifePlannerHandler(webapp.RequestHandler): #the handler for the Life Planner page. Most of the calulations occur here.
def get(self):
template_values = {}
m = 0
money_list = []
add_money = []
minus_money = []
flashcard_dict = {}
total_money = 0
income = 0
expenses = 0
A = 0
B = 0
month_income = 0
month_expenses = 0
monies = 0
valid_calendar_entriesAM = []
valid_calendar_entriesPM = []
valid_calendar_entries = []
settings = Settings.all() #gets the settings to pass into the budget
for setting in settings:
if setting.user == str(users.get_current_user()):
if setting.savings:
savings = setting.savings
template_values['savings'] = savings
if setting.monthly_income:
monthly_income = setting.monthly_income
template_values['monthly_income'] = monthly_income
if setting.monthly_expenses:
monthly_expenses = setting.monthly_expenses
template_values['monthly_expenses'] = monthly_expenses
if savings and monthly_income and monthly_expenses:
monies = int(savings) + int(monthly_income) - int(monthly_expenses)
messages = Messages.all()
for message in messages:
if message.user == str(users.get_current_user()):
if message.flashcards: #creates flashcards
if ":" in str(message.content):
l = message.content.split(":")
flashcard_dict[l[0]] = l[1]
rk = random.choice(flashcard_dict.keys())
answer = flashcard_dict[rk]
template_values['rk'] = rk
template_values['answer'] = answer
else:
pass
if message.money: #creates budget
content_money = str(message.content)
m = re.search(r'\d+', content_money)
if m:
money = m.group(0)
if "+" + str(money) in content_money:
template_values['m'] = m.group(0)
add_money.append(money)
template_values['add_money'] = add_money
while A < len(add_money):
b = add_money[A]
income += int(b)
A += 1
if "-" + str(money) in content_money:
minus_money.append(money)
template_values['minus_money'] = minus_money
while B < len(minus_money):
b = minus_money[B]
expenses += int(b)
B += 1
total_money = monies + income - expenses
template_values['income'] = income
template_values['expenses'] = expenses
template_values['total_money'] = total_money
calendars = CalendarEvents.all()
for calendar in calendars:
if calendar.user == str(users.get_current_user()):
if calendar.date == str(datetime.date.today()):
if calendar.AM:
valid_calendar_entriesAM.append(calendar)
a = sorted(valid_calendar_entriesAM, key=lambda calendar: calendar.hours)
template_values['calendarsAM'] = a
if calendar.PM:
valid_calendar_entriesPM.append(calendar)
b = sorted(valid_calendar_entriesPM, key=lambda calendar: calendar.hours)
template_values['calendarsPM'] = b
if calendar not in valid_calendar_entriesPM and calendar not in valid_calendar_entriesAM:
valid_calendar_entries.append(calendar)
c = sorted(valid_calendar_entries, key=lambda calendar: calendar.hours)
template_values['calendars'] = c
template_values['date'] = datetime.date.today()
path = os.path.join(os.path.dirname(__file__), 'life_planner.html')
self.response.out.write(template.render(path, template_values))
def post(self):
message_creator(self)
calendar_to_delete = self.request.get('check')
if calendar_to_delete:
calendars_to_destroy = CalendarEvents.get(calendar_to_delete)
if calendars_to_destroy:
calendars_to_destroy.delete()
self.get()
class FavSiteHandler(webapp.RequestHandler):
def get(self):
template_values = {}
websites = Messages.all()
for website in websites:
url = website.content
logging.info(url)
if url:
if url.__contains__('@'):
indexat = url.index('@')
if url.__contains__('@http://'):
parse = url[indexat:].replace('@','').strip()
userSite = FavSite(user = str(users.get_current_user()), site = parse)
userSite.put()
else:
parse = url[indexat:].replace('@', 'http://').strip()
userSite = FavSite(user = str(users.get_current_user()), site = parse)
userSite.put()
template_values['site'] = userSite.site
path = os.path.join(os.path.dirname(__file__), 'favSite.html')
self.response.out.write(template.render(path, template_values))
def post(self):
message_creator(self)
self.get()
def main():
application = webapp.WSGIApplication([('/',ContentHandler),
('/favSite', FavSiteHandler),
('/life_planner', LifePlannerHandler),
('/settings', SettingHandler),
('/contact', ContactHandler),
('/instructions', InstructionHandler)],
debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
main()