Add validation, still needs to actually show friendly error messages.

This commit is contained in:
Ricky Zhou (周家杰) 2007-08-16 18:21:30 -07:00
parent a1283c3a1c
commit 33d398f074
2 changed files with 83 additions and 85 deletions

View file

@ -12,17 +12,10 @@ from fas.auth import isAdmin, canAdminGroup, canSponsorGroup, canEditUser
from operator import itemgetter
from fas.user import knownUser
from fas.user import knownUser, userNameExists
class knownGroup(validators.FancyValidator):
def _to_python(self, value, state):
return value.strip()
def validate_python(self, value, state):
g = Groups.groups(groupName)
if g:
raise validators.Invalid(_("The group '%s' already exists") % value, value, state)
class unknownGroup(validators.FancyValidator):
'''Make sure that a group already exists'''
def _to_python(self, value, state):
return value.strip()
def validate_python(self, value, state):
@ -30,32 +23,38 @@ class unknownGroup(validators.FancyValidator):
if not g:
raise validators.Invalid(_("The group '%s' does not exist") % value, value, state)
class createGroup(widgets.WidgetsList):
groupName = widgets.TextField(label=_('Group Name'), validator=validators.All(knownGroup(not_empty=True, max=10), validators.String(max=32, min=3)))
fedoraGroupDesc = widgets.TextField(label=_('Description'), validator=validators.NotEmpty)
fedoraGroupOwner = widgets.TextField(label=_('Group Owner'), validator=validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3)))
fedoraGroupNeedsSponsor = widgets.CheckBox(label=_('Needs Sponsor'))
fedoraGroupUserCanRemove = widgets.CheckBox(label=_('Self Removal'))
fedoraGroupJoinMsg = widgets.TextField(label=_('Group Join Message'))
class unknownGroup(validators.FancyValidator):
'''Make sure that a group doesn't already exist'''
def _to_python(self, value, state):
return value.strip()
def validate_python(self, value, state):
g = Groups.groups(groupName)
if g:
raise validators.Invalid(_("The group '%s' already exists") % value, value, state)
createGroupForm = widgets.ListForm(fields=createGroup(), submit_text=_('Create'))
class createGroup(validators.Schema):
groupName = validators.All(unknownGroup(not_empty=True, max=10), validators.String(max=32, min=3))
fedoraGroupDesc = validators.NotEmpty
fedoraGroupOwner = validator=validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3))
class editGroup(widgets.WidgetsList):
groupName = widgets.HiddenField(validator=validators.All(unknownGroup(not_empty=True, max=10), validators.String(max=32, min=3)))
fedoraGroupDesc = widgets.TextField(label=_('Description'), validator=validators.NotEmpty)
fedoraGroupOwner = widgets.TextField(label=_('Group Owner'), validator=validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3)))
fedoraGroupNeedsSponsor = widgets.CheckBox(label=_('Needs Sponsor'))
fedoraGroupUserCanRemove = widgets.CheckBox(label=_('Self Removal'))
fedoraGroupJoinMsg = widgets.TextField(label=_('Group Join Message'))
class editGroup(validators.Schema):
groupName = validators.All(knownGroup(not_empty=True, max=10), validators.String(max=32, min=3))
fedoraGroupDesc = validators.NotEmpty
fedoraGroupOwner = validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3))
editGroupForm = widgets.ListForm(fields=editGroup(), submit_text=_('Update'))
class userNameGroupNameExists(validators.Schema):
groupName = validators.All(knownGroup(not_empty=True, max=10), validators.String(max=32, min=3))
userName = validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3))
class findUser(widgets.WidgetsList):
userName = widgets.AutoCompleteField(label=_('Username'), search_controller='search', search_param='userName', result_name='people')
action = widgets.HiddenField(default='apply', validator=validators.String(not_empty=True))
groupName = widgets.HiddenField(validator=validators.String(not_empty=True))
class groupNameExists(validators.Schema):
groupName = validators.All(knownGroup(not_empty=True, max=10), validators.String(max=32, min=3))
findUserForm = widgets.ListForm(fields=findUser(), submit_text=_('Invite'))
#class findUser(widgets.WidgetsList):
# userName = widgets.AutoCompleteField(label=_('Username'), search_controller='search', search_param='userName', result_name='people')
# action = widgets.HiddenField(default='apply', validator=validators.String(not_empty=True))
# groupName = widgets.HiddenField(validator=validators.String(not_empty=True))
#
#findUserForm = widgets.ListForm(fields=findUser(), submit_text=_('Invite'))
class Group(controllers.Controller):
@ -66,25 +65,19 @@ class Group(controllers.Controller):
'''Perhaps show a nice explanatory message about groups here?'''
return dict()
@validate(validators=groupNameExists())
@expose(template="fas.templates.group.view")
@identity.require(turbogears.identity.not_anonymous())
def view(self, groupName):
'''View group'''
# FIXME: Cleaner checks
try:
groups = Groups.byGroupName(groupName, includeUnapproved=True)
except KeyError:
raise ValueError, _('Group: %s - Does not exist!') % groupName
try:
group = Groups.groups(groupName)[groupName]
except TypeError:
raise ValueError, _('Group: %s - Does not exist!') % groupName
groups = Groups.byGroupName(groupName, includeUnapproved=True)
group = Groups.groups(groupName)[groupName]
userName = turbogears.identity.current.user_name
try:
myStatus = groups[userName].fedoraRoleStatus
except KeyError:
# Not in group
myStatus = 'Not a Member' # This has say 'Not a Member'
myStatus = 'Not a Member' # This _has_ to stay 'Not a Member'
except TypeError:
groups = {}
try:
@ -93,7 +86,7 @@ class Group(controllers.Controller):
me = UserGroup()
#searchUserForm.groupName.display('group')
#findUser.groupName.display(value='fff')
value = {'groupName': group.cn}
value = {'groupName': groupName}
groups = sorted(groups.items(), key=itemgetter(0))
return dict(userName=userName, groups=groups, group=group, me=me, value=value)
@ -103,7 +96,7 @@ class Group(controllers.Controller):
'''Create a group'''
return dict()
#@validate(form=createGroupForm)
@validate(validators=createGroup())
@expose(template="fas.templates.group.new")
@identity.require(turbogears.identity.not_anonymous())
def create(self, groupName, fedoraGroupDesc, fedoraGroupOwner, fedoraGroupNeedsSponsor=True, fedoraGroupUserCanRemove=True, fedoraGroupJoinMsg=""):
@ -141,11 +134,11 @@ class Group(controllers.Controller):
#'fedoraGroupRequires': group.fedoraGroupRequires, }
return dict(value=value)
#@validate(form=editGroupForm)
@validate(validators=editGroup())
@expose(template="fas.templates.group.edit")
@identity.require(turbogears.identity.not_anonymous())
def save(self, stuff):
#TODO
#TODO: Implement this :)
return dict()
@expose(template="fas.templates.group.list")
@ -163,6 +156,7 @@ class Group(controllers.Controller):
return dict(groups=groups, search=search, myGroups=myGroups)
# TODO: Validate
@validate(validators=userNameGroupNameExists())
@expose(template='fas.templates.group.view')
@identity.require(turbogears.identity.not_anonymous())
def apply(self, groupName, userName):
@ -175,7 +169,7 @@ class Group(controllers.Controller):
turbogears.flash(_('%(user)s has applied to %(group)s!') % {'user': userName, 'group': groupName})
turbogears.redirect('/group/view/%s' % group.cn)
# TODO: Validate (user doesn't exist case)
@validate(validators=userNameGroupNameExists())
@expose(template='fas.templates.group.view')
@identity.require(turbogears.identity.not_anonymous())
def sponsor(self, groupName, userName):
@ -197,7 +191,7 @@ class Group(controllers.Controller):
turbogears.flash(_('%s has been sponsored!') % p.cn)
turbogears.redirect('/group/view/%s' % groupName)
# TODO: Validate (user doesn't exist case)
@validate(validators=userNameGroupNameExists())
@expose(template='fas.templates.group.view')
@identity.require(turbogears.identity.not_anonymous())
def remove(self, groupName, userName):
@ -222,7 +216,7 @@ class Group(controllers.Controller):
turbogears.redirect('/group/view/%s' % groupName)
return dict()
# TODO: Validate (user doesn't exist case)
@validate(validators=userNameGroupNameExists())
@expose(template='fas.templates.group.view')
@identity.require(turbogears.identity.not_anonymous())
def upgrade(self, groupName, userName):
@ -248,7 +242,7 @@ class Group(controllers.Controller):
turbogears.flash(_('%s has been upgraded!') % userName)
turbogears.redirect('/group/view/%s' % groupName)
# TODO: Validate (user doesn't exist case)
@validate(validators=userNameGroupNameExists())
@expose(template='fas.templates.group.view')
@identity.require(turbogears.identity.not_anonymous())
def downgrade(self, groupName, userName):
@ -270,7 +264,7 @@ class Group(controllers.Controller):
turbogears.flash(_('%s has been downgraded!') % p.cn)
turbogears.redirect('/group/view/%s' % groupName)
# TODO: Validate (group doesn't exist case)
@validate(validators=groupNameExists())
@expose(template="genshi-text:fas.templates.group.dump", content_type='text/plain; charset=utf-8')
@identity.require(turbogears.identity.not_anonymous())
def dump(self, groupName=None):

View file

@ -13,14 +13,7 @@ from fas.auth import isAdmin, canAdminGroup, canSponsorGroup, canEditUser
from operator import itemgetter
class knownUser(validators.FancyValidator):
def _to_python(self, value, state):
return value.strip()
def validate_python(self, value, state):
p = Person.byUserName(value)
if p.cn:
raise validators.Invalid(_("'%s' already exists") % value, value, state)
class unknownUser(validators.FancyValidator):
'''Make sure that a user already exists'''
def _to_python(self, value, state):
return value.strip()
def validate_python(self, value, state):
@ -28,29 +21,41 @@ class unknownUser(validators.FancyValidator):
if not p.cn:
raise validators.Invalid(_("'%s' does not exist") % value, value, state)
class editUser(widgets.WidgetsList):
# cn = widgets.TextField(label='Username', validator=validators.PlainText(not_empty=True, max=10))
userName = widgets.HiddenField(validator=validators.All(unknownUser(not_empty=True, max=10), validators.String(max=32, min=3)))
givenName = widgets.TextField(label=_('Full Name'), validator=validators.String(not_empty=True, max=42))
mail = widgets.TextField(label=_('Email'), validator=validators.Email(not_empty=True, strip=True))
fedoraPersonBugzillaMail = widgets.TextField(label=_('Bugzilla Email'), validator=validators.Email(not_empty=True, strip=True))
fedoraPersonIrcNick = widgets.TextField(label=_('IRC Nick'))
fedoraPersonKeyId = widgets.TextField(label=_('PGP Key'))
telephoneNumber = widgets.TextField(label=_('Telephone Number'), validator=validators.PhoneNumber(not_empty=True))
postalAddress = widgets.TextArea(label=_('Postal Address'), validator=validators.NotEmpty)
description = widgets.TextArea(label=_('Description'))
class unknownUser(validators.FancyValidator):
'''Make sure that a user doesn't already exist'''
def _to_python(self, value, state):
return value.strip()
def validate_python(self, value, state):
p = Person.byUserName(value)
if p.cn:
raise validators.Invalid(_("'%s' already exists") % value, value, state)
class editUser(validators.Schema):
userName = validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3))
givenName = validators.String(not_empty=True, max=42)
mail = validators.Email(not_empty=True, strip=True)
fedoraPersonBugzillaMail = validators.Email(not_empty=True, strip=True)
#fedoraPersonKeyId- Save this one for later :)
telephoneNumber = validators.PhoneNumber(not_empty=True)
postalAddress = validators.NotEmpty
editUserForm = widgets.ListForm(fields=editUser(), submit_text=_('Update'))
class newUser(validators.Schema):
cn = validators.All(unknownUser(not_empty=True, max=10), validators.String(max=32, min=3))
givenName = validators.String(not_empty=True, max=42)
mail = validators.Email(not_empty=True, strip=True)
fedoraPersonBugzillaMail = validators.Email(not_empty=True, strip=True)
telephoneNumber = validators.PhoneNumber(not_empty=True)
postalAddress = validators.NotEmpty
class newUser(widgets.WidgetsList):
#cn = widgets.TextField(label='Username', validator=validators.PlainText(not_empty=True, max=10))
cn = widgets.TextField(label=_('Username'), validator=validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3)))
givenName = widgets.TextField(label=_('Full Name'), validator=validators.String(not_empty=True, max=42))
mail = widgets.TextField(label=_('Email'), validator=validators.Email(not_empty=True, strip=True))
telephoneNumber = widgets.TextField(label=_('Telephone Number'), validator=validators.PhoneNumber(not_empty=True))
postalAddress = widgets.TextArea(label=_('Postal Address'), validator=validators.NotEmpty)
class changePass(validators.Schema):
currentPassword = validators.String()
# TODO (after we're done with most testing): Add complexity requirements?
password = validators.String(min=8)
passwordCheck = validators.String()
chained_validators = [validators.FieldsMatch('password', 'passwordCheck')]
newUserForm = widgets.ListForm(fields=newUser(), submit_text=_('Sign Up'))
class userNameExists(validators.Schema):
userName = validators.All(knownUser(not_empty=True, max=10), validators.String(max=32, min=3))
class User(controllers.Controller):
@ -63,12 +68,13 @@ class User(controllers.Controller):
'''
turbogears.redirect('view/%s' % turbogears.identity.current.user_name)
@validate(validators=userNameExists())
@error_handler(error)
@expose(template="fas.templates.user.view")
@identity.require(turbogears.identity.not_anonymous())
def view(self, userName=None):
'''View a User.
'''
# TODO: Validate- check if user actually exists
if not userName:
userName = turbogears.identity.current.user_name
if turbogears.identity.current.user_name == userName:
@ -118,7 +124,7 @@ class User(controllers.Controller):
'description': user.description, }
return dict(value=value)
#@validate(form=editUserForm)
@validate(validators=editUser())
@expose(template='fas.templates.editAccount')
def save(self, userName, givenName, mail, fedoraPersonBugzillaMail, telephoneNumber, postalAddress, fedoraPersonIrcNick='', fedoraPersonKeyId='', description=''):
if not canEditUser(turbogears.identity.current.user_name, userName):
@ -164,9 +170,9 @@ class User(controllers.Controller):
if turbogears.identity.not_anonymous():
turbogears.flash(_('No need to sign up, You have an account!'))
turbogears.redirect('/user/view/%s' % turbogears.identity.current.user_name)
return dict(form=newUserForm)
return dict()
@validate(form=newUserForm) ## TODO: Use validate everywhere
@validate(validators=newUser())
@expose(template='fas.templates.new')
def create(self, cn, givenName, mail, telephoneNumber, postalAddress):
# TODO: Ensure that e-mails are unique?
@ -198,11 +204,10 @@ class User(controllers.Controller):
def changepass(self):
return dict()
#TODO: Validate
@validate(validators=changePass())
@expose(template="fas.templates.user.changepass")
@identity.require(turbogears.identity.not_anonymous())
def setpass(self, currentPassword, password, passwordCheck):
# TODO: use @validate/check password length
userName = turbogears.identity.current.user_name
try:
Person.auth(userName, currentPassword)
@ -225,15 +230,14 @@ class User(controllers.Controller):
turbogears.redirect('/user/view/%s' % turbogears.identity.current.user_name)
return dict()
# TODO: Validate
@expose(template="fas.templates.user.resetpass")
def sendpass(self, userName, mail):
# TODO: Do validation and check the password is long enough
import turbomail
# Logged in
if turbogears.identity.current.user_name:
turbogears.flash(_("You are already logged in."))
turbogears.redirect('/user/view/%s', turbogears.identity.current.user_name)
return dict()
p = Person.byUserName(userName)
if userName and mail:
if not mail == p.mail: