Skip to content

Commit 3ca6e1b

Browse files
committed
Added matches table
1 parent 0b51eab commit 3ca6e1b

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

backend/PyMatcha/models/match.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
def before_init(self, data):
38+
pass
39+
40+
@staticmethod
41+
def create(user_1: int, user_2: int, dt_matched: datetime.datetime = datetime.datetime.utcnow()) -> Match:
42+
new_match = Match(user_1=user_1, user_2=user_2, dt_matched=dt_matched)
43+
new_match.save()
44+
logging.debug("Creating new match")
45+
return new_match
46+
47+
@classmethod
48+
def create_table(cls):
49+
create_matches_table(cls.db)

backend/PyMatcha/models/user.py

Lines changed: 24 additions & 2 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
@@ -330,7 +332,7 @@ def get_likes_received(self):
330332
likes = c.fetchall()
331333
like_list = []
332334
for like in likes:
333-
like_list.append(Report(like))
335+
like_list.append(Like(like))
334336
return like_list
335337

336338
def get_likes_sent(self):
@@ -351,7 +353,7 @@ def get_likes_sent(self):
351353
likes = c.fetchall()
352354
like_list = []
353355
for like in likes:
354-
like_list.append(Report(like))
356+
like_list.append(Like(like))
355357
return like_list
356358

357359
def already_likes(self, liked_id: int) -> bool:
@@ -371,6 +373,26 @@ def already_likes(self, liked_id: int) -> bool:
371373
value = next(iter(result.values()))
372374
return bool(value)
373375

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

375397
def get_user(uid: Any[int, str]) -> Optional[User]:
376398
not_found = 0

backend/PyMatcha/utils/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from PyMatcha.utils.password import hash_password
22
from PyMatcha.utils.tables import _create_likes_table
3+
from PyMatcha.utils.tables import _create_matches_table
34
from PyMatcha.utils.tables import _create_reports_table
45
from PyMatcha.utils.tables import _create_tags_table
56
from PyMatcha.utils.tables import _create_user_table
@@ -11,6 +12,7 @@
1112
create_views_table = _create_views_table
1213
create_reports_table = _create_reports_table
1314
create_likes_table = _create_likes_table
15+
create_matches_table = _create_matches_table
1416

1517
__all__ = [
1618
"hash_password",
@@ -20,4 +22,5 @@
2022
"create_views_table",
2123
"create_reports_table",
2224
"create_likes_table",
25+
"create_matches_table",
2326
]

backend/PyMatcha/utils/tables.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,29 @@ def _create_likes_table(db):
130130
c.execute(ENABLE_SQL_NOTES)
131131

132132

133+
def _create_matches_table(db):
134+
with db.cursor() as c:
135+
logging.info("Creating table matches.")
136+
c.execute(DISABLE_SQL_NOTES)
137+
c.execute(
138+
"""
139+
CREATE TABLE IF NOT EXISTS matches
140+
(
141+
id INT auto_increment PRIMARY KEY,
142+
user_1 INT NOT NULL,
143+
user_2 INT NOT NULL,
144+
dt_matched DATETIME DEFAULT NOW()
145+
)
146+
"""
147+
)
148+
# TODO: Is conversation started boolean ?
149+
c.execute(ENABLE_SQL_NOTES)
150+
151+
133152
def create_tables(db):
134153
_create_user_table(db)
135154
_create_tags_table(db)
136155
_create_views_table(db)
137156
_create_reports_table(db)
138157
_create_likes_table(db)
158+
_create_matches_table(db)

0 commit comments

Comments
 (0)