Example #1
0
def import_matriculation_list(request, group_id):
    """ Set the group memembership of all users according to an uploaded list of matriculation numbers. """
    group = get_object_or_404(Group, pk=group_id)
    if request.method == 'POST':
        form = ImportMatriculationListForm(request.POST, request.FILES)
        if form.is_valid():
            file = form.files['mat_number_file']
            reader = csv.reader(file)
            mats = set(int(row[0]) for row in reader)
            nr_in_group = nr_not_in_group = 0
            for u in User.objects.all():
                if u.mat_number in mats:
                    u.groups.add(group)
                    nr_in_group += 1
                else:
                    u.groups.remove(group)
                    nr_not_in_group += 1
                u.save()
            request.user.message_set.create(
                message="%i users added to group %s, %i not." %
                (nr_in_group, group.name, nr_not_in_group))
            return HttpResponseRedirect(
                urlresolvers.reverse('admin:auth_group_change',
                                     args=[group_id]))
    else:
        form = ImportMatriculationListForm()
    return render_to_response(
        'admin/auth/group/import_matriculation_list.html', {
            'form': form,
            'title': "Import matriuculation number list"
        }, RequestContext(request))
Example #2
0
def import_matriculation_list(request, group_id):
    """ Set the group membership of all users according to an uploaded list of matriculation numbers. """
    group = get_object_or_404(Group, pk=group_id)
    if request.method == 'POST':
        form = ImportMatriculationListForm(request.POST, request.FILES)
        if form.is_valid():
            file = form.files['mat_number_file']
            file.seek(0)
            reader = csv.reader(io.StringIO(file.read().decode('utf-8')))
            mats = set(int(row[0]) for row in reader)

            nr_already = nr_added = nr_removed = nr_new_users = 0

            # first create all users, if required
            if form.cleaned_data['create_users']:
                existing_mats = set(
                    User.objects.values_list('mat_number', flat=True))
                for new in mats - existing_mats:
                    user = User.objects.create_user(new, '')
                    user.groups.add(Group.objects.get(name='User'))
                    user.mat_number = new
                    user.save()
                    nr_new_users += 1

            for u in User.objects.all():
                if u.mat_number in mats:
                    if u.groups.filter(id=group.pk).exists():
                        nr_already += 1
                    else:
                        u.groups.add(group)
                        nr_added += 1
                else:
                    if form.cleaned_data['remove_others']:
                        if u.groups.filter(id=group.pk).exists():
                            u.groups.remove(group)
                            nr_removed += 1
                u.save()
            messages.success(request, (
                "%i users added to group %s, %i removed, %i already in group. "
                + "%i new users created.") % (nr_added, group.name, nr_removed,
                                              nr_already, nr_new_users))
            return HttpResponseRedirect(
                urlresolvers.reverse('admin:auth_group_change',
                                     args=[group_id]))
    else:
        form = ImportMatriculationListForm()
    return render(request, 'admin/auth/group/import_matriculation_list.html', {
        'form': form,
        'title': "Import matriuculation number list"
    })
Example #3
0
def import_matriculation_list(request, group_id):
    """ Set the group membership of all users according to an uploaded list of matriculation numbers. """
    group = get_object_or_404(Group, pk=group_id)
    if request.method == 'POST':
        form = ImportMatriculationListForm(request.POST, request.FILES)
        if form.is_valid():
            file = form.files['mat_number_file']
            file.seek(0)
            reader = csv.reader(io.StringIO(file.read().decode('utf-8')))
            mats = set(int(row[0]) for row in reader)

            nr_already = nr_added = nr_removed = nr_new_users = 0

            # first create all users, if required
            if form.cleaned_data['create_users']:
                existing_mats = set(User.objects.values_list('mat_number', flat=True))
                for new in mats - existing_mats:
                    user = User.objects.create_user(new, '')
                    user.groups.add(Group.objects.get(name='User'))
                    user.mat_number = new
                    user.save()
                    nr_new_users += 1

            for u in User.objects.all():
                if u.mat_number in mats:
                    if u.groups.filter(id=group.pk).exists():
                        nr_already += 1
                    else:
                        u.groups.add(group)
                        nr_added += 1
                else:
                    if form.cleaned_data['remove_others']:
                        if u.groups.filter(id=group.pk).exists():
                            u.groups.remove(group)
                            nr_removed += 1
                u.save()
            messages.success(request,
                ("%i users added to group %s, %i removed, %i already in group. "+
                "%i new users created.") % (nr_added, group.name, nr_removed, nr_already, nr_new_users))
            return HttpResponseRedirect(urlresolvers.reverse('admin:auth_group_change', args=[group_id]))
    else:
        form = ImportMatriculationListForm()
    return render(request, 'admin/auth/group/import_matriculation_list.html', {'form': form, 'title':"Import matriuculation number list"})
Example #4
0
def import_matriculation_list(request, group_id):
    """ Set the group memembership of all users according to an uploaded list of matriculation numbers. """
    group = get_object_or_404(Group,pk=group_id)
    if request.method == 'POST':
        form = ImportMatriculationListForm(request.POST, request.FILES)
        if form.is_valid():
            file = form.files['mat_number_file']
            reader = csv.reader(file)
            mats = set(int(row[0]) for row in reader)
            nr_in_group = nr_not_in_group = 0
            for u in User.objects.all():
                if u.mat_number in mats:
                    u.groups.add(group)
                    nr_in_group += 1
                else:
                    u.groups.remove(group)
                    nr_not_in_group += 1
                u.save()
            request.user.message_set.create(message="%i users added to group %s, %i not." % (nr_in_group, group.name, nr_not_in_group))
            return HttpResponseRedirect(urlresolvers.reverse('admin:auth_group_change', args=[group_id]))
    else:
        form = ImportMatriculationListForm()
    return render_to_response('admin/auth/group/import_matriculation_list.html', {'form': form, 'title':"Import matriuculation number list"}, RequestContext(request))