Skip to content

Commit a8d5012

Browse files
committed
Moved profile routes to folder with separate files
1 parent d4dead2 commit a8d5012

File tree

7 files changed

+232
-106
lines changed

7 files changed

+232
-106
lines changed

backend/PyMatcha/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ def check_if_token_is_revoked(decrypted_token):
191191
from PyMatcha.routes.api.auth.password import auth_password_bp
192192
from PyMatcha.routes.api.auth.register import auth_register_bp
193193
from PyMatcha.routes.api.auth.login import auth_login_bp
194-
from PyMatcha.routes.api.profile import profile_bp
194+
from PyMatcha.routes.api.profile.view import profile_view_bp
195+
from PyMatcha.routes.api.profile.edit import profile_edit_bp
196+
from PyMatcha.routes.api.profile.complete import profile_complete_bp
197+
from PyMatcha.routes.api.profile.report import profile_report_bp
195198
from PyMatcha.routes.api.like import like_bp
196199

197200
logging.debug("Registering Flask blueprints")
@@ -201,7 +204,10 @@ def check_if_token_is_revoked(decrypted_token):
201204
application.register_blueprint(auth_password_bp)
202205
application.register_blueprint(auth_register_bp)
203206
application.register_blueprint(auth_login_bp)
204-
application.register_blueprint(profile_bp)
207+
application.register_blueprint(profile_view_bp)
208+
application.register_blueprint(profile_edit_bp)
209+
application.register_blueprint(profile_complete_bp)
210+
application.register_blueprint(profile_report_bp)
205211
application.register_blueprint(like_bp)
206212

207213
if application.debug:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
PyMatcha - A Python Dating Website
3+
Copyright (C) 2018-2019 jlasne/gmorer
4+
<jlasne@student.42.fr> - <gmorer@student.42.fr>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
PyMatcha - A Python Dating Website
3+
Copyright (C) 2018-2019 jlasne/gmorer
4+
<jlasne@student.42.fr> - <gmorer@student.42.fr>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
PyMatcha - A Python Dating Website
3+
Copyright (C) 2018-2019 jlasne/gmorer
4+
<jlasne@student.42.fr> - <gmorer@student.42.fr>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
19+
import datetime
20+
21+
from flask import Blueprint
22+
from flask import request
23+
from flask_jwt_extended import current_user
24+
from flask_jwt_extended import jwt_required
25+
from PyMatcha.models.tag import Tag
26+
from PyMatcha.utils.decorators import validate_params
27+
from PyMatcha.utils.errors import BadRequestError
28+
from PyMatcha.utils.success import Success
29+
30+
profile_complete_bp = Blueprint("profile_complete", __name__)
31+
32+
REQUIRED_PARAMS_COMPLETE_PROFILE = {"gender": str, "birthdate": str, "orientation": str, "bio": str, "tags": list}
33+
34+
35+
@profile_complete_bp.route("/profile/complete", methods=["POST"])
36+
@jwt_required
37+
@validate_params(REQUIRED_PARAMS_COMPLETE_PROFILE)
38+
def complete_profile():
39+
if current_user.is_profile_completed:
40+
raise BadRequestError(
41+
"The user has already completed his profile", "Go to your profile settings to edit your profile"
42+
)
43+
data = request.get_json()
44+
orientation = data["orientation"]
45+
bio = data["bio"]
46+
tags = data["tags"]
47+
gender = data["gender"]
48+
birthdate = data["birthdate"]
49+
50+
try:
51+
birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date()
52+
except ValueError:
53+
raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year)", "Try again")
54+
55+
if len(bio) <= 50:
56+
raise BadRequestError("Bio is too short", "Try again")
57+
58+
if len(tags) < 3:
59+
raise BadRequestError("At least 3 tags are required", "Try again")
60+
61+
if len(tags) != len(set(tags)):
62+
raise BadRequestError("Duplicate tags", "Try again")
63+
64+
today = datetime.datetime.utcnow()
65+
66+
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
67+
if age < 18:
68+
raise BadRequestError("You must be 18 years old or older", "Try again later")
69+
70+
for tag in tags:
71+
Tag.create(name=tag, user_id=current_user.id)
72+
73+
current_user.orientation = orientation
74+
current_user.bio = bio
75+
current_user.is_profile_completed = True
76+
current_user.gender = gender
77+
current_user.birthdate = birthdate
78+
current_user.save()
79+
return Success("Profile completed !")

backend/PyMatcha/routes/api/profile.py renamed to backend/PyMatcha/routes/api/profile/edit.py

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
from flask_jwt_extended import current_user
2727
from flask_jwt_extended import jwt_required
2828
from ip2geotools.databases.noncommercial import DbIpCity
29-
from PyMatcha.models.report import Report
30-
from PyMatcha.models.tag import Tag
3129
from PyMatcha.models.user import get_user
32-
from PyMatcha.models.view import View
3330
from PyMatcha.utils import hash_password
3431
from PyMatcha.utils.confirm_token import generate_confirmation_token
3532
from PyMatcha.utils.decorators import validate_params
@@ -39,11 +36,9 @@
3936
from PyMatcha.utils.mail import send_mail_html
4037
from PyMatcha.utils.mail import send_mail_text
4138
from PyMatcha.utils.success import Success
42-
from PyMatcha.utils.success import SuccessOutput
4339

44-
profile_bp = Blueprint("profile", __name__)
40+
profile_edit_bp = Blueprint("profile_edit", __name__)
4541

46-
REQUIRED_PARAMS_COMPLETE_PROFILE = {"gender": str, "birthdate": str, "orientation": str, "bio": str, "tags": list}
4742
REQUIRED_PARAMS_EDIT_PROFILE = {
4843
"first_name": str,
4944
"last_name": str,
@@ -56,54 +51,7 @@
5651
}
5752

5853

59-
@profile_bp.route("/profile/complete", methods=["POST"])
60-
@jwt_required
61-
@validate_params(REQUIRED_PARAMS_COMPLETE_PROFILE)
62-
def complete_profile():
63-
if current_user.is_profile_completed:
64-
raise BadRequestError(
65-
"The user has already completed his profile", "Go to your profile settings to edit your profile"
66-
)
67-
data = request.get_json()
68-
orientation = data["orientation"]
69-
bio = data["bio"]
70-
tags = data["tags"]
71-
gender = data["gender"]
72-
birthdate = data["birthdate"]
73-
74-
try:
75-
birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date()
76-
except ValueError:
77-
raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year)", "Try again")
78-
79-
if len(bio) <= 50:
80-
raise BadRequestError("Bio is too short", "Try again")
81-
82-
if len(tags) < 3:
83-
raise BadRequestError("At least 3 tags are required", "Try again")
84-
85-
if len(tags) != len(set(tags)):
86-
raise BadRequestError("Duplicate tags", "Try again")
87-
88-
today = datetime.datetime.utcnow()
89-
90-
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
91-
if age < 18:
92-
raise BadRequestError("You must be 18 years old or older", "Try again later")
93-
94-
for tag in tags:
95-
Tag.create(name=tag, user_id=current_user.id)
96-
97-
current_user.orientation = orientation
98-
current_user.bio = bio
99-
current_user.is_profile_completed = True
100-
current_user.gender = gender
101-
current_user.birthdate = birthdate
102-
current_user.save()
103-
return Success("Profile completed !")
104-
105-
106-
@profile_bp.route("/profile/edit", methods=["PUT"])
54+
@profile_edit_bp.route("/profile/edit", methods=["PUT"])
10755
@jwt_required
10856
@validate_params(REQUIRED_PARAMS_EDIT_PROFILE)
10957
def edit_profile():
@@ -153,7 +101,7 @@ def edit_profile():
153101
return Success("User successfully modified !")
154102

155103

156-
@profile_bp.route("/profile/edit/email", methods=["PUT"])
104+
@profile_edit_bp.route("/profile/edit/email", methods=["PUT"])
157105
@jwt_required
158106
@validate_params({"email": str})
159107
def edit_email():
@@ -171,7 +119,7 @@ def edit_email():
171119
return Success("Email sent for new email")
172120

173121

174-
@profile_bp.route("/profile/edit/password", methods=["PUT"])
122+
@profile_edit_bp.route("/profile/edit/password", methods=["PUT"])
175123
@jwt_required
176124
@validate_params({"old_password": str, "new_password": str})
177125
def edit_password():
@@ -191,7 +139,7 @@ def edit_password():
191139
return Success("User password successfully updated.")
192140

193141

194-
@profile_bp.route("/profile/edit/geolocation", methods=["PUT"])
142+
@profile_edit_bp.route("/profile/edit/geolocation", methods=["PUT"])
195143
@jwt_required
196144
@validate_params({"ip": str}, {"lat": float, "lng": float})
197145
def edit_geolocation():
@@ -213,50 +161,3 @@ def edit_geolocation():
213161
current_user.geohash = Geohash.encode(lat, lng)
214162
current_user.save()
215163
return Success("New location sucessfully saved.")
216-
217-
218-
@profile_bp.route("/profile/views", methods=["GET"])
219-
@jwt_required
220-
def get_profile_views():
221-
profile_views = current_user.get_views()
222-
profile_views = [v.to_dict() for v in profile_views]
223-
return SuccessOutput("views", profile_views)
224-
225-
226-
@profile_bp.route("/profile/view/<uid>", methods=["GET"])
227-
@jwt_required
228-
def view_profile(uid):
229-
try:
230-
u = get_user(uid)
231-
except NotFoundError:
232-
raise NotFoundError(f"User {uid} not found", "try again")
233-
234-
if current_user.id != u.id:
235-
View.create(profile_id=u.id, viewer_id=current_user.id)
236-
237-
return SuccessOutput("profile", u.to_dict())
238-
239-
240-
@profile_bp.route("/profile/report/<uid>", methods=["POST"])
241-
@validate_params({"reason": str}, {"details": str})
242-
@jwt_required
243-
def report_profile(uid):
244-
data = request.get_json()
245-
reason = data["reason"]
246-
247-
if reason not in ["harassment", "bot", "spam", "inappropriate content"]:
248-
raise BadRequestError("Reason must be 'harassment', 'bot', 'spam' or 'inappropriate content'", "Try again")
249-
250-
try:
251-
details = data["details"]
252-
except KeyError:
253-
details = None
254-
try:
255-
u = get_user(uid)
256-
except NotFoundError:
257-
raise NotFoundError(f"User {uid} not found", "try again")
258-
if current_user.id == u.id:
259-
raise BadRequestError("Cannot report yourself", "Try again")
260-
Report.create(reporter_id=current_user.id, reported_id=u.id, reason=reason, details=details)
261-
262-
return Success(f"Report created on user {u.email}")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
PyMatcha - A Python Dating Website
3+
Copyright (C) 2018-2019 jlasne/gmorer
4+
<jlasne@student.42.fr> - <gmorer@student.42.fr>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
19+
from flask import Blueprint
20+
from flask import request
21+
from flask_jwt_extended import current_user
22+
from flask_jwt_extended import jwt_required
23+
from PyMatcha.models.report import Report
24+
from PyMatcha.models.user import get_user
25+
from PyMatcha.utils.decorators import validate_params
26+
from PyMatcha.utils.errors import BadRequestError
27+
from PyMatcha.utils.errors import NotFoundError
28+
from PyMatcha.utils.success import Success
29+
30+
profile_report_bp = Blueprint("profile_report", __name__)
31+
32+
33+
@profile_report_bp.route("/profile/report/<uid>", methods=["POST"])
34+
@validate_params({"reason": str}, {"details": str})
35+
@jwt_required
36+
def report_profile(uid):
37+
data = request.get_json()
38+
reason = data["reason"]
39+
40+
if reason not in ["harassment", "bot", "spam", "inappropriate content"]:
41+
raise BadRequestError("Reason must be 'harassment', 'bot', 'spam' or 'inappropriate content'", "Try again")
42+
43+
try:
44+
details = data["details"]
45+
except KeyError:
46+
details = None
47+
try:
48+
u = get_user(uid)
49+
except NotFoundError:
50+
raise NotFoundError(f"User {uid} not found", "try again")
51+
if current_user.id == u.id:
52+
raise BadRequestError("Cannot report yourself", "Try again")
53+
Report.create(reporter_id=current_user.id, reported_id=u.id, reason=reason, details=details)
54+
55+
return Success(f"Report created on user {u.email}")

0 commit comments

Comments
 (0)