main image code

This commit is contained in:
Sanj 2011-10-05 20:57:02 +05:30
parent 21b06699cf
commit 724bbbd9ca
2 changed files with 30 additions and 5 deletions

View File

@ -8,6 +8,7 @@ from django.template.loader import render_to_string
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from insidepages.models import ModuleTab, ModelExtra from insidepages.models import ModuleTab, ModelExtra
from os.path import exists from os.path import exists
from sorl.thumbnail import get_thumbnail
def splitSearch(string): def splitSearch(string):
ret = [] ret = []
@ -45,15 +46,19 @@ class ItfModel(models.Model):
return self.get(self.title_field) return self.get(self.title_field)
def get_tab(self): def get_tab(self):
modelextra = self.get_modelextra()
tab = ModuleTab.objects.filter(model=modelextra)[0]
return tab
def get_modelextra(self):
try: try:
ctype = ContentType.objects.get(model=self.__class__._meta.module_name) 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). 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) ctype = get_real_ctype(self.__class__._meta.module_name)
# modelextra = ModelExtra.objects.filter(model=ctype)[0] # modelextra = ModelExtra.objects.filter(model=ctype)[0]
modelextra = ctype.modelextra_set.all()[0] modelextra = ctype.modelextra_set.all()[0]
tab = ModuleTab.objects.filter(model=modelextra)[0] return modelextra
return tab
def get_absolute_url(self): def get_absolute_url(self):
return "%s/?tab=%s&object_id=%d" % (self.get_module().get_absolute_url(), self.get_tab().slug, self.id) 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): 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 { return {
'url': '', 'thumb': thumb
'caption': ''
} }
def main_image(self):
return None
def get_template_path(self): def get_template_path(self):
kls = self.__class__ kls = self.__class__
return "modules/%s/%s.html" % (kls._meta.app_label, kls._meta.module_name) return "modules/%s/%s.html" % (kls._meta.app_label, kls._meta.module_name)

View File

@ -46,6 +46,9 @@ class BestPractice(ItfModel):
'images': self.get_images() 'images': self.get_images()
} }
def main_image(self):
return self.get_images()[0]
def list_dict(self): def list_dict(self):
return { return {
'id': self.id, 'id': self.id,