-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
134 lines (85 loc) · 3.35 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, Dashboard, Word, connect_to_db
def create_user (fname, lname, email, password):
""" Create a new user """
user = User(fname=fname, lname=lname, email=email, password=password)
db.session.add(user)
db.session.commit()
return user
def get_all_user_dashboards(user_id):
""" Get all dashboards user have by user_id"""
all_dashboards = Dashboard.query.filter(Dashboard.user_id == user_id).all()
return all_dashboards
def turn_on_reminder(user_id):
""" Set a reminder for user """
user = User.query.get(user_id)
user.send_reminder = True
db.session.commit()
def get_users_list_and_data_to_send_reminder():
""" Get all emails from users who has sen """
users = User.query.filter(User.send_reminder == True).all()
user_info = []
for user in users:
user_info.append({'email': user.email, 'name': user.fname})
return user_info
def turn_off_reminder (user_id):
""" Turn off a users reminder """
user = User.query.get(user_id)
user.send_reminder = False
db.session.commit()
def is_reminder_true (user_id):
""" Check if user has reminder turned on """
user = User.query.get(user_id)
return user.send_reminder
def create_dashboard(dashboard_title,user_id,language):
""" Create a new dashboard """
dashboard = Dashboard(dashboard_title=dashboard_title,user_id=user_id,language=language)
db.session.add(dashboard)
db.session.commit()
return dashboard
def create_word(term, definition, audio, dashboard_id):
""" Create a new word """
word = Word(term=term,definition=definition, audio=audio, dashboard_id=dashboard_id)
db.session.add(word)
db.session.commit()
return word
def update_word_definition(word_id, new_definition):
""" Update the word definition using word_id and new_definition provided by user"""
word = Word.query.filter(Word.word_id == word_id).first()
word.definition = new_definition
db.session.commit()
def delete_word(word_id):
""" Delete a word """
word = Word.query.filter(Word.word_id == word_id).first()
db.session.delete(word)
db.session.commit()
def delete_dashboard(dashboard_id):
""" Delete a dashboard"""
dashboard = Dashboard.query.filter(Dashboard.dashboard_id == dashboard_id).first()
db.session.delete(dashboard)
db.session.commit()
def edit_dashboard(dashboard_id, new_title, new_language):
""" Edit a title of dashboard"""
dashboard = Dashboard.query.filter(Dashboard.dashboard_id == dashboard_id).first()
dashboard.dashboard_title = new_title
dashboard.language = new_language
db.session.commit()
def get_user_by_id(user_id):
""" Get user by id"""
return User.query.get(user_id)
def get_user_by_email(email):
""" Get user by email"""
return User.query.filter(User.email == email).first()
def get_dashboard_by_id(dashboard_id):
""" Get user dashboard by dashboard_id"""
return Dashboard.query.filter(Dashboard.dashboard_id == dashboard_id).first()
def get_dashboard_by_title(title):
""" Get user dashboard by title"""
return Dashboard.query.filter(Dashboard.dashboard_title == title).first()
def get_all_words_from_dashboard(dashboard_id):
""" Get all words from particular dashboard"""
dashboard = Dashboard.query.filter(Dashboard.dashboard_id == dashboard_id).first()
return dashboard.words
if __name__ == "__main__":
from server import app
connect_to_db(app)