Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of otaku-info-web.
6otaku-info-web is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11otaku-info-web is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with otaku-info-web. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from typing import Dict, Any
21from puffotter.flask.base import db
22from puffotter.flask.db.User import User
23from puffotter.flask.db.ModelMixin import ModelMixin
24from otaku_info_web.Config import Config
25from bokkichat.entities.message.TextMessage import TextMessage
26from bokkichat.entities.Address import Address
29class TelegramChatId(ModelMixin, db.Model):
30 """
31 Database model that stores a telegram chat ID for a user
32 """
34 __tablename__ = "telegram_chat_ids"
35 """
36 The name of the database table
37 """
39 def __init__(self, *args, **kwargs):
40 """
41 Initializes the Model
42 :param args: The constructor arguments
43 :param kwargs: The constructor keyword arguments
44 """
45 super().__init__(*args, **kwargs)
47 user_id: int = db.Column(
48 db.Integer,
49 db.ForeignKey(
50 "users.id", ondelete="CASCADE", onupdate="CASCADE"
51 ),
52 nullable=False,
53 unique=True
54 )
55 """
56 The ID of the user associated with this telegram chat ID
57 """
59 user: User = db.relationship(
60 "User",
61 backref=db.backref(
62 "telegram_chat_ids", lazy=True, cascade="all,delete"
63 )
64 )
65 """
66 The user associated with this telegram chat ID
67 """
69 chat_id: str = db.Column(db.String(255), nullable=False)
70 """
71 The telegram chat ID
72 """
74 def send_message(self, text: str):
75 """
76 Sends a message to the chat ID
77 :param text: The text to send
78 :return: None
79 """
80 telegram = Config.TELEGRAM_BOT_CONNECTION
81 telegram.send(TextMessage(
82 telegram.address,
83 Address(self.chat_id),
84 text
85 ))
87 def __json__(self, include_children: bool = False) -> Dict[str, Any]:
88 """
89 Generates a dictionary containing the information of this model
90 :param include_children: Specifies if children data models
91 will be included or if they're limited to IDs
92 :return: A dictionary representing the model's values
93 """
94 data = {
95 "id": self.id,
96 "user_id": self.user_id,
97 "chat_id": self.chat_id
98 }
99 if include_children:
100 data["user"] = self.user.__json__(include_children)
101 return data