From 724bbbd9ca3ea3057cda5be98ea37de61a7fe54b Mon Sep 17 00:00:00 2001 From: Sanj Date: Wed, 5 Oct 2011 20:57:02 +0530 Subject: [PATCH] main image code --- itf/app/models.py | 32 +++++++++++++++++++++++++++----- itf/bestpractices/models.py | 3 +++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/itf/app/models.py b/itf/app/models.py index 7f90a15..819dbcc 100644 --- a/itf/app/models.py +++ b/itf/app/models.py @@ -8,6 +8,7 @@ from django.template.loader import render_to_string from django.contrib.contenttypes.models import ContentType from insidepages.models import ModuleTab, ModelExtra from os.path import exists +from sorl.thumbnail import get_thumbnail def splitSearch(string): ret = [] @@ -45,15 +46,19 @@ class ItfModel(models.Model): return self.get(self.title_field) def get_tab(self): + modelextra = self.get_modelextra() + tab = ModuleTab.objects.filter(model=modelextra)[0] + return tab + + + def get_modelextra(self): try: ctype = ContentType.objects.get(model=self.__class__._meta.module_name) except:#FIXME: ideally catch only MultipleObjectsReturned (figure out where to import that from :/ ) #FUCKING way ugly hack to get clashing model ctype names with django internal models working (get_real_ctypes simply checks, if there are multiple content objects with the same 'module_name', which one is_itf_model, and returns that). ctype = get_real_ctype(self.__class__._meta.module_name) - # modelextra = ModelExtra.objects.filter(model=ctype)[0] modelextra = ctype.modelextra_set.all()[0] - tab = ModuleTab.objects.filter(model=modelextra)[0] - return tab + return modelextra def get_absolute_url(self): return "%s/?tab=%s&object_id=%d" % (self.get_module().get_absolute_url(), self.get_tab().slug, self.id) @@ -68,11 +73,28 @@ class ItfModel(models.Model): def get_main_image(self): + main_image = self.main_image() + if main_image is not None: + imgfield = main_image.image + elif self.get_modelextra().default_image: + imgfield = self.get_modelextra().default_image + else: + imgfield = None + if imgfield is not None: + try: + thumb = get_thumbnail(imgfield, "150x142", crop="center") + except: + thumb = '' + else: + thumb = '' return { - 'url': '', - 'caption': '' + 'thumb': thumb } + + def main_image(self): + return None + def get_template_path(self): kls = self.__class__ return "modules/%s/%s.html" % (kls._meta.app_label, kls._meta.module_name) diff --git a/itf/bestpractices/models.py b/itf/bestpractices/models.py index 3768b2a..f589a0c 100644 --- a/itf/bestpractices/models.py +++ b/itf/bestpractices/models.py @@ -46,6 +46,9 @@ class BestPractice(ItfModel): 'images': self.get_images() } + def main_image(self): + return self.get_images()[0] + def list_dict(self): return { 'id': self.id,