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 # 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" 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 } 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({ 'url': i.image.url, 'caption': i.caption, 'thumb': i.get_thumb() }) return images 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.url if self.image.name != '' else '' } class BestPracticeCategory(models.Model): name = models.CharField(max_length=256) description = models.TextField(blank=True) def __unicode__(self): return self.name class BestPracticeLink(models.Model): url = models.URLField() text = models.TextField(blank=True) bestpractice = models.ForeignKey(BestPractice) 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 __unicode__(self): return self.caption def get_thumb(self, max_width='200'): return self.image.url #FIXME!! class Guideline(models.Model): title = models.CharField(max_length=512) text = models.TextField() def __unicode__(self): return self.title class Glossary(models.Model): term = models.CharField(max_length=256) definition = models.TextField() def __unicode__(self): return self.term