changed views to class
This commit is contained in:
parent
6a9a47fcea
commit
1fa503cc4f
6 changed files with 101 additions and 49 deletions
|
@ -17,14 +17,14 @@ from django.conf import settings
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from django.conf.urls.static import static
|
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
|
import django_cas_ng.views as cas_views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('fiche/', include('fiches.urls')),
|
path('fiche/', include('fiches.urls')),
|
||||||
path('', home, name='home'),
|
path('', HomeView.as_view(), name='home'),
|
||||||
path('birthday', birthday, name='birthday'),
|
path('birthday', BirthdayView.as_view(), name='birthday'),
|
||||||
path('accounts/login/', cas_views.LoginView.as_view(), name='cas_ng_login'),
|
path('accounts/login/', cas_views.LoginView.as_view(), name='cas_ng_login'),
|
||||||
path('logout', cas_views.LogoutView.as_view(), name='cas_ng_logout'),
|
path('logout', cas_views.LogoutView.as_view(), name='cas_ng_logout'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,23 +1,11 @@
|
||||||
from django import forms
|
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 ProfileForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Profile
|
model = Profile
|
||||||
fields = [
|
exclude = ["user"]
|
||||||
"full_name",
|
|
||||||
"nickname",
|
|
||||||
"pronoun",
|
|
||||||
"picture",
|
|
||||||
"department",
|
|
||||||
"promotion",
|
|
||||||
"birth_date",
|
|
||||||
"thurne",
|
|
||||||
"text_field",
|
|
||||||
"printing",
|
|
||||||
"keep_me",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class SearchForm(forms.Form):
|
class SearchForm(forms.Form):
|
||||||
|
@ -35,3 +23,29 @@ class SearchForm(forms.Form):
|
||||||
and not cleaned_data["department"]
|
and not cleaned_data["department"]
|
||||||
):
|
):
|
||||||
raise forms.ValidationError(("Tous les champs sont vides"), code="invalid")
|
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 = []
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ class Department(models.Model):
|
||||||
|
|
||||||
class Phone(models.Model):
|
class Phone(models.Model):
|
||||||
profile = models.ForeignKey(
|
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"))
|
name = models.CharField(max_length=255, verbose_name=_("type"))
|
||||||
number = models.CharField(max_length=1023, verbose_name=_("numéro"))
|
number = models.CharField(max_length=1023, verbose_name=_("numéro"))
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
<div id="content-edit-profile" class="content">
|
<div id="content-edit-profile" class="content">
|
||||||
<h2>{% trans "Modifier ma page d'annuaire" %}</h2>
|
<h2>{% trans "Modifier ma page d'annuaire" %}</h2>
|
||||||
|
{{ form.errors }}
|
||||||
<form method="post" action="" enctype="multipart/form-data">
|
<form method="post" action="" enctype="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="form-entry">
|
<div class="form-entry">
|
||||||
|
|
|
@ -2,6 +2,6 @@ from django.urls import path
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("edit", views.fiche_modif, name="fiche_modif"),
|
path("edit", views.EditView.as_view(), name="fiche_modif"),
|
||||||
path("<user>", views.fiche, name="fiche"),
|
path("<user>", views.FicheView.as_view(), name="fiche"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,19 +2,26 @@ from django.shortcuts import render
|
||||||
from django.shortcuts import get_object_or_404, redirect
|
from django.shortcuts import get_object_or_404, redirect
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from fiches.models import Profile
|
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.urls import reverse
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.utils.decorators import method_decorator
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail
|
||||||
from django.template.loader import render_to_string
|
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
|
@method_decorator(login_required, name="dispatch")
|
||||||
def fiche(request, user):
|
class FicheView(DetailView):
|
||||||
profile = get_object_or_404(Profile, user__username=user)
|
model = Profile
|
||||||
return render(request, "fiches/fiche.html", {"profile": profile})
|
template_name = "fiches/fiche.html"
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
return get_object_or_404(Profile, user__username=self.kwargs.get("user"))
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -22,6 +29,7 @@ def fiche_modif(request):
|
||||||
profile = request.user.profile
|
profile = request.user.profile
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = ProfileForm(request.POST, request.FILES, instance=profile)
|
form = ProfileForm(request.POST, request.FILES, instance=profile)
|
||||||
|
#form_phone = PhoneForm(request.POST, instance=)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save()
|
||||||
send_mail(
|
send_mail(
|
||||||
|
@ -39,33 +47,62 @@ def fiche_modif(request):
|
||||||
return render(request, "fiches/fiches_modif.html", {"form": form})
|
return render(request, "fiches/fiches_modif.html", {"form": form})
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@method_decorator(login_required, name="dispatch")
|
||||||
def home(request):
|
class EditView(UpdateView):
|
||||||
if request.method == "POST":
|
model = Profile
|
||||||
form = SearchForm(request.POST)
|
template_name = "fiches/fiches_modif.html"
|
||||||
if form.is_valid():
|
form_class = ProfileForm
|
||||||
result = Profile.objects.filter(
|
|
||||||
|
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(full_name__icontains=form.cleaned_data["name"])
|
||||||
| Q(nickname__icontains=form.cleaned_data["name"])
|
| Q(nickname__icontains=form.cleaned_data["name"])
|
||||||
)
|
)
|
||||||
return render(request, "fiches/home.html", {"form": form, "result": result})
|
return self.render_to_response(self.get_context_data(result=result))
|
||||||
|
|
||||||
else:
|
|
||||||
form = SearchForm()
|
|
||||||
return render(request, "fiches/home.html", {"form": form})
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@method_decorator(login_required, name="dispatch")
|
||||||
def birthday(request):
|
class BirthdayView(ListView):
|
||||||
today = timezone.now()
|
model = Profile
|
||||||
result = list(
|
template_name = "fiches/birthday.html"
|
||||||
Profile.objects.filter(birth_date__day=today.day, birth_date__month=today.month)
|
|
||||||
)
|
|
||||||
for i in range(1, 7):
|
|
||||||
today = today + timedelta(days=1)
|
def get_context_data(self, **kwargs):
|
||||||
result += list(
|
context = super().get_context_data(**kwargs)
|
||||||
Profile.objects.filter(
|
today = timezone.now()
|
||||||
birth_date__day=today.day, birth_date__month=today.month
|
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
|
Loading…
Reference in a new issue