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 check(self):
        logging.info("start check")
        if self.default_user is None:
            self.default_user = DEFAULT_USER
        if self.trello_copy_template == True:
            try:
                card = TrelloCard.get(
                    idCard=self.idCard, is_template=True, closed=False, drip=self.drip, user=self.default_user
                )
                logging.info("card found")
            except Exception as e:
                logging.error("Error in copy template")
                logging.error(str(e))
                self.card_get_error = str(e)
                return False
            if card:
                return True

        return False
    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 action(self):
        self.handle_trello_webhook = "Ran"
        logging.debug("handle new trello webhook")
        if self.hook_data["action"]["type"] == "updateCard" and self.hook_data["action"]["data"]["card"].get(
            "closed", False
        ):
            # card has been archived. check for rule prerequisites
            logging.debug("this card was archived!")
            card = TrelloCard.get(idCard=self.card_data.get("id"), user=self.default_user)
            logging.debug(card)
            logging.debug(card.checklist_finished)
            # let's check checklists first
            if card.checklist_finished == False:
                self.notify_checklist_incomplete = True

            # next let's check for approved status
            approved_commenter = None
            for action in self.comment_data["actions"]:
                if action["data"]["text"] == "approved":
                    approved_commenter = action["memberCreator"]
                    break
            if approved_commenter is None or approved_commenter["username"] not in self.board_admins:
                self.handle_unapproved_archived = True
    def action(self):
        self.trello_copy_template = "RAN: {}".format(datetime.now())
        logging.info("start action")

        client = get_client(self.default_user)
        logging.info("client found")
        card = TrelloCard.get(idCard=self.idCard, user=self.default_user)

        card = template_checklist_setup(card, client)
        self.template_idList = card.template_idList
        logging.info("template_list {}".format(card.template_idList))
        # self.json_data = card.card_json
        # self.description = self.json_data.get('description')
        logging.info("card updated")

        out = copy_template_card(client, card)

        self.out = out.id

        # if new_card is not None:
        #     self.new_id = new_card.id

        logging.info("finished")
def card_json_to_model(card, user):
    epoch = '1970-01-01T12:00:00.00+0000'
    pepoch = iso8601.parse_date(epoch)

    logging.info('card list is next: ')
    logging.info(str(card))
    new = False
    # card_obj, new = TrelloCard.get_or_create(idCard=card.get('id'))
    try:
        card_obj = TrelloCard.get(idCard=card.get('id'), user=user)
    except Process.DoesNotExist:
        logging.debug('create new.....')
        card_obj = TrelloCard(idCard=card.get('id'), user=user)
        card_obj.save()
        new = True
    except Exception as e:
        logging.info('error finding too many cards...')
        logging.debug(str(e))
        if len(TrelloCard.filter(idCard=card.get('id'))) > 1:
            for tcard in TrelloCard.filter(idCard=card.get('id'), user=user):
                tcard.delete()
            logging.debug('create new.....')
            card_obj = TrelloCard(idCard=card.get('id'), user=user)
            card_obj.save()
            new = True


    # update card

    # check webhook json response
    card_obj.idBoard = card.get('idBoard')
    card_obj.idMembers = card.get('idMembers')
    card_obj.idLabels = card.get('idLabels')
    card_obj.idChecklists = card.get('idChecklists')
    card_obj.idList = card.get('idList')
    card_obj.checklists = card.get('checklists')
    card_obj.due = card.get('due')
    card_obj.due_epoch = None
    card_obj.due_datetime = None
    card_obj.name = card.get('name')
    card_obj.shortUrl = card.get('shortUrl')
    card_obj.card_json = card
    card_obj.closed = card.get('closed', False)
    card_obj.is_template = False
    card_obj.template_idBoard = None
    card_obj.template_idList = None
    card_obj.drip = None

    logging.info(str(card_obj.due))

    if card_obj.idMemberCreator is False:
        card_obj.idMemberCreator = None

    if card_obj.due is not None and card_obj.due != '':
        try:
            # Timezone was set to UTC in the instance. This will make sure that
            # UTC is always broght in
            duedate = iso8601.parse_date(card_obj.due)
        except Exception as e:
            logging.error('TrelloCard date error: ' + str(e))
        else:
            # card_obj.due_epoch = (duedate - epoch).total_seconds()
            card_obj.due_epoch = int(duedate.strftime('%s'))
            card_obj.due_datetime = duedate

    labels = card.get('labels', [])
    if labels != []:
        for label in labels:
            name = label.get('name')
            if name == 'template checklist':
                card_items = template_checklist_parser(card_obj)
                if card_items is None:
                    card_obj.is_template = False
                else:
                    card_obj.is_template = True
                    card_obj.drip = card_items.get('drip')

    card_obj.save()

    return card_obj, new