mirror of
https://github.com/DRYTRIX/TimeTracker.git
synced 2026-05-17 18:38:46 -05:00
b4486a627f
- Webhook models: remove duplicate index definitions so db.create_all() no longer raises 'index already exists' (columns already have index=True) - ImportService: fix circular import by late-importing ClientService, ProjectService, TimeTrackingService in __init__ - reports: fix F823 by renaming unpack variable _ to _entry_count to avoid shadowing gettext _ in export_task_excel() - Code quality: add .flake8 with extend-ignore so flake8 CI passes; simplify pyproject.toml isort config (drop unsupported options) - Format: run black and isort on app/ - tests: restore minimal app fixture in test_import_export_models
71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
"""
|
|
Push Subscription model for storing browser push notification subscriptions.
|
|
"""
|
|
|
|
import json
|
|
from datetime import datetime
|
|
|
|
from app import db
|
|
from app.utils.timezone import now_in_app_timezone
|
|
|
|
|
|
class PushSubscription(db.Model):
|
|
"""Model for storing browser push notification subscriptions"""
|
|
|
|
__tablename__ = "push_subscriptions"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
user_id = db.Column(db.Integer, db.ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
|
|
|
|
# Push subscription data (JSON format from browser Push API)
|
|
endpoint = db.Column(db.Text, nullable=False) # Push service endpoint URL
|
|
keys = db.Column(db.JSON, nullable=False) # p256dh and auth keys
|
|
|
|
# Metadata
|
|
user_agent = db.Column(db.String(500), nullable=True) # Browser user agent
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
|
last_used_at = db.Column(db.DateTime, nullable=True) # Last time subscription was used
|
|
|
|
# Relationships
|
|
user = db.relationship("User", backref="push_subscriptions", lazy="joined")
|
|
|
|
def __init__(self, user_id, endpoint, keys, user_agent=None):
|
|
"""Create a push subscription"""
|
|
self.user_id = user_id
|
|
self.endpoint = endpoint
|
|
self.keys = keys if isinstance(keys, dict) else json.loads(keys) if isinstance(keys, str) else {}
|
|
self.user_agent = user_agent
|
|
|
|
def __repr__(self):
|
|
return f"<PushSubscription {self.id} for user {self.user_id}>"
|
|
|
|
def to_dict(self):
|
|
"""Convert subscription to dictionary for API responses"""
|
|
return {
|
|
"id": self.id,
|
|
"user_id": self.user_id,
|
|
"endpoint": self.endpoint,
|
|
"keys": self.keys,
|
|
"user_agent": self.user_agent,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
|
|
"last_used_at": self.last_used_at.isoformat() if self.last_used_at else None,
|
|
}
|
|
|
|
def update_last_used(self):
|
|
"""Update the last_used_at timestamp"""
|
|
self.last_used_at = now_in_app_timezone()
|
|
self.updated_at = now_in_app_timezone()
|
|
db.session.commit()
|
|
|
|
@classmethod
|
|
def get_user_subscriptions(cls, user_id):
|
|
"""Get all active subscriptions for a user"""
|
|
return cls.query.filter_by(user_id=user_id).order_by(cls.created_at.desc()).all()
|
|
|
|
@classmethod
|
|
def find_by_endpoint(cls, user_id, endpoint):
|
|
"""Find a subscription by user and endpoint"""
|
|
return cls.query.filter_by(user_id=user_id, endpoint=endpoint).first()
|