diff --git a/db.py b/db.py index dba4ca7..52a9793 100644 --- a/db.py +++ b/db.py @@ -1,8 +1,13 @@ +from datetime import datetime from typing import List, Optional from sqlalchemy import create_engine, String, select, ForeignKey from sqlalchemy.orm import Session, DeclarativeBase, Mapped, mapped_column, relationship +def tow(day: int, hour: int, minute: int): + return minute + hour * 60 + day * 60 * 24 + + class Base(DeclarativeBase): pass @@ -46,6 +51,17 @@ class User(Base): 'queue': [x.to_dict() for x in self.queue], } + def is_active_now(self): + if not len(self.schedule): + return True + dt = datetime.now() + curr_tow = tow(dt.weekday(), dt.hour, dt.minute) + for x in self.schedule: + if (tow(x.day_start, x.hour_start, x.minute_start) <= curr_tow + <= tow(x.day_end, x.hour_end, x.minute_end)): + return True + return False + class Profile(Base): __tablename__ = 'profiles' diff --git a/main.py b/main.py index 3abda0f..d48a663 100644 --- a/main.py +++ b/main.py @@ -70,8 +70,9 @@ def replication_task(): with (Session(conn) as session): for bndname in connected: for item in session.query(db.Queue).join(db.Queue.user).filter_by(bndname=bndname).all(): - replication(bndname, item.commit_id, item.schema) - session.delete(item) + if item.user.is_active_now(): + replication(bndname, item.commit_id, item.schema) + session.delete(item) session.commit() time.sleep(60)