aimusicdetection / db_models /notification.py
nininigold's picture
Upload folder using huggingface_hub
3cecacc verified
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Float, Text
from sqlalchemy.orm import relationship
from datetime import datetime
from database import Base
import json
class Notification(Base):
__tablename__ = 'notifications'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id'),nullable=True)
user = relationship("User", back_populates="notifications")
template_key = Column(String, ForeignKey('notification_templates.key')) # ํ…œํ”Œ๋ฆฟ ํ‚ค ์ฐธ์กฐ
template = relationship("NotificationTemplate")
created_at = Column(DateTime, default=datetime.utcnow)
data = Column(String)
# 'type' ํ•„๋“œ๋ฅผ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ์•Œ๋ฆผ ํƒ€์ž…์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
type = Column(String)
is_read = Column(Boolean, default=False)
deleted = Column(Boolean, default=False)
ko_template = Column(String, default=None) # ํ•œ๊ตญ์–ด ๋ฉ”์‹œ์ง€ ํ…œํ”Œ๋ฆฟ
ko_status_template = Column(String, default=None)
en_template = Column(String, default=None) #
en_status_template = Column(String, default=None)
def get_message(self, lang):
template = self.template
if lang == 'ko':
if not self.ko_template or not self.ko_status_template: # ์ฒ˜๋ฆฌํ•ด๋‘”๊ฒŒ ์—†์œผ๋ฉด ํ•ด๋‹น template์— ๋งž๊ฒŒ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค
template_string = template.ko_template
template_status = template.ko_status_template
data_dict = json.loads(self.data)
result = template_string.format(**data_dict)
self.ko_template = result
self.ko_status_template = template_status
else: # # ์ฒ˜๋ฆฌํ•ด๋‘”๊ฒŒ ์žˆ์œผ๋ฉด ๊ทธ๋ƒฅ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
result = self.ko_template
template_status = self.ko_status_template
elif lang == 'en':
if not self.en_template or not self.en_status_template:
template_string = template.en_template
template_status = template.en_status_template
data_dict = json.loads(self.data)
result = template_string.format(**data_dict)
self.en_template = result
self.en_status_template = template_status
else:
result = self.en_template
template_status = self.en_status_template
else:
raise ValueError(f"Unsupported language: {lang}")
return result, template_status
class NotificationTemplate(Base):
__tablename__ = 'notification_templates'
id = Column(Integer, primary_key=True)
key = Column(String, unique=True)
ko_template = Column(String) # ํ•œ๊ตญ์–ด ๋ฉ”์‹œ์ง€ ํ…œํ”Œ๋ฆฟ
ko_status_template = Column(String)
en_template = Column(String) #
en_status_template = Column(String)
""" ์—ฌ๊ธฐ๋Š” ์‚ฌ์šฉ ์˜ˆ์‹œ
template = NotificationTemplate(
key='music_test_complete',
ko_template='์Œ์› ๊ฒ€์‚ฌ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ํ‘œ์ ˆ๋ฅ ์€ {rate}% ์ž…๋‹ˆ๋‹ค.',
en_template='Music Test Complete! The Plagiarism Rate is {rate}%.'
)
session.add(template)
session.commit() -> ์ด๋ ‡๊ฒŒ ์ž”๋œฉ ๋„ฃ์–ด๋‘๊ณ  ์“ฐ๋ผ๋Š”๊ฑฐ ์•„๋…€..
alert = Notification(
user_id=1,
template_key='music_test_complete',
data=json.dumps({'rate': 30}) # ํฌ๋งทํŒ…์— ์‚ฌ์šฉ๋  ๋ฐ์ดํ„ฐ๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ์ €์žฅ
)
session.add(alert)
session.commit()
# ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์กฐํšŒ
alert = session.query(Notification).first()
print(alert.get_message('ko')) # ํ•œ๊ตญ์–ด ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
print(alert.get_message('en')) # ์˜์–ด ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
"""
""" ์—ฌ๊ธฐ๋Š” key์™€ ํฌ๋งทํŒ… ๋ฐ์ดํ„ฐ ๋ชจ์Œ. ๊ผด์€ Dict๋กœ ์จ๋‘๊ธด ํ•˜๋Š”๋ฐ 'key' : 'format' ๋Š๋‚Œ์œผ๋กœ ๊ฑ ๋ณด๊ณ  ์“ฐ๋ฉด ๋˜๋Š”๊ฑฐ์ž„
keys_and_formattings={
'music_test_complete' : {'rate': float.2f}
'update_notice_templage' : {'date': String} # YYYY-MM-DD
}
"""