Commit 84ca4f17 authored by Kevin Morris's avatar Kevin Morris
Browse files

add aurweb.models.user.User

+ Added aurweb.models.user.User class. This is the first example
  of an sqlalchemy ORM model. We can search for users via for example:
  `session.query(User).filter(User.ID==1).first()`, where `session` is
  a configured `aurweb.db.session` object.
+ Along with the User class, defined the AccountType class.
  Each User maintains a relationship to its AccountType via User.AccountType.
+ Added AccountType.users backref.
Signed-off-by: Kevin Morris's avatarKevin Morris <>
parent fdc5c7ca
from sqlalchemy.orm import backref, mapper, relationship
from aurweb.models.account_type import AccountType
from aurweb.schema import Users
class User:
""" An ORM model of a single Users record. """
def __init__(self, **kwargs):
self.AccountTypeID = kwargs.get("AccountTypeID")
account_type = kwargs.get("AccountType")
if account_type:
self.AccountType = account_type
self.Username = kwargs.get("Username")
self.Email = kwargs.get("Email")
self.BackupEmail = kwargs.get("BackupEmail")
self.Passwd = kwargs.get("Passwd")
self.Salt = kwargs.get("Salt")
self.RealName = kwargs.get("RealName")
self.LangPreference = kwargs.get("LangPreference")
self.Timezone = kwargs.get("Timezone")
self.Homepage = kwargs.get("Homepage")
self.IRCNick = kwargs.get("IRCNick")
self.PGPKey = kwargs.get("PGPKey")
self.RegistrationTS = kwargs.get("RegistrationTS")
self.CommentNotify = kwargs.get("CommentNotify")
self.UpdateNotify = kwargs.get("UpdateNotify")
self.OwnershipNotify = kwargs.get("OwnershipNotify")
self.SSOAccountID = kwargs.get("SSOAccountID")
def __repr__(self):
return "<User(ID='%s', AccountType='%s', Username='%s')>" % (
self.ID, str(self.AccountType), self.Username)
# Map schema.Users to User and give it some relationships.
mapper(User, Users, properties={
"AccountType": relationship(AccountType,
backref=backref("users", lazy="dynamic"))
import pytest
from aurweb.models.account_type import AccountType
from aurweb.models.user import User
from aurweb.testing import setup_test_db
def setup():
def test_user():
""" Test creating a user and reading its columns. """
from aurweb.db import session
# First, grab our target AccountType.
account_type = session.query(AccountType).filter(
AccountType.AccountType == "User").first()
user = User(
RealName="Test User", Username="test",
Email="", Passwd="abcd",
Salt="efgh", ResetKey="blahblah")
assert user in account_type.users
# Make sure the user was created and given an ID.
assert bool(user.ID)
# Search for the user via query API.
result = session.query(User).filter(User.ID == user.ID).first()
# Compare the result and our original user.
assert result.ID == user.ID
assert result.AccountType.ID == user.AccountType.ID
assert result.Username == user.Username
assert result.Email == user.Email
# Ensure we've got the correct account type.
assert user.AccountType.ID == account_type.ID
assert user.AccountType.AccountType == account_type.AccountType
# Test out user string functions.
assert repr(user) == f"<User(ID='{user.ID}', " + \
"AccountType='User', Username='test')>"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment