diff --git a/annuaire/urls.py b/annuaire/urls.py index 8fb9c48..a26909d 100644 --- a/annuaire/urls.py +++ b/annuaire/urls.py @@ -17,14 +17,14 @@ from django.conf import settings from django.contrib import admin from django.urls import path, include from django.conf.urls.static import static -from fiches.views import home, birthday +from fiches.views import BirthdayView, HomeView import django_cas_ng.views as cas_views urlpatterns = [ path('admin/', admin.site.urls), path('fiche/', include('fiches.urls')), - path('', home, name='home'), - path('birthday', birthday, name='birthday'), + path('', HomeView.as_view(), name='home'), + path('birthday', BirthdayView.as_view(), name='birthday'), path('accounts/login/', cas_views.LoginView.as_view(), name='cas_ng_login'), path('logout', cas_views.LogoutView.as_view(), name='cas_ng_logout'), ] diff --git a/fiches/forms.py b/fiches/forms.py index 8c10468..6273c99 100644 --- a/fiches/forms.py +++ b/fiches/forms.py @@ -1,23 +1,11 @@ from django import forms -from fiches.models import Profile, Department +from fiches.models import Profile, Department, Phone, Social, Mail, Address class ProfileForm(forms.ModelForm): class Meta: model = Profile - fields = [ - "full_name", - "nickname", - "pronoun", - "picture", - "department", - "promotion", - "birth_date", - "thurne", - "text_field", - "printing", - "keep_me", - ] + exclude = ["user"] class SearchForm(forms.Form): @@ -35,3 +23,29 @@ class SearchForm(forms.Form): and not cleaned_data["department"] ): raise forms.ValidationError(("Tous les champs sont vides"), code="invalid") + + +class PhoneForm(forms.BaseModelFormSet): + class Meta: + model = Phone + exclude = [] + + + +class SocialForm(forms.ModelForm): + class Meta: + model = Social + exclude = [] + + +class MailForm(forms.ModelForm): + class Meta: + model = Mail + exclude = [] + + +class AddressForm(forms.ModelForm): + class Meta: + model = Address + exclude = [] + diff --git a/fiches/models.py b/fiches/models.py index 3dbc3cf..26b367e 100644 --- a/fiches/models.py +++ b/fiches/models.py @@ -49,7 +49,7 @@ class Department(models.Model): class Phone(models.Model): profile = models.ForeignKey( - Profile, on_delete=models.CASCADE, verbose_name=_("profil") + Profile, related_name="numeros", on_delete=models.CASCADE, verbose_name=_("profil") ) name = models.CharField(max_length=255, verbose_name=_("type")) number = models.CharField(max_length=1023, verbose_name=_("numéro")) diff --git a/fiches/templates/fiches/fiches_modif.html b/fiches/templates/fiches/fiches_modif.html index 5de135b..ecbfdb9 100644 --- a/fiches/templates/fiches/fiches_modif.html +++ b/fiches/templates/fiches/fiches_modif.html @@ -6,6 +6,7 @@

{% trans "Modifier ma page d'annuaire" %}

+ {{ form.errors }}
{% csrf_token %}
diff --git a/fiches/urls.py b/fiches/urls.py index 4058767..df6d18c 100644 --- a/fiches/urls.py +++ b/fiches/urls.py @@ -2,6 +2,6 @@ from django.urls import path from . import views urlpatterns = [ - path("edit", views.fiche_modif, name="fiche_modif"), - path("", views.fiche, name="fiche"), + path("edit", views.EditView.as_view(), name="fiche_modif"), + path("", views.FicheView.as_view(), name="fiche"), ] diff --git a/fiches/views.py b/fiches/views.py index 49eb34a..0911b26 100644 --- a/fiches/views.py +++ b/fiches/views.py @@ -2,19 +2,26 @@ from django.shortcuts import render from django.shortcuts import get_object_or_404, redirect from django.contrib.auth.decorators import login_required from fiches.models import Profile -from fiches.forms import ProfileForm, SearchForm +from fiches.forms import ProfileForm, SearchForm, PhoneForm from django.urls import reverse from django.db.models import Q from django.utils import timezone +from django.utils.decorators import method_decorator from datetime import timedelta from django.core.mail import send_mail from django.template.loader import render_to_string +from django.views.generic.detail import DetailView +from django.views.generic.list import ListView +from django.views.generic.edit import FormView, UpdateView -@login_required -def fiche(request, user): - profile = get_object_or_404(Profile, user__username=user) - return render(request, "fiches/fiche.html", {"profile": profile}) +@method_decorator(login_required, name="dispatch") +class FicheView(DetailView): + model = Profile + template_name = "fiches/fiche.html" + + def get_object(self): + return get_object_or_404(Profile, user__username=self.kwargs.get("user")) @login_required @@ -22,6 +29,7 @@ def fiche_modif(request): profile = request.user.profile if request.method == "POST": form = ProfileForm(request.POST, request.FILES, instance=profile) + #form_phone = PhoneForm(request.POST, instance=) if form.is_valid(): form.save() send_mail( @@ -39,33 +47,62 @@ def fiche_modif(request): return render(request, "fiches/fiches_modif.html", {"form": form}) -@login_required -def home(request): - if request.method == "POST": - form = SearchForm(request.POST) - if form.is_valid(): - result = Profile.objects.filter( +@method_decorator(login_required, name="dispatch") +class EditView(UpdateView): + model = Profile + template_name = "fiches/fiches_modif.html" + form_class = ProfileForm + + def get_object(self): + return self.request.user.profile + + def get_success_url(self): + return reverse("fiche", args=(self.get_object().user,)) + + def form_valid(self, form): + send_mail( + "Fiche annuaire modifée", + render_to_string("fiches/mail/mail_modif.txt", {"profile": self.get_object()}), + "klub-dev@ens.psl.eu", + ["{}@clipper.ens.psl.eu".format(self.get_object().user.username)], + fail_silently=False, + ) + return super().form_valid(form) + + + +@method_decorator(login_required, name="dispatch") +class HomeView(FormView): + model = Profile + template_name = "fiches/home.html" + form_class = SearchForm + + def form_valid(self, form): + result = Profile.objects.filter( Q(full_name__icontains=form.cleaned_data["name"]) | Q(nickname__icontains=form.cleaned_data["name"]) ) - return render(request, "fiches/home.html", {"form": form, "result": result}) - - else: - form = SearchForm() - return render(request, "fiches/home.html", {"form": form}) + return self.render_to_response(self.get_context_data(result=result)) -@login_required -def birthday(request): - today = timezone.now() - result = list( - Profile.objects.filter(birth_date__day=today.day, birth_date__month=today.month) - ) - for i in range(1, 7): - today = today + timedelta(days=1) - result += list( - Profile.objects.filter( - birth_date__day=today.day, birth_date__month=today.month - ) +@method_decorator(login_required, name="dispatch") +class BirthdayView(ListView): + model = Profile + template_name = "fiches/birthday.html" + + + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + today = timezone.now() + context['result'] = list( + Profile.objects.filter(birth_date__day=today.day, birth_date__month=today.month) ) - return render(request, "fiches/birthday.html", {"result": result}) + for i in range(1, 7): + today = today + timedelta(days=1) + context['result'] += list( + Profile.objects.filter( + birth_date__day=today.day, birth_date__month=today.month + ) + ) + return context \ No newline at end of file