def vote(bot, update): logger.info("Vote message") bot_status = BotStatus.select()[0] if bot_status.vote_authorized: update.message.reply_text( 'Te interesa el proyecto:' ) # if there is not project in the database, create a new project if not Project.select().exists(): Project.create(name='PROYECTO DE PRUEBA') # ask user for each project in the database for project in Project.select(): keyboard = [[InlineKeyboardButton("Si!", callback_data="si"), InlineKeyboardButton("Nop", callback_data="no")]] reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text( text=project.name, reply_markup=reply_markup ) else: update.message.reply_text("Votaci贸n Cerrada")
def export_db_2_json(): projects = Project.select() result = {"projects": {}, "responsable_available_slots": {}} available_slots = [slot.code for slot in Slot.select()] result["available_slots"] = available_slots all_responsables = [] for project in projects: votes = list(Vote.select().where(Vote.project == project, Vote.interest)) # responsables = list(ProjectOwner.select().where(ProjectOwner.project == project)) # responsables = [responsable.username for responsable in responsables] responsables = [project.owner.username] if project.owner.username not in all_responsables: all_responsables.append(project.owner.username) votes_users = set([v.pycampista.username for v in votes]) result["projects"][project.name] = { "priority_slots": [], "difficult_level": project.difficult_level, "responsables": responsables, "votes": list(votes_users), "theme": project.topic, } for responsable in all_responsables: result["responsable_available_slots"][responsable] = available_slots # with open('cualquiera.json', 'w') as fjson: # json.dump(result, fjson, indent=2) return result
def button(bot, update): '''Save user vote in the database''' query = update.callback_query username = query.message['chat']['username'] chat_id = query.message.chat_id user = Pycampista.get_or_create(username=username, chat_id=chat_id)[0] project_name = query.message['text'] # Get project from the database project = Project.get(Project.name == project_name) # create a new vote object new_vote = Vote(pycampista=user, project=project) # Save vote in the database and send a message if query.data == "si": result = 'Interesadx en: ' + project_name + ' 馃憤' new_vote.interest = True new_vote.save(force_insert=True) else: new_vote.interest = False new_vote.save(force_insert=True) result = 'No te interesa el proyecto ' + project_name bot.edit_message_text(text=result, chat_id=query.message.chat_id, message_id=query.message.message_id)
def change_slot(bot, update): projects = Project.select() slots = Slot.select() text = update.message.text.split(' ') if not len(text) == 3: bot.send_message(chat_id=update.message.chat_id, text="""El formato de este comando es: /cambiar_slot NOMBRE_DEL_PROJECTO NUEVO SLOT ej: /cambiar_slot fades AB """) return found = False for project in projects: if project.name == text[1]: for slot in slots: if slot.code == text[2]: found = True project.slot = slot.id project.save() if found: bot.send_message(chat_id=update.message.chat_id, text="Exito") else: bot.send_message( chat_id=update.message.chat_id, text="O el slot o el nombre del projecto no estan en la db")
def owning(bot, update): '''Dialog to set project responsable''' username = update.message.from_user.username text = update.message.text chat_id = update.message.chat_id lista_proyectos = [p.name for p in Project.select()] dic_proyectos = dict(enumerate(lista_proyectos)) project_name = dic_proyectos[int(text)] new_project = Project.get(Project.name == project_name) user = Pycampista.get_or_create(username=username, chat_id=chat_id)[0] new_project.owner = user new_project.save() bot.send_message(chat_id=update.message.chat_id, text="Perfecto. Chauchi")
def primer_proyecto(bot, update): # Grabs first response and asks for the second one text = update.message.text lista_proyectos = [p.name for p in Project.select()] dic_proyectos = dict(enumerate(lista_proyectos)) proyecto_principal = dic_proyectos[int(text)] merge_projects.append(proyecto_principal) bot.send_message( chat_id=update.message.chat_id, text="Decime el segundo proyecto que querés combinar (En número)") return SEGUNDO_PROYECTO
def own(bot, update): lista_proyectos = [p.name for p in Project.select()] dic_proyectos = dict(enumerate(lista_proyectos)) bot.send_message( chat_id=update.message.chat_id, text="¿A qué proyecto querés agregarte como responsable? (Dar número)") for k, v in dic_proyectos.items(): bot.send_message(chat_id=update.message.chat_id, text="{}: {}".format(k, v)) bot.send_message(chat_id=update.message.chat_id, text="-" * 77) return OWNING
def show_schedule(bot, update): slots = Slot.select() projects = Project.select() cronograma = {} for slot in slots: cronograma[slot.code] = [] for project in projects: if project.slot_id == slot.id: cronograma[slot.code].append(project.name) bot.send_message(chat_id=update.message.chat_id, text=_dictToString(cronograma))
def show_projects(bot, update): """Prevent people for keep uploading projects""" projects = Project.select() text = [] for project in projects: project_text = "{} \n owner: {} \n topic: {} \n level: {}".format( project.name, project.owner.username, project.topic, project.difficult_level) text.append(project_text) text = "\n\n".join(text) update.message.reply_text(text)
def projects(bot, update): """Prevent people for keep uploading projects""" projects = Project.select() text = [] for project in projects: # owners = map(lambda po: po.owner.username, project.projectowner_set.iterator()) project_text = "{} \n owner: {} \n topic: {} \n level: {}".format( project.name, project.owner.username, project.topic, project.difficult_level) text.append(project_text) text = "\n\n".join(text) update.message.reply_text(text)
def make_schedule(bot, update): bot.send_message(chat_id=update.message.chat_id, text="Generando el Cronograma...") data_json = export_db_2_json() my_schedule = export_scheduled_result(data_json) for relationship in my_schedule: slot = Slot.get(Slot.code == relationship[1]) project = Project.get(Project.name == relationship[0]) project.slot = slot.id project.save() bot.send_message(chat_id=update.message.chat_id, text="Cronograma Generado!")
def merge(bot, update): if not is_auth(bot, update.message.from_user.username): return lista_proyectos = [p.name for p in Project.select()] dic_proyectos = dict(enumerate(lista_proyectos)) # Asks for user input regarding first project bot.send_message( chat_id=update.message.chat_id, text="Decime el primer proyecto que querés combinar (En número)") for k, v in dic_proyectos.items(): bot.send_message(chat_id=update.message.chat_id, text="{}: {}".format(k, v)) bot.send_message(chat_id=update.message.chat_id, text="-" * 77) return PRIMER_PROYECTO
def naming_project(bot, update): '''Dialog to set project name''' logger.info("Nombrando el proyecto") username = update.message.from_user.username text = update.message.text new_project = Project(name=text) current_projects[username] = new_project bot.send_message(chat_id=update.message.chat_id, text="Estamos cargando tu proyecto: {}!".format(username)) bot.send_message(chat_id=update.message.chat_id, text="Tu proyecto se llama: {}".format(text)) bot.send_message(chat_id=update.message.chat_id, text="""Cual es el nivel de dificultad? 1 = newbie friendly 2 = intermedio 3 = python avanzado""") return DIFICULTAD
def button(bot, update): '''Save user vote in the database''' query = update.callback_query username = query.message['chat']['username'] chat_id = query.message.chat_id user = Pycampista.get_or_create(username=username, chat_id=chat_id)[0] project_name = query.message['text'] # Get project from the database project = Project.get(Project.name == project_name) # create a new vote object new_vote = Vote( pycampista=user, project=project, _project_pycampista_id="{}-{}".format(project.id, user.id), ) # Save vote in the database and send a message if query.data == "si": result = 'Interesadx en: ' + project_name + ' 馃憤' new_vote.interest = True else: new_vote.interest = False result = 'No te interesa el proyecto ' + project_name try: new_vote.save() bot.edit_message_text(text=result, chat_id=query.message.chat_id, message_id=query.message.message_id) except peewee.IntegrityError: logger.warning("Error al guardar el voto de {} del proyecto {}".format( username, project_name )) bot.edit_message_text( text="Ya hab铆as votado el proyecto {}!!".format(project_name), chat_id=query.message.chat_id, message_id=query.message.message_id )
def segundo_proyecto(bot, update): text = update.message.text # Grabs second reply lista_proyectos = [p.name for p in Project.select()] dic_proyectos = dict(enumerate(lista_proyectos)) proyecto_secundario = dic_proyectos[int(text)] merge_projects.append(proyecto_secundario) # Queries data for both projects to be merged proyecto_primario_query = Project.get(Project.name == merge_projects[0]) proyecto_secundario_query = Project.get(Project.name == merge_projects[1]) # Queries for positive votes matching the project's ID query_votos_primario = Vote.select().where( Vote.project == proyecto_primario_query, Vote.interest == 1) query_votos_secundario = Vote.select().where( Vote.project == proyecto_secundario_query, Vote.interest == 1) # Creates list with user IDs from previous votes, combines them and creates # a list with non repeating items lista_votos_primario = [ voto.pycampista_id for voto in query_votos_primario ] lista_votos_secundario = [ voto.pycampista_id for voto in query_votos_secundario ] lista_unica = lista_votos_primario.copy() lista_unica.extend(lista_votos_secundario) lista_unica = set(lista_unica) # Iterates through the unique list and any voter that is not present in the # first project, gets its user ID loaded with a positive vote into the # first project. for i in lista_unica: if i not in lista_votos_primario: Vote.get_or_create(project_id=proyecto_primario_query.id, pycampista_id=i, interest=1) # Queries for project owner ID and project ID matching the first and second # project query project_owner_id_primario = ProjectOwner.select().where( ProjectOwner.project_id == proyecto_primario_query) project_owner_id_secundario = ProjectOwner.select().where( ProjectOwner.project_id == proyecto_secundario_query) # Creates list with user IDs from owners, combines them and creates a list # with non repeating items lista_owner_primario = [ proyecto.owner_id for proyecto in project_owner_id_primario ] lista_owner_secundario = [ proyecto.owner_id for proyecto in project_owner_id_secundario ] lista_unica_owner = lista_owner_primario.copy() lista_unica_owner.extend(lista_owner_secundario) lista_unica_owner = set(lista_unica_owner) # Iterates through the unique list and any owner that is not present in the # first project, gets its user ID loaded as an owner of the first project. for i in lista_unica_owner: if i not in lista_owner_primario: ProjectOwner.get_or_create(project_id=proyecto_primario_query.id, owner_id=i) # Combines both names into the first project name nombre_primario = proyecto_primario_query.name nombre_secundario = proyecto_secundario_query.name nuevo_nombre_proyecto = nombre_primario + " / " + nombre_secundario proyecto_primario_query.name = nuevo_nombre_proyecto proyecto_primario_query.save() # Deletes second project data and allows the dev to insert Rage Against the # Machine reference. killing_in_the_name_of = Project.delete().where( Project.id == proyecto_secundario_query.id) bulls_on_parade = Vote.delete().where( Vote.project_id == proyecto_secundario_query.id) calm_like_a_bomb = ProjectOwner.delete().where( ProjectOwner.project_id == proyecto_secundario_query.id) killing_in_the_name_of.execute() bulls_on_parade.execute() calm_like_a_bomb.execute() bot.send_message(chat_id=update.message.chat_id, text="Los proyectos han sido combinados.")
def show_schedule(bot, update): """Print the schedule for people to see on telegram""" projects = Project.select() text = []
import json from pycamp_bot.models import Project, ProjectOwner, Slot, Vote projects = Project.select() project_owners = ProjectOwner.select() result = {"projects": {}, "responsable_available_slots": {}} available_slots = [slot.code for slot in Slot.select()] available_slots = ["A1", "A2", "B1", "B2", "B3", "B4", "B5", "B6"] result["available_slots"] = available_slots all_responsables = [] for project in projects: votes = list(Vote.select().where(Vote.project == project, Vote.interest)) # responsables = list(ProjectOwner.select().where(ProjectOwner.project == project)) # responsables = [responsable.username for responsable in responsables] responsables = [project.owner.username] if project.owner.username not in all_responsables: all_responsables.append(project.owner.username) votes_users = set([v.pycampista.username for v in votes]) result["projects"][project.name] = { "priority_slots": [], "difficult_level": project.difficult_level, "responsables": responsables, "votes": list(votes_users), "theme": project.topic, }