|
1 | 1 | import datetime |
2 | | -from telegram.ext import CommandHandler |
| 2 | +from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters |
3 | 3 | from pycamp_bot.models import Pycamp |
4 | 4 | from pycamp_bot.models import Pycampista |
5 | 5 | from pycamp_bot.models import PycampistaAtPycamp |
6 | 6 | from pycamp_bot.commands.auth import admin_needed |
7 | 7 | from pycamp_bot.logger import logger |
8 | 8 |
|
9 | 9 |
|
| 10 | +SET_DATE_STATE = "set_fate" |
| 11 | +SET_DURATION_STATE = "set_duration" |
| 12 | +WRAP_UP_STATE = "wrap_up" |
| 13 | + |
| 14 | + |
10 | 15 | def get_pycamp_by_name(name): |
11 | 16 | pycamps = Pycamp.select().where(Pycamp.headquarters == name) |
12 | 17 | count = pycamps.count() |
@@ -68,36 +73,98 @@ async def set_active_pycamp(update, context): |
68 | 73 |
|
69 | 74 | @admin_needed |
70 | 75 | async def add_pycamp(update, context): |
71 | | - parameters = update.message.text.split(' ') |
72 | | - if not len(parameters) == 2: |
| 76 | + parameters = update.message.text.split(' ', 1) |
| 77 | + if len(parameters) < 2: |
73 | 78 | await context.bot.send_message( |
74 | 79 | chat_id=update.message.chat_id, |
75 | | - text="El comando necesita un parametro (pycamp name)") |
| 80 | + text="El comando necesita un parametro (headquarters)") |
| 81 | + return |
| 82 | + hq = parameters[1].strip() |
| 83 | + if not hq: |
| 84 | + await context.bot.send_message( |
| 85 | + chat_id=update.message.chat_id, |
| 86 | + text="El parámetro headquarters no puede ser vacío") |
76 | 87 | return |
77 | 88 |
|
78 | | - pycamp = Pycamp.get_or_create(headquarters=parameters[1])[0] |
| 89 | + pycamp = Pycamp.get_or_create(headquarters=hq, active=True)[0] |
| 90 | + pycamp.set_as_only_active() |
| 91 | + logger.info('Creado: {}'.format(pycamp)) |
79 | 92 |
|
| 93 | + msg = "El Pycamp {} fue creado.\n¿Cuándo empieza? (formato yyyy-mm-dd)" |
80 | 94 | await context.bot.send_message( |
81 | 95 | chat_id=update.message.chat_id, |
82 | | - text="El Pycamp {} fue creado.".format(pycamp.headquarters)) |
| 96 | + text=msg.format(pycamp.headquarters) |
| 97 | + ) |
| 98 | + return SET_DATE_STATE |
83 | 99 |
|
84 | 100 |
|
85 | | -@active_needed |
86 | | -@admin_needed |
87 | | -async def start_pycamp(update, context): |
88 | | - parameters = update.message.text.split(' ') |
89 | | - if len(parameters) == 2: |
90 | | - date = datetime.datetime.fromisoformat(parameters[1]) |
91 | | - else: |
92 | | - date = datetime.datetime.now() |
| 101 | +async def define_start_date(update, context): |
| 102 | + text = update.message.text |
| 103 | + try: |
| 104 | + start_date = datetime.datetime.fromisoformat(text) |
| 105 | + except ValueError: |
| 106 | + await context.bot.send_message( |
| 107 | + chat_id=update.message.chat_id, |
| 108 | + text="mmm no entiendo esa fecha, de nuevo?" |
| 109 | + ) |
| 110 | + return SET_DATE_STATE |
| 111 | + |
| 112 | + _, pycamp = get_active_pycamp() |
| 113 | + pycamp.init = start_date |
| 114 | + pycamp.save() |
93 | 115 |
|
94 | | - is_active, pycamp = get_active_pycamp() |
95 | | - pycamp.init = date |
| 116 | + await context.bot.send_message( |
| 117 | + chat_id=update.message.chat_id, |
| 118 | + text="¿Cuantos días dura el PyCamp?" |
| 119 | + ) |
| 120 | + return SET_DURATION_STATE |
| 121 | + |
| 122 | + |
| 123 | +async def define_duration(update, context): |
| 124 | + text = update.message.text.strip() |
| 125 | + try: |
| 126 | + duration = int(text) |
| 127 | + except ValueError: |
| 128 | + await context.bot.send_message( |
| 129 | + chat_id=update.message.chat_id, |
| 130 | + text="mmm no entiendo. Poné un número entero porfa." |
| 131 | + ) |
| 132 | + return SET_DURATION_STATE |
| 133 | + |
| 134 | + _, pycamp = get_active_pycamp() |
| 135 | + pycamp.end = pycamp.init + datetime.timedelta(days=duration - 1) |
96 | 136 | pycamp.save() |
97 | 137 |
|
| 138 | + msg = "Listo, el PyCamp '{}' está activo, desde el {} hasta el {}".format( |
| 139 | + pycamp.headquarters, |
| 140 | + pycamp.init.date(), |
| 141 | + pycamp.end.date() |
| 142 | + ) |
| 143 | + await context.bot.send_message( |
| 144 | + chat_id=update.message.chat_id, |
| 145 | + text=msg |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +async def cancel(update, context): |
98 | 150 | await context.bot.send_message( |
99 | 151 | chat_id=update.message.chat_id, |
100 | | - text="Empezó Pycamp :) ! {}".format(date)) |
| 152 | + text="Se canceló la carga del PyCamp...") |
| 153 | + return ConversationHandler.END |
| 154 | + |
| 155 | + |
| 156 | +load_start_pycamp = ConversationHandler( |
| 157 | + entry_points=[CommandHandler('empezar_pycamp', add_pycamp)], |
| 158 | + states={ |
| 159 | + SET_DATE_STATE: [MessageHandler(filters.TEXT, define_start_date)], |
| 160 | + SET_DURATION_STATE: [MessageHandler(filters.TEXT, define_duration)] |
| 161 | + }, |
| 162 | + fallbacks=[CommandHandler('cancel', cancel)] |
| 163 | +) |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
101 | 168 |
|
102 | 169 |
|
103 | 170 | @active_needed |
@@ -162,19 +229,14 @@ async def list_pycampistas(update, context): |
162 | 229 |
|
163 | 230 |
|
164 | 231 | def set_handlers(application): |
165 | | - application.add_handler( |
166 | | - CommandHandler('empezar_pycamp', start_pycamp)) |
| 232 | + application.add_handler(load_start_pycamp) |
167 | 233 | application.add_handler( |
168 | 234 | CommandHandler('terminar_pycamp', end_pycamp)) |
169 | 235 | application.add_handler( |
170 | 236 | CommandHandler('activar_pycamp', set_active_pycamp)) |
171 | | - application.add_handler( |
172 | | - CommandHandler('agregar_pycamp', add_pycamp)) |
173 | 237 | application.add_handler( |
174 | 238 | CommandHandler('pycamps', list_pycamps)) |
175 | 239 | application.add_handler( |
176 | 240 | CommandHandler('voy_al_pycamp', add_pycampista_to_pycamp)) |
177 | 241 | application.add_handler( |
178 | 242 | CommandHandler('pycampistas', list_pycampistas)) |
179 | | - |
180 | | - |
|
0 commit comments