def create(real_user, user, email, entity=None, project=None, organization=None, role=APP_ROLE_READ): class Form(formencode.Schema): email = formencode.All(fv.Email(not_empty=True), fv.MaxLength(256)) role = fv.OneOf(APP_ROLES, not_empty=True) validate(Form, role=role, email=email) obj = entity or project or organization try: invite = users.Invite.create(user, email, obj, role=role) commit() except AppException, e: if e.code == DUPLICATE: raise ClientException('%s has already been invited to %s' % (email, obj.name), DUPLICATE) raise ClientException(e.msg, e.code)
def create(real_user, user, organization, **params): """ Creates a project. """ class ProjectForm(formencode.Schema): name = formencode.All(fv.UnicodeString(not_empty=True), UniqueName(organization)) description = fv.UnicodeString(not_empty=False) scrubbed = validate(ProjectForm, **params) project = projects.Project(name=scrubbed.name, creator=user, description=scrubbed.description, organization=organization) Session.add(project) Session.flush() #email users = organization.interested_users if user in users: users.remove(user) email.send(users, 'create_project.txt', { 'project': project, 'creator': user }) return project
def create(**params): """ Creates a user. DO NOT EXPOSE THIS to the web api. Please. """ numusers = len(Session.query(users.User).all()) scrubbed = validate(RegisterForm, **params) logger.info(scrubbed) user = users.User() Session.add(user) user.email = scrubbed.email user.username = '******' in scrubbed and scrubbed.username or scrubbed.email user.password = scrubbed.password user.set_timezone_int(scrubbed.default_timezone) if scrubbed.get('name'): name = scrubbed.get('name').split(' ', 1) user.first_name = name[0].strip() user.last_name = len(name) == 2 and name[1].strip() or u'' else: user.first_name = scrubbed.get('first_name') user.last_name = scrubbed.get('last_name') #first user is an admin. if numusers == 0: user.role = users.ROLE_ADMIN return user
def attach_user(real_user, user, organization, u, role=users.APP_ROLE_READ, status=STATUS_PENDING): params = validate(RoleStatusForm, role=role, status=status) orgu = organization.get_user_connection(u, status=None) if orgu: raise ClientException('User already attached', INVALID) role = organization.get_role(user) if role == users.APP_ROLE_ADMIN: #special. We adhere to role and status vars orgu = organization.attach_user(u, role=params.role, status=params.status) else: # anyone can attempt to attach themselves, but they will be pending approval in # read group only orgu = organization.attach_user(u, role=users.APP_ROLE_READ, status=STATUS_PENDING) return orgu
def explode_no_auth(real_user, user, type=None, **kw): """ An action to test the error handling of the stack. The tests use this. """ if type == 'app': raise AppException('This is an app exception!', code=INVALID, field='type') elif type == 'client': raise ClientException('Oh Noes, ClientException!', code=INVALID, field='type') elif type == 'client404': raise ClientException('Oh Noes, ClientException NOT FOUND!', code=NOT_FOUND, field='type') elif type == 'client403': raise ClientException('Oh Noes, ClientException FORBIDDEN!', code=FORBIDDEN, field='type') elif type == 'explosion': 1 / 0 elif type == 'http': abort(404, 'This thing was not found!') elif type == 'validation': class Rawr(formencode.Schema): meow = fv.Number() scrubbed = validate(Rawr, meow='zzzz') return kw
def set_comment_completion_status(real_user, user, comment, status=None, **kw): """ Set completion status """ class StatusForm(formencode.Schema): status = fv.OneOf([STATUS_OPEN, STATUS_COMPLETED], not_empty=True) scrubbed = validate(StatusForm, status=status) return comment.set_completion_status(user, scrubbed.status)
def set_pref(real_user, user, key, value, use_real_user=True): class Pref(formencode.Schema): key = fv.MaxLength(64, not_empty=True) value = fv.MaxLength(64, not_empty=False) scrubbed = validate(Pref, key=key, value=value) u = user if use_real_user: u = real_user u.set_preference(scrubbed.key, scrubbed.value or '')
def attach_user(real_user, user, project, u, role=users.APP_ROLE_READ):#, status=STATUS_APPROVED): #maybe later params = validate(RoleStatusForm, role=role, status=STATUS_APPROVED) pu = project.get_user_connection(u, status=None) if pu: print pu raise ClientException('User already attached', INVALID) orgu = project.attach_user(u, role=params.role, status=params.status) return orgu
def create(real_user, user, email, entity=None, project=None, organization=None, role=APP_ROLE_READ): class Form(formencode.Schema): email = formencode.All(fv.Email(not_empty=True), fv.MaxLength(256)) role = fv.OneOf(APP_ROLES, not_empty=True) validate(Form, role=role, email=email) obj = entity or project or organization try: invite = users.Invite.create(user, email, obj, role=role) commit() except AppException, e: if e.code == DUPLICATE: raise ClientException( '%s has already been invited to %s' % (email, obj.name), DUPLICATE) raise ClientException(e.msg, e.code)
def create(real_user, user, **params): """ Creates an organization. Attaches it to User. """ scrubbed = validate(CreateForm, **params) scrubbed.setdefault('is_active', True) scrubbed['name'] = scrubbed['company_name'] del scrubbed['company_name'] #attach the user as a creator. org = users.Organization(creator=user, **scrubbed) Session.add(org) #connect user to org as admin of org org.attach_user(user, role=users.APP_ROLE_ADMIN, status=STATUS_APPROVED) Session.add(activity.NewOrganization(user, org)) Session.flush() return org
def explode_no_auth(real_user, user, type=None, **kw): """ An action to test the error handling of the stack. The tests use this. """ if type == 'app': raise AppException('This is an app exception!', code=INVALID, field='type') elif type == 'client': raise ClientException('Oh Noes, ClientException!', code=INVALID, field='type') elif type == 'client404': raise ClientException('Oh Noes, ClientException NOT FOUND!', code=NOT_FOUND, field='type') elif type == 'client403': raise ClientException('Oh Noes, ClientException FORBIDDEN!', code=FORBIDDEN, field='type') elif type == 'explosion': 1/0 elif type == 'http': abort(404, 'This thing was not found!') elif type == 'validation': class Rawr(formencode.Schema): meow = fv.Number() scrubbed = validate(Rawr, meow='zzzz') return kw
def is_unique(subdomain): validate(SubdomainTestForm, subdomain=subdomain) return {'is': True}