def get_roles(): """ Returns list of RoleModel Entities from the db """ query = RoleModel.all() count = query.count() return query.fetch(count)
def setup(): """ This view will check the datastore for a SetupModel entity with the same version id as this instance. If the entity exists it will redirect the user to the homepage. Otherwise it will create some default data. WARNING: This is simply a convenience method. It will also clear out all data for all versions!! It will need to be changed in subsequent versions """ query = SetupModel.all() query.filter("version =", os.environ["CURRENT_VERSION_ID"]) if query.count() == 0: # the app hasn't been setup yet # db.delete(db.Entry.all(keys_only=True)) boehms = FamilyModel(name="boehms") boehms.put() snm = FamilyModel(name="s & m") snm.put() newpham = FamilyModel(name="new pham") newpham.put() default_users = [] default_users.append( create_user("Devin", "Schwab", "dts34", "default", family=boehms.key(), avatar="*****@*****.**") ) default_users.append(create_user("Jon", "Chan", "jtc77", "default")) default_users.append(create_user("Zach", "Zanes", "zzz111", "password")) default_users.append(create_user("Zach", "Zaney", "zzz222", "password1@")) default_users.append(create_user("Adam", "Min", "admin", "password")) webmaster_role = RoleModel(name="webmaster", desc="administrator for the website") webmaster_role.put() brother_role = RoleModel(name="brother", desc="general brother in the chapter") brother_role.put() pledge_role = RoleModel(name="pledge", desc="pledge in the chapter") pledge_role.put() neophyte_role = RoleModel(name="neophyte", desc="neophyte in the chapter") neophyte_role.put() # default_users = find_users() urole1 = UserRoleModel(user=default_users[0].key(), role=webmaster_role.key()) urole2 = UserRoleModel(user=default_users[0].key(), role=brother_role.key()) urole3 = UserRoleModel(user=default_users[1].key(), role=webmaster_role.key()) urole4 = UserRoleModel(user=default_users[1].key(), role=webmaster_role.key()) urole5 = UserRoleModel(user=default_users[2].key(), role=brother_role.key()) urole6 = UserRoleModel(user=default_users[4].key(), role=webmaster_role.key()) urole1.put() urole2.put() urole3.put() urole4.put() urole5.put() urole6.put() version = SetupModel(version=os.environ["CURRENT_VERSION_ID"]) version.put() flash("Setup the application!", "success") else: flash("Application is already setup", "error") return redirect("/")
def setup(): """ This view will check the datastore for a SetupModel entity with the same version id as this instance. If the entity exists it will redirect the user to the homepage. Otherwise it will create some default data. WARNING: This is simply a convenience method. It will also clear out all data for all versions!! It will need to be changed in subsequent versions """ query = SetupModel.all() query.filter('version =', os.environ['CURRENT_VERSION_ID']) if query.count() == 0: # the app hasn't been setup yet #db.delete(db.Entry.all(keys_only=True)) boehms = FamilyModel(name='boehms') boehms.put() snm = FamilyModel(name='s & m') snm.put() newpham = FamilyModel(name='new pham') newpham.put() default_users = [] default_users.append( create_user('Devin', 'Schwab', 'dts34', 'default', family=boehms.key(), avatar='*****@*****.**')) default_users.append(create_user('Jon', 'Chan', 'jtc77', 'default')) default_users.append(create_user('Zach', 'Zanes', 'zzz111', 'password')) default_users.append( create_user('Zach', 'Zaney', 'zzz222', 'password1@')) default_users.append(create_user('Adam', 'Min', 'admin', 'password')) webmaster_role = RoleModel(name='webmaster', desc='administrator for the website') webmaster_role.put() brother_role = RoleModel(name='brother', desc='general brother in the chapter') brother_role.put() pledge_role = RoleModel(name='pledge', desc='pledge in the chapter') pledge_role.put() neophyte_role = RoleModel(name='neophyte', desc='neophyte in the chapter') neophyte_role.put() #default_users = find_users() urole1 = UserRoleModel(user=default_users[0].key(), role=webmaster_role.key()) urole2 = UserRoleModel(user=default_users[0].key(), role=brother_role.key()) urole3 = UserRoleModel(user=default_users[1].key(), role=webmaster_role.key()) urole4 = UserRoleModel(user=default_users[1].key(), role=webmaster_role.key()) urole5 = UserRoleModel(user=default_users[2].key(), role=brother_role.key()) urole6 = UserRoleModel(user=default_users[4].key(), role=webmaster_role.key()) urole1.put() urole2.put() urole3.put() urole4.put() urole5.put() urole6.put() version = SetupModel(version=os.environ['CURRENT_VERSION_ID']) version.put() flash('Setup the application!', 'success') else: flash('Application is already setup', 'error') return redirect('/')
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')
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')