def companyView(): kwargs = {'title': 'Company information', 'formWidth': '350'} form = companyForm() if getTenant(): tenant = getTenant() if getContactPerson(): if g.sijax.is_sijax_request: g.sijax.register_object(SijaxHandler) return g.sijax.process_request() contact = getContactPerson() form = companyForm(regNo=tenant[u'regNo'], companyName=tenant[u'name'], addr=tenant[u'addr'], addr2=tenant[u'addr2'], postcode=tenant[u'postcode'], city=tenant[u'city'], contactName=str(contact['uuid']), phone=contact['phone'], email=contact['email']) users = [(str(r['uuid']), str(r['name'] + ' - ' + r['email'])) for r in getUsers()['users']] form.contactName.choices = users return render_template('settings/companyView.html', form=form, **kwargs)
def usersTable(): users = getUsers(includes=['includeRoles', 'includeGroups'])['users'] tableData = [] for u in users: roles = '' for r in u['roles']: roles = roles + str(r['title']) +'<br>' groups = '' for gr in u['groups']: groups = groups + str(gr['name']) +'<br>' temp = [u['uuid'],u['name'],u['email'], roles, groups] tableData.append(temp) return tableData
def usersTable(): users = getUsers() if not 'error' in users: users = users['users'] tableData = [] for u in users: roles = '' for r in u['roles']: roles = roles + str(r['title']) + '<br>' groups = '' for gr in u['groups']: groups = groups + str(gr['name']) + '<br>' temp = [u['uuid'], u['name'], u['email'], roles, groups] tableData.append(temp) return tableData else: return users['error']
def companyView(): kwargs = {'title': 'Company information', 'formWidth': '350'} compForm = companyForm() if g.sijax.is_sijax_request: g.sijax.register_object(SijaxHandler) return g.sijax.process_request() tenant = getCurrentTenant() kwargs['tenant'] = tenant contact = getContactPerson() if 'error' in contact: contact = { 'uuid': '', 'contactName': None, 'email': None, 'phone': None } errorMessage('Please assign contact person') else: contact = contact['success'] kwargs['contact'] = contact compForm = companyForm(regNo=tenant[u'regNo'], companyName=tenant[u'name'], addr=tenant[u'addr'], addr2=tenant[u'addr2'], postcode=tenant[u'postcode'], city=tenant[u'city']) contForm = contactForm(contactName=contact['uuid'], email=contact['email'], phone=contact['phone']) users = [(str(r['uuid']), str(r['name'] + ' - ' + r['email'])) for r in getUsers()['users']] users.insert(0, ('', '')) contForm.contactName.choices = users return render_template('settings/newCompanyView.html', contactForm=contForm, companyForm=compForm, **kwargs)
def userView(uuid=None, function=None): # universal variables form = userForm() kwargs = {'contentTitle': 'Users', 'width': '', 'formWidth': '400'} # Get users if function == None: kwargs['tableColumns'] = ['User name', 'Email', 'Roles', 'Groups'] kwargs['tableData'] = usersTable() return render_template('listView.html', **kwargs) elif function == 'delete': delUsr = deleteUser(uuid) if 'error' in delUsr: errorMessage(delUsr['error']) print delUsr return redirect(url_for('userBP.userView')) else: if function == 'update': usr = getUser(uuid=uuid, includes=['includeRoles', 'includeGroups'])['user'] kwargs['contentTitle'] = 'Update user' role = 'User' for r in usr['roles']: if r['title'] == 'Administrator': role = 'Administrator' elif r['title'] == 'Superuser': role = 'Superuser' grpForm = groupForm() usrForm = userForm( userName=usr['name'], userEmail=usr['email'], userPhone=usr['phone'], userGroups=[str(r['uuid']) for r in usr['groups']], userRole=role) # Get all groups usrForm.userGroups.choices = [(str(r['uuid']), r['name']) for r in getGroups()['groups']] if g.sijax.is_sijax_request: g.sijax.register_object(SijaxHandler) return g.sijax.process_request() if usrForm.validate_on_submit(): dataDict = { 'name': usrForm.userName.data, 'email': usrForm.userEmail.data, 'phone': usrForm.userPhone.data, 'roles': [usrForm.userRole.data], 'groups': [usrForm.userGroups.data] } updateUser = putUser(dataDict=dataDict, uuid=uuid) if not 'error' in updateUser: apiMessage(updateUser) return redirect(url_for('userBP.userView')) else: return unicode(updateUser) return render_template('user/userForm.html', usrForm=usrForm, grpForm=grpForm, **kwargs) elif function == 'new': usrForm = userForm(userRole='User') grpForm = groupForm() grpForm.groupUsers.choices = [(str(r['uuid']), r['email']) for r in getUsers()['users']] kwargs['contentTitle'] = 'New user' groups = [(str(r['uuid']), r['name']) for r in getGroups()['groups']] usrForm.userGroups.choices = groups if g.sijax.is_sijax_request: g.sijax.register_object(SijaxHandler) return g.sijax.process_request() if usrForm.validate_on_submit(): dataDict = { 'name': usrForm.userName.data, 'email': usrForm.userEmail.data, 'phone': usrForm.userPhone.data } roles = ['User'] if usrForm.userRole.data == 'Superuser': roles.append('Superuser') elif usrForm.userRole.data == 'Administrator': roles.append('Superuser') roles.append('Administrator') dataDict['roles'] = roles dataDict['groups'] = usrForm.userGroups.data newUser = postUser(dataDict) if 'success' in newUser: successMessage('The user has been created') subject = u'Confirm signup' confirm_url = url_for('authBP.confirmEmailView', token=newUser['token'], _external=True) html = render_template('email/verify.html', confirm_url=confirm_url) sendMail(subject=subject, sender='Henrik Poulsen', recipients=[usrForm.userEmail.data], html_body=html, text_body=None) return redirect(url_for('userBP.userView')) else: apiMessage(newUser) return render_template('user/userForm.html', usrForm=usrForm, grpForm=grpForm, **kwargs)
def groupView(function=None, uuid=None): # global variables kwargs = { 'contentTitle': 'User groups', 'width': '800', 'formWidth': '350', 'tableColumns': ['User group', 'Description', 'Users assigned to group'] } if function == None: req = getGroups(includes=['includeUsers'])['groups'] kwargs['tableData'] = [[ r['uuid'], r['name'], r['desc'], len(r['users']) ] for r in req] return render_template('listView.html', **kwargs) elif function == 'delete': delGroup = deleteGroup(uuid) apiMessage(delGroup) return redirect(url_for('userBP.groupView')) else: if function == 'update': kwargs['contentTitle'] = 'Update User Group' grp = getGroup(uuid, includes=['includeUsers'])['group'] form = groupForm( groupName=grp['name'], groupDesc=grp['desc'], groupUsers=[unicode(r['uuid']) for r in grp['users']]) form.groupUsers.choices = [(unicode(r['uuid']), r['email']) for r in getUsers()['users']] if form.validate_on_submit(): dataDict = { 'name': form.groupName.data, 'desc': form.groupDesc.data, 'users': [unicode(r) for r in form.groupUsers.data] } updateGroup = putGroup(dataDict=dataDict, uuid=uuid) if 'error' in updateGroup: apiMessage(updateGroup) else: apiMessage(updateGroup) return redirect(url_for('userBP.groupView')) elif function == 'new': kwargs['contentTitle'] = 'New User Group' form = groupForm() form.groupUsers.choices = [(unicode(r['uuid']), r['email']) for r in getUsers()['users']] if form.validate_on_submit(): dataDict = { 'name': form.groupName.data, 'desc': form.groupDesc.data, 'users': [unicode(r) for r in form.groupUsers.data] } newGroup = postGroup(dataDict) if 'error' in newGroup: apiMessage(newGroup) else: apiMessage(newGroup) return redirect(url_for('userBP.groupView')) return render_template('user/groupForm.html', form=form, **kwargs)
def groupView(function=None, id=None): # global variables kwargs = { 'title': 'User groups', 'width': '600', 'formWidth': '350', 'contentTitle': 'Add new user Group', 'tableColumns': ['User group', 'Description', 'Users assigned to group'] } if function == None: # perform API request req = getGroups(includes=['includeUsers'])['groups'] # set data for listView kwargs['tableData'] = [[ r['id'], r['name'], r['desc'], len(r['users']) ] for r in req] # return view return render_template('listView.html', **kwargs) elif function == 'delete': delGroup = deleteGroup(id) apiMessage(delGroup) return redirect(url_for('userBP.groupView')) else: if function == 'update': # Get single group grp = getGroup(id, includes=['includeUsers']) form = groupForm(groupName=grp['name'], groupDesc=grp['desc'], groupUsers=[str(r['id']) for r in grp['users']]) form.groupUsers.choices = [(str(r['id']), r['email']) for r in getUsers()['users']] if form.validate_on_submit(): dataDict = { 'name': form.groupName.data, 'desc': form.groupDesc.data, 'users': [int(r) for r in form.groupUsers.data] } updateGroup = putGroup(dataDict=dataDict, id=id) if 'error' in updateGroup: apiMessage(updateGroup) else: apiMessage(updateGroup) return redirect(url_for('userBP.groupView')) elif function == 'new': form = groupForm() form.groupUsers.choices = [(str(r['id']), r['email']) for r in getUsers()['users']] if form.validate_on_submit(): dataDict = { 'name': form.groupName.data, 'desc': form.groupDesc.data, 'users': [int(r) for r in form.groupUsers.data] } newGroup = postGroup(dataDict) if 'error' in newGroup: apiMessage(newGroup) else: apiMessage(newGroup) return redirect(url_for('userBP.groupView')) return render_template('user/groupForm.html', form=form, **kwargs)