Browse Source

Enforce that a validated bill has a pdf

- improve data consistence using sql transactions
- make the business logic more explicit in templates
Jocelyn Delande 8 years ago
parent
commit
66343b9c95

+ 6 - 5
coin/billing/models.py

@@ -7,7 +7,7 @@ import uuid
 import os
 import os
 from decimal import Decimal
 from decimal import Decimal
 
 
-from django.db import models
+from django.db import models, transaction
 from django.db.models.signals import post_save
 from django.db.models.signals import post_save
 from django.dispatch import receiver
 from django.dispatch import receiver
 
 
@@ -111,15 +111,16 @@ class Invoice(models.Model):
         pdf_file = render_as_pdf('billing/invoice_pdf.html', context)
         pdf_file = render_as_pdf('billing/invoice_pdf.html', context)
         self.pdf.save('%s.pdf' % self.number, pdf_file)
         self.pdf.save('%s.pdf' % self.number, pdf_file)
 
 
+    @transaction.atomic
     def validate(self):
     def validate(self):
         """
         """
         Switch invoice to validate mode. This set to False the draft field
         Switch invoice to validate mode. This set to False the draft field
         and generate the pdf
         and generate the pdf
         """
         """
-        if not self.pdf_exists():
-            self.validated = True
-            self.save()
-            self.generate_pdf()
+        self.validated = True
+        self.save()
+        self.generate_pdf()
+        assert self.pdf_exists()
 
 
     def save(self, *args, **kwargs):
     def save(self, *args, **kwargs):
         self.number = Invoice.objects.get_next_invoice_number(self.date)
         self.number = Invoice.objects.get_next_invoice_number(self.date)

+ 1 - 1
coin/billing/templates/admin/billing/invoice/change_form.html

@@ -3,7 +3,7 @@
 {% block object-tools-items %}
 {% block object-tools-items %}
     {% if not original.validated %}
     {% if not original.validated %}
         <li><a href="{% url 'admin:invoice_validate' id=object_id %}">Valider la facture</a></li>
         <li><a href="{% url 'admin:invoice_validate' id=object_id %}">Valider la facture</a></li>
-    {% elif original.pdf_exists %}
+    {% elif original.validated %}
         <li><a href="{% url 'billing:invoice_pdf' id=object_id %}">Télécharger le PDF</a></li>
         <li><a href="{% url 'billing:invoice_pdf' id=object_id %}">Télécharger le PDF</a></li>
     {% endif %}
     {% endif %}
     {{ block.super }}
     {{ block.super }}

+ 1 - 1
coin/billing/templates/billing/invoice.html

@@ -7,7 +7,7 @@
         <p>Émise le {{ invoice.date }}</p>
         <p>Émise le {{ invoice.date }}</p>
     </div>
     </div>
     <div class="large-4 columns">
     <div class="large-4 columns">
-        {% if invoice.pdf_exists %}<a href="{% url 'billing:invoice_pdf' id=invoice.number %}" target="_blank" class="button expand"><i class="fa fa-file-pdf-o"></i> Télécharger en PDF</a>{% endif %}
+        {% if invoice.validated %}<a href="{% url 'billing:invoice_pdf' id=invoice.number %}" target="_blank" class="button expand"><i class="fa fa-file-pdf-o"></i> Télécharger en PDF</a>{% endif %}
     </div>
     </div>
 </div>
 </div>
 
 

+ 1 - 1
coin/members/templates/members/invoices.html

@@ -20,7 +20,7 @@
             <td>{{ invoice.date }}</td>
             <td>{{ invoice.date }}</td>
             <td>{{ invoice.amount }}</td>
             <td>{{ invoice.amount }}</td>
             <td{% if invoice.amount_remaining_to_pay > 0 %} class="unpaid"{% endif %}>{{ invoice.amount_remaining_to_pay }}</td>
             <td{% if invoice.amount_remaining_to_pay > 0 %} class="unpaid"{% endif %}>{{ invoice.amount_remaining_to_pay }}</td>
-            <td>{% if invoice.pdf_exists %}<a href="{% url 'billing:invoice_pdf' id=invoice.number %}"><i class="fa fa-file-pdf-o"></i> PDF</a>{% endif %}</td>
+            <td>{% if invoice.validated %}<a href="{% url 'billing:invoice_pdf' id=invoice.number %}"><i class="fa fa-file-pdf-o"></i> PDF</a>{% endif %}</td>
         </tr>
         </tr>
         {% empty %}
         {% empty %}
         <tr class="placeholder"><td colspan="6">Aucune facture.</td></tr>
         <tr class="placeholder"><td colspan="6">Aucune facture.</td></tr>