def action(self):
     self.notify_checklist_incomplete = 'Ran'
     logging.debug(self.last_actor)
     hook = Webhook.get(model_id=self.card_data['idBoard'])
     card = TrelloCard.get(idCard=self.card_data['id'])
     logging.debug(hook.user)
     unarchived = unarchive_card(self.card_data['id'], hook.user)
     if unarchived == True:
         if len(self.card_data['idMembers']) == 0:
             trello_user = TrelloUserInfo.get(trello_id=card.idMemberCreator)
             send_email('*****@*****.**', """
                 Hello, A card has been archived that has an incomplete checklist. It has
                 been unarchived. Please take a moment to look into this matter. %s Thanks!
                 The Nebri Support Team This email should have been sent to %s.
             """ % (self.card_data['shortUrl'], trello_user.username), "An Incomplete Card has been Archived")
         else:
             for member in self.card_data['idMembers']:
                 trello_user = TrelloUserInfo.get(trello_id=member)
                 send_email('*****@*****.**', """
                 Hello, A card has been archived that has an incomplete checklist. It has
                 been unarchived. Please take a moment to look into this matter. %s Thanks!
                 The Nebri Support Team This email should have been sent to %s.
                 """ % (self.card_data['shortUrl'], trello_user.username), "An Incomplete Card has been Archived")
     else:
         send_email('*****@*****.**', """
         An error occurred... %s
         """ % unarchived, "An Error Occurred during Archiving")
def delete_hooks(user, hook_id=None):
    try:
        client = get_client(user)
        if hook_id is None:
            # we delete them  all!
            hooked_ids = [h.get('idModel') for h in client.fetch_json('/members/me/tokens?webhooks=true') if h.get('idModel')]
            logging.debug('delete all the hooks!')
            for hook in hooked_ids:
                try:
                    client.fetch_json(
                        '/webhooks/%s' % hook,
                        http_method='DELETE'
                    )
                except Exception as e:
                    logging.debug(str(e))
                    return str(e)
        else:
            logging.debug('delete just one hook')
            try:
                client.fetch_json(
                    '/webhooks/%s' % hook_id,
                    http_method='DELETE'
                )
            except Exception as e:
                logging.error(str(e))
                return str(e)

        hooked_ids = [h.get('idModel') for h in client.fetch_json('/members/me/tokens?webhooks=true') if h.get('idModel')]
        webhooks = Webhook.filter()
        for hook in webhooks:
            if hook.trello_id not in hooked_ids:
                hook.delete()
        return True
    except Exception as e:
        return str(e)
def create_webhook(idboard, client, user):
    board = client.fetch_json('/boards/%s' % idboard)
    hook = {
        'desc': 'Webhook for board %s' %  board.get('name'),
        'callback_url': '%s/api/v1/trello_webhook/callback?user=%s&id=%s' %(INSTANCE_HTTPS_URL, user, idboard),
        'id_model': idboard,
        'type_model': 'board'
    }
    new_hook = Webhook(
        user=user,
        description=hook['desc'],
        callback=hook['callback_url'],
        model_id=hook['id_model'],
        model_type=hook['type_model']
    )
    new_hook.save()
    logging.info(new_hook)
    webhook = client.create_hook(hook['callback_url'], hook['id_model'], desc=hook['desc'])
    logging.info(webhook)
    if webhook is False:
        # an error occurred during webhook creation. let's try manual creation.
        data = {'callbackURL': hook['callback_url'], 'idModel': hook['id_model'],
                'description': hook['desc']}
        webhook = client.fetch_json('/webhooks', http_method='POST', post_args=data)
        logging.info(webhook)
        new_hook.trello_id = webhook['id']
    else:
        new_hook.trello_id = webhook.id
    new_hook.save()
def settings(request):
    logging.debug(request.BODY)
    if request.FORM:
        user = request.user
        try:
            hooks = Webhook.filter(user=user)
            logging.debug(hooks)
            logging.debug(request.FORM)
        except:
            logging.debug('oops')
    return '200 OK'
    def action(self):
        self.handle_unapproved_archived = 'Ran'
        hook = Webhook.get(model_id=self.card_data['idBoard'])
        try:
            card = TrelloCard.get(idCard=self.card_data['id'], user=self.default_user)
        except Process.DoesNotExist:
            return

        unarchived = unarchive_card(self.card_data['id'], hook.user)
        if unarchived == True:
            trello_user = TrelloUserInfo.get(trello_id=card.idMemberCreator)
            send_email('*****@*****.**', """
            Hello, A card has been archived which was not approved by a board admin. It has
            been unarchived. Please take a moment to look into this matter. %s Thanks!
            The Nebri Support Team  Team This email should have been sent to %s.
            """ % (self.card_data['shortUrl'], trello_user.username), "An Unapproved Card has been Archived")
        else:
            send_email('*****@*****.**', """
            An error occurred... %s
            """ % unarchived, "An Error Occurred during Archiving")
def callback(request):
    logging.debug('webhook received!')
    logging.debug('what in the world')
    try:
        webhook = Webhook.get(model_id=request.GET['id'])
    except Exception as e:
        logging.info('ERROR: %s' % (str(e)))
    user = request.GET['user']
    client = get_client(user)
    logging.debug(client)
    comment_data = None
    card_json = None
    if request.BODY == '':
        # this is a test webhook for setup. return ok.
        return '200 OK'
    if 'card' in request.BODY['action']['data']:
        logging.debug('update or create card!')
        card_json = client.fetch_json('cards/%s?checklists=all&' % request.BODY['action']['data']['card']['id'])
        try:
            card, new = card_json_to_model(card_json, user)
            logging.debug(card.idMemberCreator)
            if card.idMemberCreator is None or card.idMemberCreator is False:
                card_creator = get_card_creator(card.idCard, client)
                card.idMemberCreator = card_creator
                card.save()
        except Exception as e:
            logging.debug(str(e))
        comment_data = client.fetch_json('cards/%s?actions=commentCard' % request.BODY['action']['data']['card']['id'])
        logging.debug(comment_data)
        board_admins = [admin.get('username') for admin in client.fetch_json('boards/%s/members/admins' % request.BODY['action']['data']['board']['id']) if admin.get('username')]
        logging.debug(board_admins)
        p = Process.objects.create()
        p.hook_data = request.BODY
        p.card_data = card_json
        p.comment_data = comment_data
        p.board_admins = board_admins
        p.handle_trello_webhook = True
        p.default_user = user
        p.save()
        logging.debug(p)
    return '200 OK'