Skip to content

Commit bb1d312

Browse files
committed
Fix how slot weekday name is determined
1 parent fb4d0a3 commit bb1d312

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

src/pycamp_bot/commands/projects.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
from pycamp_bot.commands.base import msg_to_active_pycamp_chat
99
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
1010
from pycamp_bot.commands.auth import admin_needed, get_admins_username
11-
from pycamp_bot.commands.schedule import DIAS
12-
from pycamp_bot.utils import escape_markdown
11+
from pycamp_bot.utils import escape_markdown, get_slot_weekday_name
1312

1413
current_projects = {}
1514

@@ -546,7 +545,7 @@ async def show_my_projects(update, context):
546545

547546
for vote in votes:
548547
slot_day_code = vote.project.slot.code[0]
549-
slot_day_name = DIAS[slot_day_code]
548+
slot_day_name = get_slot_weekday_name(slot_day_code)
550549

551550
if slot_day_code != prev_slot_day_code:
552551
text_chunks.append(f'*{slot_day_name}*')

src/pycamp_bot/commands/schedule.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pycamp_bot.commands.auth import admin_needed
55
from pycamp_bot.scheduler.db_to_json import export_db_2_json
66
from pycamp_bot.scheduler.schedule_calculator import export_scheduled_result
7-
from pycamp_bot.utils import escape_markdown
7+
from pycamp_bot.utils import escape_markdown, get_slot_weekday_name
88

99

1010
DAY_SLOT_TIME = {
@@ -14,17 +14,6 @@
1414
}
1515

1616

17-
DIAS = {
18-
'A':'Jueves',
19-
'B':'Viernes',
20-
'C':'Sabado',
21-
'D':'Domingo',
22-
'E':'Lunes',
23-
'F':'Martes',
24-
'G':'Miercoles',
25-
}
26-
27-
2817
async def cancel(update, context):
2918
await context.bot.send_message(
3019
chat_id=update.message.chat_id,
@@ -153,29 +142,34 @@ async def make_schedule(update, context):
153142
)
154143

155144

156-
async def check_day_tab(day, slots, cronograma, i):
157-
try:
158-
if day != DIAS[slots[i-1].code[0]]:
159-
cronograma.append('')
160-
cronograma.append(f'*{day}:*')
161-
except Exception as e:
162-
print("ERROR ", e)
145+
async def check_day_tab(slot, prev_slot, cronograma):
146+
def append_day_name():
147+
cronograma.append(f'*{get_slot_weekday_name(slot.code[0])}:*')
148+
149+
if prev_slot is None:
150+
append_day_name()
151+
elif slot.code[0] != prev_slot.code[0]:
152+
cronograma.append('')
153+
append_day_name()
163154

164155

165156
async def show_schedule(update, context):
166157
slots = Slot.select()
167158
projects = Project.select()
168159
cronograma = []
169160

161+
prev_slot = None
162+
170163
for i, slot in enumerate(slots):
171-
day = DIAS[slot.code[0]]
172-
await check_day_tab(day, slots, cronograma, i)
164+
await check_day_tab(slot, prev_slot, cronograma)
173165

174166
for project in projects:
175167
if project.slot_id == slot.id:
176168
cronograma.append(f'{slot.start}:00 *{escape_markdown(project.name)}*')
177169
cronograma.append(f'Owner: @{escape_markdown(project.owner.username)}')
178170

171+
prev_slot = slot
172+
179173
await context.bot.send_message(
180174
chat_id=update.message.chat_id,
181175
text='\n'.join(cronograma),

src/pycamp_bot/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pycamp_bot.models import Pycamp
2+
13
def escape_markdown(string):
24
# See: https://core.telegram.org/bots/api#markdownv2-style
35

@@ -7,3 +9,24 @@ def escape_markdown(string):
79
new_string = new_string.replace(char, f'\\{char}')
810

911
return new_string
12+
13+
14+
def get_slot_weekday_name(slot_day_code):
15+
ISO_WEEKDAY_NAMES = {
16+
0: 'Lunes',
17+
1: 'Martes',
18+
2: 'Miércoles',
19+
3: 'Jueves',
20+
4: 'Viernes',
21+
5: 'Sábado',
22+
6: 'Domingo',
23+
}
24+
25+
pycamp_start_weekday = Pycamp.get(Pycamp.active == True).init.weekday()
26+
27+
# Convert slot day code to a zero-based code, to use it as an
28+
# offset to get the weekday name of the slot
29+
offset = ord(slot_day_code) - ord('A')
30+
day_name = ISO_WEEKDAY_NAMES[pycamp_start_weekday + offset]
31+
32+
return day_name

0 commit comments

Comments
 (0)