add Link model

This commit is contained in:
Wojciech Kwolek 2022-01-09 04:36:03 +01:00
parent 41426351a0
commit 59e958bbaa
2 changed files with 45 additions and 8 deletions

28
main.py
View File

@ -1,11 +1,15 @@
import os
from dataclasses import dataclass
import sqlite3
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
import logging import logging
import os
import sqlite3
from dataclasses import dataclass
from datetime import datetime
from dotenv import load_dotenv from dotenv import load_dotenv
from telegram import Update
from telegram.ext import CallbackContext, CommandHandler, Updater
from models import Link
load_dotenv() load_dotenv()
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG,
@ -46,15 +50,23 @@ class Bot:
link TEXT, link TEXT,
user_id TEXT, user_id TEXT,
read_at DATETIME, read_at DATETIME,
added_at DATETIME, added_at DATETIME
postponed_at DATETIME );
)
""") """)
def cmd_start(self, update: Update, context: CallbackContext): def cmd_start(self, update: Update, context: CallbackContext):
context.bot.send_message(chat_id=update.effective_chat.id, text='Hi!') context.bot.send_message(chat_id=update.effective_chat.id, text='Hi!')
# TODO: timezones
# TODO: ask to set time or something # TODO: ask to set time or something
def cmd_test(self, update: Update, context: CallbackContext):
l = Link(
link="2137",
user_id="papiez"
)
with self.db as db:
l.create(db)
def run(self): def run(self):
self.updater.start_polling() self.updater.start_polling()

25
models.py Normal file
View File

@ -0,0 +1,25 @@
import sqlite3
from dataclasses import dataclass
from datetime import datetime
@dataclass
class Link:
link: str
user_id: str
id: int = None
read_at: datetime = None
added_at: datetime = None
def create(self, db: sqlite3.Connection):
self.added_at = datetime.now()
cur = db.cursor()
rows = (cur.execute(
"INSERT INTO links(link, user_id, added_at) VALUES(?, ?, ?) RETURNING id;",
(self.link, self.user_id, self.added_at)
))
r = list(rows)
assert len(r) == 1
assert len(r[0]) == 1
assert int(r[0][0])
self.id = r[0][0]