|
25 | 25 | from flask import request |
26 | 26 | from flask_jwt_extended import current_user |
27 | 27 | from flask_jwt_extended import jwt_required |
| 28 | +from PyMatcha.models.tag import Tag |
28 | 29 | from PyMatcha.models.user import get_user |
29 | 30 | from PyMatcha.utils import hash_password |
30 | 31 | from PyMatcha.utils.confirm_token import generate_confirmation_token |
@@ -116,33 +117,58 @@ def edit_profile_gender(): |
116 | 117 | return Success("Gender successfully modified!") |
117 | 118 |
|
118 | 119 |
|
119 | | -# gender = data["gender"] |
120 | | -# orientation = data["orientation"] |
121 | | -# birthdate = data["birthdate"] |
122 | | -# TAGS |
123 | | -# |
124 | | -# try: |
125 | | -# birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date() |
126 | | -# except ValueError: |
127 | | -# raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year).") |
128 | | -# |
129 | | -# today = datetime.datetime.utcnow() |
130 | | -# |
131 | | -# age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day)) |
132 | | -# if age < 18: |
133 | | -# raise BadRequestError("You must be 18 years old or older.") |
134 | | -# |
135 | | - |
136 | | -# |
137 | | -# if orientation not in ["heterosexual", "homosexual", "bisexual", "other"]: |
138 | | -# raise BadRequestError("Orientation must be heterosexual, homosexual, bisexual or other.") |
139 | | -# |
140 | | - |
141 | | -# |
142 | | -# current_user.gender = gender |
143 | | -# current_user.orientation = orientation |
144 | | -# current_user.birthdate = birthdate |
145 | | -# current_user.save() |
| 120 | +@profile_edit_bp.route("/profile/edit/orientation", methods=["PATCH"]) |
| 121 | +@validate_params({"orientation": str}) |
| 122 | +@jwt_required |
| 123 | +def edit_profile_orientation(): |
| 124 | + if not current_user.is_profile_completed: |
| 125 | + raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.") |
| 126 | + data = request.get_json() |
| 127 | + orientation = data["orientation"] |
| 128 | + if orientation not in ["heterosexual", "homosexual", "bisexual", "other"]: |
| 129 | + raise BadRequestError("Orientation must be heterosexual, homosexual, bisexual or other.") |
| 130 | + current_user.orientation = orientation |
| 131 | + current_user.save() |
| 132 | + return Success("Orientation successfully modified!") |
| 133 | + |
| 134 | + |
| 135 | +@profile_edit_bp.route("/profile/edit/birthdate", methods=["PATCH"]) |
| 136 | +@validate_params({"birthdate": str}) |
| 137 | +@jwt_required |
| 138 | +def edit_profile_birthdate(): |
| 139 | + if not current_user.is_profile_completed: |
| 140 | + raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.") |
| 141 | + data = request.get_json() |
| 142 | + birthdate = data["birthdate"] |
| 143 | + try: |
| 144 | + birthdate = datetime.datetime.strptime(birthdate, "%d/%m/%Y").date() |
| 145 | + except ValueError: |
| 146 | + raise BadRequestError("Birthdate format must be %d/%m/%Y (day/month/year).") |
| 147 | + |
| 148 | + today = datetime.datetime.utcnow() |
| 149 | + |
| 150 | + age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day)) |
| 151 | + if age < 18: |
| 152 | + raise BadRequestError("You must be 18 years old or older.") |
| 153 | + current_user.birthdate = birthdate |
| 154 | + current_user.save() |
| 155 | + return Success("Birthdate successfully modified!") |
| 156 | + |
| 157 | + |
| 158 | +@profile_edit_bp.route("/profile/edit/tags", methods=["PATCH"]) |
| 159 | +@validate_params({"tags": list}) |
| 160 | +@jwt_required |
| 161 | +def edit_profile_tags(): |
| 162 | + if not current_user.is_profile_completed: |
| 163 | + raise BadRequestError("The user has not completed his profile.", "Complete your profile and try again.") |
| 164 | + data = request.get_json() |
| 165 | + tags = data["tags"] |
| 166 | + current_tags = Tag.get_multis(user_id=current_user.id) |
| 167 | + for t in current_tags: |
| 168 | + t.delete() |
| 169 | + for tag in tags: |
| 170 | + Tag.create(name=tag, user_id=current_user.id) |
| 171 | + return Success("Tags successfully modified!") |
146 | 172 |
|
147 | 173 |
|
148 | 174 | @profile_edit_bp.route("/profile/edit/email", methods=["PUT"]) |
|
0 commit comments