from django.db import models from tagging.fields import TagField from django.core.paginator import Paginator, InvalidPage, EmptyPage from app.models import ItfModel from django.forms import ModelForm # from django.forms import # import forms class BestPractice(ItfModel): title = models.CharField(max_length=512) story = models.TextField() guideline = models.TextField(blank=True) guidelines = models.ManyToManyField("Guideline", null=True, blank=True) law = models.TextField(blank=True) law_image = models.ImageField(upload_to='upload/images/bestpractices/law/', blank=True, null=True) theatre = models.TextField(blank=True, help_text="Spotlight on Theatre text") quick_howto = models.TextField(blank=True) tags = TagField(blank=True, help_text="Enter as many tags as you like, separated by commas.") category = models.ForeignKey("BestPracticeCategory", null=True) added = models.DateTimeField(auto_now_add=True, null=True) modified = models.DateTimeField(auto_now=True, null=True) fts_fields = ['title', 'story', 'guideline', 'law', 'theatre', 'quick_howto'] fk_filters = ['category'] sort_fields = ['title'] add_form = "BestPracticeForm" getters = ['info_dict', 'list_dict', 'no_of_stories'] def __unicode__(self): return str(self.id) + ": " + self.title def info_dict(self): return { 'id': self.id, 'title': self.title, 'story': self.story, 'stories': map(lambda x: x.get_dict(), BestPracticeStory.objects.filter(bestpractice=self)), 'guideline': self.guideline, 'law': self.law, 'law_image': self.law_image.url if self.law_image.name != '' else '', 'theatre': self.theatre, 'quick_howto': self.quick_howto, 'category': self.category.name, 'category_id': self.category.id, 'images': self.get_images(), 'links': BestPracticeLink.objects.filter(bestpractice=self) } def main_image(self): imgs = self.get_images() if len(imgs) > 0: img = imgs[0] return img.image else: return None def list_dict(self): return { 'id': self.id, 'title': self.title, 'category': self.category.name, } def preview_dict(self): return { 'id': self.id, 'title': self.title, 'category': self.category.name, 'story': self.story, 'images': self.get_images() } def get_images(self): images = [] for i in BestPracticeImage.objects.filter(bestpractice=self): images.append(i) ''' images.append({ 'url': i.image.url, 'caption': i.caption, 'thumb': i.get_thumb() }) ''' return images ''' Model get methods (getters) ''' def no_of_stories(self): return BestPracticeStory.objects.filter(bestpractice=self).count() class BestPracticeDownload(models.Model): language = models.CharField(max_length=255) fil = models.FileField(upload_to="upload/bestpractices_downloads/") class BestPracticeStory(models.Model): text = models.TextField() image = models.ImageField(upload_to='upload/images/bestpractices/stories/', blank=True, null=True) bestpractice = models.ForeignKey(BestPractice) def __unicode__(self): return self.text def get_dict(self): return { 'text': self.text, 'image': self.image if self.image.name != '' else None } class BestPracticeFAQ(ItfModel): question = models.TextField() answer = models.TextField() title_field = 'question' def __unicode__(self): return self.question def list_dict(self): return { 'id': self.id, 'question': self.question, 'answer': self.answer, } def info_dict(self): return { 'id': self.id, 'question': self.question, 'answer': self.answer } class BestPracticeCategory(models.Model): name = models.CharField(max_length=256) description = models.TextField(blank=True) def __unicode__(self): return self.name def get_dict(self): return { 'name': self.name, 'description': self.description } class BestPracticeLink(ItfModel): url = models.URLField() text = models.TextField(blank=True) bestpractice = models.ForeignKey(BestPractice) def get_dict(self): return { 'url': self.url, 'text': self.text } def list_dict(self): return { 'id': self.id, 'text': self.text, 'url': self.url, } def __unicode__(self): return self.url class BestPracticeImage(models.Model): image = models.ImageField(upload_to='upload/images/bestpractices/') caption = models.CharField(max_length=512, blank=True) bestpractice = models.ForeignKey(BestPractice) def get_dict(self): return { 'image': self.image.url, 'caption': self.caption, } def __unicode__(self): return self.caption def get_thumb(self, max_width='200'): return self.image.url #FIXME!! class Guideline(ItfModel): title = models.CharField(max_length=512) text = models.TextField() def get_dict(self): return { 'title': self.title, 'text': self.text } def list_dict(self): return { 'id': self.id, 'title': self.title, } def info_dict(self): return { 'id': self.id, 'title': self.title, 'text': self.text } def __unicode__(self): return self.title class Glossary(models.Model): term = models.CharField(max_length=256) definition = models.TextField() def get_dict(self): return { 'term': self.term, 'definition': self.definition } def list_dict(self): return { 'id': self.id, 'term': self.term, 'definition': self.definition } def __unicode__(self): return self.term