Skip to content

Commit a93b5d4

Browse files
committed
Added notifications table
1 parent 9031e8b commit a93b5d4

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
PyMatcha - A Python Dating Website
3+
Copyright (C) 2018-2019 jlasne/gmorer
4+
<jlasne@student.42.fr> - <lauris.skraucis@gmail.com>
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 logging
22+
from datetime import datetime
23+
24+
from PyMatcha.utils import create_notifications_table
25+
from PyMatcha.utils.orm import Field
26+
from PyMatcha.utils.orm import Model
27+
28+
29+
class Notification(Model):
30+
table_name = "notifications"
31+
32+
id = Field(int, modifiable=False)
33+
dt_received = Field(datetime, fmt="%Y-%m-%d %H:%M:%S")
34+
content = Field(str)
35+
type = Field(str)
36+
is_seen = Field(bool)
37+
link_to = Field(str)
38+
39+
@staticmethod
40+
def create(
41+
content: str, type: str, link_to: str, is_seen: bool = False, dt_received: datetime = datetime.utcnow()
42+
) -> Notification:
43+
new_notif = Notification(content=content, type=type, link_to=link_to, is_seen=is_seen, dt_received=dt_received)
44+
new_notif.save()
45+
logging.debug("Creating new notification")
46+
return new_notif
47+
48+
@classmethod
49+
def create_table(cls):
50+
create_notifications_table(cls.db)

backend/PyMatcha/utils/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from PyMatcha.utils.tables import _create_likes_table
55
from PyMatcha.utils.tables import _create_matches_table
66
from PyMatcha.utils.tables import _create_messages_table
7+
from PyMatcha.utils.tables import _create_notifications_table
78
from PyMatcha.utils.tables import _create_reports_table
89
from PyMatcha.utils.tables import _create_tags_table
910
from PyMatcha.utils.tables import _create_user_table
@@ -19,6 +20,7 @@
1920
create_messages_table = _create_messages_table
2021
create_images_table = _create_images_table
2122
create_blocks_table = _create_blocks_table
23+
create_notifications_table = _create_notifications_table
2224

2325
__all__ = [
2426
"hash_password",
@@ -32,4 +34,5 @@
3234
"create_messages_table",
3335
"create_images_table",
3436
"create_blocks_table",
37+
"create_notifications_table",
3538
]

backend/PyMatcha/utils/tables.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def _create_images_table(db):
192192
user_id INT NOT NULL,
193193
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
194194
link VARCHAR(256) NOT NULL,
195-
is_primary BOOLEAN DEFAULT FALSE
195+
is_primary BOOLEAN DEFAULT FALSE
196196
) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
197197
"""
198198
)
@@ -219,6 +219,27 @@ def _create_blocks_table(db):
219219
c.close()
220220

221221

222+
def _create_notifications_table(db):
223+
with db.cursor() as c:
224+
logging.info("Creating table notifications.")
225+
c.execute(DISABLE_SQL_NOTES)
226+
c.execute(
227+
"""
228+
CREATE TABLE IF NOT EXISTS notifications
229+
(
230+
id INT auto_increment PRIMARY KEY,
231+
dt_received DATETIME DEFAULT NOW(),
232+
content LONGTEXT NOT NULL,
233+
type ENUM('match', 'like', 'superlike', 'unlike', 'view', 'message'),
234+
is_seen BOOLEAN DEFAULT FALSE,
235+
link_to VARCHAR(256)
236+
) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
237+
"""
238+
)
239+
c.execute(ENABLE_SQL_NOTES)
240+
c.close()
241+
242+
222243
def create_tables(db):
223244
_create_user_table(db)
224245
_create_tags_table(db)
@@ -229,3 +250,4 @@ def create_tables(db):
229250
_create_messages_table(db)
230251
_create_images_table(db)
231252
_create_blocks_table(db)
253+
_create_notifications_table(db)

0 commit comments

Comments
 (0)