More skip days commands
This commit is contained in:
parent
32a185060b
commit
3e1c780169
3 changed files with 54 additions and 11 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
from .models import database, Repeat, SkipDay, OrderStatus, PunishmentStatus
|
from .models import database, Repeat, SkipDay, OrderStatus, PunishmentStatus
|
||||||
|
|
||||||
def initdb():
|
def initdb():
|
||||||
|
|
@ -32,6 +34,16 @@ def repeat_clear():
|
||||||
def skip_day_put(date):
|
def skip_day_put(date):
|
||||||
return SkipDay.create(date=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):
|
def skip_day_contains(date):
|
||||||
q = SkipDay.select().where(SkipDay.date == date)
|
q = SkipDay.select().where(SkipDay.date == date)
|
||||||
return len(q) > 0
|
return len(q) > 0
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,14 @@
|
||||||
import re
|
|
||||||
import logging
|
import logging
|
||||||
import peewee
|
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
|
from .telegram import TelegramCommand
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class SkipDayAddCommand(TelegramCommand):
|
class SkipDayAddCommand(TelegramCommand):
|
||||||
command_text = "/skip_day"
|
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):
|
async def exec_inner(self, text, update, session, forResponse=None, reply=None):
|
||||||
try:
|
try:
|
||||||
|
|
@ -17,15 +16,44 @@ class SkipDayAddCommand(TelegramCommand):
|
||||||
|
|
||||||
response = await forResponse()
|
response = await forResponse()
|
||||||
|
|
||||||
m = self.date_regex.match(response)
|
skip_day = datetime.date.fromisoformat(response)
|
||||||
if(m is not None):
|
skip_day_put(skip_day)
|
||||||
skip_day_put(m.group('date'))
|
|
||||||
yield f"Skip day {m.group('date')} has been added"
|
yield f"Skip day {response} has been added"
|
||||||
else:
|
except ValueError:
|
||||||
yield "Please enter a valid date"
|
yield "That date was not valid"
|
||||||
except peewee.IntegrityError:
|
except peewee.IntegrityError:
|
||||||
yield "That day has already been added"
|
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 = [
|
commands = [
|
||||||
SkipDayAddCommand()
|
SkipDayAddCommand(),
|
||||||
|
SkipDayDeleteCommand(),
|
||||||
|
SkipDaysCommand()
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ class TelegramCommand():
|
||||||
|
|
||||||
def matches(self, text):
|
def matches(self, text):
|
||||||
return (
|
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)
|
(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):
|
if(chat_id != TELEGRAM_CHAT_ID):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if('text' not in update['message']):
|
||||||
|
continue
|
||||||
|
|
||||||
if(chat_id in command_futures):
|
if(chat_id in command_futures):
|
||||||
command_futures[chat_id].set_result(
|
command_futures[chat_id].set_result(
|
||||||
update['message']['text']
|
update['message']['text']
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue