-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
170 lines (133 loc) · 5.89 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""CRUD operations"""
from model import db, User, Category, Activity, Event, EventParticipant, BucketList, ExpertAdvice, connect_to_db
from flask import render_template
def create_activity(name, overview, category_id, season):
activity = Activity(name=name, overview=overview, category_id=category_id, season=season)
return activity
def get_activities():
"""Return all activities"""
return Activity.query.all()
def get_all_categories():
"""Return all categories"""
return Category.query.all()
def get_activities_by_category(category_id):
"""Return all activities by category"""
return Activity.query.filter(Activity.category_id == category_id).all()
def create_category(description):
""""return a new category object """
category = Category(description=description)
return category
def get_activity_by_id(activity_id):
"""Return activity id"""
return Activity.query.get(activity_id)
def get_user_by_email(email):
"""Return a user by email"""
# look for a user in the User table whose email matches the given email.
# filter adds this condition to the query.
# user.email == email checks if the email field matches the given email.
# .first() gets the first matching user or returns None if no match is found.
return User.query.filter(User.email == email).first()
def get_user_by_username(username):
"""Return a user by username"""
return User.query.filter(User.username == username).first()
def create_user(username, email, password):
"""Create and return a new user."""
user = User(username=username, email=email, password=password)
db.session.add(user)
db.session.commit()
return user
def get_events():
"""Return all events."""
return Event.query.all()
def create_event(activity_id, title, description, date_time, location, skill_level_requirement, cost, user_id=None):
"""Create and return an event."""
event = Event(activity_id=activity_id,
title=title,
description=description,
date_time=date_time,
location=location,
skill_level_requirement=skill_level_requirement,
cost=cost,
user_id=user_id
)
return event
def get_events_by_activity(activity_id):
"""Return events filtered by activity ID."""
# filter events where the activity_id matches the given activity_id
# event.activity_id == activity_id checks if the activity_id field matches the provided ID
# .all() gets the all matching event or returns None if no match is found
return Event.query.filter(Event.activity_id == activity_id).all()
def add_user_to_event(user_id, event_id):
""" Add user to event participants"""
# retrieve the event and user from the database
event = get_event_by_id(event_id)
user = get_user_by_id(user_id)
if event and user:
# check if the user is already participating in the event
existing_participation = EventParticipant.query.filter_by(user_id=user_id, event_id=event_id).first()
if not existing_participation:
# add the user to the event's participants
new_participation = EventParticipant(user_id=user_id, event_id=event_id, status='Joined', dates_created=datetime.now())
db.session.add(new_participation)
db.session.commit()
return True
return False
def get_event_by_id(event_id):
"""Return a specific event by its ID."""
return Event.query.get(event_id)
def is_user_participating(user_id, event_id):
"""Check if the user is already participating in the event"""
return EventParticipant.query.filter_by(user_id=user_id, event_id=event_id).first()
def create_event_participation(event_id, user_id):
"""Create a new event participation record."""
print("Event id", event_id, type(event_id), "User id", user_id, type(user_id))
print("User", User.query.get(user_id))
print("Event", Event.query.get(event_id))
new_participation = EventParticipant(
event_id=event_id,
user_id=user_id)
#status=status,
#dates_created=dates_created
db.session.add(new_participation)
db.session.commit()
return new_participation
def get_user_by_id(user_id):
"""Return user by their ID."""
return User.query.get(user_id)
def get_events_by_user(user_id):
""""Return events created by a specific user"""
return Event.query.filter(Event.user_id == user_id).all()
def get_event_participants(user_id):
""""Return all events a specific user is participating in"""
return EventParticipant.query.filter_by(user_id=user_id).all()
def get_bucket_list_items(user_id):
""""Return all bucket list items for user"""
return BucketList.query.filter(BucketList.user_id == user_id).all()
def add_bucket_list_item(user_id, activity_id):
"""Add a bucket list item for a user"""
new_bucket_list_item = BucketList(user_id=user_id, activity_id=activity_id, status='pending')
db.session.add(new_bucket_list_item)
db.session.commit()
return new_bucket_list_item
def get_bucket_list_items(user_id):
"""Return all bucket list items for a user"""
return BucketList.query.filter(BucketList.user_id==user_id).all()
def mark_bucket_list_item_completed(bucket_list_id):
"""Mark a bucket list item as completed"""
bucket_list_item = BucketList.query.get(bucket_list_id)
if bucket_list_item:
bucket_list_item.status = 'completed'
db.session.commit()
return bucket_list_item
return None
def delete_bucket_list_item(bucket_list_id):
"""Delete a bucket list item"""
bucket_list_item = BucketList.query.get(bucket_list_id)
if bucket_list_item:
db.session.delete(bucket_list_item)
db.session.commit()
return bucket_list_item
return None
if __name__ == '__main__':
from server import app
connect_to_db(app)