Skip to content

Commit 1338904

Browse files
authored
Merge pull request #57 from PyAr/ticket-49
Github Issue #49
2 parents 6f9e6bc + 481f0c2 commit 1338904

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

src/pycamp_bot/commands/projects.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import logging
2+
import peewee
13
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters
24
from pycamp_bot.models import Pycampista, Project, Vote
35
from pycamp_bot.commands.base import msg_to_active_pycamp_chat
46
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
5-
from pycamp_bot.commands.auth import admin_needed
6-
from pycamp_bot.logger import logger
7+
from pycamp_bot.commands.auth import admin_needed, get_admins_username
78

89

910
current_projects = {}
1011
NOMBRE, DIFICULTAD, TOPIC = ["nombre", "dificultad", "topic"]
1112

13+
logger = logging.getLogger(__name__)
14+
1215

1316
def load_authorized(f):
1417
async def wrap(*args, **kargs):
@@ -50,7 +53,7 @@ async def naming_project(update, context):
5053
'''Dialog to set project name'''
5154
logger.info("Nombrando el proyecto")
5255
username = update.message.from_user.username
53-
text = update.message.text
56+
text = update.message.text.lower()
5457

5558
new_project = Project(name=text)
5659
current_projects[username] = new_project
@@ -61,7 +64,7 @@ async def naming_project(update, context):
6164
)
6265
await context.bot.send_message(
6366
chat_id=update.message.chat_id,
64-
text="Tu proyecto se llama: {}".format(text)
67+
text="Tu proyecto se llama: {}".format(text.title())
6568
)
6669
await context.bot.send_message(
6770
chat_id=update.message.chat_id,
@@ -186,6 +189,45 @@ async def end_project_load(update, context):
186189
fallbacks=[CommandHandler('cancel', cancel)])
187190

188191

192+
async def delete_project(update, context):
193+
"""delete project loaded"""
194+
username = update.message.from_user.username
195+
project_name_splited = update.message.text.split()
196+
project_name_lower = [i.lower() for i in project_name_splited]
197+
198+
if len(project_name_splited) < 2:
199+
await context.bot.send_message(
200+
chat_id=update.message.chat_id,
201+
text="Debes ingresar el nombre de proyecto a eliminar. \n Ej: /borrar_proyecto intro django."
202+
)
203+
return
204+
else:
205+
try:
206+
project_name = ' '.join(project_name_lower[1:])
207+
project = Project.select().where(Project.name == project_name).get()
208+
except:
209+
await context.bot.send_message(
210+
chat_id=update.message.chat_id,
211+
text="No se encontró el proyecto '{}'.".format(project_name)
212+
)
213+
return
214+
215+
216+
if username != project.owner.username and username not in get_admins_username():
217+
await context.bot.send_message(
218+
chat_id=update.message.chat_id,
219+
text="No sos ni admin ni el owner de este proyecto, Careta."
220+
)
221+
return
222+
else:
223+
project.delete_instance()
224+
await context.bot.send_message(
225+
chat_id=update.message.chat_id,
226+
text="Se ha eliminado el proyecto {} satisfactoriamente.".format(project_name.title())
227+
)
228+
return
229+
230+
189231
async def show_projects(update, context):
190232
"""Prevent people for keep uploading projects"""
191233
projects = Project.select()
@@ -202,6 +244,8 @@ async def show_projects(update, context):
202244
if participants_count > 0:
203245
project_text += "\n Interesades: {}".format(participants_count)
204246
text.append(project_text)
247+
else:
248+
text = None
205249

206250
if text:
207251
text = "\n\n".join(text)
@@ -217,5 +261,8 @@ def set_handlers(application):
217261
CommandHandler('empezar_carga_proyectos', start_project_load))
218262
application.add_handler(
219263
CommandHandler('terminar_carga_proyectos', end_project_load))
264+
application.add_handler(
265+
CommandHandler('borrar_proyecto', delete_project))
220266
application.add_handler(
221267
CommandHandler('proyectos', show_projects))
268+

0 commit comments

Comments
 (0)