From cdfd6c3e491306b8bc69518a43fdcfc5d1dd39e8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 3 Feb 2020 23:08:00 +0100 Subject: [PATCH] Promote aliases to royalnet --- royalnet/backpack/tables/__init__.py | 2 ++ royalnet/backpack/tables/aliases.py | 35 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 royalnet/backpack/tables/aliases.py diff --git a/royalnet/backpack/tables/__init__.py b/royalnet/backpack/tables/__init__.py index b33c646f..d4c750e9 100644 --- a/royalnet/backpack/tables/__init__.py +++ b/royalnet/backpack/tables/__init__.py @@ -3,6 +3,7 @@ from .users import User from .telegram import Telegram from .discord import Discord from .matrix import Matrix +from .aliases import Alias # Enter the tables of your Pack here! available_tables = { @@ -10,6 +11,7 @@ available_tables = { Telegram, Discord, Matrix, + Alias } # Don't change this, it should automatically generate __all__ diff --git a/royalnet/backpack/tables/aliases.py b/royalnet/backpack/tables/aliases.py new file mode 100644 index 00000000..0f4ec1f8 --- /dev/null +++ b/royalnet/backpack/tables/aliases.py @@ -0,0 +1,35 @@ +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") + + @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 + + def __repr__(self): + return f"" + + def __str__(self): + return f"{self.alias}->{self.royal_id}"