def set_fill_for_message(self, message: Message, fill: FillDto) -> None:
     fill_json = fill.to_json()
     self.rdb.set(f'{message.chat.chat_id}_{message.message_id}_fill',
                  fill_json)
     self.logger.debug(
         f'Save to cache fill {fill_json} for chat {message.chat.chat_id}, message {message.message_id}'
     )
Beispiel #2
0
 def parse(self, message: Message) -> Optional[IParsedMessage[FillDto]]:
     """Returns FillDto on successful parse or None if no fill was found."""
     message_text = message.text
     cnt = 0
     for number_match in re.finditer(number_regexp, message_text):
         cnt += 1
         amount = number_match.group()
         before_phrase = message_text[:number_match.start()].strip()
         after_phrase = message_text[number_match.end():].strip()
         if len(before_phrase) > 0 and len(after_phrase) > 0:
             description = ' '.join([before_phrase, after_phrase])
         else:
             description = before_phrase + after_phrase
     if cnt == 1:
         scope = self.card_fill_service.get_scope(message.chat.chat_id)
         fill = FillDto(
             id=None,
             user=UserDto.from_telegramapi(message.from_user),
             fill_date=datetime.fromtimestamp(message.date),
             amount=float(amount),
             description=description,
             category=None,
             scope=scope
         )
         return IParsedMessage(original_message=message, data=fill)
     return None
 def get_fill_for_message(self, message: Message) -> Optional[FillDto]:
     fill_json = self.rdb.get(
         f'{message.chat.chat_id}_{message.message_id}_fill')
     self.logger.info(
         f'Get from cache fill {fill_json} for chat {message.chat.chat_id}, message {message.message_id}'
     )
     if not fill_json:
         return None
     return FillDto.from_json(fill_json)
    def handle_new_fill(self, fill: FillDto) -> FillDto:
        db_session = self.DbSession()
        try:
            user = db_session.query(TelegramUser).get(fill.user.id)
            if not user:
                new_user = TelegramUser(
                    user_id=fill.user.id,
                    is_bot=fill.user.is_bot,
                    first_name=fill.user.first_name,
                    last_name=fill.user.last_name,
                    username=fill.user.username,
                    language_code=fill.user.language_code,
                )
                db_session.add(new_user)
                user = new_user
                self.logger.info(f'Create new user {user}')

            fill_category = db_session.query(Category).get('OTHER')
            try:
                fill_category: Category = next(
                    filter(
                        lambda cat: cat.fill_fits_category(fill.description),
                        db_session.query(Category).all()))
            except StopIteration:
                pass
            fill.category = CategoryDto.from_model(fill_category)

            card_fill = CardFill(
                user_id=user.user_id,
                fill_date=fill.fill_date,
                amount=fill.amount,
                description=fill.description,
                category_code=fill_category.code,
                fill_scope=fill.scope.scope_id,
            )
            db_session.add(card_fill)
            db_session.commit()
            fill.id = card_fill.fill_id
            self.logger.info(f'Save fill {fill}')
            return fill
        finally:
            self.DbSession.remove()
 def change_category_for_fill(self, fill_id: int,
                              target_category_code: str) -> FillDto:
     db_session = self.DbSession()
     try:
         fill = db_session.query(CardFill).get(fill_id)
         category = db_session.query(Category).get(target_category_code)
         old_category = fill.category
         fill.category_code = category.code
         if old_category.code == 'OTHER' and fill.description:
             category.add_alias(fill.description.lower())
             self.logger.info(
                 f'Add alias {fill.description} to category {category}')
         db_session.commit()
         self.logger.info(f'Change category for fill {fill} to {category}')
         return FillDto.from_model(fill)
     finally:
         self.DbSession.remove()
 def get_user_fills_in_months(self, user: UserDto, months: List[Month],
                              year: int,
                              scope: FillScopeDto) -> List[FillDto]:
     db_session = self.DbSession()
     try:
         user_fills = [
             FillDto.from_model(fill) for fill in db_session.query(
                 TelegramUser).get(user.id).card_fills
         ]
         month_numbers = [month.value for month in months]
         return list(
             filter(
                 lambda cf:
                 (cf.fill_date.month in month_numbers and cf.fill_date.year
                  == year and cf.scope.scope_id == scope.scope_id),
                 user_fills))
     finally:
         self.DbSession.remove()
 def get_fill_by_id(self, fill_id: int) -> FillDto:
     db_session = self.DbSession()
     try:
         return FillDto.from_model(db_session.query(CardFill).get(fill_id))
     finally:
         self.DbSession.remove()