Skip to content

Commit 8953be7

Browse files
authored
Merge pull request #181 from Seluj78/matches
2 parents 0b51eab + 65ad2b7 commit 8953be7

27 files changed

+927
-508
lines changed

PyMatcha.postman_collection.json

Lines changed: 153 additions & 86 deletions
Large diffs are not rendered by default.

backend/PyMatcha/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898

9999
@jwt.expired_token_loader
100100
def expired_token_callback(expired_token):
101-
logging.warning("Token {} expired".format(expired_token))
101+
logging.debug("Token {} expired".format(expired_token))
102102
resp = {
103103
"code": 401,
104104
"error": {
@@ -187,16 +187,30 @@ def check_if_token_is_revoked(decrypted_token):
187187

188188
from PyMatcha.routes.api.ping_pong import ping_pong_bp
189189
from PyMatcha.routes.api.user import user_bp
190-
from PyMatcha.routes.api.auth import auth_bp
191-
from PyMatcha.routes.api.profile import profile_bp
190+
from PyMatcha.routes.api.auth.email import auth_email_bp
191+
from PyMatcha.routes.api.auth.password import auth_password_bp
192+
from PyMatcha.routes.api.auth.register import auth_register_bp
193+
from PyMatcha.routes.api.auth.login import auth_login_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
192198
from PyMatcha.routes.api.like import like_bp
199+
from PyMatcha.routes.api.match import match_bp
193200

194201
logging.debug("Registering Flask blueprints")
195202
application.register_blueprint(ping_pong_bp)
196203
application.register_blueprint(user_bp)
197-
application.register_blueprint(auth_bp)
198-
application.register_blueprint(profile_bp)
204+
application.register_blueprint(auth_email_bp)
205+
application.register_blueprint(auth_password_bp)
206+
application.register_blueprint(auth_register_bp)
207+
application.register_blueprint(auth_login_bp)
208+
application.register_blueprint(profile_view_bp)
209+
application.register_blueprint(profile_edit_bp)
210+
application.register_blueprint(profile_complete_bp)
211+
application.register_blueprint(profile_report_bp)
199212
application.register_blueprint(like_bp)
213+
application.register_blueprint(match_bp)
200214

201215
if application.debug:
202216
logging.debug("Registering debug route")

backend/PyMatcha/models/like.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class Like(Model):
3535
dt_liked = Field(datetime.datetime, fmt="%Y-%m-%d %H:%M:%S")
3636
is_superlike = Field(bool)
3737

38-
def before_init(self, data):
39-
pass
40-
4138
@staticmethod
4239
def create(
4340
liker_id: int,

backend/PyMatcha/models/match.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 __future__ import annotations
20+
21+
import datetime
22+
import logging
23+
24+
from PyMatcha.utils import create_matches_table
25+
from PyMatcha.utils.orm import Field
26+
from PyMatcha.utils.orm import Model
27+
28+
29+
class Match(Model):
30+
table_name = "matches"
31+
32+
id = Field(int, modifiable=False)
33+
user_1 = Field(int)
34+
user_2 = Field(int)
35+
dt_matched = Field(datetime.datetime, fmt="%Y-%m-%d %H:%M:%S")
36+
37+
@staticmethod
38+
def create(user_1: int, user_2: int, dt_matched: datetime.datetime = datetime.datetime.utcnow()) -> Match:
39+
new_match = Match(user_1=user_1, user_2=user_2, dt_matched=dt_matched)
40+
new_match.save()
41+
logging.debug("Creating new match")
42+
return new_match
43+
44+
@classmethod
45+
def create_table(cls):
46+
create_matches_table(cls.db)

backend/PyMatcha/models/report.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ class Report(Model):
3737
reason = Field(str)
3838
status = Field(str)
3939

40-
def before_init(self, data):
41-
pass
42-
4340
@staticmethod
4441
def create(
4542
reported_id: int,

backend/PyMatcha/models/tag.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ class Tag(Model):
3232
user_id = Field(int)
3333
name = Field(str)
3434

35-
def before_init(self, data):
36-
pass
37-
3835
@staticmethod
3936
def create(user_id: int, name="") -> Tag:
4037
new_tag = Tag(user_id=user_id, name=name)

backend/PyMatcha/models/user.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from typing import Optional
2727

2828
import Geohash
29+
from PyMatcha.models.like import Like
30+
from PyMatcha.models.match import Match
2931
from PyMatcha.models.report import Report
3032
from PyMatcha.models.tag import Tag
3133
from PyMatcha.models.view import View
@@ -60,12 +62,6 @@ class User(Model):
6062
confirmed_on = Field(datetime.datetime, fmt="%Y-%m-%d %H:%M:%S")
6163
previous_reset_token = Field(str)
6264

63-
def before_init(self, data):
64-
pass
65-
# Not used, use User.create and password will be hashed.
66-
# if "password" in data:
67-
# self.password.value = hash_password(data["password"])
68-
6965
def check_password(self, password: str) -> bool:
7066
logging.debug("Checking password again {} hashed password".format(self.id))
7167
_hash, salt = self.password.split(":")
@@ -166,7 +162,7 @@ def register(email: str, username: str, password: str, first_name: str, last_nam
166162
except ValueError:
167163
pass
168164
else:
169-
logging.warning("Email {} taken".format(email))
165+
logging.debug("Email {} taken".format(email))
170166
raise ConflictError("Email {} taken".format(email), "Use another email")
171167

172168
# Check username availability
@@ -330,7 +326,7 @@ def get_likes_received(self):
330326
likes = c.fetchall()
331327
like_list = []
332328
for like in likes:
333-
like_list.append(Report(like))
329+
like_list.append(Like(like))
334330
return like_list
335331

336332
def get_likes_sent(self):
@@ -351,7 +347,7 @@ def get_likes_sent(self):
351347
likes = c.fetchall()
352348
like_list = []
353349
for like in likes:
354-
like_list.append(Report(like))
350+
like_list.append(Like(like))
355351
return like_list
356352

357353
def already_likes(self, liked_id: int) -> bool:
@@ -371,6 +367,26 @@ def already_likes(self, liked_id: int) -> bool:
371367
value = next(iter(result.values()))
372368
return bool(value)
373369

370+
def get_matches(self):
371+
logging.debug("Getting all matches for user {}".format(self.id))
372+
with self.db.cursor() as c:
373+
c.execute(
374+
"""
375+
SELECT matches.id as id, matches.user_1 as user_1,
376+
matches.user_2 as user_2, matches.dt_matched as dt_matched
377+
FROM users
378+
INNER JOIN matches on users.id = matches.user_1 or users.id = matches.user_2
379+
WHERE users.id = CAST({} AS UNSIGNED)
380+
""".format(
381+
self.id
382+
)
383+
)
384+
matches = c.fetchall()
385+
match_list = []
386+
for match in matches:
387+
match_list.append(Match(match))
388+
return match_list
389+
374390

375391
def get_user(uid: Any[int, str]) -> Optional[User]:
376392
not_found = 0
@@ -400,7 +416,7 @@ def get_user(uid: Any[int, str]) -> Optional[User]:
400416
f_user = user
401417
# If none of those worked, throw an error
402418
if not_found == 3:
403-
logging.warning("User {} not found.".format(uid))
419+
logging.debug("User {} not found.".format(uid))
404420
raise NotFoundError("User {} not found.".format(uid), "Try again with another uid")
405421
logging.debug("Found user {} from {}".format(f_user.id, uid))
406422
return f_user

backend/PyMatcha/models/view.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ class View(Model):
3434
viewer_id = Field(int)
3535
dt_seen = Field(datetime.datetime)
3636

37-
def before_init(self, data):
38-
pass
39-
4037
@staticmethod
4138
def create(profile_id: int, viewer_id: int, dt_seen: datetime.datetime = datetime.datetime.utcnow()) -> View:
4239
new_view = View(profile_id=profile_id, viewer_id=viewer_id, dt_seen=dt_seen)

0 commit comments

Comments
 (0)