Files
2026-03-15 19:52:21 +00:00

203 lines
7.8 KiB
Python

"""
Project: Magic Tracker
Author: Edward Middleton-Smith
Shuffle & Skirmish
Technology: Business Objects
Feature: User Relationship Business Object
"""
# internal
from business_objects.base import Base
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
import lib.argument_validation as av
from extensions import db
from helpers.helper_app import Helper_App
# external
from dataclasses import dataclass
from typing import ClassVar
from sqlalchemy import Uuid
from sqlalchemy.types import Text, Boolean, Integer
# import uuid
class User_Relationship(SQLAlchemy_ABC, Base):
ATTR_RELATIONSHIP_ID: ClassVar[str] = 'relationship_id'
FLAG_FOLLOWER_USER_ID: ClassVar[str] = 'follower_user_id'
FLAG_FOLLOWING_USER_ID: ClassVar[str] = 'following_user_id'
FLAG_IS_FOLLOWING: ClassVar[str] = 'is_following'
FLAG_IS_BLOCKED: ClassVar[str] = 'is_blocked'
NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_RELATIONSHIP_ID
NAME_ATTR_OPTION_TEXT: ClassVar[str] = ATTR_RELATIONSHIP_ID
__tablename__ = 'tcg_user_relationship'
__table_args__ = { 'extend_existing': True }
relationship_id = db.Column(db.Integer, primary_key=True)
follower_user_id = db.Column(db.Integer)
following_user_id = db.Column(db.Integer)
is_following = db.Column(db.Boolean)
is_blocked = db.Column(db.Boolean)
active = db.Column(db.Boolean)
created_on = db.Column(db.DateTime)
created_by_user_id = db.Column(db.Integer)
updated_last_on = db.Column(db.DateTime)
updated_last_by_user_id = db.Column(db.Integer)
change_set_id = db.Column(db.Integer)
def __init__(self):
self.relationship_id = 0
self.is_new = False
super().__init__()
@classmethod
def from_db_user_relationship(cls, query_row):
_m = f'{cls.__qualname__}.from_db_user_relationship'
player = cls()
player.relationship_id = query_row[0]
player.follower_user_id = query_row[1]
player.following_user_id = query_row[2]
player.is_following = query_row[3]
player.is_blocked = query_row[4]
player.active = av.input_bool(query_row[5], cls.FLAG_ACTIVE, _m)
player.created_on = query_row[6]
player.created_by_user_id = query_row[7]
return player
@classmethod
def from_json(cls, json):
_m = f'{cls.__qualname__}.from_json'
player = cls()
if json is None: return player
player.relationship_id = json.get(cls.ATTR_RELATIONSHIP_ID, -1)
player.follower_user_id = json.get(cls.FLAG_FOLLOWER_USER_ID, None)
player.following_user_id = json.get(cls.FLAG_FOLLOWING_USER_ID, None)
player.is_following = json.get(cls.FLAG_IS_FOLLOWING, None)
player.is_blocked = json.get(cls.FLAG_IS_BLOCKED, None)
player.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
player.created_on = json.get(cls.FLAG_CREATED_ON, None)
player.created_by_user_id = json.get(Base.ATTR_USER_ID, None)
return player
def to_json(self):
as_json = {
**self.get_shared_json_attributes(self)
, self.ATTR_RELATIONSHIP_ID: self.relationship_id
, self.FLAG_FOLLOWER_USER_ID: self.follower_user_id
, self.FLAG_FOLLOWING_USER_ID: self.following_user_id
, self.FLAG_IS_FOLLOWING: self.is_following
, self.FLAG_IS_BLOCKED: self.is_blocked
, self.FLAG_ACTIVE: self.active
, self.FLAG_CREATED_ON: self.created_on
}
return as_json
def __repr__(self):
return f'''
{self.__class__.__name__}(
{self.ATTR_RELATIONSHIP_ID}: {self.relationship_id}
{self.FLAG_FOLLOWER_USER_ID}: {self.follower_user_id}
{self.FLAG_FOLLOWING_USER_ID}: {self.following_user_id}
{self.FLAG_IS_FOLLOWING}: {self.is_following}
{self.FLAG_IS_BLOCKED}: {self.is_blocked}
{self.FLAG_ACTIVE}: {self.active}
{self.FLAG_CREATED_ON}: {self.created_on}
)
'''
class User_Relationship_Temp(db.Model, Base):
__tablename__ = 'tcg_user_relationship_temp'
__table_args__ = { 'extend_existing': True }
temp_id = db.Column(db.Integer, primary_key=True)
relationship_id = db.Column(db.Integer)
follower_user_id = db.Column(db.Integer)
following_user_id = db.Column(db.Integer)
is_following = db.Column(db.Boolean)
is_blocked = db.Column(db.Boolean)
active = db.Column(db.Boolean)
created_on = db.Column(db.DateTime)
guid = db.Column(Uuid) #, default = uuid.uuid4)
def __init__(self):
super().__init__()
@classmethod
def from_user_relationship(cls, relationship, guid):
_m = 'User_Relationship_Temp.from_user_relationship'
temp = cls()
temp.relationship_id = relationship.relationship_id
temp.follower_user_id = relationship.follower_user_id
temp.following_user_id = relationship.following_user_id
temp.is_following = relationship.is_following
temp.is_blocked = relationship.is_blocked
temp.active = relationship.active
temp.created_on = relationship.created_on
temp.guid = guid
return temp
class Parameters_User_Relationship(Get_Many_Parameters_Base):
access_user_id: int
get_inactive_user_relationship: bool
get_all_user_follower: bool
follower_user_ids: str
get_all_user_following: bool
following_user_ids: str
get_inactive_user: bool
require_all_id_filters_met: bool
require_any_id_filters_met: bool
@classmethod
def get_default(cls, user_id_session):
return cls(
access_user_id = user_id_session
, get_inactive_user_relationship = True
, get_all_user_follower = False
, follower_user_ids = ''
, get_all_user_following = True
, following_user_ids = ''
, get_inactive_user = False
, require_all_id_filters_met = True
, require_any_id_filters_met = True
)
@classmethod
def from_json(cls, json):
return cls(
access_user_id = json.get('a_access_user_id', None)
, get_inactive_user_relationship = json.get('a_get_inactive_user_relationship', False)
, get_all_user_follower = json.get('a_get_all_user_follower', False)
, follower_user_ids = json.get('a_follower_user_ids', '')
, get_all_user_following = json.get('a_get_all_user_following', False)
, following_user_ids = json.get('a_following_user_ids', '')
, get_inactive_user = json.get('a_get_inactive_user', False)
, require_all_id_filters_met = json.get('a_require_all_id_filters_met', True)
, require_any_id_filters_met = json.get('a_require_any_id_filters_met', True)
)
def to_json(self):
return {
'a_access_user_id': self.access_user_id
, 'a_get_inactive_user_relationship': self.get_inactive_user_relationship
, 'a_get_all_user_follower': self.get_all_user_follower
, 'a_follower_user_ids': self.follower_user_ids
, 'a_get_all_user_following': self.get_all_user_following
, 'a_following_user_ids': self.following_user_ids
, 'a_get_inactive_user': self.get_inactive_user
, 'a_require_all_id_filters_met': self.require_all_id_filters_met
, 'a_require_any_id_filters_met': self.require_any_id_filters_met
}
@staticmethod
def get_type_hints():
return {
'a_access_user_id': Integer
, 'a_get_inactive_user_relationship': Boolean
, 'a_get_all_user_follower': Boolean
, 'a_follower_user_ids': Text
, 'a_get_all_user_following': Boolean
, 'a_following_user_ids': Text
, 'a_get_inactive_user': Boolean
, 'a_require_all_id_filters_met': Boolean
, 'a_require_any_id_filters_met': Boolean
}