diff --git a/app.db b/app.db index 873a7cc..e4c781c 100644 Binary files a/app.db and b/app.db differ diff --git a/app.py b/app.py index 7346dc4..12b7e5e 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +import json from flask import Flask, redirect, request, jsonify, render_template, url_for from config import Config from db import ( @@ -33,6 +34,25 @@ init_scheduler(app) os.makedirs("logs", exist_ok=True) + +class SmartLogger(logging.Logger): + def _log( + self, + level, + msg, + args, + exc_info=None, + extra=None, + stack_info=False, + stacklevel=2, + ): + if isinstance(msg, (dict, list)): + msg = json.dumps(msg, indent=4, ensure_ascii=False) + super()._log(level, msg, args, exc_info, extra, stack_info, stacklevel) + + +logging.setLoggerClass(SmartLogger) + logging.basicConfig( level=logging.INFO, format="%(asctime)s | %(levelname)s | %(name)s | %(message)s", diff --git a/vk_handler.py b/vk_handler.py index 3da9be4..b115af0 100644 --- a/vk_handler.py +++ b/vk_handler.py @@ -54,29 +54,33 @@ def handle_vk_birthdate(): def is_leap_year(year: int) -> bool: return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) - # Основное условие: совпадение дня и месяца - conditions = [ + # условия ТОЛЬКО по дате + date_conditions = [ and_( func.strftime("%d", UsersBirthdate.birthdate) == day, func.strftime("%m", UsersBirthdate.birthdate) == month, ) ] - # Если 28 февраля и год НЕ високосный — добавляем родившихся 29.02 + # 29 февраля для невисокосного года if today.month == 2 and today.day == 28 and not is_leap_year(today.year): - conditions.append( + date_conditions.append( and_( func.strftime("%d", UsersBirthdate.birthdate) == "29", func.strftime("%m", UsersBirthdate.birthdate) == "02", ) ) - conditions.append(UsersBirthdate.enabled == True) - birthdayUsers = UsersBirthdate.query.filter(or_(*conditions)).all() + birthdayUsers = UsersBirthdate.query.filter( + UsersBirthdate.enabled.is_(True), + or_(*date_conditions), + ).all() if not birthdayUsers: - logger.info("Нет пользователей с днем рождения") + logger.info(f"Нет пользователей с днем рождения {day}.{month}") return + else: + logger.info(f"Найдено {len(birthdayUsers)} пользователей с днем рождения") vkApi = VkAPI.query.first() if not vkApi: @@ -95,7 +99,7 @@ def handle_vk_birthdate(): owner_id=-vkApi.group_id, from_group=1, message=user.congratulations.strip(), - attachments=user.photo_link, + attachments=user.photo_link.replace("https://vk.com/", ""), ) logger.info(f"Пост #{new_post.get('post_id')} создан")