Skip to content

Commit b77ffca

Browse files
committed
Github Issue #49
1 parent 6f9e6bc commit b77ffca

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

src/pycamp_bot/commands/projects.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import logging
12
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters
23
from pycamp_bot.models import Pycampista, Project, Vote
34
from pycamp_bot.commands.base import msg_to_active_pycamp_chat
45
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
6+
from pycamp_bot.commands.auth import admin_needed, get_admins_username
77

88

99
current_projects = {}
1010
NOMBRE, DIFICULTAD, TOPIC = ["nombre", "dificultad", "topic"]
1111

12+
logger = logging.getLogger(__name__)
13+
1214

1315
def load_authorized(f):
1416
async def wrap(*args, **kargs):
@@ -50,7 +52,7 @@ async def naming_project(update, context):
5052
'''Dialog to set project name'''
5153
logger.info("Nombrando el proyecto")
5254
username = update.message.from_user.username
53-
text = update.message.text
55+
text = update.message.text.lower()
5456

5557
new_project = Project(name=text)
5658
current_projects[username] = new_project
@@ -61,7 +63,7 @@ async def naming_project(update, context):
6163
)
6264
await context.bot.send_message(
6365
chat_id=update.message.chat_id,
64-
text="Tu proyecto se llama: {}".format(text)
66+
text="Tu proyecto se llama: {}".format(text.title())
6567
)
6668
await context.bot.send_message(
6769
chat_id=update.message.chat_id,
@@ -186,6 +188,45 @@ async def end_project_load(update, context):
186188
fallbacks=[CommandHandler('cancel', cancel)])
187189

188190

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

206249
if text:
207250
text = "\n\n".join(text)
@@ -217,5 +260,8 @@ def set_handlers(application):
217260
CommandHandler('empezar_carga_proyectos', start_project_load))
218261
application.add_handler(
219262
CommandHandler('terminar_carga_proyectos', end_project_load))
263+
application.add_handler(
264+
CommandHandler('borrar_proyecto', delete_project))
220265
application.add_handler(
221266
CommandHandler('proyectos', show_projects))
267+

0 commit comments

Comments
 (0)