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-06-14 21:58:04 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2016-06-19 23:00:08 +00:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2016-06-27 22:31:24 +00:00
|
|
|
from django.views.generic import ListView
|
2016-06-07 20:59:13 +00:00
|
|
|
|
2016-06-25 16:58:19 +00:00
|
|
|
from registration.backends.default.views import RegistrationView
|
|
|
|
|
2016-06-25 15:38:01 +00:00
|
|
|
from .forms import ParticipationForm, ProfileForm, ProfileOrgaForm, UserForm
|
2016-06-27 22:31:24 +00:00
|
|
|
from .mixins import StaffRequiredMixin
|
2016-06-25 15:38:01 +00:00
|
|
|
from .models import Participation, Profile
|
|
|
|
from .utils import can_edit_profile
|
2016-06-11 13:42:40 +00:00
|
|
|
|
2016-06-25 16:58:19 +00:00
|
|
|
RESET_PASSWORD_BUTTON = ('password_reset', 'warning', 'Reset your password')
|
|
|
|
CHANGE_PASSWORD_BUTTON = ('password_change', 'warning', 'Change password')
|
|
|
|
|
|
|
|
|
|
|
|
class Registration(RegistrationView):
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
return super().get_context_data(buttons=[RESET_PASSWORD_BUTTON], **kwargs)
|
|
|
|
|
2016-06-11 13:42:40 +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-06-25 16:58:19 +00:00
|
|
|
ParticipationForm(request.POST or None, instance=Participation.on_site.get(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-06-27 22:31:24 +00:00
|
|
|
class ParticipantList(StaffRequiredMixin, ListView):
|
|
|
|
queryset = Participation.on_site.all()
|
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()
|
|
|
|
|
|
|
|
form = ProfileOrgaForm(request.POST or None, instance=profile)
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
messages.success(request, 'Profile updated successfully.')
|
|
|
|
else:
|
|
|
|
messages.error(request, 'Please correct those errors.')
|
|
|
|
|
|
|
|
return render(request, 'accounts/edit_profile.html', {'form': form, 'profile': profile})
|