diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 43267f6df..ecc43fc07 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -83,7 +83,7 @@ from ishtar_common.models_imports import ImporterModel, ImporterType, \ from ishtar_common.templatetags.link_to_window import simple_link_to_window from ishtar_common.utils import get_cache, disable_for_loaddata, create_slug, \ get_all_field_names, merge_tsvectors, cached_label_changed, \ - generate_relation_graph, max_size_help, task + generate_relation_graph, max_size_help, task, SecretaryRenderer __all__ = [ 'ImporterModel', 'ImporterType', 'ImporterDefault', 'ImporterDefaultValues', @@ -136,6 +136,8 @@ class ValueGetter(object): if not prefix: prefix = self._prefix values = {} + if hasattr(self, "qrcode"): + values['qrcode_path'] = self.qrcode_path for field_name in get_all_field_names(self): if not hasattr(self, field_name) or \ field_name in self.GET_VALUES_EXCLUDE_FIELDS: @@ -1632,6 +1634,14 @@ class QRCodeItem(models.Model, ImageContainerModel): class Meta: abstract = True + @property + def qrcode_path(self): + if not self.qrcode: + self.generate_qrcode() + if not self.qrcode: # error on qrcode generation + return "" + return self.qrcode.path + def generate_qrcode(self, request=None, secure=True, tmpdir=None): url = self.get_absolute_url() site = Site.objects.get_current() @@ -1863,7 +1873,30 @@ class GeoItem(models.Model): return self._geojson_serialize('multi_polygon') -class BaseHistorizedItem(FullSearch, Imported, JsonData, FixAssociated): +class TemplateItem: + @classmethod + def _label_templates_q(cls): + model_name = "{}.{}".format( + cls.__module__, cls.__name__).replace( + "models_finds", "models").replace( + "models_treatments", "models") + return DocumentTemplate.objects.filter( + associated_model__klass=model_name, + for_labels=True, + available=True + ) + + @classmethod + def has_label_templates(cls): + return cls._label_templates_q().count() + + @classmethod + def label_templates(cls): + return cls._label_templates_q() + + +class BaseHistorizedItem(TemplateItem, FullSearch, Imported, + JsonData, FixAssociated): """ Historized item with external ID management. All historized items are searchable and have a data json field. @@ -3064,8 +3097,7 @@ class Dashboard(object): class DocumentTemplate(models.Model): name = models.CharField(_("Name"), max_length=100) - slug = models.SlugField(_("Slug"), blank=True, null=True, max_length=100, - unique=True) + slug = models.SlugField(_("Slug"), max_length=100, unique=True) associated_model = models.ForeignKey(ImporterModel) template = models.FileField( _("Template"), upload_to="templates/%Y/", help_text=max_size_help()) @@ -3109,6 +3141,9 @@ class DocumentTemplate(models.Model): for item in items.distinct().order_by(*cls._meta.ordering).all(): yield (item.pk, _(str(item))) + def get_baselink_for_labels(self): + return reverse('generate-labels', args=[self.slug]) + def publish(self, c_object): tempdir = tempfile.mkdtemp("-ishtardocs") output_name = tempdir + os.path.sep + \ @@ -3125,6 +3160,35 @@ class DocumentTemplate(models.Model): output.write(result) return output_name + def publish_labels(self, objects): + if not objects: + return + tempdir = tempfile.mkdtemp("-ishtarlabels") + main_output_name = tempdir + os.path.sep + \ + slugify(self.name.replace(' ', '_').lower()) + u'-' + \ + datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + suffix = "." + self.template.name.split('.')[-1] + len_objects = len(objects) + for idx in range(int(len(objects) / self.label_per_page) + 1): + values = {"items": []} + for subidx in range(self.label_per_page): + c_idx = idx * self.label_per_page + subidx + if c_idx >= len_objects: + break + obj = objects[c_idx] + values["items"].append(obj.get_values()) + engine = SecretaryRenderer() + try: + result = engine.render(self.template, **values) + except TemplateSyntaxError as e: + raise TemplateSyntaxError(str(e), e.lineno) + output_name = main_output_name + "-" + str(idx + 1) + suffix + output = open(output_name, 'wb') + output.write(result) + # output_name = main_output_name + suffix + # TODO: merge docs - return the last for now + return output_name + class NumberManager(models.Manager): def get_by_natural_key(self, number): |