def auth(): try: body = request.json or {} if "email" not in body or "password" not in body: raise UnprocessableEntity("Email or password is required") email = body["email"] password = body["password"] logger.info("Initializing auth user") user = factory.get_user() token = user.auth(email, password) return token except (Unauthorized, UnprocessableEntity) as error: code = error.code message = error.message error = {"message": message} response = make_response(error, code) return response except Exception as error: error = {"error": str(error)} response = make_response(error, 500) return response
def format_cards(self, cards): logger.info("Initializing format cards") user_id = self._config.get_trello_user() formatted_cards = [] for card in cards: name = card["name"] url = card["url"] member_id = card["idMembers"][0] if len( card["idMembers"]) > 0 else None due = card["due"] due_complete = card["dueComplete"] if user_id == member_id and due is not None and not due_complete: due = self._date_helper.to_date(due) formatted_card = { "name": name, "due": due, "url": url, "user": user_id } formatted_cards.append(formatted_card) return formatted_cards
def get(self, filters): logger.info("Initializing get tasks") search_filter = {"$and": []} if "id" in filters: search_filter["$and"].append( {"_id": self._db.to_object_id(filters["id"])}) if "user" in filters: search_filter["$and"].append({"usuId": filters["user"]}) if "start_date" in filters or "end_date" in filters: date_filter = {"date": {}} if "start_date" in filters: date_filter["date"]["$gte"] = self._date_helper.to_date( filters["start_date"]) if "end_date" in filters: date_filter["date"]["$lte"] = self._date_helper.to_date( filters["end_date"]) search_filter["$and"].append(date_filter) if "isConclude" in filters: search_filter["$and"].append({"isConclude": filters["isConclude"]}) if "is_failed" in filters: search_filter["$and"].append( {"is_failed": { "$ne": not filters["is_failed"] }}) r = self._db.get(self._collection, search_filter, sort=[["date", 1]]) return r
def __init__(self, trello_card_controller, zac_task_controller, routine_controller, model_helper): logger.info("Initializing Tasks") self._trello_card_controller = trello_card_controller self._zac_task_controller = zac_task_controller self._routine_controller = routine_controller self._model_helper = model_helper
def get(self, user, query): logger.info("Initializing get tasks") trello_user = user["trello_user"] telegram_user = user["telegram_user"] trello_query = query trello_query["user"] = trello_user trello_cards = self._trello_card_controller.get(trello_query) is_conclude = query.is_conclude if "is_conclude" in query else False is_failed = query.is_failed if "is_failed" in query else False zac_query = query zac_query["user"] = telegram_user zac_query["isConclude"] = is_conclude zac_query["is_failed"] = is_failed zac_tasks = self._zac_task_controller.get(zac_query) cards = self._model_helper.trello_cards(trello_cards) tasks = self._model_helper.zac_tasks(zac_tasks) tasks.extend(cards) tasks.sort(key=lambda x: x.get("date") or x.get("due")) return {"tasks": tasks}
def get_boards(self): logger.info("Initializing get boards") url = self._config.get_boards_url() params = self._config.get_trello_auth() boards = self._request_helper.get(url, params) return boards
def main(): logger.info("Initialize Zac") event = threading.Event() routine = ThreadJob(updated_cards, event, 86400) routine.start() http_server.serve_forever()
def __init__(self, dao, date_helper, crypto_helper, model_helper, jwt_helper): logger.info("Initializing user") self._dao = dao self._date_helper = date_helper self._crypto_helper = crypto_helper self._model_helper = model_helper self._jwt_helper = jwt_helper
def process_boards(self, boards): logger.info("Initializing process boards") for board in boards: id = board["shortLink"] is_closed = board["closed"] if not is_closed: self.process_cards(id)
def get_trello_auth(self): logger.info("Getting trello's auth ") key = self.get_trello_key() token = self.get_trello_token() params = {"key": key, "token": token} return params
def __init__(self): logger.info("Initializing Trello crawler") factory_controller = FactoryController() self._config = Config() self._request_helper = RequestHelper() self._date_helper = DateHelper() self._trello_card_controller = factory_controller.get_trello_card()
def main(): config = Config.build() api = TwitterApi.build(auth_config=config.auth) kafka_producer = KafkaProducer(bootstrap_servers=config.sink.kafka.url) kafka_sink = KafkaSink(topic=config.sink.kafka.topic, producer=kafka_producer) logger.info('Running...') stream = TwitterStream.build(api=api, filters=filters, sink=kafka_sink) stream.run()
def to_sha256(self, text): logger.info("Initialize encoding") text_encoded = text.encode() salt = self._config.get_salt() text_to_transform = text_encoded + salt.encode() sha256_text = hashlib.sha256(text_to_transform).hexdigest() return sha256_text
def get_cards(self, board_id): logger.info("Initializing get cards by {}".format(board_id)) url = self._config.get_cards_url() params = self._config.get_trello_auth() url = url.format(board_id) cards = self._request_helper.get(url, params) return cards
def auth(self, email, password): logger.info("Initializing auth") encrypted_password = self._crypto_helper.to_sha256(password) users = self._dao.get({"email": email, "password": encrypted_password}) if len(users) == 0: raise Unauthorized("Email or password is incorrect") user = self._model_helper.user(users[0]) token = self._jwt_helper.encode_token(user) return {"token": token}
def get(self, filters): logger.info("Initializing get tasks") search_filter = {"$and": []} if "user" in filters: search_filter["$and"].append({"userId": filters["user"]}) if "days" in filters: search_filter["$and"].append({"days": {"$in": filters["days"]}}) if "isActive" in filters: search_filter["$and"].append({"isActive": filters["isActive"]}) r = self._db.get(self._collection, search_filter, sort=[["date", 1]]) return r
def conclude(self, user, id): logger.info("Initializing conclude task") telegram_user = user["telegram_user"] filter = {"id": id, "user": telegram_user} tasks = self._zac_task_controller.get(filter) if len(tasks) == 0: logger.error("Task not found") raise NotFound("Task not found") task = tasks[0] if task["isConclude"] or ("is_failed" in task and task["is_failed"]): logger.error("Task is already concluded or failed") raise Conflict("Task is already concluded or failed") self._zac_task_controller.update(filter, {"isConclude": True})
def update(self, filters, entity): logger.info("Initializing update tasks") update_filter = {"$and": []} update_entity = {"$set": {}} if "id" in filters: update_filter["$and"].append( {"_id": self._db.to_object_id(filters["id"])}) if "user" in filters: update_filter["$and"].append({"usuId": filters["user"]}) if "isConclude" in entity: update_entity["$set"]["isConclude"] = entity["isConclude"] if "is_failed" in entity: update_entity["$set"]["is_failed"] = entity["is_failed"] r = self._db.update(self._collection, update_filter, update_entity) return r
def get(self, filters): logger.info("Initializing get cards") search_filter = {"$and": []} if "user" in filters: search_filter["$and"].append({"user": filters["user"]}) if "start_date" in filters or "end_date" in filters: date_filter = {"due": {}} if "start_date" in filters: date_filter["due"]["$gte"] = self._date_helper.to_date( filters["start_date"]) if "end_date" in filters: date_filter["due"]["$lte"] = self._date_helper.to_date( filters["end_date"]) search_filter["$and"].append(date_filter) r = self._db.get(self._collection, search_filter, sort=[["due", 1]]) return r
def get_scheduled(self, user, query): logger.info("Initializing get scheduled tasks") trello_user = user["trello_user"] telegram_user = user["telegram_user"] trello_query = query trello_query["user"] = trello_user trello_cards = self._trello_card_controller.get(trello_query) is_conclude = query.is_conclude if "is_conclude" in query else False is_failed = query.is_failed if "is_failed" in query else False zac_query = query zac_query["user"] = telegram_user # zac_query["isConclude"] = is_conclude # zac_query["is_failed"] = is_failed zac_tasks = self._zac_task_controller.get(zac_query) is_active = query.is_active if "is_active" in query else True routine_query = query routine_query["user"] = telegram_user routine_query["days"] = json.loads(query["days"]) routine_query["isActive"] = is_active zac_routines = self._routine_controller.get(routine_query) cards = self._model_helper.trello_cards(trello_cards) tasks = self._model_helper.zac_tasks(zac_tasks) routines = self._model_helper.zac_routines(zac_routines) tasks.extend(cards) tasks.sort(key=lambda x: x.get("date") or x.get("due")) return {"tasks": tasks, "routines": routines}
def get(self, filters): logger.info("Initializing get routine") routines = self._dao.get(filters) return routines
def __init__(self, dao, date_helper): logger.info("Initializing Routine") self._dao = dao
def get(self, filters): logger.info("Initializing get users") cards = self._dao.get(filters) return cards
def integrate_cards(*args, **kwargs): logger.info("Initializing updated cards") trello_crawler = TrelloCrawler() trello_crawler.integrate_cards() return {"message": "Done"}
def __init__(self, db_helper, date_helper): logger.info("Initializing TrelloCardDAO") self._db = db_helper self._date_helper = date_helper self._collection = "trello_cards"
def delete(self, filters): logger.info("Initializing delete cards") r = self._db.delete(self._collection, filters) return r
def save(self, trello_card): logger.info("Initializing save card") r = self._db.insert(self._collection, trello_card) return r
def save_cards(self, cards): logger.info("Initializing save cards") for card in cards: self._trello_card_controller.save(card)
def integrate_cards(self): logger.info("Initializing integration of cards") self._trello_card_controller.delete({}) boards = self.get_boards() self.process_boards(boards)
def process_cards(self, board_id): logger.info("Initializing process cards") cards = self.get_cards(board_id) cards = self.format_cards(cards) self.save_cards(cards)