changed views to class

This commit is contained in:
jluiselli 2020-09-16 15:38:38 +02:00
parent 6a9a47fcea
commit 1fa503cc4f
6 changed files with 101 additions and 49 deletions

View file

@ -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'),
]

View file

@ -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 = []

View file

@ -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"))

View file

@ -6,6 +6,7 @@
<div id="content-edit-profile" class="content">
<h2>{% trans "Modifier ma page d'annuaire" %}</h2>
{{ form.errors }}
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-entry">

View file

@ -2,6 +2,6 @@ from django.urls import path
from . import views
urlpatterns = [
path("edit", views.fiche_modif, name="fiche_modif"),
path("<user>", views.fiche, name="fiche"),
path("edit", views.EditView.as_view(), name="fiche_modif"),
path("<user>", views.FicheView.as_view(), name="fiche"),
]

View file

@ -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():
@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):
@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()
result = list(
context['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(
context['result'] += list(
Profile.objects.filter(
birth_date__day=today.day, birth_date__month=today.month
)
)
return render(request, "fiches/birthday.html", {"result": result})
return context