More skip days commands

This commit is contained in:
Johnny Gear 2025-11-16 11:58:34 -06:00
parent 32a185060b
commit 3e1c780169
3 changed files with 54 additions and 11 deletions

View file

@ -1,3 +1,5 @@
import datetime
from .models import database, Repeat, SkipDay, OrderStatus, PunishmentStatus
def initdb():
@ -32,6 +34,16 @@ def repeat_clear():
def skip_day_put(date):
return SkipDay.create(date=date)
def skip_day_delete(date):
q = SkipDay.delete().where(SkipDay.date == date)
return q.execute()
def skip_days_upcoming():
return (SkipDay.select()
.where(SkipDay.date >= datetime.date.today())
.order_by(SkipDay.date)
.limit(10))
def skip_day_contains(date):
q = SkipDay.select().where(SkipDay.date == date)
return len(q) > 0

View file

@ -1,15 +1,14 @@
import re
import logging
import peewee
import datetime
from db.queries import skip_day_put
from db.queries import skip_day_put, skip_day_delete, skip_days_upcoming
from .telegram import TelegramCommand
logger = logging.getLogger(__name__)
class SkipDayAddCommand(TelegramCommand):
command_text = "/skip_day"
date_regex = re.compile(r"^(?P<date>\d{4}-\d{2}-\d{2})$")
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
try:
@ -17,15 +16,44 @@ class SkipDayAddCommand(TelegramCommand):
response = await forResponse()
m = self.date_regex.match(response)
if(m is not None):
skip_day_put(m.group('date'))
yield f"Skip day {m.group('date')} has been added"
else:
yield "Please enter a valid date"
skip_day = datetime.date.fromisoformat(response)
skip_day_put(skip_day)
yield f"Skip day {response} has been added"
except ValueError:
yield "That date was not valid"
except peewee.IntegrityError:
yield "That day has already been added"
class SkipDayDeleteCommand(TelegramCommand):
command_text = "/skip_day_delete"
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
try:
yield "Please enter a skip day to delete"
response = await forResponse()
skip_day = datetime.date.fromisoformat(response)
rows = skip_day_delete(skip_day)
if rows > 0:
yield f"Skip day {skip_day} has been deleted"
else:
yield f"Skip day {skip_day} does not exist"
except ValueError:
yield "That date was not valid"
class SkipDaysCommand(TelegramCommand):
command_text = "/skip_days"
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
dates = skip_days_upcoming()
yield ("Upcoming skip days -\n" +
"\n".join([d.date.isoformat() for d in dates]))
commands = [
SkipDayAddCommand()
SkipDayAddCommand(),
SkipDayDeleteCommand(),
SkipDaysCommand()
]

View file

@ -60,7 +60,7 @@ class TelegramCommand():
def matches(self, text):
return (
(self.command_text is not None and text.startswith(self.command_text)) or
(self.command_text is not None and text == self.command_text) or
(self.command_regex is not None and self.command_regex.match(text) is not None)
)
@ -99,6 +99,9 @@ async def handle_commands(commands=[], loop=None):
if(chat_id != TELEGRAM_CHAT_ID):
continue
if('text' not in update['message']):
continue
if(chat_id in command_futures):
command_futures[chat_id].set_result(
update['message']['text']