/
crud.py
153 lines (87 loc) · 3.52 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
"""CRUD operations."""
from model import db, User, Profile, Comment, connect_to_db
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_users():
"""Return all users."""
return User.query.all()
# for showing on url & webpage
def get_user_by_username(username):
"""Return a user by username."""
# return User.query.get(username)
return User.query.filter(User.username == username).first()
# for account register/login
def get_user_by_email(email):
"""Return a user by email."""
return User.query.filter(User.email == email).first()
# for account register/login
def get_all_emails():
"""Return all emails."""
email_list = []
for user in get_users():
email_list.append(user.email)
return email_list
# for account register/login
def get_user_by_id(user_id):
"""Return a user by their primary key"""
# return User.query.get(user_id)
# return User.query.filter_by(user_id=user_id).one()
return User.query.filter(User.user_id==user_id).first()
def create_profile(about, experience, skill, project, education, contact, user_id):
"""Create and return a new profile."""
profile = Profile(about=about,
experience=experience,
skill=skill,
project=project,
education=education,
contact=contact,
user_id=user_id)
db.session.add(profile)
db.session.commit()
return profile
def get_profiles():
"""Return all profiles."""
return Profile.query.all()
def get_profile_by_user_id(user_id):
"""Return a profile by their user_id."""
return Profile.query.filter(Profile.user_id==user_id).all()
# return Profile.query.filter(Profile.user_id==user_id).first()
def get_profile_by_profile_id(profile_id):
"""Return a profile by their profile_id."""
return Profile.query.filter_by(profile_id=profile_id).one()
# return Profile.query.filter_by(profile_id=profile_id).first()
def get_profile_by_about(about):
"""Return a profile by about."""
return Profile.query.filter(Profile.about == about).first()
def get_profile_by_experience(experience):
"""Return a profile by experience."""
return Profile.query.filter(Profile.experience == experience).first()
def get_profile_by_project(project):
"""Return a profile by project."""
return Profile.query.filter(Profile.project == project).first()
def get_profile_by_skill(skill):
"""Return a profile by skill."""
return Profile.query.filter(Profile.skill == skill).first()
def get_profile_by_education(education):
"""Return a profile by education."""
return Profile.query.filter(Profile.education == education).first()
def get_profile_by_contact(contact):
"""Return a profile by contact."""
return Profile.query.filter(Profile.contact == contact).first()
def create_comment(comment, like, profile_id):
"""Create and return a new comment."""
comment = Comment(comment=comment, like=like, profile_id=profile_id)
db.session.add(comment)
db.session.commit()
return comment
def get_comment_by_id(user_id):
"""Return a comment by their user_id."""
return Profile.query.filter_by(user_id=user_id).all()
# Functions start here!
if __name__ == '__main__':
from server import app
connect_to_db(app)