-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
55 lines (38 loc) · 1.46 KB
/
crud.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
"""CRUD operations."""
from model import db, User, Image, ImagePermission
from datetime import datetime
from werkzeug.security import generate_password_hash
def create_user(username, password):
"""Create and return a new user."""
user = User(username=username, password_hash=generate_password_hash(password))
db.session.add(user)
db.session.commit()
return user
def get_user_by_username(username):
"""Return a user by username."""
return User.query.filter_by(username=username).first()
def get_user_by_username_and_password(username, password):
"""Return a user by username and password."""
user = get_user_by_username(username)
if not user:
return None
if user.check_password(password):
return user
def create_image(image_url, owner, permission="PRIVATE"):
"""Create and return a new image."""
image = Image(image_url=image_url,
owner=owner,
permission=permission)
db.session.add(image)
db.session.commit()
return image
def get_all_images_for_user(username):
"""Return all images belonging to a user."""
user = get_user_by_username(username)
return user.images
def get_public_images_for_user(username):
"""Return all public images belonging to a user."""
user = get_user_by_username(username)
images = user.images
public_images = [img for img in images if img.permission.value == "PUBLIC"]
return public_images