Skip to content

Commit 2091a17

Browse files
committed
Add repository URL to project
1 parent b0b505f commit 2091a17

File tree

4 files changed

+161
-18
lines changed

4 files changed

+161
-18
lines changed

src/pycamp_bot/commands/manage_pycamp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ async def define_duration(update, context):
152152
chat_id=update.message.chat_id,
153153
text=msg
154154
)
155+
return ConversationHandler.END
155156

156157

157158
async def cancel(update, context):

src/pycamp_bot/commands/projects.py

Lines changed: 152 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import 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
45
from pycamp_bot.models import Pycampista, Project, Slot, Vote
56
from pycamp_bot.commands.base import msg_to_active_pycamp_chat
67
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
@@ -10,7 +11,15 @@
1011

1112

1213
current_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

1524
logger = 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
153276
async 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

338465
def 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(

src/pycamp_bot/commands/voting.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from pycamp_bot.logger import logger
1010

1111

12+
VOTE_PATTERN = 'vote'
13+
14+
1215
def vote_authorized(func):
1316
@functools.wraps(func)
1417
async def wrap(*args):
@@ -61,7 +64,7 @@ async def button(update, context):
6164

6265
# Save vote in the database and confirm the chosen proyects.
6366

64-
if query.data == "si":
67+
if query.data.split(':')[1] == "si":
6568
result = f"✅ Sumade a {project_name}!"
6669
new_vote.interest = True
6770
else:
@@ -96,8 +99,8 @@ async def vote(update, context):
9699

97100
# ask user for each project in the database
98101
for project in Project.select():
99-
keyboard = [[InlineKeyboardButton("Me Sumo!", callback_data="si"),
100-
InlineKeyboardButton("Paso", callback_data="no")]]
102+
keyboard = [[InlineKeyboardButton("Me Sumo!", callback_data=f"{VOTE_PATTERN}:si"),
103+
InlineKeyboardButton("Paso", callback_data=f"{VOTE_PATTERN}:no")]]
101104

102105
reply_markup = InlineKeyboardMarkup(keyboard)
103106

@@ -129,7 +132,7 @@ async def vote_count(update, context):
129132

130133
def set_handlers(application):
131134
application.add_handler(
132-
CallbackQueryHandler(button))
135+
CallbackQueryHandler(button, pattern=VOTE_PATTERN))
133136
application.add_handler(
134137
CommandHandler('empezar_votacion_proyectos', start_voting))
135138
application.add_handler(

src/pycamp_bot/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class Project(BaseModel):
158158
topic = pw.CharField(null=True)
159159
slot = pw.ForeignKeyField(Slot, null=True)
160160
owner = pw.ForeignKeyField(Pycampista)
161+
repository_url = pw.CharField(null=True)
161162

162163

163164
class Vote(BaseModel):

0 commit comments

Comments
 (0)