Browse Source

Add documents view

Giving an overview for each document.
Jocelyn Delande 9 years ago
parent
commit
403c6446a0

+ 4 - 0
costs/models.py

@@ -23,6 +23,10 @@ class Document(models.Model):
     def __str__(self):
         return '{} {:%b %Y}'.format(self.name, self.date)
 
+    def get_absolute_url(self):
+        return reverse('detail-document', kwargs={'pk': self.pk})
+
+
 
 class AbstractItem(models.Model):
     name = models.CharField(max_length=130)

+ 71 - 0
costs/templates/costs/document_detail.html

@@ -0,0 +1,71 @@
+{% load costs %}
+
+<h1>{{ document.name  }}</h1>
+<aside>
+  <p><em>{{ document.date|date:"M Y" }}</em></p>
+</aside>
+
+<h2>Services proposés</h2>
+<ul>
+{% for service in document.service_set.all %}
+  <li>
+    <a href="{{ service.get_absolute_url }}">{{ service.name }}</a>
+  </li>
+{% empty %}
+  <li>Pas de service pour l'instant.</li>
+{% endfor %}
+</ul>
+
+<h2>Coûts</h2>
+
+<table>
+  <thead>
+    <tr>
+      <th>Désignation</th><th>Occupation</th><th>Coût mensuel</th>
+    </tr>
+  </thead>
+  <tbody>
+
+{% for cost in document.cost_set.all %}
+    <tr>
+      <td>
+        <a href="{{ cost.get_absolute_url }}" title="{{ cost.description }}">
+          {{ cost.name }}
+        </a>
+      </td>
+      <td>{{ cost.used|percent }}</td>
+      <td>{{ cost.price|price }}</td>
+    </tr>
+{% empty %}
+  <tr><td>Pas de coût pour l'instant.</td></tr>
+{% endfor %}
+    <tbody>
+</table>
+
+<h2>Matériel et frais d'accès</h2>
+<table>
+  <thead>
+    <tr>
+      <th>Désignation</th><th>Occupation</th><th>Coût d'achat</th>
+    </tr>
+  </thead>
+  <tbody>
+
+{% for good in document.good_set.all %}
+  <tr>
+    <td>
+      <a title="{{ good.description }}" href="{{ good.get_absolute_url }}">
+        {{ good.name }}
+      </a>
+    </td>
+    <td>
+      {{ good.used|percent }}
+    </td>
+    <td>
+      {{good.price|price }}
+    </td>
+  </tr>
+{% empty %}
+  <tr><td>Pas de matériel pour l'instant.</td></tr>
+{% endfor %}
+</table>

+ 17 - 0
costs/templates/costs/documents_list.html

@@ -0,0 +1,17 @@
+{% if documents.count > 0 %}
+<ul>
+{% for doc in documents %}
+  <li>
+    <a href="{{ doc.get_absolute_url }}">{{ doc.name }}</a>
+    ({{ doc.date|date:"M Y" }}, {{ doc.service_set.count }} services)
+  </li>
+
+{% endfor %}
+</ul>
+
+
+{% else %}
+<p>
+Pas de documents pour l'heure.
+</p>
+{% endif %}

+ 5 - 0
costs/templatetags/costs.py

@@ -11,3 +11,8 @@ def price(v):
 @register.filter
 def round(v):
     return '{:.1f}'.format(v)
+
+
+@register.filter
+def percent(v):
+    return format(v, '.0%')

+ 6 - 0
costs/urls.py

@@ -3,6 +3,12 @@ from . import views
 
 urlpatterns = [
     url(r'^$', views.index),
+    url(r'^documents/(?P<pk>\d+)',
+        views.detail_document,
+        name='detail-document'),
+    url(r'^documents$',
+        views.list_documents,
+        name='list-documents'),
     url(r'^services$', views.list_services, name='list-services'),
     url(r'^services/(?P<pk>\d+)$', views.detail_service, name='detail-service'),
     url(r'^resources$', views.list_resources, name='list-resource'),

+ 16 - 2
costs/views.py

@@ -1,8 +1,8 @@
 from itertools import chain
 
-from django.shortcuts import render
+from django.shortcuts import render, get_object_or_404
 
-from .models import Service, Cost, Good, CostUse, GoodUse
+from .models import Document, Service, Cost, Good, CostUse, GoodUse
 
 
 def index(request):
@@ -25,6 +25,20 @@ def list_resources(request):
     return render(request, 'costs/resources_list.html', context)
 
 
+def list_documents(request):
+    docs = Document.objects.all().prefetch_related('service_set')
+
+    return render(
+        request, 'costs/documents_list.html', {'documents': docs})
+
+
+def detail_document(request, pk):
+    doc = get_object_or_404(Document, pk=pk)
+
+    return render(
+        request, 'costs/document_detail.html', {'document': doc})
+
+
 def detail_service(request, pk):
     service = Service.objects.get(pk=pk)