11import logging
22import peewee
3- from telegram .ext import ConversationHandler , CommandHandler , MessageHandler , filters
3+ from telegram import InlineKeyboardButton , InlineKeyboardMarkup
4+ from telegram .ext import CallbackQueryHandler , ConversationHandler , CommandHandler , MessageHandler , filters
45from pycamp_bot .models import Pycampista , Project , Slot , Vote
56from pycamp_bot .commands .base import msg_to_active_pycamp_chat
67from pycamp_bot .commands .manage_pycamp import active_needed , get_active_pycamp
1011
1112
1213current_projects = {}
13- NOMBRE , DIFICULTAD , TOPIC = ["nombre" , "dificultad" , "topic" ]
14+
15+ NOMBRE = "nombre"
16+ DIFICULTAD = "dificultad"
17+ TOPIC = "topic"
18+ CHECK_REPOSITORIO = "check_repositorio"
19+ REPOSITORIO = "repositorio"
20+
21+ REPO_EXISTS_PATTERN = 'repoexists'
22+ PROJECT_NAME_PATTERN = 'projectname'
1423
1524logger = logging .getLogger (__name__ )
1625
@@ -57,7 +66,11 @@ async def naming_project(update, context):
5766 username = update .message .from_user .username
5867 name = update .message .text
5968
69+ user = Pycampista .get_or_create (username = username , chat_id = update .message .chat_id )[0 ]
70+
6071 new_project = Project (name = name )
72+ new_project .owner = user
73+
6174 current_projects [username ] = new_project
6275
6376 await context .bot .send_message (
@@ -118,26 +131,81 @@ async def project_topic(update, context):
118131 new_project = current_projects [username ]
119132 new_project .topic = text
120133
121- chat_id = update .message .chat_id
122- user = Pycampista .get_or_create (username = username , chat_id = chat_id )[0 ]
134+ await context .bot .send_message (
135+ chat_id = update .message .chat_id ,
136+ text = "Excelente {}! La temática de tu proyecto es: {}." .format (username , text )
137+ )
138+
139+ keyboard = [
140+ [
141+ InlineKeyboardButton ("Sí" , callback_data = f"{ REPO_EXISTS_PATTERN } :si" ),
142+ InlineKeyboardButton ("No" , callback_data = f"{ REPO_EXISTS_PATTERN } :no" ),
143+ ]
144+ ]
145+ reply_markup = InlineKeyboardMarkup (keyboard )
146+
147+ await context .bot .send_message (
148+ chat_id = update .message .chat_id ,
149+ text = "¿Existe un repositorio para tu proyecto?" ,
150+ reply_markup = reply_markup ,
151+ )
152+
153+ return CHECK_REPOSITORIO
154+
155+
156+ async def ask_if_repository_exists (update , context ):
157+ '''Dialog to ask if a repository exists'''
158+ callback_query = update .callback_query
159+ chat = callback_query .message .chat
160+
161+ if callback_query .data .split (':' )[1 ] == "si" :
162+ await context .bot .send_message (
163+ chat_id = chat .id ,
164+ text = "¿Cuál es la URL del repositorio?" ,
165+ )
166+ return REPOSITORIO
167+ else :
168+ await context .bot .send_message (
169+ chat_id = chat .id ,
170+ text = "Si creás un repositorio, podés agregarlo con /agregar_repositorio."
171+ )
172+ await save_project (callback_query .from_user .username , chat .id , context )
173+ return ConversationHandler .END
174+
175+
176+ async def save_project (username , chat_id , context ):
177+ '''Save project to database'''
178+ new_project = current_projects [username ]
123179
124- new_project .owner = user
125180 try :
126181 new_project .save ()
127182 except peewee .IntegrityError :
128183 await context .bot .send_message (
129- chat_id = update . message . chat_id ,
184+ chat_id = chat_id ,
130185 text = "Ups ese proyecto ya fue cargado"
131186 )
132- return ConversationHandler .END
187+ else :
188+ await context .bot .send_message (
189+ chat_id = chat_id ,
190+ text = "Tu proyecto ha sido cargado"
191+ )
192+
193+
194+ async def project_repository (update , context ):
195+ '''Dialog to set project repository'''
196+ username = update .message .from_user .username
197+ text = update .message .text
198+
199+ new_project = current_projects [username ]
200+ new_project .repository_url = text
133201
134202 await context .bot .send_message (
135203 chat_id = update .message .chat_id ,
136- text = "Excelente {}! La temática de tu proyecto es: {}." .format (username , text ))
137- await context .bot .send_message (
138- chat_id = update .message .chat_id ,
139- text = "Tu proyecto ha sido cargado"
204+ text = f"El repositorio de tu proyecto es: { text } ."
140205 )
206+
207+ await save_project (username , update .message .chat_id , context )
208+
141209 return ConversationHandler .END
142210
143211
@@ -148,6 +216,61 @@ async def cancel(update, context):
148216 return ConversationHandler .END
149217
150218
219+ @active_needed
220+ async def ask_project_name (update , context ):
221+ '''Command to start the agregar_repositorio dialog'''
222+ username = update .message .from_user .username
223+
224+ projects = Project .select ().join (Pycampista ).where (Pycampista .username == username )
225+
226+ keyboard = [
227+ [InlineKeyboardButton (project .name , callback_data = f"{ PROJECT_NAME_PATTERN } :{ project .name } " ) for project in projects ]
228+ ]
229+ reply_markup = InlineKeyboardMarkup (keyboard )
230+
231+ await context .bot .send_message (
232+ chat_id = update .message .chat_id ,
233+ text = "¿A qué proyecto querés agregar un repositorio?" ,
234+ reply_markup = reply_markup ,
235+ )
236+
237+ return 1
238+
239+
240+ async def ask_repository_name (update , context ):
241+ '''Dialog to set project name'''
242+ callback_query = update .callback_query
243+ chat = callback_query .message .chat
244+
245+ username = callback_query .from_user .username
246+
247+ current_projects [username ] = callback_query .data .split (':' )[1 ]
248+
249+ await context .bot .send_message (
250+ chat_id = chat .id ,
251+ text = "¿Cuál es la URL del repositorio?" ,
252+ )
253+ return 2
254+
255+
256+ @active_needed
257+ async def add_repository (update , context ):
258+ '''Dialog to set repository'''
259+ username = update .message .from_user .username
260+ text = update .message .text
261+
262+ project = Project .select ().where (Project .name == current_projects [username ]).get ()
263+
264+ project .repository_url = text
265+ project .save ()
266+
267+ await context .bot .send_message (
268+ chat_id = update .message .chat_id ,
269+ text = f'Repositorio "{ text } " agregado al proyecto "{ current_projects [username ]} "' ,
270+ )
271+ return ConversationHandler .END
272+
273+
151274@active_needed
152275@admin_needed
153276async def start_project_load (update , context ):
@@ -186,7 +309,10 @@ async def end_project_load(update, context):
186309 states = {
187310 NOMBRE : [MessageHandler (filters .TEXT , naming_project )],
188311 DIFICULTAD : [MessageHandler (filters .TEXT , project_level )],
189- TOPIC : [MessageHandler (filters .TEXT , project_topic )]},
312+ TOPIC : [MessageHandler (filters .TEXT , project_topic )],
313+ CHECK_REPOSITORIO : [CallbackQueryHandler (ask_if_repository_exists , pattern = REPO_EXISTS_PATTERN )],
314+ REPOSITORIO : [MessageHandler (filters .TEXT , project_repository )],
315+ },
190316 fallbacks = [CommandHandler ('cancel' , cancel )])
191317
192318
@@ -236,11 +362,12 @@ async def show_projects(update, context):
236362 projects = Project .select ()
237363 text = []
238364 for project in projects :
239- project_text = "{} \n Owner: {} \n Temática: {} \n Nivel: {}" .format (
365+ project_text = "{} \n Owner: {} \n Temática: {} \n Nivel: {} \n Repositorio: {} " .format (
240366 project .name ,
241367 project .owner .username ,
242368 project .topic ,
243- project .difficult_level
369+ project .difficult_level ,
370+ project .repository_url ,
244371 )
245372 participants_count = Vote .select ().where (
246373 (Vote .project == project ) & (Vote .interest )).count ()
@@ -336,7 +463,18 @@ async def show_my_projects(update, context):
336463 await update .message .reply_text (text , parse_mode = 'MarkdownV2' )
337464
338465def set_handlers (application ):
466+ add_repository_handler = ConversationHandler (
467+ entry_points = [CommandHandler ('agregar_repositorio' , ask_project_name )],
468+ states = {
469+ 1 : [CallbackQueryHandler (ask_repository_name , pattern = PROJECT_NAME_PATTERN )],
470+ 2 : [MessageHandler (filters .TEXT , add_repository )],
471+ },
472+ fallbacks = [CommandHandler ('cancel' , cancel )],
473+ )
474+
339475 application .add_handler (load_project_handler )
476+ application .add_handler (add_repository_handler )
477+
340478 application .add_handler (
341479 CommandHandler ('empezar_carga_proyectos' , start_project_load ))
342480 application .add_handler (
0 commit comments