Not very thoroughly tested. Attempt to trade off memory for number of selects (?)

This commit is contained in:
Ricky Zhou (周家杰) 2008-03-10 14:18:08 -04:00
parent 3f94ff13d7
commit dbdf0325ab
2 changed files with 27 additions and 25 deletions

View file

@ -15,14 +15,13 @@ def isAdmin(person):
'''
admingroup = config.get('admingroup')
try:
group = Groups.by_name(admingroup)
except InvalidRequestError:
if person.group_roles[admingroup].role_status == 'approved':
return True
else:
return False
except KeyError:
print '%s - Your admin group could not be found!' % admingroup
return False
if group in person.approved_memberships:
return True
else:
return False
def canAdminGroup(person, group):
'''
@ -74,29 +73,26 @@ def signedCLAPrivs(person):
'''
Returns True if the user has completed the GPG-signed CLA
'''
cla_sign_group =config.get('cla_sign_group')
try:
cla_sign_group = Groups.by_name(config.get('cla_sign_group'))
except InvalidRequestError:
turbogears.flash(_("cla_sign_group Does not exist! Please create it!"))
return False
if isApproved(person, cla_sign_group):
return True
else:
if person.group_roles[cla_sign_group].role_status == 'approved':
return True
else:
return False
except KeyError:
return False
def clickedCLAPrivs(person):
'''
Returns True if the user has completed the click-through CLA
'''
cla_click_group = config.get('cla_click_group')
try:
cla_click_group = Groups.by_name(config.get('cla_click_group'))
except InvalidRequestError:
turbogears.flash(_("cla_click_group Does not exist! Please create it!"))
return False
if signedCLAPrivs(person) or \
isApproved(person, cla_click_group):
return True
else:
if person.group_roles[cla_click_group].role_status == 'approved':
return True
else:
return False
except KeyError:
return False
def canEditUser(person, target):

View file

@ -34,7 +34,7 @@ from sqlalchemy.orm import relation
from sqlalchemy import String, Unicode, Integer, DateTime
# A few sqlalchemy tricks:
# Allow viewing foreign key relations as a dictionary
from sqlalchemy.orm.collections import column_mapped_collection
from sqlalchemy.orm.collections import column_mapped_collection, attribute_mapped_collection
# Allow us to reference the remote table of a many:many as a simple list
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy import select, and_
@ -294,6 +294,7 @@ class PersonRoles(SABase):
'''Record people that are members of groups.'''
def __repr__(cls):
return "PersonRole(%s,%s,%s,%s)" % (cls.member.username, cls.group.name, cls.role_type, cls.role_status)
groupname = association_proxy('group', 'name')
class Configs(SABase):
'''Configs for applications that a Fedora Contributor uses.'''
@ -412,10 +413,10 @@ class VisitIdentity(SABase):
# mappers for filtering roles
#
mapper(ApprovedRoles, ApprovedRolesSelect, properties = {
'group': relation(Groups, backref='approved_roles')
'group': relation(Groups, backref='approved_roles', lazy = False)
})
mapper(UnApprovedRoles, UnApprovedRolesSelect, properties = {
'group': relation(Groups, backref='unapproved_roles')
'group': relation(Groups, backref='unapproved_roles', lazy = False)
})
mapper(People, PeopleTable, properties = {
@ -425,6 +426,10 @@ mapper(People, PeopleTable, properties = {
'person_emails': relation(PersonEmails, backref = 'person',
collection_class = column_mapped_collection(
PersonEmailsTable.c.email)),
# This name is kind of confusing. It's to allow person.group_roles['groupname'] in order to make auth.py (hopefully) slightly faster.
'group_roles': relation(PersonRoles,
collection_class = attribute_mapped_collection('groupname'),
primaryjoin = PeopleTable.c.id==PersonRolesTable.c.person_id),
'approved_roles': relation(ApprovedRoles, backref='member',
primaryjoin = PeopleTable.c.id==ApprovedRoles.c.person_id),
'unapproved_roles': relation(UnApprovedRoles, backref='member',
@ -438,7 +443,8 @@ mapper(EmailPurposes, EmailPurposesTable, properties = {
mapper(PersonRoles, PersonRolesTable, properties = {
'member': relation(People, backref = 'roles', lazy = False,
primaryjoin=PersonRolesTable.c.person_id==PeopleTable.c.id),
'group': relation(Groups, backref='roles'),
'group': relation(Groups, backref='roles', lazy = False,
primaryjoin=PersonRolesTable.c.group_id==GroupsTable.c.id),
'sponsor': relation(People, uselist=False,
primaryjoin = PersonRolesTable.c.sponsor_id==PeopleTable.c.id)
})