Example #1
0
def loginView(lang='dk'):
    if not 'token' in session:
        g.lang = lang
        kwargs = {'formWidth':300,
                  'contentTitle':messageText('newPassword'),
                  'breadcrumbs': breadCrumbs('authBP.loginView')}

        form = loginForm()
        if form.validate_on_submit():
            regNo = form.regNo.data
            email = form.email.data
            password = form.password.data

            dataDict = {'regNo':regNo,
                        'email':email,
                        'password':password}

            req = authAPI('login', method='post', dataDict=dataDict)
            if 'success' in req:
                session['token'] = req['token']
                session['email'] = req['email']
                session['roles'] = req['roles']
                flashMessage('loginSuccess')
                return redirect(url_for('indexView', lang=lang))
            else:
                print req
                flashMessage('loginError')

        return render_template(lang+'/auth/loginForm.html', form=form, **kwargs)
    else:
        flashMessage('alreadyLoggedIn')
        return redirect(url_for('indexView', lang=lang))
Example #2
0
def userProfileView(lang='dk'):
    g.lang = lang
    kwargs = {
        'title': messageText('userProfileTitle'),
        'breadcrumbs': breadCrumbs('userBP.userProfileView')
    }

    return render_template(lang + '/user/userProfileView.html', **kwargs)
Example #3
0
def settingsView(lang=None):
    g.lang = lang
    kwargs = {
        'title': messageText('settingsTitle'),
        'formWidth': '350',
        'breadcrumbs': breadCrumbs('settingsBP.settingsView')
    }
    return render_template(lang + '/settings/settingsView.html', **kwargs)
Example #4
0
def companyView(lang=None):
    g.lang = lang
    form = companyForm()
    kwargs = {
        'title': messageText('companyTitle'),
        'formWidth': '350',
        'breadcrumbs': breadCrumbs('settingsBP.companyView')
    }
    return render_template(lang + '/settings/companyView.html',
                           form=form,
                           **kwargs)
Example #5
0
def registerView(lang='dk'):
    if not 'token' in session:
        # universal variables
        form = registerForm()
        kwargs = {'formWidth':400,
                  'breadcrumbs': breadCrumbs('authBP.registerView')}

        if g.sijax.is_sijax_request:
            g.sijax.register_callback('validate', validateCVR)
            return g.sijax.process_request()

        if form.validate_on_submit():
            dataDict = {'regNo' : form.regNo.data,
                        'companyName' : form.companyName.data,
                        'userName' : form.userName.data,
                        'email' : form.email.data,
                        'password' : form.password.data}

            req = authAPI('register', method='post', dataDict=dataDict)

            if r.status_code == 409:
                flashmessage('accountExists')
            elif r.status_code == 404:
                flashmessage('cvrCheckError')
            elif 'error' in req:
                if req['error'] == 'Not valid email-address':
                    flashMessage('validateEmail')
            elif 'success' in req:
                # send email confirmation
                subject = u'Bekræft tilmelding'
                tok = req['token']
                email = req['email']
                confirm_url = url_for('authBP.confirmEmailView',token=tok, _external=True, lang=lang)
                html = render_template(lang+'/email/verify.html', confirm_url=confirm_url)
    #
                sendMail(subject=subject,
                         sender='Henrik Poulsen',
                         recipients=[email],
                         html_body=html,
                         text_body = None)
                flashMessage('loginSuccess')
                return redirect(url_for('indexView', lang=lang))

        return render_template(lang+'/auth/registerForm.html', form=form, **kwargs)
    else:
        flashMessage('alreadyRegistered')
        return redirect(url_for('indexView', lang=lang))
Example #6
0
def setPasswordView(lang='dk', tok=None):
    g.lang = lang
    kwargs = {'formWidth':300,
              'contentTitle':str(tok),
              'title':messageText('setPasswordTitle'),
              'breadcrumbs': breadCrumbs('authBP.setPasswordView')}

    form = setPasswordForm()

    if form.validate_on_submit():
        req = authAPI('setPassword', method='post', dataDict=dataDict, token=session['token'])
        if r.status_code == 404:
            flashMessage('userDoesNotExist')
        elif 'success' in req:
            flashMessage('passwordSet')

    return render_template(lang+'/auth/setPasswordForm.html', form=form, **kwargs)
Example #7
0
def changePasswordView(lang='dk'):
    g.lang = lang
    kwargs = {
        'formWidth': 300,
        'contentTitle': messageText('changePassword'),
        'breadcrumbs': breadCrumbs('userBP.changePasswordView')
    }

    form = changePasswordForm()

    if form.validate_on_submit():

        dataDict = {'password': form.password.data}

        req = authAPI(endpoint='changePassword',
                      method='put',
                      dataDict=dataDict,
                      token=session['token'])
        flashMessage('passwordChanged')

    return render_template(lang + '/user/changePasswordForm.html',
                           form=form,
                           **kwargs)
Example #8
0
def userView(lang=None, id=None, function=None):
    # universal variables

    g.lang = lang
    form = userForm()
    kwargs = {
        'title': messageText('usersTitle'),
        'width': '',
        'formWidth': '400',
        'breadcrumbs': breadCrumbs('userBP.userView')
    }

    # Get users
    if function == None:
        users = getUsers()
        kwargs['tableColumns'] = columns(['userNameCol', 'emailCol'])
        kwargs['tableData'] = [[r['id'], r['name'], r['email']] for r in users]

        return render_template(lang + '/listView.html', **kwargs)
    elif function == 'delete':
        delUsr = deleteUser(id)

        if 'error' in delUsr:
            errorFlash(delUsr['error'])
        elif 'success' in delUsr:
            flashMessage('userDeleted')

        return redirect(url_for('userBP.userView', lang=lang))
    else:
        if function == 'update':
            # Get single user
            usr = getUser(id, includes=['includeRoles', 'includeGroups'])

            form = userForm(name=usr['name'],
                            email=usr['email'],
                            phone=usr['phone'],
                            groups=[str(r['id']) for r in usr['groups']])

            if 'roles' in usr:
                for r in usr['roles']:
                    if r['title'] == 'Administrator':
                        form.isAdmin.checked = True
                    if r['title'] == 'Superuser':
                        form.isSuperuser.checked = True
#
# Get all groups
            form.groups.choices = [(str(r['id']), r['name'])
                                   for r in getGroups()]

            if form.validate_on_submit():
                dataDict = {
                    'name': form.name.data,
                    'email': form.email.data,
                    'phone': form.phone.data
                }

                roles = []
                if form.isAdmin.data:
                    roles.append('Administrator')
                if form.isSuperuser.data:
                    roles.append('Superuser')
                dataDict['roles'] = roles
                dataDict['groups'] = form.groups.data
                updateUser = putUser(dataDict=dataDict, id=id)
                if 'error' in updateUser:
                    errorFlash(updateUser['error'])
                elif 'success' in updateUser:
                    flashMessage('userUpdated')

                return redirect(url_for('userBP.userView', lang=lang))

            return render_template(lang + '/user/userForm.html',
                                   form=form,
                                   **kwargs)
        elif function == 'new':
            form = userForm()
            groups = [(str(r['id']), r['name']) for r in getGroups()]
            form.groups.choices = groups

            if form.validate_on_submit():
                dataDict = {
                    'name': form.name.data,
                    'email': form.email.data,
                    'phone': form.phone.data
                }
                roles = []
                if form.isAdmin.data:
                    roles.append('Administrator')
                if form.isSuperuser.data:
                    roles.append('Superuser')
                dataDict['roles'] = roles
                dataDict['groups'] = form.groups.data
                newUser = postUser(dataDict)
                if 'error' in newUser:
                    if newUser['error'] == 'User already exist':
                        flashMessage('userExists')
                    else:
                        errorFlash(newUser['error'])
                elif 'success' in newUser:
                    flashMessage('userCreated')

                return redirect(url_for('userBP.userView', lang=lang))
            return render_template(lang + '/user/userForm.html',
                                   form=form,
                                   **kwargs)

    return render_template(lang + '/listView.html', **kwargs)
Example #9
0
def groupView(function=None, id=None, lang=None):
    # global variables
    g.lang = lang
    kwargs = {
        'title': messageText('userGrpTitle'),
        'width': '600',
        'formWidth': '350',
        'breadcrumbs': breadCrumbs('userBP.groupView')
    }

    if function == None:
        # perform API request
        req = getGroups(includes=['includeUsers'])

        # set data for listView
        kwargs['tableColumns'] = columns(['usrGroupCol', 'usersCol'])
        kwargs['tableData'] = [[r['id'], r['name'],
                                len(r['users'])] for r in req]

        # return view
        return render_template(lang + '/listView.html', **kwargs)
    elif function == 'delete':
        delGroup = deleteGroup(id)

        if 'error' in delGroup:
            if delGroup['error'] == 'Group has users':
                flashMessage('grpHasUsers')
            else:
                errorFlash(delGroup['error'])
        elif 'success' in delGroup:
            flashMessage('grpDeleted')

        return redirect(url_for('userBP.groupView', lang=lang))

    else:
        if function == 'update':
            # Get single group
            grp = getGroup(id, includes=['includeUsers'])
            form = groupForm(name=grp['name'],
                             desc=grp['desc'],
                             users=[str(r['id']) for r in grp['users']])
            form.users.choices = [(str(r['id']), r['email'])
                                  for r in getUsers()]
            if form.validate_on_submit():
                dataDict = {
                    'name': form.name.data,
                    'desc': form.desc.data,
                    'users': [int(r) for r in form.users.data]
                }
                updateGroup = putGroup(dataDict=dataDict, id=id)
                if 'error' in updateGroup:
                    if updateGroup['error'] == 'Group already exist':
                        flashMessage('grpUpdated')
                    else:
                        errorFlash(updateGroup['error'])
                elif 'success' in updateGroup:
                    flashMessage('grpNew')

                return redirect(url_for('userBP.groupView', lang=lang))

        elif function == 'new':
            form = groupForm()
            form.users.choices = [(str(r['id']), r['email'])
                                  for r in getUsers()]

            if form.validate_on_submit():
                dataDict = {
                    'name': form.name.data,
                    'desc': form.desc.data,
                    'users': [int(r) for r in form.users.data]
                }
                newGroup = postGroup(dataDict)

                if 'error' in newGroup:
                    if newGroup['error'] == 'Group already exist':
                        flashMessage('grpExists')
                    else:
                        errorFlash(newGroup['error'])
                elif 'success' in newGroup:
                    flashMessage('grpNew')

                return redirect(url_for('userBP.groupView', lang=lang))
        return render_template(lang + '/user/groupForm.html', form=form)