Example #1
0
def launch_anova():
    with database.create_connection() as conn:
        cols = []
        for i in ANOVA_COLS:
            if i in request.args:
                cols.append(i)
        cols.append('Income')
        data = database.get(conn, cols)
        anova_result = analyzer.anova_cols(data, cols)
        return render_template("anova_result.html",
                               result=anova_result.split('\n'))
Example #2
0
    def negative_validation(self, update, context, additional_context=None):
        print('negativ_validation')
        chat_id = update.message.chat.id
        if additional_context is not None:
            inviter, candidate = additional_context
        else:
            inviter = context.user_data.get('inviter')
            candidate = context.user_data.get('new_member')
        unset_timer(context)
        try:
            unset_timer(context)
            handlers_remover(self.dispatcher,
                             'yes_handler',
                             user_id=inviter.tg_id,
                             chat_id=chat_id,
                             group=2)
            handlers_remover(self.dispatcher,
                             'no_handler',
                             user_id=inviter.tg_id,
                             chat_id=chat_id,
                             group=2)
        except AttributeError:
            pass
        chat = db.get(db.Chat, chat_id=chat_id)
        if chat.ban_mode == 1:
            until_date = int(time.time()) + 31
        else:
            until_date = int(time.time()) + 31  #31 seconds is a minimal period
        context.bot.restrict_chat_member(chat_id=chat_id,
                                         user_id=candidate.id,
                                         permissions=ChatPermissions(
                                             can_send_messages=True,
                                             can_send_media_messages=True,
                                             can_send_other_messages=True,
                                             can_add_web_page_previews=True,
                                             can_send_polls=True,
                                             can_invite_users=True,
                                             can_pin_messages=True))

        self.updater.bot.kick_chat_member(chat_id=chat_id,
                                          user_id=candidate.id,
                                          until_date=until_date)

        try:
            if inviter.username:
                logging.info(f'{inviter.username} denied' +
                             f' {candidate.username or candidate.first_name}')
            logging.info(f'{inviter.first_name or "Chat"} denied' +
                         f' {candidate.username or candidate.first_name}')
        except AttributeError:
            logging.info(f'{candidate.username or candidate.first_name}' +
                         ' ran out of tries')
Example #3
0
 def add_msg_to_db(self, update, context):
     chat_id = update.message.chat.id
     chat = db.get(db.Chat, chat_id=chat_id)
     from_user_id = update.message.from_user.id
     from_user = db.get(db.User, tg_id=from_user_id, enter_chat_id=chat_id)
     if not from_user:
         logging.info('User is not in DB')
     else:
         mat = 0
         caps = 0
         msg_type = detect_msg_type(update.message)
         new_msg = {
             'from_id': from_user.id,
             'from_username': from_user.username,
             'from_first_name': from_user.first_name,
             'msg_date': datetime.utcnow(),
             'msg_type': msg_type,
             'text': update.message.text or update.message.caption,
             'chat_id': chat.chat_id,
             'mat': mat,
             'caps': caps
         }
         db.create(db.Message, **new_msg)
Example #4
0
def analyze():
    field1 = escape(request.args.get('field1'))
    field2 = escape(request.args.get('field2'))
    with database.create_connection() as conn:
        data = database.get(conn, [field1, field2])
        contingency_table = analyzer.get_contingency_table(
            data, field1, field2)
        statistic, expected_table = analyzer.get_statistic_and_expected_table(
            contingency_table)
        return render_template("analysis.html",
                               field1=field1,
                               field2=field2,
                               contingency=contingency_table,
                               statistic=statistic,
                               expected=expected_table)
Example #5
0
 def manual_invite_validation(self, update, context):
     print('manual_invite_validation')
     chat_id = update.message.chat.id
     inviter = update.message.from_user
     new_member = context.user_data.get('new_member')
     inviter = db.get(db.User, tg_id=inviter.id, enter_chat_id=chat_id)
     if inviter.invites > 0:
         context.user_data.update({'inviter': inviter})
         return self.positive_validation(update, context)
     #31 seconds is a minimum ban period
     until_date = int(time.time()) + 31
     msg(self.updater,
         chat_id=chat_id,
         text=(f'{inviter.first_name},' + f'{CANT_INVITE_MSG}'))
     return self.updater.bot.kick_chat_member(chat_id=chat_id,
                                              user_id=new_member.id,
                                              until_date=until_date)
Example #6
0
 def door_handler(self) -> None:
     """Door checking thread."""
     try:
         door = DoorInterface()
         while True:
             log.debug("Waiting for card")
             card = door.get_card()
             log.debug("Scanned: %s", card)
             entry = database.get(card)
             if entry:
                 log.info("Opening door for: %s", entry.name)
                 door.open()
             elif self.add_next_card:
                 log.info("Adding card %s for %s", card, self.add_next_card)
                 database.add(CardEntry(self.add_next_card, card))
                 database.commit()
                 self.add_next_card = None
             else:
                 log.info("Rejected %s", card)
                 self.door.flash_red()
     except Exception:
         # Blow everything up if we crash
         os._exit(0)
         raise
Example #7
0
    def new_member_validation(self, update, context, tries):
        print('new_member_validation')
        chat_id = update.message.chat.id
        candidate = context.user_data.get('new_member')
        unset_timer(context)

        handlers_remover(self.dispatcher,
                         'mention_handler',
                         user_id=candidate.id,
                         chat_id=chat_id,
                         group=3)
        handlers_remover(self.dispatcher,
                         'not_mention_handler',
                         user_id=candidate.id,
                         chat_id=chat_id,
                         group=3)

        inviter_username = (update.message.text).strip('@')
        try:
            inviter = (db.get(
                db.User, username=inviter_username, enter_chat_id=chat_id)
                       or db.get(db.User,
                                 tg_id=update.message.entities[0].user.id,
                                 enter_chat_id=chat_id))
        except AttributeError:
            inviter = None
        if inviter and inviter.invites > 0:
            context.bot.restrict_chat_member(
                chat_id=chat_id,
                user_id=candidate.id,
                permissions=ChatPermissions(
                    can_send_messages=False,
                    can_send_media_messages=False,
                    can_send_other_messages=False,
                    can_add_web_page_previews=False,
                ))
            set_timer(context, due=INVITER_DUE)
            if inviter.username:
                text = (f'@{inviter.username},' + VALIDATION_MSG + ' ' +
                        f'({candidate.first_name}/' +
                        f'{candidate.username or "без username"})?')
                self.updater.bot.sendMessage(
                    chat_id=chat_id,
                    text=text,
                    entities=[
                        MessageEntity(type='mention',
                                      offset=0,
                                      length=30,
                                      user=User(id=inviter.tg_id,
                                                is_bot=False,
                                                first_name=inviter.first_name))
                    ],
                    reply_markup=validation_keyboard)
            else:
                text = (
                    f'[{inviter.first_name}](tg://user?id={inviter.tg_id}),' +
                    f'{VALIDATION_MSG} {candidate.first_name}/' +
                    f'{candidate.username or "без username"}?')
                msg(self.updater,
                    chat_id=chat_id,
                    text=text,
                    entities=[
                        MessageEntity(type='mention',
                                      offset=0,
                                      length=30,
                                      user=User(id=inviter.tg_id,
                                                is_bot=False,
                                                first_name=inviter.first_name))
                    ],
                    reply_markup=validation_keyboard,
                    parse_mode='MarkdownV2')

            y_callback = lambda update, context: self.positive_validation(
                update, context, additional_context=(inviter, candidate))
            yes_handler = MessageHandler(
                filters=(Filters.regex('Да') & Filters.chat(chat_id)
                         & Filters.user(inviter.tg_id)),
                callback=y_callback)
            yes_handler.__name__ = 'yes_handler' + str(
                inviter.tg_id) + str(chat_id)

            n_callback = lambda update, context: self.negative_validation(
                update, context, additional_context=(inviter, candidate))
            no_handler = MessageHandler(
                filters=(Filters.regex('Нет') & Filters.chat(chat_id)
                         & Filters.user(inviter.tg_id)),
                callback=n_callback)

            no_handler.__name__ = 'no_handler' + str(
                inviter.tg_id) + str(chat_id)

            self.dispatcher.add_handler(yes_handler, group=2)
            self.dispatcher.add_handler(no_handler, group=2)
        else:
            msg(self.updater, chat_id=chat_id, text=VALIDATION_NEGATIVE_MSG)
            tries -= 1
            update.message.entities = []
            update.message.text = ''
            return self.link_invite_validation(update, context, tries)
Example #8
0
def add_slot_values_from_database(slot, category, exceptions=set()):
    for value in database.get(category, tuple()):
        if value not in exceptions:
            ontology['slots'][slot].add(value)
import sys
from database import database
from prover import Prover

if __name__ == '__main__':
    if len(sys.argv >= 1):
        device_id = sys.argv[1]
    else:
        print('No device id provided')

    if len(sys.argv >= 2):
        server_url = sys.argv[2]
    else:
        server_url = 'http://localhost:5000'

    device = database.get(device_id)
    if not device:
        print('No device with id {0} found in the database'.format(device_id))

    prover = Prover(device, server_url)

    prover.authenticate()
Example #10
0
def add_slot_values_from_database(slot, category, exceptions=set()):
    for value in database.get(category, tuple()):
        if value not in exceptions:
            ontology['slots'][slot].add(value)