/
helper.py
128 lines (106 loc) · 3.94 KB
/
helper.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
import string
from urllib2 import urlopen
from sqlalchemy_imageattach.entity import Image, image_attachment
from sqlalchemy_imageattach.context import store_context
from database_setup import Base, User, Antibody, Cytotoxin, Adc
from database_setup import AntibodyLot, CytotoxinLot, AdcLot
from database_setup import UserImg, AntibodyImg, CytotoxinImg, AdcImg
from settings import fs_store, ALLOWED_EXTENSIONS
from init_db import session
# User Helper Functions
def create_user(login_session):
"""
Create a new user in the db using user info in the login_session
"""
new_user = User(name=login_session['username'], email=login_session[
'email'])
session.add(new_user)
session.commit()
user = session.query(User).filter_by(email=login_session['email']).one()
attach_picture_url(User, user.id, login_session['picture'])
return user.id
def get_user_info(user_id):
"""Get user object in the db using its user_id"""
user = session.query(User).filter_by(id=user_id).one()
return user
def get_user_id(email):
"""Get user's id in the db using its e-mail address"""
try:
user = session.query(User).filter_by(email=email).one()
return user.id
except:
return None
def login_info(login_session):
"""Provide login status to pass onto html templates"""
email, user_id, logged_in = None, None, False
if 'email' in login_session:
email = login_session['email']
user_id = get_user_id(login_session['email'])
logged_in = True
return (email, user_id, logged_in)
def set_category(dbtype):
"""Provide category/item data to pass onto html templates"""
# define object and object lots
obj = eval(dbtype.capitalize())
items = eval(dbtype.capitalize()+'Lot')
# query the object items and object lots items
cat = session.query(obj).order_by(obj.name).all()
lots = session.query(items).all()
# create a dict to associate object id with its respective object lot items
lot_dict = {}
for x in range(1, session.query(obj).count()+1):
lot_dict[x] = (session.query(items)
.filter(getattr(items, dbtype+'_id') == x)
.order_by(items.date).all())
return (cat, lot_dict, lots)
def allowed_file(filename):
"""
Determine whether the uploaded file has an allowed file extension.
Arg:
filename: the uploaded file's name
Returns:
Boolean. True if the file extension is one of those listed under
ALLOWED_EXTENSIONS. False if otherwise.
"""
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def attach_picture(table, item_id, location):
"""
A helper function used in populator.py to upload picture to the db
Args:
table: The category which the picture belongs to
item_id: The category's id number which the picture should be
uploaded to
location: local directory of where the picture is found
Returns:
None
"""
try:
item = session.query(table).filter_by(id=item_id).one()
with store_context(fs_store):
with open(location, 'rb') as f:
item.picture.from_file(f)
session.commit()
except Exception:
session.rollback()
raise
def attach_picture_url(table, item_id, location):
"""
A helper function used in populator.py to upload picture to the db
from web
Args:
table: The category which the picture belongs to
item_id: The category's id number which the picture should be
uploaded to
location: a web url of where the picture is found
Returns:
None
"""
try:
item = session.query(table).filter_by(id=item_id).one()
with store_context(fs_store):
item.picture.from_file(urlopen(location))
session.commit()
except Exception:
session.rollback()
raise