Правка поздравлеия с ДР
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
from flask import Flask, redirect, request, jsonify, render_template, url_for
|
from flask import Flask, redirect, request, jsonify, render_template, url_for
|
||||||
from config import Config
|
from config import Config
|
||||||
from db import (
|
from db import (
|
||||||
@@ -33,6 +34,25 @@ init_scheduler(app)
|
|||||||
|
|
||||||
os.makedirs("logs", exist_ok=True)
|
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(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
||||||
|
|||||||
+12
-8
@@ -54,29 +54,33 @@ def handle_vk_birthdate():
|
|||||||
def is_leap_year(year: int) -> bool:
|
def is_leap_year(year: int) -> bool:
|
||||||
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
|
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
|
||||||
|
|
||||||
# Основное условие: совпадение дня и месяца
|
# условия ТОЛЬКО по дате
|
||||||
conditions = [
|
date_conditions = [
|
||||||
and_(
|
and_(
|
||||||
func.strftime("%d", UsersBirthdate.birthdate) == day,
|
func.strftime("%d", UsersBirthdate.birthdate) == day,
|
||||||
func.strftime("%m", UsersBirthdate.birthdate) == month,
|
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):
|
if today.month == 2 and today.day == 28 and not is_leap_year(today.year):
|
||||||
conditions.append(
|
date_conditions.append(
|
||||||
and_(
|
and_(
|
||||||
func.strftime("%d", UsersBirthdate.birthdate) == "29",
|
func.strftime("%d", UsersBirthdate.birthdate) == "29",
|
||||||
func.strftime("%m", UsersBirthdate.birthdate) == "02",
|
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:
|
if not birthdayUsers:
|
||||||
logger.info("Нет пользователей с днем рождения")
|
logger.info(f"Нет пользователей с днем рождения {day}.{month}")
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
|
logger.info(f"Найдено {len(birthdayUsers)} пользователей с днем рождения")
|
||||||
|
|
||||||
vkApi = VkAPI.query.first()
|
vkApi = VkAPI.query.first()
|
||||||
if not vkApi:
|
if not vkApi:
|
||||||
@@ -95,7 +99,7 @@ def handle_vk_birthdate():
|
|||||||
owner_id=-vkApi.group_id,
|
owner_id=-vkApi.group_id,
|
||||||
from_group=1,
|
from_group=1,
|
||||||
message=user.congratulations.strip(),
|
message=user.congratulations.strip(),
|
||||||
attachments=user.photo_link,
|
attachments=user.photo_link.replace("https://vk.com/", ""),
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f"Пост #{new_post.get('post_id')} создан")
|
logger.info(f"Пост #{new_post.get('post_id')} создан")
|
||||||
|
|||||||
Reference in New Issue
Block a user