Commits

Anonymous committed dc06c4c

Une vue détails générique, un formulaire factorisé, des urls DRY

  • Participants
  • Parent commits fd6499c

Comments (0)

Files changed (21)

File personal_calendar/forms.py

 from django.forms import ModelForm
 from personal_calendar.models import Evenement
 from personal_calendar.models import Evenement_Participant
+from django.contrib.auth.models import User
+from django.forms import HiddenInput
+
 
 class EventForm(ModelForm):
     class Meta:
 class Evenement_ParticipantForm(ModelForm):
     class Meta:
         model = Evenement_Participant
-        
+    def __init__(self,*args, **kwargs):
+        super(Evenement_ParticipantForm, self).__init__(*args, **kwargs)
+        self.fields['evenement'].widget = HiddenInput()
+        if 'evenement' in self.initial:
+            participants = [user.pk for user in self.initial['evenement'].participants.all()]
+            self.fields['participant'].queryset=User.objects.exclude(
+                pk__in = participants)

File personal_calendar/models.py

 #-*-coding:utf-8 -*-
 from django.db import models
 from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
 #on rend disponible l'objet "user" pour le référencer dans les modèles
 
 
     lieu = models.TextField()
 
     def get_absolute_url(self):
-        return "/agenda/%s/details" % self.id
+        return reverse('details', kwargs={'pk':self.pk})
 
     def delete_url(self):
-        return "/agenda/%s/delete/" % self.id
+        return reverse('delete', kwargs={'pk':self.pk})
+
+    def __unicode__(self):
+        return self.nom
+
 
 class Evenement_Participant(models.Model):
     #notre table de relation
     def delete_url(self):
         return "/agenda/%i/participant/%i/delete/" % (self.evenement.id,
                                                       self.participant.id)
+
+
     class Meta:
         unique_together = ("evenement","participant")

File personal_calendar/static/css/bootstrap-responsive.css

File contents unchanged.

File personal_calendar/static/css/bootstrap-responsive.min.css

File contents unchanged.

File personal_calendar/static/css/bootstrap.css

File contents unchanged.

File personal_calendar/static/css/bootstrap.min.css

File contents unchanged.

File personal_calendar/static/img/glyphicons-halflings-white.png

Old
Old image
New
New image

File personal_calendar/static/img/glyphicons-halflings.png

Old
Old image
New
New image

File personal_calendar/static/js/bootstrap.js

File contents unchanged.

File personal_calendar/static/js/bootstrap.min.js

File contents unchanged.

File personal_calendar/static/js/datepicker-fr.js

File contents unchanged.

File personal_calendar/static/js/jquery.js

File contents unchanged.

File personal_calendar/templates/personal_calendar/blocks/delete_form.html

File contents unchanged.

File personal_calendar/templates/personal_calendar/blocks/event_detail.html

File contents unchanged.

File personal_calendar/templates/personal_calendar/blocks/pagination.html

File contents unchanged.

File personal_calendar/templates/personal_calendar/blocks/participant_form.html

 <form action="" method="POST" id="participant_form">
 {% csrf_token  %}
 {{form.as_p}}
-<input type="submit" value="Créer" />
+<input type="submit" value="Ajouter un participant" />
 </form>

File personal_calendar/templates/personal_calendar/evenement_detail.html

+{% extends "base.html" %}
+
+{% block titre %}
+    | {{object.nom}}
+{% endblock titre %} 
+
+{% block content %}
+<h1>{{object.nom}} (<a href="/agenda/{{object.id}}/update/">Modifier</a>)</h1>
+<p>{{object.description}}</p>
+<p>le {{object.date}}</p>
+<p>à {{object.lieu}}</p> 
+<div id="participants">
+{% for participant in object.evenement_participant_set.all %}
+<div class="well">
+{{participant.participant}}: {{participant.get_status_display}}
+{% with delete_url=participant.delete_url %}
+{% include "personal_calendar/blocks/delete_form.html" %}
+{% endwith %}
+</div>
+{% endfor %}
+</div>
+{% include "personal_calendar/blocks/participant_form.html" %}
+{% with delete_url=object.delete_url %}
+{% include "personal_calendar/blocks/delete_form.html" %}
+{% endwith %}
+{% endblock content %}

File personal_calendar/templates/personal_calendar/evenement_list.html

File contents unchanged.

File personal_calendar/urls.py

 from django.conf.urls.defaults import patterns, include, url
-from views import create, delete, details, update, delete_participant, Evenement_Liste
-from models import Evenement
+from views import create, delete, details, update, delete_participant, Evenement_Liste, Evenement_Detail
 
 urlpatterns = patterns('',
     url(r'^create/$', create),
-    url(r'^(\d+)/details/$', details),
-    url(r'^listes/$', Evenement_Liste.as_view(paginate_by=1)),
+    url(r'^(?P<pk>\d+)/detail/$', Evenement_Detail.as_view(), name='details'),
+    url(r'^liste/$', Evenement_Liste.as_view(paginate_by=1)),
     url(r'^liste/(?P<champ>[\w-]+)/(?P<terme>[\w-]+)/$',Evenement_Liste.as_view(
             paginate_by=10
             )

File personal_calendar/views.py

 from django.template.loader import render_to_string
 from django.template import RequestContext
 from django.views.generic.list import ListView
+from django.views.generic.detail import DetailView
 import datetime
 
 def create(request):
         form = EventForm()
     return render(request,'personal_calendar/event/create.html',{'form':form})
 
+class Evenement_Detail(DetailView):
+    model = Evenement
+
+    def get_context_data(self, **kwargs):
+        context = super(Evenement_Detail, self).get_context_data(**kwargs)
+        form = Evenement_ParticipantForm(initial = {'evenement':self.object})
+        context['form'] = form
+        return context
+
+    def post(self, request, *args, **kwargs):
+        form = Evenement_ParticipantForm(self.request.POST)
+        if form.is_valid():
+            form.save()
+            if self.request.is_ajax():
+                delete_form = render_to_string(
+                    "personal_calendar/blocks/delete_form.html",
+                    {'delete_url': form.instance.delete_url(),
+                     },
+                    RequestContext(request)
+                    )
+                data = {'participant': form.instance.participant.username,
+                        'get_status_display': form.instance.get_status_display(),
+                        'delete_form': delete_form}
+                return HttpResponse(
+                    json.dumps(data),
+                    mimetype="application/json"
+                    )
+            else:
+                # Pas d'ajax
+                return HttpResponseRedirect('/agenda/%s/detail/' % id)
+        else:
+            if request.is_ajax():
+                return render(
+                    request, 'personal_calendar/blocks/participant_form.html',
+                    {'event':form.instance.evenement,
+                     'form': form})
+            else:
+                return render(request,'personal_calendar/event/details.html',{
+                        'event':form.instance.evenement,
+                        'form':form})
+
 def details(request, id):
     event = Evenement.objects.get(pk = id)
     if request.method == "POST":
         form = Evenement_ParticipantForm(request.POST)
         if form.is_valid():
             form.save()
-            print form.instance
             if request.is_ajax():
                 delete_form = render_to_string(
                     "personal_calendar/blocks/delete_form.html",
                 data = {'participant': form.instance.participant.username,
                         'get_status_display': form.instance.get_status_display(),
                         'delete_form': delete_form}
-                print data
                 return HttpResponse(
                     json.dumps(data),
                     mimetype="application/json"
             return HttpResponseRedirect('/agenda/%s/details/' % id)
     else:
         form = Evenement_ParticipantForm(initial = {'evenement':event})
-        participants = [user.pk for user in event.participants.all()]
-        form.fields['participant'].queryset=User.objects.exclude(
-            pk__in = participants)
-        form.fields['evenement'].widget = HiddenInput()
     if request.is_ajax():
         return render_to_response(
             'personal_calendar/blocks/participant_form.html',

File templates/base.html

             $(this).parent().parent().children('.event').toggle()
 	    e.preventDefault()
         });
-        $("#participant_form").submit(function(e){
+        $("#participant_form").live("submit", function(e){
             $.ajax(
                 {
                     type:"POST",