You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.7 KiB
46 lines
1.7 KiB
# -*- 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
|
|
|