-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelhandlers.py
129 lines (113 loc) · 4.25 KB
/
modelhandlers.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
from models import User, Item, ItemCompletion, Session
from pkg_resources import StringIO
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.sql.functions import random
import dbutils
import os
import simplejson
import tornado
import logging
logger = logging.getLogger('modelhandlers')
"""
username
item_id
<file>
"""
class ItemCompletionHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
username = self.get_argument('username')
item_id = self.get_argument('item_id')
try: file1 = self.request.files['file'][0]
except: file1 = None
final_string = ''
session = Session()
try:
user = dbutils.get_or_create(session, User, username=username)
item = dbutils.get_or_create(session, Item, item_id=item_id)
item_completion = dbutils.get_or_create(session, ItemCompletion, item_id=item.id, user_id=user.id)
if file1 is not None:
original_fname = file1['filename']
extension = os.path.splitext(original_fname)[1]
final_filename = item_id + extension
if not os.path.exists("./uploads"):
os.makedirs("./uploads")
if not os.path.exists("./uploads/%s" % username):
os.makedirs("./uploads/%s" % username)
output_file = open("./uploads/" + username + "/" + final_filename, 'wb')
output_file.write(file1['body'])
item_completion.file_path = "uploads/" + username + "/" + final_filename
session.add(item_completion)
session.commit()
final_string = "You have crossed item " + item_id + " off your bucket list!"
except Exception, e:
session.rollback()
logger.exception(e)
final_string = "Oops! Something went wrong. Please try again"
finally:
Session.remove()
self.finish(final_string)
"""
username
"""
class GetCompletedItemsHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
username = self.get_argument('username')
session = Session()
try:
user = dbutils.get_or_create(session, User, username=username)
item_array = []
for item_completion in user.completed_items:
item = item_completion.item
info_dict = item.serialize()
if item_completion.file_path is not None:
info_dict['image'] = item_completion.file_path
else:
info_dict['image'] = ''
item_array.append(info_dict)
# completed_items = session.Query(ItemCompletion).filter()
except Exception, e:
session.rollback()
Session.remove()
self.finish(simplejson.dumps(item_array))
"""
username
"""
class CreateUserHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
username = self.get_argument('username')
session = Session()
try:
user = dbutils.get_or_create(session, User, username=username)
final_string = "User creation successful!"
except Exception, e:
session.rollback()
final_string = "User creation failed."
finally:
Session.remove()
self.finish(final_string)
class DefineItemHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
item_id = self.get_argument('itemid')
description = self.get_argument('description', '')
session = Session()
try:
item = dbutils.get_or_create(session, Item, item_id=item_id)
item.description = description
session.add(item)
session.flush()
session.commit()
finish_string = "Item added"
# completed_items = session.Query(ItemCompletion).filter()
except Exception, e:
session.rollback()
finish_string = "Item not added"
finally:
Session.remove()
self.finish(finish_string)