edbob/edbob/db/extensions/auth/model.py
2012-04-16 20:04:48 -05:00

103 lines
3.1 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic Software Framework
# Copyright © 2010-2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob.db.extensions.auth.model`` -- Schema Definition
"""
from sqlalchemy import *
from sqlalchemy.orm import relationship
from sqlalchemy.ext.associationproxy import association_proxy
import edbob
from edbob.db.model import Base, uuid_column
__all__ = ['Person', 'User']
def get_person_display_name(context):
first_name = context.current_parameters['first_name']
last_name = context.current_parameters['last_name']
if not (first_name or last_name):
return None
return '%(first_name)s %(last_name)s' % locals()
class Person(Base):
"""
Represents a real, living and breathing person. (Or, at least was
previously living and breathing, in the case of the deceased.)
"""
__tablename__ = 'people'
uuid = uuid_column()
first_name = Column(String(50))
last_name = Column(String(50))
display_name = Column(String(100), default=get_person_display_name)
def __repr__(self):
return "<Person: %s>" % self.display_name
def __str__(self):
return str(self.display_name or '')
class User(Base):
"""
Represents a user of the system. This may or may not correspond to a real
person, i.e. some users may exist solely for automated tasks.
"""
__tablename__ = 'users'
uuid = uuid_column()
username = Column(String(25), nullable=False, unique=True)
password = Column(String(60))
salt = Column(String(29))
person_uuid = Column(String(32), ForeignKey('people.uuid'))
person = relationship(Person, backref='user')
# display_name = association_proxy('person', 'display_name')
# roles = association_proxy('_roles', 'role',
# creator=lambda x: UserRole(role=x),
# getset_factory=getset_factory)
def __repr__(self):
return "<User: %s>" % self.username
def __str__(self):
return str(self.username or '')
@property
def display_name(self):
"""
Returns the user's ``person.display_name``, if present, otherwise the
``username``.
"""
if self.person and self.person.display_name:
return self.person.display_name
return self.username