-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
66 lines (47 loc) · 1.46 KB
/
helpers.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
from model import db, User, Image, Tag
##### * General queries * #####
def get_all_users():
return User.query.all()
def get_user_by_user_id(user_id):
return User.query.filter(User.user_id == user_id).first()
##### * User registration & login * #####
def check_username(username):
"""Check whether given username exists in users table."""
return User.query.filter(User.username == username).first()
def check_email(email):
"""Check whether given email exists in users table."""
return User.query.filter(User.email == email).first()
def register_user(username, email, password):
"""Add a user to the database."""
user = User(
username=username,
email=email,
password_hashed=User.get_hash(password))
db.session.add(user)
db.session.commit()
return user
##### * Image upload and retrieval * #####
def upload_image(url, notes, user_id, private=False, tag_id=None):
image = Image(
url=url,
notes=notes,
user_id=user_id,
private=private,
tag_id=tag_id
)
db.session.add(image)
db.session.commit()
return image
def get_images_by_user(user_id):
return Image.query.filter(Image.user_id == user_id).all()
##### * Tags * #####
def create_tag(name, icon, hex_code, user_id):
tag = Tag(
name=name,
icon=icon,
hex_code=hex_code,
user_id=user_id
)
db.session.add(tag)
db.session.commit()
return tag