parent
eaed37e3bb
commit
895ed84248
13 changed files with 75 additions and 27 deletions
@ -0,0 +1,46 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
from django.db import models |
||||||
|
from django.core.exceptions import FieldError |
||||||
|
|
||||||
|
|
||||||
|
class LinkedDocsMixin(models.Model): |
||||||
|
"""Mixin: добавляет метод для получения списка связанных документов.""" |
||||||
|
LINKED_DOCS_MODELS = ('Invoice', 'Faktura', 'Nakladn', 'AktRabot',) |
||||||
|
|
||||||
|
class Meta: |
||||||
|
abstract = True |
||||||
|
|
||||||
|
def linked_docs(self): |
||||||
|
if getattr(self, '_cache_linked_docs', None) is None: |
||||||
|
self._cache_linked_docs = {} |
||||||
|
|
||||||
|
model_name = self.__class__.__name__ # имя модели на инстансе которой вызвали метод linked_docs |
||||||
|
|
||||||
|
if model_name == 'Invoice': |
||||||
|
invoice_id = self.id |
||||||
|
|
||||||
|
for doc in self.LINKED_DOCS_MODELS: |
||||||
|
if doc != 'Invoice': |
||||||
|
queryset = models.get_model('docs', doc).objects.filter(invoice=invoice_id) |
||||||
|
|
||||||
|
if queryset: |
||||||
|
self._cache_linked_docs[doc] = queryset |
||||||
|
|
||||||
|
else: |
||||||
|
invoice_id = getattr(self, 'invoice_id', None) |
||||||
|
|
||||||
|
if invoice_id: |
||||||
|
for doc in self.LINKED_DOCS_MODELS: |
||||||
|
doc_model = models.get_model('docs', doc) |
||||||
|
|
||||||
|
if doc != 'Invoice': |
||||||
|
queryset = doc_model.objects.filter(invoice_id=invoice_id) |
||||||
|
if model_name == doc: |
||||||
|
queryset = queryset.exclude(id=self.id) |
||||||
|
else: |
||||||
|
queryset = doc_model.objects.filter(id=invoice_id) |
||||||
|
|
||||||
|
if queryset: |
||||||
|
self._cache_linked_docs[doc] = queryset |
||||||
|
|
||||||
|
return self._cache_linked_docs |
||||||
@ -1,16 +1,20 @@ |
|||||||
{% if obj.docs_for_invoice %} |
{% if obj.linked_docs %} |
||||||
|
|
||||||
<div class="doc-panel-header">Связанные документы</div> |
<div class="doc-panel-header">Связанные документы</div> |
||||||
|
|
||||||
{% for akt in obj.docs_for_invoice.AktRabot %} |
{% for invoice in obj.linked_docs.Invoice %} |
||||||
|
<a href="{% url 'docs_invoice_img' invoice.id %}" class="doc">{{ invoice }}</a> |
||||||
|
{% endfor %} |
||||||
|
|
||||||
|
{% for akt in obj.linked_docs.AktRabot %} |
||||||
<a href="{% url 'docs_aktrabot_img' akt.id %}" class="doc">{{ akt }}</a> |
<a href="{% url 'docs_aktrabot_img' akt.id %}" class="doc">{{ akt }}</a> |
||||||
{% endfor %} |
{% endfor %} |
||||||
|
|
||||||
{% for nakladn in obj.docs_for_invoice.Nakladn %} |
{% for nakladn in obj.linked_docs.Nakladn %} |
||||||
<a href="{% url 'docs_nakladn_img' nakladn.id %}" class="doc">{{ nakladn }}</a> |
<a href="{% url 'docs_nakladn_img' nakladn.id %}" class="doc">{{ nakladn }}</a> |
||||||
{% endfor %} |
{% endfor %} |
||||||
|
|
||||||
{% for faktura in obj.docs_for_invoice.Faktura %} |
{% for faktura in obj.linked_docs.Faktura %} |
||||||
<a href="{% url 'docs_faktura_img' faktura.id %}" class="doc">{{ faktura }}</a> |
<a href="{% url 'docs_faktura_img' faktura.id %}" class="doc">{{ faktura }}</a> |
||||||
{% endfor %} |
{% endfor %} |
||||||
|
|
||||||
Loading…
Reference in new issue