-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
68 lines (61 loc) · 2.27 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask_login import UserMixin
from database import get_db
from datetime import datetime
class User(UserMixin):
def __init__(self, id, name, email, phone=None, address=None, about=None, profile_image=None):
self.id = id
self.name = name
self.email = email
self.phone = phone
self.address = address
self.about = about
self.profile_image = profile_image
@staticmethod
def get(user_id):
connection = get_db()
cursor = connection.cursor()
cursor.execute("""
SELECT id, name, email, phone, address, about, profile_image
FROM users
WHERE id=%s
""", (user_id,))
user = cursor.fetchone()
cursor.close()
if not user:
return None
# Sesuaikan urutan atribut dengan query SELECT
return User(user[0], user[1], user[2], user[3], user[4], user[5], user[6])
class Notification:
def __init__(self, user_id, message, created_at, read=False):
self.user_id = user_id
self.message = message
self.created_at = created_at
self.read = read
@staticmethod
def get_new_notifications():
connection = get_db()
cursor = connection.cursor()
cursor.execute("""
SELECT u.name, n.message, TIMESTAMPDIFF(MINUTE, n.created_at, NOW()) as time_ago
FROM notifications n
JOIN users u ON n.user_id = u.id
WHERE n.read = FALSE
ORDER BY n.created_at DESC
""")
notifications = cursor.fetchall()
cursor.close()
return [{'user_name': notif[0], 'message': notif[1], 'time_ago': f'{notif[2]} min'} for notif in notifications]
@staticmethod
def get_old_notifications():
connection = get_db()
cursor = connection.cursor()
cursor.execute("""
SELECT u.name, n.message, TIMESTAMPDIFF(MINUTE, n.created_at, NOW()) as time_ago
FROM notifications n
JOIN users u ON n.user_id = u.id
WHERE n.read = TRUE
ORDER BY n.created_at DESC
""")
notifications = cursor.fetchall()
cursor.close()
return [{'user_name': notif[0], 'message': notif[1], 'time_ago': f'{notif[2]} min'} for notif in notifications]