示例#1
0
def add(request):
    response, parameters = validate_request(request)
    if response.status_code == 204:
        if 'file' in request.FILES:
            if manager.add(request.FILES['file'], parameters['address'], parameters['name']):
                response.status_code = 200
    return response
示例#2
0
def loop():

    while True:

        helpers.clear()

        print("========================")
        print("  BIENVENIDO AL GESTOR  ")
        print("========================")
        print("[1] Listar clientes     ")
        print("[2] Mostrar cliente     ")
        print("[3] Añadir cliente      ")
        print("[4] Modificar cliente   ")
        print("[5] Borrar cliente      ")
        print("[6] Salir               ")
        print("========================")

        option = input("> ")

        helpers.clear()

        if option == '1':
            print("Listando los clientes...\n")
            manager.show_all()

        if option == '2':
            print("Mostrando un cliente...\n")
            manager.find()
        if option == '3':
            print("Añadiendo un cliente...\n")
            manager.add()
            print("Cliente añadido correctamente")
        if option == '4':
            print("Modificando un cliente...\n")
            if manager.edit():
                print("Cliente modificado correctamente\n")
        if option == '5':
            print("Borrando un cliente...\n")
            manager.delete()
            print("Cliente borrado correctamente")
        if option == '6':
            print("Saliendo...\n")
            break

        input("\nPresiona ENTER para continuar...")
示例#3
0
def add(request):
    response, parameters = validate_request(request)

    if response.status_code == 204:
        if manager.delete(parameters['address'], parameters['name']):
            if 'file' in request.FILES:
                if manager.add(image, address, name):
                    response.status_code = 200
    return response
def add(request):
    '''
    calls the methods of the package manager (records an image)
    and returns True or False to the end of the process.
    '''
    response, parameters = validate_request(request)
    if response.status_code == 204:
        if 'file' in request.FILES:
            if manager.add(request.FILES['file'], parameters['address'], parameters['name']):
                response.status_code = 200
    return response
示例#5
0
 def add(x):
     print(switch.get(x))
     manager.add()
     print("Cliente añadido correctamente\n")
示例#6
0
    def on_chat_message(self, message):
        content_type, chat_type, chat_id = telepot.glance(message)

        command = message['text']
        # For headmaster to track replies from /count sequence
        try:
            reply_source_message_id = (
                message['reply_to_message'])['message_id']
            headcount_input = (message['text'])
        except:
            reply_source_message_id = None
            headcount_input = -1

        logger.info('Received \'%s\' from \'%s\' (%s)' %
                    (command, manager.getName(chat_id), chat_id))

        # To save time
        def reply(reply):
            logger.info('Replied \'%s\' to %s' % (reply, chat_id))
            #self.sendMessage(chat_id, reply)
            self.sender.sendMessage(reply)

        def dm(target_id, message):
            logger.info('%s messaged \'%s\' to %s' %
                        (chat_id, reply, target_id))
            #self.sendMessage(target_id, message)
            bot.sendMessage(target_id, message)

        def yell(message):
            broadcaster.yell('all', message, chat_id)
            logger.info('%s yelled by %s.' % message, (str(chat_id)))

        def request_add(target_id, message, to_add_cg, to_add_id, to_add_name):
            logger.info('Superadmin attention was requested from %s to %s' %
                        (chat_id, target_id))
            reply_markup = ReplyKeyboardMarkup(
                keyboard=[
                    [
                        KeyboardButton(text='/add %s %s %s' %
                                       (to_add_cg, to_add_name, to_add_id))
                    ],
                    ['Reject'],
                ],
                one_time_keyboard=True,
            )
            #self.sendMessage(target_id, message, reply_markup=reply_markup)
            bot.sendMessage(target_id, message, reply_markup=reply_markup)

        # Groups cannot talk to the bot
        if chat_id < 0:
            return

        # Return a system ready flag to the admin when the command is complete.
        adminFlag = False

        # ================================ COMMANDS FOR ADMINS
        # This is for superadministrators.
        if authorized.isSuperadmin(chat_id):
            if command == '/alethea':
                reply('Alethea is wonderful <3')
                return

            elif command.startswith('/rm'):
                matches = re.match(
                    '\/rm\s+(MJ|VJA|VJB|TPJA|TPJB|TJ|DMH|CJ\sA|CJ\sB|CJ\sC|SA\sA|SA\sB|AJ\/YJ|SR|NY\/EJ|RJA|RJB\/SJI|RJC|IJ)\s+([a-zA-Z ]+)',
                    command, re.IGNORECASE)
                if matches is None:
                    reply(
                        'SUPERADMIN: Please follow the appropriate format: \'/rm CG Their Name'
                    )
                else:
                    cg = matches.group(1)
                    name = matches.group(2)
                    rm_message = 'Attempting to remove %s from %s.' % (name,
                                                                       cg)
                    logger.info(rm_message)
                    reply(manager.remove(cg, name, chat_id))
                    return

            elif command == '/event clear':
                reply(manager.forceDeleteEvent())
                return

        # This is for administrators.
        if authorized.isAdmin(chat_id):
            # /admin
            if command == '/admin':
                adminFlag = True
                reply(
                    'You are an admin. Commands available:\n/admin - View this\n/yell AUDIENCE: MSG\n/ls - Show all users in database\n/find FROM NAME - Show list of names you are finding.\n/update HOUSE NAME FIELD PARAM\n/purge HOUSE NAME\n/scoreb - Show scoreboard\n/award HOUSE POINTS'
                )

            elif command.startswith('/add'):
                matches = re.match(
                    '\/add\s+(MJ|VJA|VJB|TPJA|TPJB|TJ|DMH|CJ\sA|CJ\sB|CJ\sC|SA\sA|SA\sB|AJ\/YJ|SR|NY\/EJ|RJA|RJB\/SJI|RJC|IJ)\s+([0-9]+)\s+([a-zA-Z ]+)',
                    command, re.IGNORECASE)
                if matches is None:
                    reply(
                        'ADMIN: Please follow the appropriate format: \'/add cg chat_id Their Name\''
                    )
                else:
                    cg = matches.group(1)
                    target_id = matches.group(2)
                    name = matches.group(3)
                    added_message = 'Attempting to register %s (%s) into %s.' % (
                        target_id, name, cg)
                    logger.info(added_message)
                    bot.sendMessage(
                        target_id,
                        manager.add(cg.lower(), name.title(), target_id))
                    logger.info('Succesfully registered %s (%s) into %s.' %
                                (target_id, name, cg))
                    reply('%s (%s) added' % (name, cg.upper()))
                    return

            elif command.startswith('/update'):
                if command == '/update':
                    reply('/update HOUSE NAME FIELD PARAM')
                    return
                else:
                    adminFlag = True
                    house, name, field, content = re.match(
                        '/update\s+([a-zA-Z]+)\s+([a-zA-Z\s]+)\s+(name|type)\s+([a-zA-Z]+)\s*',
                        command).groups()
                    reply(
                        manager.updater(house.lower(), name.title(),
                                        field.lower(), content.lower(),
                                        chat_id))

            elif command.startswith('/ls'):
                if command == '/ls':
                    reply(
                        'Did you mean /ls la?\n\nla: List all users\ncg: List all users in cg'
                    )
                    return
                elif command == '/ls la':
                    adminFlag = True
                    reply(manager.getEnumerate('all', chat_id))
                else:
                    adminFlag = True
                    cg = re.match('/ls\s([a-zA-Z]+)', command).group(1).lower()
                    reply(manager.getEnumerate(cg, chat_id))

            elif command.startswith('/find'):
                if command == '/find':
                    reply('Enter search params!')
                    return
                else:
                    adminFlag = True
                    house, name = re.match('/find\s([a-zA-Z]+)\s([a-zA-Z\s]+)',
                                           command).groups()
                    reply(manager.find(cg.lower(), name.title(), chat_id))

            elif command.startswith('/event'):
                if command == '/event':
                    reply(
                        'Use format \'/event new Event Name\' or \'/event end\' or \'/event reopen\''
                    )
                    return
                elif command == '/event reopen':
                    adminFlag = True
                    reply(manager.reopenEvent())
                    broadcaster.yell(bot, 'all', 'Attendance taking reopened.',
                                     chat_id)
                elif command == '/event end':
                    adminFlag = True
                    reply(manager.forceEndEvent())
                    broadcaster.yell(bot, 'all',
                                     'Attendance taking has ended.', chat_id)
                elif command.startswith('/event new'):
                    adminFlag = True
                    event_name = command.replace('/event new ', '')
                    if manager.eventDoesNotExist():
                        broadcaster.yell(
                            bot, 'all',
                            'Counting attendance for %s has begun. Get /count -ing'
                            % event_name, chat_id)
                    reply(manager.raiseEvent(event_name))
                elif command == '/event report':
                    adminFlag = True
                    reply(printGrandTally())
                else:
                    reply('Improper parameters supplied for /event')
                    return
            elif command.startswith('/yell'):
                if command == ('/yell'):
                    reply('/yell what?')
                    return
                adminFlag = True
                message = command.replace('/yell ', '')
                broadcaster.yell(bot, 'all', str(message), chat_id)
                reply('Message yelled to all.')

            if adminFlag == True:
                reply('System ready.')
                return

        # /24601
        if command == '/24601':
            reply(str(chat_id))
            return
        # /cg
        if command == '/cg':
            reply('Use the following CG abbreviation codes:')
            reply('\n'.join(str(x) for x in authorized.cg_list))
            return
        # /stop
        if command == '/stop':
            if manager.removeById(chat_id):
                reply('Goodbye.')
            else:
                reply('You cannot stop what you did not begin.')
            return

        # ================================ COMMANDS FOR REGISTERED USERS
        # This is for registered participants
        if authorized.isRegistered(chat_id):
            # Tracking Headmaster queries
            if self.track_reply:
                # Obtain CG at first run.
                if self._query_cg == None:
                    self._query_cg = manager.getCG(chat_id)
                # Throw warning messages if user exits halfway through.
                if command == 'exit':
                    if self._progress == 0:
                        reply('/count process interrupted. Your data is safe.')
                    else:
                        reply(
                            '/count process interrupted. Your data is corrupted. Please try again.'
                        )
                    self.close()
                # Check if user is counting for the first time.
                if self._progress == 0:
                    self._first_try = manager.isFirstTry(self._query_cg)
                # Edit each field of the attendance as we go along.
                try:
                    count = int(command)
                    if count < 0:
                        raise Exception('Think positive only.')
                    manager.updateAttendance(self._query_cg,
                                             question_order[self._progress],
                                             count)
                except Exception as e:
                    reply(str(e))
                    reply('Your data is corrupted. Please restart /count.')
                    self.close()
                self._progress += 1
                # Complete method if all fields have been populated.
                if self._progress >= question_limit:
                    self.sender.sendMessage(
                        str(getCGFinalString(self._query_cg)))
                    if manager.setAttendanceDoneForEvent(
                            self._query_cg, self._first_try):
                        reply(
                            'Congratulations! You are the last to submit your attendance. Here you go ~'
                        )
                    else:
                        reply(
                            'Congratulations! You are not the last to submit your attendance. Peace.'
                        )
                    reply(manager.submitGrandAttendance(self._query_cg))
                    self.close()
                # otherwise send the next question
                self.sender.sendMessage(
                    str(question_bank.get(question_order[self._progress])))

            else:
                # /help [<command>]
                if command == '/help':
                    reply(helper.getNaiveHelp())
                elif command.startswith('/help'):
                    keyword = re.match('\s*/help\s+([a-z]+)\s*',
                                       command).group(1)
                    reply(helper.getHelp(keyword))
                # /retrieve_key
                elif command == '/retrieve_key':
                    reply(str(chat_id))
                # /me
                elif command == '/me':
                    reply(manager.getMe(chat_id))

                elif command == 'alethea':
                    bot.sendSticker(chat_id, 'CAADBQADxQYAAszG4gK3wUYfyR3TSQI')

                elif command == '/count':
                    if not manager.eventDoesNotExist(
                    ) and not manager.eventHasEnded():
                        # self.sender.sendMessage('Shall we begin?',
                        #     reply_markup=InlineKeyboardMarkup(
                        #         inline_keyboard=[[
                        #             InlineKeyboardButton(text='Ok', callback_data='start'),
                        #         ]]
                        #     )
                        # )
                        # self.close()
                        _progress = 0
                        reply(
                            'WARNING: You will potentially override existing data if you do not go through with the full procedure. If so, type exit to leave now.'
                        )
                        reply(
                            str(
                                question_bank.get(
                                    question_order[self._progress])))
                        self.track_reply = True
                    else:
                        reply(
                            'No one is counting attendance now. You may wish to do other productive things.'
                        )

                # Handles replies from Headmaster
                # elif reply_source_message_id != None:
                #     if self._query_cg == None:
                #         self._query_cg = manager.getCG(chat_id).lower()
                #         logging.info(self._query_cg)

                #     if self._progress >= question_limit:
                #         self.sender.sendMessage(str(manager.getCGFinalString(self._query_cg), reply_markup=None))
                #         _progress = 0
                #         self.close()

                #     if headcount_input == -1:
                #         reply('NaN. Restart.')
                #         self.close()

                #     try:
                #         if manager.updateAttendance(self._query_cg, question_order[self._progress], int(headcount_input)):
                #             logging.info('Database updated.')
                #         self._progress += 1
                #         self.sender.sendMessage(str(question_bank.get(question_order[self._progress])), reply_markup=
                #             ForceReply(force_reply=True))
                #     except ValueError:
                #         reply('NaN. Restart.')
                #         self.close()
                else:
                    reply(easter.responseHandler(command))

        # ================================ COMMANDS FOR UNREGISTERED USERS
        # for '/start'
        elif command == '/start':
            reply(
                'Hello there. Please enter \'/start Full Name CG\'\n\nEg: /start Alethea Sim TJ\n'
            )
            reply('For full list of CG abbreviations type in /cg')
            reply(
                'Please note that currently only JC East, North and South may use this system.'
            )
        elif command.startswith('/start'):
            regex_pattern = '\/start\s+([a-zA-Z ]+)\s+('

            # extract all given cgs in authorized into a regex id pattern
            for cg in authorized.cg_list:
                regex_pattern += cg + '|'
            regex_pattern = rreplace(regex_pattern, '|', ')', 1)

            matches = re.match(regex_pattern, command, re.IGNORECASE)
            if matches is None:
                reply(
                    'Either you did not follow the appropriate format: \'/start Your Name CG\' or you did not use the correct CG code. (/cg)'
                )
            else:
                name = matches.group(1).title()
                cg = matches.group(2).lower()

                # map CG to appropriate cluster
                cluster = authorized.getCluster(cg)
                approver_id = authorized.address_book.get(cluster)

                request_message = '%s (%s) from %s, %s wants to register.' % (
                    chat_id, name, cluster, cg)
                logger.info(request_message)
                reply('Hello, %s!' % name)

                # ask rep to approve registration
                request_add(authorized.superadmin, request_message, cg, name,
                            chat_id)
                request_add(approver_id, request_message, cg, name, chat_id)
                reply(
                    'Your request was sent to your cluster rep (JC %s) for approval. If you do not hear back within a minute, try again.'
                    % authorized.getClusterFriendlyString(cluster))

        # otherwise it must be trying to talk to ARIADNE!
        else:
            reply(
                'You are not registered. Hit /start or contact Justin (@njyjn) for more information.'
            )
        return