Skip to content

Commit b604dbd

Browse files
committed
put functionality on models
1 parent 14c9a69 commit b604dbd

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/pycamp_bot/models.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,24 @@ def set_as_only_active(self):
8989
self.active = True
9090
self.save()
9191

92+
def add_wizard(self, username, chat_id):
93+
pycampista = Pycampista.get_or_create(username=username, chat_id=chat_id)[0]
94+
pycampista.wizard = True
95+
pycampista.save()
96+
PycampistaAtPycamp.get_or_create(pycamp=self, pycampista=pycampista)
97+
return pycampista
98+
9299
def get_wizards(self):
93-
return Pycampista.select().where(Pycampista.wizard == 1)
100+
pac = PycampistaAtPycamp.select().join(Pycampista).where(
101+
(PycampistaAtPycamp.pycamp == self) &
102+
(PycampistaAtPycamp.pycampista.wizard == True)
103+
)
104+
return [p.pycampista for p in pac]
94105

95106
def get_current_wizard(self):
96107
"""Return the Pycampista instance that's the currently scheduled wizard."""
97108
now = datetime.now()
98-
current_wizards = WizardAtPycamp.select().where(
109+
current_wizards = WizardAtPycamp.select().where(
99110
(WizardAtPycamp.pycamp == self) &
100111
(WizardAtPycamp.init <= now) &
101112
(WizardAtPycamp.end > now)

src/pycamp_bot/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from pycamp_bot.models import Pycamp
2+
from pycamp_bot.logger import logger
3+
24

35
def escape_markdown(string):
46
# See: https://core.telegram.org/bots/api#markdownv2-style
@@ -30,3 +32,21 @@ def get_slot_weekday_name(slot_day_code):
3032
day_name = ISO_WEEKDAY_NAMES[pycamp_start_weekday + offset]
3133

3234
return day_name
35+
def active_pycamp_needed(f):
36+
from pycamp_bot.commands.manage_pycamp import get_active_pycamp
37+
async def wrap(*args, **kargs):
38+
update, context = args
39+
40+
_, pycamp = get_active_pycamp()
41+
if pycamp is None:
42+
msg = "🔥 %s: This operation (%s) needs an active PyCamp. Talk to an admin." % (
43+
update.message.from_user.username, str(f.__name__)
44+
)
45+
await context.bot.send_message(
46+
chat_id=update.message.chat_id,
47+
text=msg)
48+
logger.warning(msg)
49+
return
50+
51+
return await f(*args, pycamp=pycamp)
52+
return wrap

0 commit comments

Comments
 (0)