nopaque/app/models/avatar.py

41 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-03-05 15:02:23 +00:00
from flask import current_app
from flask_hashids import HashidMixin
2024-03-07 14:49:04 +00:00
from pathlib import Path
2024-03-05 15:02:23 +00:00
from app import db
from .file_mixin import FileMixin
class Avatar(HashidMixin, FileMixin, db.Model):
__tablename__ = 'avatars'
# Primary key
id = db.Column(db.Integer, primary_key=True)
# Foreign keys
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
# Relationships
user = db.relationship('User', back_populates='avatar')
@property
2024-03-07 14:49:04 +00:00
def path(self) -> Path:
return self.user.path / 'avatar'
# return os.path.join(self.user.path, 'avatar')
2024-03-05 15:02:23 +00:00
def delete(self):
try:
2024-03-07 14:49:04 +00:00
self.path.unlink(missing_ok=True)
2024-03-05 15:02:23 +00:00
except OSError as e:
current_app.logger.error(e)
2024-03-07 14:49:04 +00:00
raise
2024-03-05 15:02:23 +00:00
db.session.delete(self)
def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = {
'id': self.hashid,
**self.file_mixin_to_json_serializeable()
}
if backrefs:
json_serializeable['user'] = \
self.user.to_json_serializeable(backrefs=True)
if relationships:
pass
return json_serializeable