Exemple #1
0
def register(request):
    context = {}
    
    if request.method == 'POST':
        kennitala_form = KennitalaForm(request.POST)
        if kennitala_form.is_valid():
            student = Student.objects.get(kennitala=kennitala_form.cleaned_data['kennitala'])
            suggested_usernames = suggest_usernames(student.first_name, student.last_name)

            # See if the user actually posted some stuff, other that its
            # kennitala to get to this page
            post_keys        = request.POST.keys()
            other_post_stuff = None
            if "id_username_0" in post_keys or "password" in post_keys:
                other_post_stuff = request.POST

            password_form = PasswordForm(other_post_stuff)
            username_form = UsernameForm(other_post_stuff)
            username_form.fields['username'].choices = [(x, x) for x in suggested_usernames]

            if username_form.is_valid() and password_form.is_valid():
                username = username_form.cleaned_data['username']
                log.info("Creating user %s for student %s (kt %s)", username,
                    student, student.kennitala)

                user = User.objects.create(username=username, password="******") # pw overridde below
                userp = UserProfile(
                            user      = user,
                            kennitala = student.kennitala,
                            user_type = ContentType.objects.get_for_model(student),
                            status = UserStatus.objects.get(pk=ACTIVE_USER),
                        )

                userp.set_password(password_form.cleaned_data['password'])
                userp.set_dirty()
                userp.save()
                
                return HttpResponseRedirect("/register/done/")
                
                
            context['password_form']  = password_form.as_ul()
            context['username_form']  = username_form.as_ul()
            context['student']  = student
    else:
        kennitala_form = KennitalaForm()


    context['kennitala_form'] = kennitala_form.as_ul()

    return render_to_response(
        'registration/register.html', context,
        context_instance=RequestContext(request))
Exemple #2
0
def edit_user(request, kennitala, user_id=None):
    ou   = get_object_or_404(OrganizationalUnit, pk=kennitala)

    if user_id:
        userp  = get_object_or_404(UserProfile, pk=user_id, kennitala=kennitala)
        title = u"Breyta notanda"
    else:
        userp  = None
        title = u"Bæta við notanda"

    if request.method == "POST":
        user = None
        form = UserForm(request.POST)
        if form.is_valid(current_holder=userp):
            if not user_id:
                user = User.objects.create(username=form.cleaned_data['username'], password="******")
                userp = UserProfile(
                            user      = user,
                            kennitala = ou.kennitala,
                            user_type = ContentType.objects.get_for_model(ou),
                        )

            userp.status = UserStatus.objects.get(pk=form.cleaned_data['status'])
            
            if form.cleaned_data['password']:
                userp.set_password(form.cleaned_data['password'])

            userp.set_dirty()
            # TODO: Catch invalid state transitions.
            userp.save()

            return HttpResponseRedirect("/ou/list/")
    else:
        initial = {'kennitala': kennitala, 'status': 1}
        
        if user_id:
            initial['status']     = userp.status.id
            initial['username']   = userp.user.username
        
        form = UserForm(initial=initial)

    return render_to_response('ou/edit.html',
        {'form': form.as_ul(), 'title': title, 'userp': userp,
            'editwarning': True if userp else False },
        context_instance=RequestContext(request))