misc: produce and accepts URLs with facture's code (#21075)

This commit is contained in:
Benjamin Dauvergne 2021-11-20 00:05:38 +01:00
parent c21a07c9c7
commit 6da1978bea
4 changed files with 34 additions and 3 deletions

View File

@ -16,6 +16,7 @@
import datetime as dt
import re
from adminsortable2.admin import SortableInlineAdminMixin
from django import http
@ -402,6 +403,28 @@ class FactureAdmin(LookupAllowed, admin.ModelAdmin):
show_contrat.short_description = 'Contrat'
# adapt get_object and get_changelist_instance to produce and accept URL
# with facture's code
FACTURE_RE = re.compile('^F(?P<year>20[0-9]{2})(?P<ordre>[0-9]{4})$')
def get_object(self, request, object_id, from_field=None):
m = self.FACTURE_RE.match(object_id)
if m:
queryset = self.get_queryset(request)
model = queryset.model
year = int(m.group('year'))
ordre = int(m.group('ordre'))
try:
return queryset.get(emission__year=year, ordre=ordre)
except model.DoesNotExist:
pass
return super().get_object(request, object_id, from_field=from_field)
def get_changelist_instance(self, request):
changelist = super().get_changelist_instance(request)
changelist.pk_attname = 'pk_or_code'
return changelist
class PaymentAdmin(LookupAllowed, admin.ModelAdmin, CommonPaymentInline):
form = forms.PaymentForm

View File

@ -263,13 +263,21 @@ class Facture(models.Model):
def code(self):
if not self.ordre and self.proforma:
return 'Facture proforma du %s' % self.emission
ctx = {'year': self.emission.year}
ctx = {}
ctx.update(self.__dict__)
ctx.update({'year': self.emission.year})
if ctx["ordre"] is None:
return "Ordre is missing"
format = getattr(settings, "FACTURE_CODE_FORMAT", "F%(year)s%(ordre)04d")
return format % ctx
@property
def pk_or_code(self):
# used in ModelAdmin to create URLs for humans
if self.ordre is not None:
return self.code()
return self.pk
def save(self):
if self.ordre is None and not self.proforma:
self.ordre = self.last_ordre_plus_one()

View File

@ -12,7 +12,7 @@
{% for l in factures %}
{% with l.facture as facture %}
<tr class="facture{% if l.encaissements %} encaissement{% endif %}{% if l.vieille %} vieille{% endif %}"><td>
<div class="facture"><a href="{% url "admin:eo_facture_facture_change" facture.id %}">{{ facture.code }} - {{facture.intitule}}<a/> pour {{ facture.solde|amountformat }} {{ facture.contrat.client.monnaie }} TTC à <a href="{% url "admin:eo_facture_client_change" facture.client.id %}">{{facture.client}}</a></div>
<div class="facture"><a href="{% url "admin:eo_facture_facture_change" facture.pk_or_code %}">{{ facture.code }} - {{facture.intitule}}<a/> pour {{ facture.solde|amountformat }} {{ facture.contrat.client.monnaie }} TTC à <a href="{% url "admin:eo_facture_client_change" facture.client.id %}">{{facture.client}}</a></div>
</td>
<td>{{facture.emission|ago}}</td>
</tr>

View File

@ -35,7 +35,7 @@ def test_homepage(app):
compte_en_banque = homepage.click("Compte en banque")
str(compte_en_banque)
factures = homepage.click("Factures")
factures_00137 = factures.click('F2019.00137')
factures_00137 = factures.click('F20190137')
str(factures_00137)
rapid = factures.click('Rapid')
str(rapid)