1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-11-27 02:13:07 +00:00
med/users/views.py

82 lines
2.7 KiB
Python
Raw Normal View History

2019-08-02 12:57:53 +00:00
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
2019-08-19 10:17:49 +00:00
from django.contrib.auth.models import Group
from django.db import transaction
2019-08-09 21:12:56 +00:00
from django.shortcuts import redirect, render
2019-08-02 12:57:53 +00:00
from django.template.context_processors import csrf
2019-08-08 19:51:37 +00:00
from django.utils.translation import ugettext_lazy as _
2019-08-17 10:12:10 +00:00
from rest_framework import viewsets
from reversion import revisions as reversion
2019-08-08 14:18:15 +00:00
from users.forms import BaseInfoForm
2019-08-09 21:12:56 +00:00
from users.models import Adhesion, User
2019-08-19 10:17:49 +00:00
from .serializers import GroupSerializer, UserSerializer
def form(ctx, template, request):
c = ctx
c.update(csrf(request))
return render(request, template, c)
2019-08-02 12:57:53 +00:00
@login_required
2019-08-08 14:18:15 +00:00
def edit_info(request):
2019-08-08 13:35:25 +00:00
"""
2019-08-08 14:18:15 +00:00
Edite son utilisateur
2019-08-08 13:35:25 +00:00
"""
2019-08-08 14:18:15 +00:00
user = BaseInfoForm(request.POST or None, instance=request.user)
if user.is_valid():
with transaction.atomic(), reversion.create_revision():
user.save()
reversion.set_user(request.user)
2019-08-08 13:35:25 +00:00
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in user.changed_data))
messages.success(request, "L'user a bien été modifié")
2019-08-10 08:44:17 +00:00
return redirect("index")
2019-08-08 19:51:37 +00:00
return form({
'form': user,
'password_change': True,
'title': _('Edit user profile'),
}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@login_required
2019-08-08 14:18:15 +00:00
@permission_required('users.add_adhesion')
def adherer(request, userid):
try:
users = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
2019-08-10 08:44:17 +00:00
return redirect("admin:users_user_changelist")
2019-08-10 14:13:32 +00:00
adh_year = Adhesion.objects.all().order_by('starting_in').reverse().first()
2019-08-10 08:44:17 +00:00
if not adh_year:
messages.error(request, "Année d'adhésion non définie")
return redirect("admin:users_user_changelist")
with transaction.atomic(), reversion.create_revision():
reversion.set_user(request.user)
2019-08-10 14:22:04 +00:00
adh_year.members.add(users)
2019-08-08 13:35:25 +00:00
adh_year.save()
reversion.set_comment("Adhesion de %s" % users)
messages.success(request, "Adhesion effectuee")
2019-08-10 08:44:17 +00:00
return redirect("admin:users_user_changelist")
2019-08-17 10:12:10 +00:00
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all()
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer