1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-18 07:34:20 +00:00
royalnet/royalpack/tables/aliases.py

36 lines
949 B
Python
Raw Normal View History

2019-11-11 08:56:08 +00:00
from sqlalchemy import Column, \
Integer, \
String, \
ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declared_attr
class Alias:
__tablename__ = "aliases"
@declared_attr
def royal_id(self):
return Column(Integer, ForeignKey("users.uid"))
@declared_attr
def alias(self):
return Column(String, primary_key=True)
@declared_attr
def royal(self):
return relationship("User", backref="aliases")
2020-01-20 23:54:55 +00:00
@classmethod
def find_by_alias(cls, alchemy, session, alias: str):
result = session.query(alchemy.get(cls)).filter_by(alias=alias.lower()).one_or_none()
if result is not None:
result = result.royal
return result
2019-11-11 08:56:08 +00:00
def __repr__(self):
return f"<Alias {str(self)}>"
def __str__(self):
return f"{self.alias}->{self.royal_id}"