-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.py
133 lines (78 loc) · 3.36 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
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
"""CRUD operations."""
from model import db, User, Collection, Picture, connect_to_db
from datetime import datetime
# <!--------------------------------------------------------------->
# <-- Routes for user -->
# <!--------------------------------------------------------------->
def create_user(email, password):
"""Create and return a new user."""
new_user = User(email=email, password=password)
db.session.add(new_user)
db.session.commit()
return new_user
def get_user_by_id(user_id):
"""Return a user by primary key."""
return User.query.get(user_id)
def get_user_by_email(email):
"""Return a user by email."""
return User.query.filter(User.email == email).first()
# <!--------------------------------------------------------------->
# <-- Routes for collections -->
# <!--------------------------------------------------------------->
def get_collections():
"""Return all collections."""
return Collection.query.all()
def get_collection_by_id(collection_id):
"""Return a collection by primary key."""
return Collection.query.get(collection_id)
def create_collection(user_id, notes, date_saved=datetime.today()):
"""Create and return a new collection."""
new_collection = Collection(user_id=user_id, notes=notes, date_saved=date_saved)
db.session.add(new_collection)
db.session.commit()
return new_collection
def get_collection_by_email(email):
"""Return all collections belonging to a user"""
user_collection = Collection.query.join(User)
return user_collection.filter(User.email == email).all()
def get_city_by_collection(collection_id):
"""Get city name in notes with collection_id"""
city = get_collection_by_id(collection_id)
return city.notes
def delete_collection(collection_id):
"""Delete a collection object from the database."""
collection_object = get_collection_by_id(collection_id)
db.session.delete(collection_object)
db.session.commit()
return collection_object
# <!--------------------------------------------------------------->
# <-- Routes for pictures -->
# <!--------------------------------------------------------------->
def create_picture(collection_id, url):
"""Create and return a new picture."""
new_picture = Picture(collection_id=collection_id, url=url)
db.session.add(new_picture)
db.session.commit()
return new_picture
def get_pictures_by_collection(collection_id):
"""Return all pictures in collection."""
collection_pictures = Picture.query.join(Collection)
return collection_pictures.filter(Collection.collection_id == collection_id).all()
def get_first_image(collection_id):
"""Get the first image in the collection. """
collection = get_pictures_by_collection(collection_id)
first_image = collection[0]
return first_image.url
def get_first_image_all_collections(email):
"""Get a dictionary of user collection and first image in each collection per user"""
#Get collection from user_id
#Get pictures from collection
#Get first picture from collection
collections = get_collection_by_email(email)
first_in_collection = {}
for collection in collections:
first_in_collection[collection] = get_first_image(collection.collection_id)
return first_in_collection
if __name__ == '__main__':
from server import app
connect_to_db(app)