Exemple #1
0
def user_import(request):
    try:
        request.user.profile
        messages.error(request, _('The import function is available for the superuser (without user profile) only.'))
        return redirect(reverse('user_overview'))
    except Profile.DoesNotExist:
        pass
    except AttributeError:
        # AnonymousUser
        pass

    if request.method == 'POST':
        form = UserImportForm(request.POST, request.FILES)
        if form.is_valid():
            profiles = Profile.objects.all()
            for profile in profiles:
                profile.user.delete()
                profile.delete()
            i = -1
            for line in request.FILES['csvfile']:
                i += 1
                if i > 0:
                    (first_name, last_name, gender, group, type, committee) = line.strip().split(',')
                    user = User()
                    user.last_name = last_name
                    user.first_name = first_name
                    user.username = gen_username(first_name, last_name)
                    #user.email = email
                    user.save()
                    profile = Profile()
                    profile.user = user
                    profile.gender = gender
                    profile.group = group
                    profile.type = type
                    profile.committee = committee
                    profile.firstpassword = gen_password()
                    profile.user.set_password(profile.firstpassword)
                    profile.save()

                    if type == 'delegate':
                        delegate = Group.objects.get(name='Delegierte')
                        user.groups.add(delegate)
                    else:
                        observer = Group.objects.get(name='Beobachter')
                        user.groups.add(observer)

            messages.success(request, _('%d new participants were successfully imported.') % i)
        else:
            messages.error(request, _('Please check the form for errors.'))
    else:
        messages.warning(request, _("Attention: All existing participants will be removed if you import new participants."))
        form = UserImportForm()
    return {
        'form': form,
    }
Exemple #2
0
def edit(request, user_id=None):
    """
    View zum editieren und neuanlegen von Usern mit Profile
    """
    if user_id is not None:
        user = User.objects.get(id=user_id)
    else:
        user = None

    if request.method == 'POST':
        if user_id is None:
            userform = UserNewForm(request.POST, prefix="user")
            profileform = ProfileForm(request.POST, prefix="profile")
        else:
            userform = UserEditForm(request.POST, instance=user, prefix="user")
            profileform = ProfileForm(request.POST, instance=user.profile, prefix="profile")

        if userform.is_valid and profileform.is_valid:
            user = userform.save()
            if user_id is None:
                user.username = gen_username(user.first_name, user.last_name)
                user.save()
            profile = profileform.save(commit=False)
            profile.user = user
            profile.firstpassword = gen_password()
            profile.user.set_password(profile.firstpassword)
            user.save()
            profile.save()
            if user_id is None:
                messages.success(request, _('New participant was successfully created.'))
            else:
                messages.success(request, _('Participant was successfully modified.'))
            if not 'apply' in request.POST:
                return redirect(reverse('user_overview'))
            if user_id is None:
                return redirect(reverse('user_edit', args=[user.id]))
        else:
            messages.error(request, _('Please check the form for errors.'))
    else:
        if user_id is None:
            userform = UserNewForm(prefix="user")
            profileform = ProfileForm(prefix="profile")
        else:
            userform = UserEditForm(instance=user, prefix="user")
            profileform = ProfileForm(instance=user.profile, prefix="profile")

    return {
        'userform': userform,
        'profileform': profileform,
        'edituser': user,
    }