2016-06-11 13:47:08 +00:00
|
|
|
from django.contrib import messages
|
2016-06-07 20:59:13 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2016-07-09 19:30:49 +00:00
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2016-07-09 22:22:10 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2016-07-11 19:33:34 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
2016-07-23 12:17:11 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-06-07 20:59:13 +00:00
|
|
|
|
2016-07-09 22:15:08 +00:00
|
|
|
from .decorators import staff_required
|
2016-07-11 19:33:34 +00:00
|
|
|
from .forms import (NewParticipationForm, ParticipationForm,
|
|
|
|
ParticipationOrgaForm, ProfileForm, ProfileOrgaForm, UserForm)
|
|
|
|
from .models import Participation, Profile, User
|
2016-07-04 23:16:33 +00:00
|
|
|
from .utils import can_edit_profile, is_orga
|
2016-06-11 13:42:40 +00:00
|
|
|
|
2016-07-23 12:17:11 +00:00
|
|
|
RESET_PASSWORD_BUTTON = ('password_reset', 'warning', _('Reset your password'))
|
|
|
|
CHANGE_PASSWORD_BUTTON = ('password_change', 'warning', _('Change password'))
|
2016-07-23 15:20:57 +00:00
|
|
|
CHANGE_AVATAR_BUTTON = ('avatar_change', 'default', _('Change avatar'))
|
2016-06-25 16:58:19 +00:00
|
|
|
|
|
|
|
|
2016-06-07 20:59:13 +00:00
|
|
|
@login_required
|
|
|
|
def profile(request):
|
2016-06-11 13:42:40 +00:00
|
|
|
|
2016-06-12 15:11:11 +00:00
|
|
|
forms = [UserForm(request.POST or None, instance=request.user),
|
2016-06-25 10:33:04 +00:00
|
|
|
ProfileForm(request.POST or None, instance=request.user.profile),
|
2016-07-11 19:33:34 +00:00
|
|
|
ParticipationForm(request.POST or None, instance=Participation.objects.get(site=get_current_site(request),
|
|
|
|
user=request.user))]
|
2016-06-07 20:59:13 +00:00
|
|
|
|
|
|
|
if request.method == 'POST':
|
2016-06-11 13:42:40 +00:00
|
|
|
if all(form.is_valid() for form in forms):
|
|
|
|
for form in forms:
|
|
|
|
form.save()
|
|
|
|
messages.success(request, 'Profile updated successfully.')
|
|
|
|
else:
|
|
|
|
messages.error(request, 'Please correct those errors.')
|
|
|
|
|
2016-06-25 16:58:19 +00:00
|
|
|
return render(request, 'accounts/profile.html', {'forms': forms, 'buttons': [CHANGE_PASSWORD_BUTTON]})
|
2016-06-14 21:58:04 +00:00
|
|
|
|
|
|
|
|
2016-07-09 22:15:08 +00:00
|
|
|
@staff_required
|
|
|
|
def participation_list(request):
|
|
|
|
participation_list = Participation.objects.filter(site=get_current_site(request)).all()
|
|
|
|
form = NewParticipationForm(request.POST or None)
|
|
|
|
|
|
|
|
if request.method == 'POST' and form.is_valid():
|
2016-07-09 22:22:10 +00:00
|
|
|
if not Participation.objects.get(user=request.user, site=get_current_site(request)).is_orga():
|
|
|
|
raise PermissionDenied()
|
2016-07-09 22:15:08 +00:00
|
|
|
user = User.objects.get(username=form.cleaned_data['participant'])
|
|
|
|
participation, created = Participation.objects.get_or_create(user=user, site=get_current_site(request))
|
|
|
|
if created:
|
|
|
|
messages.success(request, "%s added to participant" % user.profile)
|
|
|
|
else:
|
|
|
|
messages.info(request, "%s is already a participant" % user.profile)
|
|
|
|
return redirect(reverse('list-participant'))
|
|
|
|
|
|
|
|
return render(request, 'accounts/participation_list.html', {
|
|
|
|
'participation_list': participation_list,
|
|
|
|
'form': form,
|
|
|
|
})
|
2016-06-19 23:00:08 +00:00
|
|
|
|
|
|
|
|
2016-06-25 15:38:01 +00:00
|
|
|
@login_required
|
|
|
|
def edit(request, username):
|
|
|
|
|
|
|
|
profile = get_object_or_404(Profile, user__username=username)
|
|
|
|
if not can_edit_profile(request, profile):
|
|
|
|
raise PermissionDenied()
|
|
|
|
|
2016-07-11 19:33:34 +00:00
|
|
|
participation_form = ParticipationOrgaForm if is_orga(request, request.user) else ParticipationForm
|
2016-07-04 23:16:33 +00:00
|
|
|
forms = [UserForm(request.POST or None, instance=profile.user),
|
|
|
|
ProfileOrgaForm(request.POST or None, instance=profile),
|
2016-07-11 19:33:34 +00:00
|
|
|
participation_form(request.POST or None,
|
|
|
|
instance=Participation.objects.get(site=get_current_site(request), user=profile.user))]
|
2016-06-25 15:38:01 +00:00
|
|
|
|
|
|
|
if request.method == 'POST':
|
2016-07-04 23:16:33 +00:00
|
|
|
if all(form.is_valid() for form in forms):
|
|
|
|
for form in forms:
|
|
|
|
form.save()
|
2016-06-25 15:38:01 +00:00
|
|
|
messages.success(request, 'Profile updated successfully.')
|
|
|
|
else:
|
|
|
|
messages.error(request, 'Please correct those errors.')
|
|
|
|
|
2016-07-04 23:16:33 +00:00
|
|
|
return render(request, 'accounts/edit_profile.html', {'forms': forms, 'profile': profile})
|