Beispiel #1
0
def display_edit_user_account(cwruid):
    """
    This view allows the user and administrators
    to edit the account information of that user
    """
    import urllib, urlparse

    permissions = check_permissions(cwruid)
    if not permissions[0] and not permissions[1]:
        return permission_denied(cwruid)

    # get the user object for this page
    try:
        user = find_users(1,cwruid=('=', cwruid))[0]
    except IndexError:
        return render_template('404.html'), 404

    main_form = forms.MainUpdateUserForm(None)

    # initialize admin form if this user has
    # admin privileges
    admin_form = None
    if permissions[1]:
        admin_form = forms.AdminUpdateUserForm(None)

        # set the choices
        admin_form.family.choices = get_family_choices()
        admin_form.roles.choices = get_role_choices()

    # populate the main form
    main_form.fname.data = user.fname
    main_form.mname.data = user.mname
    main_form.lname.data = user.lname
    main_form.avatar.data = user.avatar

    # initialize the admin_form if needed
    if admin_form is not None:
        if user.family is not None:
            admin_form.family.data = user.family.name
        if user.big is not None:
            admin_form.big.data = user.big.cwruid

        query = UserRoleModel.all()
        query.filter('user ='******'members/edit_account.html',
                           user=user,
                           permissions=permissions,
                           main_form=main_form,
                           admin_form=admin_form)
def display_edit_user_account(cwruid):
    """
    This view allows the user and administrators
    to edit the account information of that user
    """
    import urllib, urlparse

    permissions = check_permissions(cwruid)
    if not permissions[0] and not permissions[1]:
        return permission_denied(cwruid)

    # get the user object for this page
    try:
        user = find_users(1, cwruid=('=', cwruid))[0]
    except IndexError:
        return render_template('404.html'), 404

    main_form = forms.MainUpdateUserForm(None)

    # initialize admin form if this user has
    # admin privileges
    admin_form = None
    if permissions[1]:
        admin_form = forms.AdminUpdateUserForm(None)

        # set the choices
        admin_form.family.choices = get_family_choices()
        admin_form.roles.choices = get_role_choices()

    # populate the main form
    main_form.fname.data = user.fname
    main_form.mname.data = user.mname
    main_form.lname.data = user.lname
    main_form.avatar.data = user.avatar

    # initialize the admin_form if needed
    if admin_form is not None:
        if user.family is not None:
            admin_form.family.data = user.family.name
        if user.big is not None:
            admin_form.big.data = user.big.cwruid

        query = UserRoleModel.all()
        query.filter('user ='******'members/edit_account.html',
                           user=user,
                           permissions=permissions,
                           main_form=main_form,
                           admin_form=admin_form)
Beispiel #3
0
def handle_edit_account_admin_json(cwruid):
    """
    This view handles the AJAX request
    for the AdminUpdateUserForm submission
    from the display_edit_account(cwruid) view
    """
    
    permissions = check_permissions(cwruid)
    if not permissions[0] and not permissions[1]:
        return jsonify({'result':'failure', 'msg':'Permission denied'})

    admin_form = forms.AdminUpdateUserForm()

    # set the choices
    admin_form.family.choices = get_family_choices()
    admin_form.roles.choices = get_role_choices()

    if admin_form.validate():
        try:
            user = find_users(1, cwruid=('=', cwruid))[0]
        except IndexError:
            return jsonify({'result':'failure: no such user', 'name':'admin', 'errors': {}})

        if admin_form.big.data != '':
            try:
                big = find_users(1, cwruid=('=', admin_form.big.data))[0]
                user.big = big.key()
            except IndexError:
                user.big = None
                return jsonify({'result':'failure: no such big', 'name':'admin', 'errors': {}})
        else:
            user.big = None


        if admin_form.family.data != 'none':
            query = models.FamilyModel.all()
            query.filter('name =', admin_form.family.data)
            try:
                family = query.fetch(query.count())[0]
                user.family = family.key()
            except IndexError:
                user.family = None
                return jsonify({'result':'failure: no such family', 'name':'admin', 'errors': {}})
        else:
            user.family = None


        query = UserRoleModel.all()
        query.filter('user ='******'name =', role)
                try:
                    new_role = role_query.fetch(query.count())[0]
                except IndexError:
                    return jsonify({'result':'failure: no such role', 'name':'admin', 'errors': {}})
                new_urole = UserRoleModel(user=user.key(),
                                          role=new_role.key())
                new_urole.put()
            else:
                del uroles[index]
        for urole in uroles:
            urole.delete()

        user.save()
        
        return jsonify({'result':'success'})
    else:
        return jsonify({'result':'failure', 'name':'admin', 'errors': admin_form.errors})
Beispiel #4
0
def create_user():
    """
    View for creating a user
    """

    from application.generate_keys import generate_randomkey
    
    form = forms.CreateUserForm(request.form)

    form.family.choices = get_family_choices()
    
    form.roles.choices = get_role_choices()

    if request.method == 'POST':
        if form.validate():
            # create the user with information specified in form
            fname = form.fname.data
            lname = form.lname.data
            cwruid = form.cwruid.data

            # generate a new temporary password
            password = generate_randomkey(16)

            # get optional attributes
            optional_attr = {}
            if form.mname.data != '':
                optional_attr['mname'] = form.mname.data
                
            if form.family.data != 'none':
                # look up family instance
                query = models.FamilyModel.all()
                query.filter('name =', form.family.data)
                families = query.fetch(1)
                if len(families) != 1:
                    form.family.errors.append(u'Family %s does not exist' % form.family.data)
                    return render_template('members/create.html',
                                           create_user_form=form)
                optional_attr['family'] = families[0].key()
                
            if form.big.data != '':
                # look up big instance
                users = find_users(cwruid=('=', form.big.data))
                if len(users) != 1:
                    form.big.errors.append(u'User %s does not exist' % form.big.data)
                    return render_template('members/create.html',
                                           create_user_form=form)
                optional_attr['big'] = users[0].key()
                
            if form.avatar.data != '':
                optional_attr['avatar'] = form.avatar.data
            
            try:
                new_user = accounts.create_user(fname, lname, cwruid, password, **optional_attr)
                if new_user is None:
                    raise AttributeError('Something went wrong with user creation')

                # add the case email address to the user
                email = models.EmailModel(user=new_user.key(),
                                          email='*****@*****.**' % new_user.cwruid,
                                          name='Case Email')
                email.put()

                # add the roles to the user
                for role in form.roles.data:
                    query = RoleModel.all()
                    query.filter('name =', role)

                    if query.count() != 1:
                        flash('Role %s does not exist' % role, 'error')
                        continue

                    desired_role = query.fetch(1)[0]

                    new_urole = UserRoleModel(user=new_user.key(), role=desired_role.key())
                    new_urole.put()
                    
                flash('User created successfully', 'success')

                form = None
                form = forms.CreateUserForm()
                form.family.choices = get_family_choices()
                form.roles.choices = get_role_choices()

                send_new_user_mail(fname, lname, cwruid, password)
            except AttributeError, e:
                flash(str(e), 'error')
def handle_edit_account_admin_json(cwruid):
    """
    This view handles the AJAX request
    for the AdminUpdateUserForm submission
    from the display_edit_account(cwruid) view
    """

    permissions = check_permissions(cwruid)
    if not permissions[0] and not permissions[1]:
        return jsonify({'result': 'failure', 'msg': 'Permission denied'})

    admin_form = forms.AdminUpdateUserForm()

    # set the choices
    admin_form.family.choices = get_family_choices()
    admin_form.roles.choices = get_role_choices()

    if admin_form.validate():
        try:
            user = find_users(1, cwruid=('=', cwruid))[0]
        except IndexError:
            return jsonify({
                'result': 'failure: no such user',
                'name': 'admin',
                'errors': {}
            })

        if admin_form.big.data != '':
            try:
                big = find_users(1, cwruid=('=', admin_form.big.data))[0]
                user.big = big.key()
            except IndexError:
                user.big = None
                return jsonify({
                    'result': 'failure: no such big',
                    'name': 'admin',
                    'errors': {}
                })
        else:
            user.big = None

        if admin_form.family.data != 'none':
            query = models.FamilyModel.all()
            query.filter('name =', admin_form.family.data)
            try:
                family = query.fetch(query.count())[0]
                user.family = family.key()
            except IndexError:
                user.family = None
                return jsonify({
                    'result': 'failure: no such family',
                    'name': 'admin',
                    'errors': {}
                })
        else:
            user.family = None

        query = UserRoleModel.all()
        query.filter('user ='******'name =', role)
                try:
                    new_role = role_query.fetch(query.count())[0]
                except IndexError:
                    return jsonify({
                        'result': 'failure: no such role',
                        'name': 'admin',
                        'errors': {}
                    })
                new_urole = UserRoleModel(user=user.key(), role=new_role.key())
                new_urole.put()
            else:
                del uroles[index]
        for urole in uroles:
            urole.delete()

        user.save()

        return jsonify({'result': 'success'})
    else:
        return jsonify({
            'result': 'failure',
            'name': 'admin',
            'errors': admin_form.errors
        })
def create_user():
    """
    View for creating a user
    """

    from application.generate_keys import generate_randomkey

    form = forms.CreateUserForm(request.form)

    form.family.choices = get_family_choices()

    form.roles.choices = get_role_choices()

    if request.method == 'POST':
        if form.validate():
            # create the user with information specified in form
            fname = form.fname.data
            lname = form.lname.data
            cwruid = form.cwruid.data

            # generate a new temporary password
            password = generate_randomkey(16)

            # get optional attributes
            optional_attr = {}
            if form.mname.data != '':
                optional_attr['mname'] = form.mname.data

            if form.family.data != 'none':
                # look up family instance
                query = models.FamilyModel.all()
                query.filter('name =', form.family.data)
                families = query.fetch(1)
                if len(families) != 1:
                    form.family.errors.append(u'Family %s does not exist' %
                                              form.family.data)
                    return render_template('members/create.html',
                                           create_user_form=form)
                optional_attr['family'] = families[0].key()

            if form.big.data != '':
                # look up big instance
                users = find_users(cwruid=('=', form.big.data))
                if len(users) != 1:
                    form.big.errors.append(u'User %s does not exist' %
                                           form.big.data)
                    return render_template('members/create.html',
                                           create_user_form=form)
                optional_attr['big'] = users[0].key()

            if form.avatar.data != '':
                optional_attr['avatar'] = form.avatar.data

            try:
                new_user = accounts.create_user(fname, lname, cwruid, password,
                                                **optional_attr)
                if new_user is None:
                    raise AttributeError(
                        'Something went wrong with user creation')

                # add the case email address to the user
                email = models.EmailModel(user=new_user.key(),
                                          email='*****@*****.**' %
                                          new_user.cwruid,
                                          name='Case Email')
                email.put()

                # add the roles to the user
                for role in form.roles.data:
                    query = RoleModel.all()
                    query.filter('name =', role)

                    if query.count() != 1:
                        flash('Role %s does not exist' % role, 'error')
                        continue

                    desired_role = query.fetch(1)[0]

                    new_urole = UserRoleModel(user=new_user.key(),
                                              role=desired_role.key())
                    new_urole.put()

                flash('User created successfully', 'success')

                form = None
                form = forms.CreateUserForm()
                form.family.choices = get_family_choices()
                form.roles.choices = get_role_choices()

                send_new_user_mail(fname, lname, cwruid, password)
            except AttributeError, e:
                flash(str(e), 'error')