2011-07-31 10:46:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
openslides.participant.views
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Views for the participant app.
|
|
|
|
|
2012-04-25 22:29:19 +02:00
|
|
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
# for python 2.5 support
|
2011-11-29 11:02:31 +01:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
import csv
|
2011-09-10 00:16:57 +02:00
|
|
|
from urllib import urlencode
|
2012-04-13 11:35:53 +02:00
|
|
|
|
2011-09-12 08:32:05 +02:00
|
|
|
try:
|
|
|
|
from urlparse import parse_qs
|
2012-08-08 10:34:23 +02:00
|
|
|
except ImportError: # python <= 2.5 grab it from cgi
|
2011-09-12 08:32:05 +02:00
|
|
|
from cgi import parse_qs
|
2011-09-10 00:16:57 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
from reportlab.lib import colors
|
|
|
|
from reportlab.lib.units import cm
|
2012-08-08 10:34:23 +02:00
|
|
|
from reportlab.platypus import (
|
|
|
|
SimpleDocTemplate, PageBreak, Paragraph, LongTable, Spacer, Table,
|
|
|
|
TableStyle)
|
2012-07-07 15:26:00 +02:00
|
|
|
|
|
|
|
from django.db import transaction
|
2011-07-31 10:46:29 +02:00
|
|
|
from django.contrib import messages
|
2012-07-07 15:26:00 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib.auth.models import User, Group
|
2012-07-11 09:46:15 +02:00
|
|
|
from django.contrib.auth.forms import PasswordChangeForm
|
2012-07-18 10:46:07 +02:00
|
|
|
from django.contrib.auth.views import login as django_login
|
2011-07-31 10:46:29 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2012-07-07 15:26:00 +02:00
|
|
|
from django.shortcuts import redirect
|
2012-07-20 11:40:36 +02:00
|
|
|
from django.utils.translation import ugettext as _, ungettext, ugettext_lazy
|
2012-04-20 23:23:50 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
from openslides.utils import csv_ext
|
|
|
|
from openslides.utils.pdf import stylesheet
|
|
|
|
from openslides.utils.template import Tab
|
2012-08-08 10:34:23 +02:00
|
|
|
from openslides.utils.utils import (
|
|
|
|
template, permission_required, gen_confirm_form, ajax_request, decodedict,
|
|
|
|
encodedict, delete_default_permissions, html_strong)
|
2012-08-10 13:22:09 +02:00
|
|
|
from openslides.utils.views import (
|
2012-08-10 19:19:41 +02:00
|
|
|
FormView, PDFView, TemplateView, CreateView, UpdateView, DeleteView,
|
|
|
|
RedirectView, SingleObjectMixin, ListView)
|
2011-09-03 19:42:43 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
from openslides.config.models import config
|
2012-04-20 23:23:50 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
from openslides.participant.models import OpenSlidesUser, OpenSlidesGroup
|
2012-07-07 15:26:00 +02:00
|
|
|
from openslides.participant.api import gen_username, gen_password
|
2012-08-08 10:34:23 +02:00
|
|
|
from openslides.participant.forms import (
|
2012-08-10 13:22:09 +02:00
|
|
|
UserCreateForm, UserUpdateForm, OpenSlidesUserForm, UsersettingsForm,
|
2012-08-08 10:34:23 +02:00
|
|
|
UserImportForm, GroupForm, AdminPasswordChangeForm, ConfigForm)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
class Overview(ListView):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 11:51:45 +02:00
|
|
|
Show all participants.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 11:51:45 +02:00
|
|
|
permission_required = 'participant.can_see_participant'
|
|
|
|
template_name = 'participant/overview.html'
|
2012-08-10 19:19:41 +02:00
|
|
|
context_object_name = 'users'
|
2012-08-10 11:51:45 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
def get_queryset(self):
|
2011-07-31 10:46:29 +02:00
|
|
|
try:
|
2012-08-10 11:51:45 +02:00
|
|
|
sortfilter = encodedict(parse_qs(
|
|
|
|
self.request.COOKIES['participant_sortfilter']))
|
|
|
|
except KeyError:
|
|
|
|
sortfilter = {}
|
|
|
|
|
|
|
|
for value in [u'gender', u'category', u'type', u'committee', u'status',
|
|
|
|
u'sort', u'reverse']:
|
|
|
|
if value in self.request.REQUEST:
|
|
|
|
if self.request.REQUEST[value] == '---':
|
|
|
|
try:
|
|
|
|
del sortfilter[value]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
sortfilter[value] = [self.request.REQUEST[value]]
|
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
query = OpenSlidesUser.objects
|
2012-08-10 11:51:45 +02:00
|
|
|
if 'gender' in sortfilter:
|
2012-08-10 19:19:41 +02:00
|
|
|
query = query.filter(gender__iexact=sortfilter['gender'][0])
|
2012-08-10 11:51:45 +02:00
|
|
|
if 'category' in sortfilter:
|
2012-08-10 19:19:41 +02:00
|
|
|
query = query.filter(category__iexact=sortfilter['category'][0])
|
2012-08-10 11:51:45 +02:00
|
|
|
if 'type' in sortfilter:
|
2012-08-10 19:19:41 +02:00
|
|
|
query = query.filter(type__iexact=sortfilter['type'][0])
|
2012-08-10 11:51:45 +02:00
|
|
|
if 'committee' in sortfilter:
|
2012-08-10 19:19:41 +02:00
|
|
|
query = query.filter(committee__iexact=sortfilter['committee'][0])
|
2012-08-10 11:51:45 +02:00
|
|
|
if 'status' in sortfilter:
|
|
|
|
query = query.filter(is_active=sortfilter['status'][0])
|
|
|
|
if 'sort' in sortfilter:
|
|
|
|
if sortfilter['sort'][0] in ['first_name', 'last_name', 'last_login']:
|
|
|
|
query = query.order_by(sortfilter['sort'][0])
|
|
|
|
elif (sortfilter['sort'][0] in
|
|
|
|
['category', 'type', 'committee', 'comment']):
|
|
|
|
query = query.order_by(
|
|
|
|
'openslidesuser__%s' % sortfilter['sort'][0])
|
2012-08-07 22:43:57 +02:00
|
|
|
else:
|
2012-08-10 11:51:45 +02:00
|
|
|
query = query.order_by('last_name')
|
|
|
|
|
|
|
|
if 'reverse' in sortfilter:
|
|
|
|
query = query.reverse()
|
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
self.sortfilter = sortfilter
|
|
|
|
|
|
|
|
return query.all()
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(Overview, self).get_context_data(**kwargs)
|
2012-08-10 11:51:45 +02:00
|
|
|
|
|
|
|
all_users = User.objects.count()
|
|
|
|
|
|
|
|
# quotient of selected users and all users
|
|
|
|
if all_users > 0:
|
2012-08-10 19:19:41 +02:00
|
|
|
percent = self.object_list.count() * 100 / float(all_users)
|
2012-08-07 22:43:57 +02:00
|
|
|
else:
|
2012-08-10 11:51:45 +02:00
|
|
|
percent = 0
|
|
|
|
|
|
|
|
# list of all existing categories
|
|
|
|
categories = [p['category'] for p in OpenSlidesUser.objects.values('category')
|
|
|
|
.exclude(category='').distinct()]
|
|
|
|
|
|
|
|
# list of all existing committees
|
|
|
|
committees = [p['committee'] for p in OpenSlidesUser.objects.values('committee')
|
|
|
|
.exclude(committee='').distinct()]
|
|
|
|
context.update({
|
|
|
|
'allusers': all_users,
|
|
|
|
'percent': round(percent, 1),
|
|
|
|
'categories': categories,
|
|
|
|
'committees': committees,
|
2012-08-10 19:19:41 +02:00
|
|
|
'cookie': ['participant_sortfilter', urlencode(decodedict(self.sortfilter),
|
2012-08-10 11:51:45 +02:00
|
|
|
doseq=True)],
|
2012-08-10 19:19:41 +02:00
|
|
|
'sortfilter': self.sortfilter})
|
2012-08-10 11:51:45 +02:00
|
|
|
return context
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-08-10 13:22:09 +02:00
|
|
|
class UserCreateView(CreateView):
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2012-08-10 13:22:09 +02:00
|
|
|
Create a new participant.
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2012-08-10 13:22:09 +02:00
|
|
|
permission_required = 'participant.can_manage_participant'
|
|
|
|
template_name = 'participant/edit.html'
|
|
|
|
model = OpenSlidesUser
|
|
|
|
context_object_name = 'edit_user'
|
|
|
|
form_class = UserCreateForm
|
|
|
|
success_url = 'user_overview'
|
|
|
|
apply_url = 'participant_edit'
|
|
|
|
|
|
|
|
def manipulate_object(self, form):
|
|
|
|
self.object.username = gen_username(form.cleaned_data['first_name'], form.cleaned_data['last_name'])
|
2012-08-10 19:19:41 +02:00
|
|
|
if not self.object.firstpassword:
|
|
|
|
self.object.firstpassword = gen_password()
|
2012-08-10 13:22:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
class UserUpdateView(UpdateView):
|
2012-08-10 13:29:46 +02:00
|
|
|
"""
|
|
|
|
Update an existing participant.
|
|
|
|
"""
|
2012-08-10 13:22:09 +02:00
|
|
|
permission_required = 'participant.can_manage_participant'
|
|
|
|
template_name = 'participant/edit.html'
|
|
|
|
model = OpenSlidesUser
|
|
|
|
context_object_name = 'edit_user'
|
|
|
|
form_class = UserUpdateForm
|
|
|
|
success_url = 'user_overview'
|
|
|
|
apply_url = 'participant_edit'
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-08-10 13:29:46 +02:00
|
|
|
class UserDeleteView(DeleteView):
|
|
|
|
"""
|
|
|
|
Delete an participant.
|
|
|
|
"""
|
|
|
|
permission_required = 'participant.can_manage_participant'
|
|
|
|
model = OpenSlidesUser
|
|
|
|
url = 'user_overview'
|
|
|
|
|
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
class SetUserStatusView(RedirectView, SingleObjectMixin):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
Activate or deactivate an user.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
permission_required = 'participant.can_manage_participant'
|
|
|
|
allow_ajax = True
|
|
|
|
url = 'user_overview'
|
|
|
|
model = OpenSlidesUser
|
2012-04-02 08:35:10 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
def pre_redirect(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
action = kwargs['action']
|
|
|
|
if action == 'activate':
|
|
|
|
self.object.is_active = True
|
|
|
|
elif action == 'deactivate':
|
|
|
|
self.object.is_active = False
|
|
|
|
elif action == 'toggle':
|
|
|
|
self.object.is_active = not self.object.is_active
|
|
|
|
self.object.save()
|
|
|
|
return super(SetUserStatusView, self).pre_redirect(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_ajax_context(self, **kwargs):
|
|
|
|
context = super(SetUserStatusView, self).get_ajax_context(**kwargs)
|
|
|
|
context['active'] = self.object.is_active
|
|
|
|
return context
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
class ParticipantsListPDF(PDFView):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
Generate the userliste as PDF.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
permission_required = 'participant.can_see_participant'
|
|
|
|
filename = ugettext_lazy("Participant-list")
|
|
|
|
document_title = ugettext_lazy('List of Participants')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
def append_to_pdf(self, story):
|
|
|
|
data = [['#', _('Last Name'), _('First Name'), _('Group'), _('Type'),
|
|
|
|
_('Committee')]]
|
|
|
|
sort = 'last_name'
|
|
|
|
counter = 0
|
|
|
|
for user in OpenSlidesUser.objects.all().order_by(sort):
|
|
|
|
counter += 1
|
|
|
|
data.append([
|
|
|
|
counter,
|
|
|
|
Paragraph(user.last_name, stylesheet['Tablecell']),
|
|
|
|
Paragraph(user.first_name, stylesheet['Tablecell']),
|
|
|
|
Paragraph(user.category, stylesheet['Tablecell']),
|
|
|
|
Paragraph(user.type, stylesheet['Tablecell']),
|
|
|
|
Paragraph(user.committee, stylesheet['Tablecell'])
|
|
|
|
])
|
|
|
|
t = LongTable(data,
|
|
|
|
style=[
|
|
|
|
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
|
|
|
('LINEABOVE', (0, 0), (-1, 0), 2, colors.black),
|
|
|
|
('LINEABOVE', (0, 1), (-1, 1), 1, colors.black),
|
|
|
|
('LINEBELOW', (0, -1), (-1, -1), 2, colors.black),
|
|
|
|
('ROWBACKGROUNDS', (0, 1), (-1, -1),
|
|
|
|
(colors.white, (.9, .9, .9)))])
|
|
|
|
t._argW[0] = 0.75 * cm
|
|
|
|
story.append(t)
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
|
|
|
|
class ParticipantsPasswordsPDF(PDFView):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
Generate the Welcomepaper for the users.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
permission_required = 'participant.can_manage_participant'
|
|
|
|
filename = ugettext_lazy("Participant-passwords")
|
|
|
|
top_space = 0
|
2011-11-14 16:37:12 +01:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
def get_template(self, buffer):
|
|
|
|
return SimpleDocTemplate(buffer, topMargin=-6, bottomMargin=-6,
|
|
|
|
leftMargin=0, rightMargin=0, showBoundary=False)
|
2012-08-03 12:44:34 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
def build_document(self, pdf_document, story):
|
|
|
|
pdf_document.build(story)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
def append_to_pdf(self, story):
|
|
|
|
data = []
|
|
|
|
participant_pdf_system_url = config["participant_pdf_system_url"]
|
|
|
|
participant_pdf_welcometext = config["participant_pdf_welcometext"]
|
|
|
|
for user in OpenSlidesUser.objects.all().order_by('last_name'):
|
|
|
|
cell = []
|
|
|
|
cell.append(Spacer(0, 0.8 * cm))
|
|
|
|
cell.append(Paragraph(_("Account for OpenSlides"),
|
|
|
|
stylesheet['Ballot_title']))
|
|
|
|
cell.append(Paragraph(_("for %s") % (user),
|
|
|
|
stylesheet['Ballot_subtitle']))
|
|
|
|
cell.append(Spacer(0, 0.5 * cm))
|
|
|
|
cell.append(Paragraph(_("User: %s") % (user.username),
|
|
|
|
stylesheet['Monotype']))
|
|
|
|
cell.append(Paragraph(_("Password: %s")
|
|
|
|
% (user.firstpassword), stylesheet['Monotype']))
|
|
|
|
cell.append(Spacer(0, 0.5 * cm))
|
|
|
|
cell.append(Paragraph(_("URL: %s")
|
|
|
|
% (participant_pdf_system_url),
|
|
|
|
stylesheet['Ballot_option']))
|
|
|
|
cell.append(Spacer(0, 0.5 * cm))
|
|
|
|
cell2 = []
|
|
|
|
cell2.append(Spacer(0, 0.8 * cm))
|
|
|
|
if participant_pdf_welcometext is not None:
|
|
|
|
cell2.append(Paragraph(
|
|
|
|
participant_pdf_welcometext.replace('\r\n', '<br/>'),
|
|
|
|
stylesheet['Ballot_subtitle']))
|
|
|
|
|
|
|
|
data.append([cell, cell2])
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
# add empty table line if no participants available
|
|
|
|
if not data:
|
|
|
|
data.append(['', ''])
|
|
|
|
# build table
|
|
|
|
t = Table(data, 10.5 * cm, 7.42 * cm)
|
|
|
|
t.setStyle(TableStyle([
|
|
|
|
('LINEBELOW', (0, 0), (-1, 0), 0.25, colors.grey),
|
|
|
|
('LINEBELOW', (0, 1), (-1, 1), 0.25, colors.grey),
|
|
|
|
('LINEBELOW', (0, 1), (-1, -1), 0.25, colors.grey),
|
|
|
|
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
|
|
|
]))
|
|
|
|
story.append(t)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
@login_required
|
|
|
|
@template('participant/settings.html')
|
|
|
|
def user_settings(request):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
|
|
|
Edit own user account.
|
|
|
|
"""
|
2011-07-31 10:46:29 +02:00
|
|
|
if request.method == 'POST':
|
2012-08-08 10:34:23 +02:00
|
|
|
form_user = UsersettingsForm(request.POST, instance=request.user)
|
2012-07-01 16:56:01 +02:00
|
|
|
if form_user.is_valid():
|
2011-07-31 10:46:29 +02:00
|
|
|
form_user.save()
|
|
|
|
messages.success(request, _('User settings successfully saved.'))
|
|
|
|
else:
|
|
|
|
messages.error(request, _('Please check the form for errors.'))
|
|
|
|
else:
|
2012-07-01 16:56:01 +02:00
|
|
|
form_user = UsersettingsForm(instance=request.user)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
'form_user': form_user,
|
|
|
|
'edituser': request.user,
|
|
|
|
}
|
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-07-11 09:46:15 +02:00
|
|
|
@login_required
|
|
|
|
@template('participant/password_change.html')
|
|
|
|
def user_settings_password(request):
|
|
|
|
"""
|
|
|
|
Edit own password.
|
|
|
|
"""
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = PasswordChangeForm(request.user, request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
messages.success(request, _('Password successfully changed.'))
|
|
|
|
return redirect(reverse('user_settings'))
|
|
|
|
else:
|
|
|
|
messages.error(request, _('Please check the form for errors.'))
|
|
|
|
else:
|
|
|
|
form = PasswordChangeForm(user=request.user)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'form': form,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-04 12:21:58 +02:00
|
|
|
@permission_required('participant.can_manage_participant')
|
2011-07-31 10:46:29 +02:00
|
|
|
@template('participant/import.html')
|
|
|
|
def user_import(request):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
|
|
|
Import Users via csv.
|
|
|
|
"""
|
|
|
|
from openslides.application.models import Application
|
2011-07-31 10:46:29 +02:00
|
|
|
try:
|
|
|
|
request.user.profile
|
2012-07-18 21:15:12 +02:00
|
|
|
messages.error(request, _('The import function is available for the admin (without user profile) only.'))
|
2011-09-09 10:55:33 +02:00
|
|
|
return redirect(reverse('user_overview'))
|
2011-07-31 10:46:29 +02:00
|
|
|
except Profile.DoesNotExist:
|
|
|
|
pass
|
2011-11-14 16:37:12 +01:00
|
|
|
except AttributeError:
|
|
|
|
# AnonymousUser
|
|
|
|
pass
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = UserImportForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
2011-11-22 18:49:36 +01:00
|
|
|
try:
|
2012-04-13 22:33:47 +02:00
|
|
|
# check for valid encoding (will raise UnicodeDecodeError if not)
|
|
|
|
request.FILES['csvfile'].read().decode('utf-8')
|
|
|
|
request.FILES['csvfile'].seek(0)
|
|
|
|
|
2011-11-22 18:49:36 +01:00
|
|
|
with transaction.commit_on_success():
|
2012-04-13 11:35:53 +02:00
|
|
|
|
2012-04-12 19:11:07 +02:00
|
|
|
old_users = {}
|
|
|
|
applications_mapped = 0
|
|
|
|
applications_review = 0
|
|
|
|
applications_removed = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
janitor = User.objects.get(username='__system__.janitor')
|
|
|
|
except User.DoesNotExist:
|
|
|
|
janitor = User()
|
|
|
|
janitor.first_name = ''
|
|
|
|
janitor.last_name = ''
|
|
|
|
janitor.username = '__system__.janitor'
|
|
|
|
janitor.save()
|
|
|
|
|
|
|
|
applications = Application.objects.all()
|
|
|
|
for application in applications:
|
|
|
|
if form.cleaned_data['application_handling'] == 'DISCARD':
|
|
|
|
# need to do this explicit since some applications may belong
|
|
|
|
# to __system__.janitor which is a permanent user
|
|
|
|
application.delete(force=True)
|
|
|
|
applications_removed += 1
|
|
|
|
else:
|
|
|
|
# collect all applications and map them to their submitters
|
|
|
|
submitter = application.submitter
|
|
|
|
skey = '%s_%s' % (submitter.first_name, submitter.last_name)
|
|
|
|
|
|
|
|
if not skey in old_users:
|
|
|
|
old_users[skey] = []
|
|
|
|
old_users[skey].append(application.id)
|
|
|
|
|
|
|
|
application.submitter = janitor
|
|
|
|
application.save()
|
|
|
|
|
|
|
|
if application.supporter.all():
|
|
|
|
application.writelog(_('Supporters removed after user import.'), user=request.user)
|
|
|
|
|
2011-11-22 18:49:36 +01:00
|
|
|
profiles = Profile.objects.all()
|
|
|
|
for profile in profiles:
|
|
|
|
profile.user.delete()
|
|
|
|
profile.delete()
|
|
|
|
i = -1
|
|
|
|
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
|
2012-07-07 15:26:00 +02:00
|
|
|
dialect = csv_ext.patchup(dialect)
|
2011-11-22 18:49:36 +01:00
|
|
|
request.FILES['csvfile'].seek(0)
|
2012-04-13 22:33:47 +02:00
|
|
|
|
2012-04-13 22:02:14 +02:00
|
|
|
for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)):
|
2011-11-22 18:49:36 +01:00
|
|
|
i += 1
|
|
|
|
if i > 0:
|
2012-04-13 22:02:14 +02:00
|
|
|
try:
|
|
|
|
(first_name, last_name, gender, group, type, committee, comment) = line[:7]
|
|
|
|
except ValueError:
|
|
|
|
messages.error(request, _('Ignoring malformed line %d in import file.') % (lno + 1))
|
|
|
|
i -= 1
|
|
|
|
continue
|
2011-11-22 18:49:36 +01:00
|
|
|
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
|
2012-04-02 08:36:22 +02:00
|
|
|
profile.comment = comment
|
2011-11-22 18:49:36 +01:00
|
|
|
profile.firstpassword = gen_password()
|
|
|
|
profile.user.set_password(profile.firstpassword)
|
2012-07-12 14:37:01 +02:00
|
|
|
profile.user.save()
|
2011-11-22 18:49:36 +01:00
|
|
|
profile.save()
|
|
|
|
|
|
|
|
if type == 'delegate':
|
2012-05-17 22:03:48 +02:00
|
|
|
delegate = Group.objects.get(name='Delegierter')
|
2011-11-22 18:49:36 +01:00
|
|
|
user.groups.add(delegate)
|
|
|
|
else:
|
|
|
|
observer = Group.objects.get(name='Beobachter')
|
|
|
|
user.groups.add(observer)
|
|
|
|
|
2012-04-12 19:11:07 +02:00
|
|
|
if form.cleaned_data['application_handling'] == 'REASSIGN':
|
|
|
|
# live remap
|
|
|
|
skey = '%s_%s' % (user.first_name, user.last_name)
|
|
|
|
if skey in old_users:
|
|
|
|
for appid in old_users[skey]:
|
|
|
|
try:
|
|
|
|
application = Application.objects.get(id=appid)
|
|
|
|
application.submitter = user
|
|
|
|
application.save()
|
|
|
|
application.writelog(_('Reassigned to "%s" after (re)importing users.') % ("%s %s" % (user.first_name, user.last_name)), user=request.user)
|
|
|
|
applications_mapped += 1
|
|
|
|
except Application.DoesNotExist:
|
|
|
|
messages.error(request, _('Could not reassing application %d - object not found!') % appid)
|
|
|
|
del old_users[skey]
|
|
|
|
|
|
|
|
if old_users:
|
|
|
|
# mark all applications without a valid user as 'needs review'
|
|
|
|
# this will account for *all* applications if application_mode == 'INREVIEW'
|
|
|
|
for skey in old_users:
|
|
|
|
for appid in old_users[skey]:
|
|
|
|
try:
|
|
|
|
application = Application.objects.get(id=appid)
|
|
|
|
if application.status != 'rev':
|
|
|
|
application.set_status(user=request.user, status='rev', force=True)
|
|
|
|
applications_review += 1
|
|
|
|
except Application.DoesNotExist:
|
|
|
|
messages.error(request, _('Could not reassing application %d - object not found!') % appid)
|
|
|
|
|
|
|
|
if applications_review:
|
|
|
|
messages.warning(request, ungettext('%d application could not be reassigned and needs a review!',
|
|
|
|
'%d applications could not be reassigned and need a review!', applications_review) % applications_review)
|
|
|
|
if applications_mapped:
|
|
|
|
messages.success(request, ungettext('%d application was successfully reassigned.',
|
|
|
|
'%d applications were successfully reassigned.', applications_mapped) % applications_mapped)
|
|
|
|
if applications_removed:
|
|
|
|
messages.warning(request, ungettext('%d application was discarded.',
|
|
|
|
'%d applications were discarded.', applications_removed) % applications_removed)
|
|
|
|
|
2012-04-13 22:02:14 +02:00
|
|
|
if i > 0:
|
|
|
|
messages.success(request, _('%d new participants were successfully imported.') % i)
|
2012-04-02 08:36:22 +02:00
|
|
|
return redirect(reverse('user_overview'))
|
2011-11-22 18:49:36 +01:00
|
|
|
except csv.Error:
|
|
|
|
message.error(request, _('Import aborted because of severe errors in the input file.'))
|
2012-04-13 22:33:47 +02:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
messages.error(request, _('Import file has wrong character encoding, only UTF-8 is supported!'))
|
2011-07-31 10:46:29 +02:00
|
|
|
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."))
|
2012-04-12 19:11:07 +02:00
|
|
|
if Application.objects.all():
|
|
|
|
messages.warning(request, _("Attention: Supporters from all existing applications will be removed."))
|
|
|
|
messages.warning(request, _("Attention: Applications which can't be mapped to new users will be set to 'Needs Review'."))
|
2011-07-31 10:46:29 +02:00
|
|
|
form = UserImportForm()
|
|
|
|
return {
|
|
|
|
'form': form,
|
2011-09-03 17:17:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-04 12:21:58 +02:00
|
|
|
@permission_required('participant.can_manage_participant')
|
2011-09-03 17:17:29 +02:00
|
|
|
def reset_password(request, user_id):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
|
|
|
Reset the Password.
|
|
|
|
"""
|
2011-09-03 17:17:29 +02:00
|
|
|
user = User.objects.get(pk=user_id)
|
|
|
|
if request.method == 'POST':
|
|
|
|
user.profile.reset_password()
|
2012-07-07 15:26:00 +02:00
|
|
|
messages.success(request,
|
|
|
|
_('The Password for <b>%s</b> was successfully reset.') % user)
|
2011-09-03 17:17:29 +02:00
|
|
|
else:
|
2012-07-07 15:26:00 +02:00
|
|
|
gen_confirm_form(request,
|
|
|
|
_('Do you really want to reset the password for <b>%s</b>?') % user,
|
|
|
|
reverse('user_reset_password', args=[user_id]))
|
2011-09-03 17:17:29 +02:00
|
|
|
return redirect(reverse('user_edit', args=[user_id]))
|
2012-03-18 17:11:58 +01:00
|
|
|
|
|
|
|
|
2012-07-18 10:46:07 +02:00
|
|
|
def login(request):
|
2012-07-19 11:43:48 +02:00
|
|
|
extra_content = {}
|
2012-07-18 10:46:07 +02:00
|
|
|
try:
|
|
|
|
admin = User.objects.get(pk=1)
|
|
|
|
if admin.check_password(config['admin_password']):
|
2012-07-19 12:27:45 +02:00
|
|
|
extra_content['first_time_message'] = _(
|
2012-07-19 11:43:48 +02:00
|
|
|
"Installation was successfully! Use %(user)s "
|
|
|
|
"(password: %(password)s) for first login.<br>"
|
|
|
|
"<strong>Important:</strong> Please change the password after "
|
|
|
|
"first login! Otherwise this message still appears for everyone "
|
|
|
|
"and could be a security risk.") % {
|
2012-07-18 10:46:07 +02:00
|
|
|
'user': html_strong(admin.username),
|
|
|
|
'password': html_strong(config['admin_password'])}
|
2012-07-19 11:43:48 +02:00
|
|
|
extra_content['next'] = reverse('password_change')
|
2012-07-18 10:46:07 +02:00
|
|
|
except User.DoesNotExist:
|
2012-07-19 11:43:48 +02:00
|
|
|
pass
|
|
|
|
return django_login(request, template_name='participant/login.html', extra_context=extra_content)
|
2012-07-18 10:46:07 +02:00
|
|
|
|
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
@permission_required('participant.can_manage_participant')
|
|
|
|
@template('participant/group_overview.html')
|
|
|
|
def get_group_overview(request):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
Show all groups.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
if config['system_enable_anonymous']:
|
|
|
|
groups = Group.objects.all()
|
|
|
|
else:
|
|
|
|
groups = Group.objects.exclude(name='Anonymous')
|
|
|
|
return {
|
|
|
|
'groups': groups,
|
|
|
|
}
|
2012-04-15 12:39:28 +02:00
|
|
|
|
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
@permission_required('participant.can_manage_participant')
|
|
|
|
@template('participant/group_edit.html')
|
|
|
|
def group_edit(request, group_id=None):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
Edit a group.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
if group_id is not None:
|
|
|
|
try:
|
|
|
|
group = Group.objects.get(id=group_id)
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
# TODO: return a 404 Object
|
|
|
|
raise NameError("There is no group %d" % group_id)
|
|
|
|
else:
|
|
|
|
group = None
|
|
|
|
delete_default_permissions()
|
2012-04-20 23:23:50 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = GroupForm(request.POST, instance=group)
|
|
|
|
if form.is_valid():
|
|
|
|
# TODO: This can be done inside the form
|
|
|
|
group_name = form.cleaned_data['name'].lower()
|
|
|
|
|
|
|
|
# TODO: Why is this code called on any request and not only, if the
|
|
|
|
# anonymous_group is edited?
|
2012-04-20 23:23:50 +02:00
|
|
|
try:
|
2012-08-10 19:19:41 +02:00
|
|
|
anonymous_group = Group.objects.get(name='Anonymous')
|
|
|
|
except Group.DoesNotExist:
|
|
|
|
anonymous_group = None
|
2012-04-20 23:23:50 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
# special handling for anonymous auth
|
|
|
|
# TODO: This code should be a form validator.
|
|
|
|
if group is None and group_name.strip().lower() == 'anonymous':
|
|
|
|
# don't allow to create this group
|
|
|
|
messages.error(request,
|
|
|
|
_('Group name "%s" is reserved for internal use.')
|
|
|
|
% group_name)
|
|
|
|
return {
|
|
|
|
'form': form,
|
|
|
|
'group': group
|
|
|
|
}
|
2012-04-20 23:23:50 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
group = form.save()
|
|
|
|
try:
|
|
|
|
openslides_group = OpenSlidesGroup.objects.get(group=group)
|
|
|
|
except OpenSlidesGroup.DoesNotExist:
|
|
|
|
django_group = None
|
|
|
|
if form.cleaned_data['as_user'] and django_group is None:
|
|
|
|
OpenSlidesGroup(group=group).save()
|
|
|
|
elif not form.cleaned_data['as_user'] and django_group:
|
|
|
|
django_group.delete()
|
2012-04-27 22:47:41 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
if anonymous_group is not None and \
|
|
|
|
anonymous_group.id == group.id:
|
|
|
|
# prevent name changes -
|
|
|
|
# XXX: I'm sure this could be done as *one* group.save()
|
|
|
|
group.name = 'Anonymous'
|
|
|
|
group.save()
|
2012-04-29 18:58:51 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
if group_id is None:
|
|
|
|
messages.success(request,
|
|
|
|
_('New group was successfully created.'))
|
|
|
|
else:
|
|
|
|
messages.success(request, _('Group was successfully modified.'))
|
|
|
|
if not 'apply' in request.POST:
|
|
|
|
return redirect(reverse('user_group_overview'))
|
|
|
|
if group_id is None:
|
|
|
|
return redirect(reverse('user_group_edit', args=[group.id]))
|
|
|
|
else:
|
|
|
|
messages.error(request, _('Please check the form for errors.'))
|
|
|
|
else:
|
|
|
|
if group and OpenSlidesGroup.objects.filter(group=group).exists():
|
|
|
|
initial = {'as_user': True}
|
|
|
|
else:
|
|
|
|
initial = {'as_user': False}
|
2012-04-29 18:58:51 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
form = GroupForm(instance=group, initial=initial)
|
|
|
|
return {
|
|
|
|
'form': form,
|
|
|
|
'group': group,
|
|
|
|
}
|
2012-04-27 22:47:41 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
|
|
|
|
@permission_required('participant.can_manage_participant')
|
|
|
|
def group_delete(request, group_id):
|
|
|
|
"""
|
|
|
|
Delete a group.
|
|
|
|
"""
|
|
|
|
group = Group.objects.get(pk=group_id)
|
|
|
|
if request.method == 'POST':
|
|
|
|
group.delete()
|
|
|
|
messages.success(request,
|
|
|
|
_('Group <b>%s</b> was successfully deleted.') % group)
|
|
|
|
else:
|
|
|
|
gen_confirm_form(request,
|
|
|
|
_('Do you really want to delete <b>%s</b>?') % group,
|
|
|
|
reverse('user_group_delete', args=[group_id]))
|
|
|
|
return redirect(reverse('user_group_overview'))
|
2012-04-20 23:23:50 +02:00
|
|
|
|
|
|
|
|
2012-04-15 12:39:28 +02:00
|
|
|
class Config(FormView):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
|
|
|
Config page for the participant app.
|
|
|
|
"""
|
2012-04-15 12:39:28 +02:00
|
|
|
permission_required = 'config.can_manage_config'
|
|
|
|
form_class = ConfigForm
|
|
|
|
template_name = 'participant/config.html'
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
return {
|
|
|
|
'participant_pdf_system_url': config['participant_pdf_system_url'],
|
2012-07-07 15:26:00 +02:00
|
|
|
'participant_pdf_welcometext': config['participant_pdf_welcometext']
|
2012-04-15 12:39:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2012-07-07 15:26:00 +02:00
|
|
|
config['participant_pdf_system_url'] = \
|
|
|
|
form.cleaned_data['participant_pdf_system_url']
|
|
|
|
config['participant_pdf_welcometext'] = \
|
|
|
|
form.cleaned_data['participant_pdf_welcometext']
|
|
|
|
messages.success(self.request,
|
|
|
|
_('Participants settings successfully saved.'))
|
2012-04-18 16:57:33 +02:00
|
|
|
return super(Config, self).form_valid(form)
|
2012-08-10 19:19:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def register_tab(request):
|
|
|
|
"""
|
|
|
|
Register the participant tab.
|
|
|
|
"""
|
|
|
|
selected = request.path.startswith('/participant/')
|
|
|
|
return Tab(
|
|
|
|
title=_('Participants'),
|
|
|
|
url=reverse('user_overview'),
|
|
|
|
permission=request.user.has_perm('participant.can_see_participant')
|
|
|
|
or request.user.has_perm('participant.can_manage_participant'),
|
|
|
|
selected=selected,
|
|
|
|
)
|