Beispiel #1
0
    def cancelmanger(self, data, user_data, for_user):
        session = DBSession()

        day = self.get_db_day(session)
        if not day:
            day = Day(date=datetime.date.today())
            session.add(day)
            session.commit()

        outputs = []

        if for_user:
            user = self.get_db_user(session, for_user)
        else:
            user = self.get_db_user(session, user_data['name'])

        if not user:
            send_error(data['channel'],
                       'Erreur zjaifnazgoizangoiazg',
                       thread=data["ts"])

        presence = session.query(Presence).filter(
            Presence.user_id == user.id, Presence.day_id == day.id).first()
        if not presence:
            if for_user:
                outputs.append(
                    "@{0} n'étais pas inscrit, pas de soucis <@{1}> !".format(
                        user.name, user_data['id']))
            else:
                outputs.append(
                    "Tu n'étais pas inscrit, pas de soucis <@{0}> !".format(
                        user_data['id']))
        else:
            session.query(Presence).filter_by(id=presence.id).delete()
            session.commit()
            if for_user:
                outputs.append("J'ai supprimé l'inscription de @{0}.".format(
                    user.name))
            else:
                outputs.append(
                    "J'ai supprimé ta demande <@{0}>, dommage ça allait être trop bon !"
                    .format(user_data['id']))

            outputs.append("Si besoin tu !Manger à nouveau :)")
        send_info(data['channel'],
                  text='\n'.join(outputs),
                  markdown=True,
                  ephemeral_user=user_data["id"])
        data['text'] = '!QuiMange'
        self.quimange(data, user_data)
Beispiel #2
0
    def achat(self, data, user_data, amount, description):

        session = DBSession()
        today = datetime.date.today()
        user = self.get_db_user(session, user_data['name'])

        expense = Expense(user_id=user.id,
                          amount=amount,
                          description=description)
        session.add(expense)
        session.commit()

        send_info(data['channel'],
                  text='Achat enregistré !',
                  markdown=True,
                  thread=data["ts"])
Beispiel #3
0
def delete_user(user):
    """Sync user with slack."""
    session = DBSession()
    db_user = session.query(User).filter(User.name == user).first()
    if not db_user:
        logger.warning('User {0} does not exist'.format(user))
        exit(0)

    balance = db_user.balance
    logger.info('test {0} {1}'.format(user, balance))

    if balance == 0.0:
        session.delete(db_user)
        session.commit()
        logger.info('User {0} deleted !'.format(user))
    else:
        logger.warning('User {0} has a balance of {1}'.format(user, balance))
        logger.warning('Must be reseted to 0 :)')
        exit(1)
Beispiel #4
0
def sync():
    """Sync user with slack."""
    endpoint = config.get('default', 'endpoint')
    bot_token = config.get(endpoint, 'bot_token')
    slack_client = SlackClient(bot_token)
    session = DBSession()

    api_call = slack_client.api_call("users.list")
    if api_call.get('ok'):
        # retrieve all users so we can find our bot
        users = api_call.get('members')
        for user in users:
            if user['name'] == 'slackbot':
                continue
            if user['deleted']:
                continue
            if not user['is_bot']:
                _sync_user(user, session)
    else:
        logger.warning('Error', api_call)
    session.commit()
Beispiel #5
0
    def cancelcuisiner(self, data, user_data):
        session = DBSession()

        day = self.get_db_day(session)
        if not day:
            day = Day(date=datetime.date.today())
            session.add(day)
            session.commit()

        outputs = []

        user = self.get_db_user(session, user_data['name'])

        if not user:
            send_error(data['channel'],
                       'Erreur zjaifnazgoizangoiazg',
                       thread=data["ts"])

        presence = session.query(Presence).filter(
            Presence.user_id == user.id, Presence.day_id == day.id).first()
        if not presence:
            outputs.append(
                "Tu n'étais pas inscrit, pas de soucis <@{0}> !".format(
                    user_data['id']))
        else:
            presence.cook = 0
            session.add(presence)
            session.commit()
            outputs.append("J'ai supprimé le fait que tu avais cuisiné")

        send_info(data['channel'],
                  text='\n'.join(outputs),
                  markdown=True,
                  ephemeral_user=user_data["id"])
        data['text'] = '!QuiMange'
        self.quimange(data, user_data)
Beispiel #6
0
    def quimange(self, data, user_data):
        outputs = []
        session = DBSession()

        day = self.get_db_day(session)
        if not day:
            day = Day(date=datetime.date.today())
            session.add(day)
            session.commit()

        presences = session.query(Presence).filter(
            Presence.day_id == day.id).all()
        if not presences:
            outputs.append("Personne d'inscrit aujourd'hui, sniff :(")
        else:
            total = 0
            for presence in presences:
                user = presence.user
                total += presence.meals
                guest = presence.meals - 1
                if guest > 0:
                    outputs.append("{0} avec {1} invités".format(
                        user.name, guest))
                else:
                    outputs.append(user.name)
                if presence.cook == 1:
                    outputs.append("C'est {0} qui cuisinera".format(user.name))
            if total > 1:
                outputs.insert(0,
                               "Ce midi, {0} personnes mangent:".format(total))
            else:
                outputs.insert(
                    0, "Ce midi, {0} personne mange, bravo à elle !!".format(
                        total))

        send_info(data['channel'], text='\n'.join(outputs), markdown=True)
Beispiel #7
0
    def cuisiner(self, data, user_data):

        session = DBSession()

        day = self.get_db_day(session)
        if not day:
            day = Day(date=datetime.date.today())
            session.add(day)
            session.commit()

        outputs = []

        user = self.get_db_user(session, user_data['name'])

        if not user:
            send_error(data['channel'],
                       'Erreur il faut ```python manage.py sync``` d\'abort !',
                       thread=data["ts"])

        presence = session.query(Presence).filter(
            Presence.user_id == user.id, Presence.day_id == day.id).first()

        if not presence:
            presence = Presence(user_id=user.id, day_id=day.id, cook=1)
            session.add(presence)
            session.commit()

            outputs.append(
                "J'ai pris en compte le fait que tu cuisinais aujourd'hui")

        else:
            presence.cook = 1
            session.add(presence)
            session.commit()

            outputs.append(
                "J'ai pris en compte le fait que tu cuisinais aujourd'hui")
        send_info(data['channel'],
                  text='\n'.join(outputs),
                  markdown=True,
                  ephemeral_user=user_data["id"])
        data['text'] = '!QuiMange'
        self.quimange(data, user_data)
Beispiel #8
0
    def manger(self, data, user_data, guest, for_user):

        session = DBSession()
        if guest < 0:
            send_error(
                data['channel'],
                "Touche à ton cul avec ton nombre négatif <@{0}> ;)".format(
                    user_data['id']),
                markdown=True,
                thread=data["ts"])
            return

        day = self.get_db_day(session)
        if not day:
            day = Day(date=datetime.date.today())
            session.add(day)
            session.commit()

        outputs = []

        if for_user:
            user = self.get_db_user(session, for_user)
            from_user = self.get_db_user(session, user_data['name'])
        else:
            user = self.get_db_user(session, user_data['name'])

        if not user:
            send_error(data['channel'],
                       'Erreur il faut ```python manage.py sync``` d\'abort !',
                       thread=data["ts"])

        presence = session.query(Presence).filter(
            Presence.user_id == user.id, Presence.day_id == day.id).first()

        if not presence:
            presence = Presence(user_id=user.id,
                                day_id=day.id,
                                meals=guest + 1)
            session.add(presence)
            session.commit()

            if for_user:
                outputs.append(
                    "Demande enregistrée par <@{0}> pour <@{1}>".format(
                        from_user.slackid, user.slackid))
                if guest > 0:
                    outputs.append("Je compterai {0} part(s)".format(guest +
                                                                     1))
            else:
                outputs.append("J'ai pris en compte ta demande <@{0}>".format(
                    user_data['id']))
                if guest > 0:
                    outputs.append(
                        "Je te compterai {0} part ce jour".format(guest + 1))
        else:
            if presence.meals != guest + 1:
                presence.meals = guest + 1
                session.add(presence)
                session.commit()

                if for_user:
                    if guest > 0:
                        outputs.append(
                            "Demande modifiée par <@{0}> pour <@{1}>, {2} invité(s) rajoutés"
                            .format(from_user.slackid, user.slackid, guest))
                    else:
                        outputs.append(
                            "Demande modifiée par <@{0}> pour <@{1}>, les invités ont été supprimés"
                            .format(from_user.slackid, user.slackid, guest))
                else:
                    if guest > 0:
                        outputs.append(
                            "J'ai modifié ta demande, et rajouté {0} invité(s) <@{1}>"
                            .format(guest, user.slackid))
                    else:
                        outputs.append(
                            "J'ai modifié ta demande, et enlevé les invités <@{1}>"
                            .format(guest, user.slackid))
            else:
                if for_user:
                    outputs.append(
                        "Rien n'a changé, <@{0}> était déjà inscrit <@{1}> :)".
                        format(user.slackid, from_user.slackid))
                else:
                    outputs.append(
                        "Rien n'a changé, tu étais déjà inscrit <@{0}> :)".
                        format(user.slackid))

        outputs.append(
            "Si besoin tu peux !CancelManger ou !Manger avec des invités :)")
        if for_user:
            send_info(data['channel'],
                      text='\n'.join(outputs),
                      markdown=True,
                      ephemeral_user=user.slackid)
            send_info(data['channel'],
                      text='\n'.join(outputs),
                      markdown=True,
                      ephemeral_user=from_user.slackid)
        else:
            send_info(data['channel'],
                      text='\n'.join(outputs),
                      markdown=True,
                      ephemeral_user=user_data["id"])
        data['text'] = '!QuiMange'
        self.quimange(data, user_data)