def users_edit(id):
    instance = resource_instance('users', id)

    if (current_user.has_role('network-admin')
            and instance.network != current_user.network):
        abort(403)

    if (current_user.has_role('gateway-admin')
            and (instance.network != current_user.network
                 or instance.gateway != current_user.gateway)):
        abort(403)

    form = UserForm(obj=instance)

    if current_user.has_role('network-admin'):
        del form.gateway

    if current_user == instance:
        del form.active
        del form.roles

    if form.validate_on_submit():
        if form.password.data == '':
            del form.password

        form.populate_obj(instance)
        db.session.commit()

        flash('Update %s successful' % instance)
        return redirect(url_for('.users_index'))
    return render_template('users/edit.html', form=form, instance=instance)
Exemple #2
0
def register_volunteer(request):

    registered = False
    organization_list = get_organizations_by_name()

    if organization_list:

        if request.method == 'POST':

            #each form must have it's own namespace (prefix) if multiple forms are to be put inside one <form> tag
            user_form = UserForm(request.POST, prefix="usr")
            volunteer_form = VolunteerForm(request.POST, request.FILES, prefix="vol")

            if user_form.is_valid() and volunteer_form.is_valid():

                if 'resume_file' in request.FILES:
                    my_file = volunteer_form.cleaned_data['resume_file']
                    if not validate_file(my_file):
                        return render(
                            request,
                            'auth/register.html',
                            {'user_form' : user_form, 'volunteer_form' : volunteer_form, 'registered' : registered, 'organization_list' : organization_list,}
                        )

                user = user_form.save();

                user.set_password(user.password)
                user.save()
           
                volunteer = volunteer_form.save(commit=False)
                volunteer.user = user

                #if an organization isn't chosen from the dropdown, then organization_id will be 0
                organization_id = request.POST.get('organization_name')
                organization = get_organization_by_id(organization_id)

                if organization:
                    volunteer.organization = organization

                volunteer.save()

                registered = True
            else:
                print user_form.errors, volunteer_form.errors
        else:
            user_form = UserForm(prefix="usr")
            volunteer_form = VolunteerForm(prefix="vol") 

        return render(
            request,
            'auth/register.html',
            {'user_form' : user_form, 'volunteer_form' : volunteer_form, 'registered' : registered, 'organization_list' : organization_list,}
        )

    else:
        return HttpResponseRedirect(reverse('organization:error'))
Exemple #3
0
def razredi_dijaki_dijak_uredi(request, razred, dijak):
    profesor = request.user_profile
    razred = get_object_or_404(profesor.razredi, id=razred)
    dijak = get_object_or_404(razred.dijaki_cached, id=dijak)
    
    data = request.POST if request.method == 'POST' else None
    
    user_form = UserForm(instance=dijak.uporabnik, data=data, prefix='user')
    dijak_form = DijakForm(instance=dijak, data=data, prefix='dijak')
    sp_form = NaslovForm(instance=dijak.stalno_prebivalisce, data=data, prefix='sp')
    zp_form = NaslovForm(instance=dijak.zacasno_prebivalisce, data=data, prefix='zp')
    
    valid = True
    
    if user_form.is_valid() and dijak_form.is_valid():
        user = user_form.save(commit=False)
        dijak = dijak_form.save(commit=False)
        
        sp = None
        zp = None
        
        if not form_data_empty(sp_form):
            if sp_form.is_valid():
                sp = sp_form.save(commit=False)
            else:
                valid = False
        
        if not form_data_empty(zp_form):
            if zp_form.is_valid():
                zp = zp_form.save(commit=False)
            else:
                valid = False
        
        if valid:
            if sp:
                sp.save()
            
            if zp:
                zp.save()
            
            dijak.stalno_prebivalisce = sp
            dijak.zacasno_prebivalisce = zp
            
            user.save()
            dijak.save()
        
    else:
        valid = False
            
    if valid:
        messages.info(request, u'Podatki o dijaku so shranjeni.')
        
        return HttpResponseRedirect(reverse('profesor_razredi_dijaki_podatki', args=[razred.id, dijak.id]))
    
    return locals()
Exemple #4
0
def razredi_dijaki_mati_uredi(request, razred, dijak):
    profesor = request.user_profile
    razred = get_object_or_404(profesor.razredi, id=razred)
    dijak = get_object_or_404(razred.dijaki_cached, id=dijak)
    
    stars = dijak.mati
    
    data = request.POST if request.method == 'POST' else None
    
    user_form = UserForm(instance=stars.uporabnik if stars else None, data=data, prefix='user')
    stars_form = StarsForm(instance=stars, data=data, prefix='stars')
    p_form = NaslovForm(instance=stars.prebivalisce if stars else None, data=data, prefix='p')
    
    valid = True
    
    if user_form.is_valid() and stars_form.is_valid():
        user = user_form.save(commit=False)
        stars = stars_form.save(commit=False)
        
        p = None
        
        if not form_data_empty(p_form):
            if p_form.is_valid():
                p = p_form.save(commit=False)
            else:
                valid = False
        
        if valid:
            if p:
                p.save()
            
            stars.prebivalisce = p
            
            if not user.id:
                user.gen_username()
                user.set_unusable_password()
            
            user.save()
            
            stars.uporabnik = user
            stars.save()
            
            dijak.mati = stars
            dijak.save()
        
    else:
        valid = False
            
    if valid:
        messages.info(request, u'Podatki o materi so shranjeni.')
        
        return HttpResponseRedirect(reverse('profesor_razredi_dijaki_podatki', args=[razred.id, dijak.id]))
    
    return locals()
def auth_register(request):
    """
    Register new user and make login
    or return errors if not possible.
    """
    form = UserForm(json.loads(request.body))
    if form.is_valid():
        user = form.save()
        login(request, user)
        return Response({'errors': None})
    else:
        return Response({'errors': form.errors})
def users_new():
    form = UserForm()

    if current_user.has_role('gateway-admin'):
        del form.roles

    return resource_new('users', form)
Exemple #7
0
def register(request):

    registered = False
    if request.method == 'POST':

        #grab info from raw form information
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():

            #save form data to database
            user = user_form.save();

            #hash password with the set password method
            user.set_password(user.password)
            user.save()

            #we need to set user attribute ourselves, we set 
            #commit=False. This delays saving the model until
            #we're ready to avoid integrity problems
            profile = profile_form.save(commit=False)

            #reference the User model to the UserProfile instance
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            profile.save()
            
            registered = True            
        else:
            print user_form.errors, profile_form.errors

    else:
        #render unbound forms
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render(
        request,
        'auth/register.html',
        {'user_form': user_form, 'profile_form' : profile_form, 'registered' : registered,}
    )
Exemple #8
0
def register(request):
    """docstring for register"""
    nextUrl = request.GET.get('next')
    if request.method == 'POST':
        uf = UserForm(request.POST)
        if uf.is_valid():
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            email = uf.cleaned_data['email']
            user = User.objects.create_user(username=username,password=password,email=email)
            user.save()
            # login after register
            new_user = authenticate(username=request.POST['username'],password=request.POST['password'])
            login(request, new_user)
            return HttpResponseRedirect(nextUrl)
    else:
        uf = UserForm()
    return render_to_response('account/register.html', {
            'next':nextUrl,
            'userform':uf
            },context_instance=RequestContext(request))
Exemple #9
0
def edit_user(user_id):
    # print(current_user.get_id())
    # print(current_user.admin)
    showadminfield = False
    if current_user.admin and current_user.get_id() != user_id:
        showadminfield = True

    if current_user.admin is False and current_user.id != user_id:
        abort(404)

    user = User.query.filter_by(id=user_id).first()
    form = UserForm()
    if request.method == 'GET':
        form.username.data = user.username
        form.name.data = user.name
        form.email.data = user.email
        form.address.data = user.address
        form.zip_code.data = user.zip_code
        form.city.data = user.city
        form.phone_no.data = user.phone_no
        form.admin.data = user.admin
    elif form.validate_on_submit():
        user.username = form.username.data
        user.name = form.name.data
        user.email = form.email.data
        user.address = form.address.data
        user.zip_code = form.zip_code.data
        user.city = form.city.data
        user.phone_no = form.phone_no.data
        if current_user.admin and current_user.get_id() != user_id:
            user.admin = form.admin.data
        db.session.commit()
        flash('User successfully updated', 'success')
        return redirect(url_for('auth.users'))

    return render_template(signupTemplate,
                           form=form,
                           action=url_for('auth.edit_user', user_id=user_id),
                           showadminfield=showadminfield)
Exemple #10
0
def razredi_dijaki_dijak_dodaj(request, razred):
    profesor = request.user_profile
    razred = get_object_or_404(profesor.razredi, id=razred)
    
    data = request.POST if request.method == 'POST' else None
    
    form = UserForm(data=data)
    
    if form.is_valid():
        user = form.save(commit=False)
        user.gen_username()
        user.set_unusable_password()
        user.save()
        
        dijak = Dijak.objects.create(uporabnik=user)
        
        razred.dijaki.add(dijak)
        
        messages.info(request, u'Dijak je dodan.')
        
        return HttpResponseRedirect(reverse('profesor_razredi_dijaki_dijak_uredi', args=[razred.id, dijak.id]))
    
    return locals()
def users_index():
    form = UserForm()
    return resource_index('users', form=form)