it/itf/bestpractices/models.py

215 lines
5.2 KiB
Python
Raw Normal View History

2010-12-06 22:34:49 +00:00
from django.db import models
from tagging.fields import TagField
2011-01-04 06:27:10 +00:00
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from app.models import ItfModel
2011-02-01 15:13:30 +00:00
# from django.forms import
# import forms
2010-12-06 22:34:49 +00:00
2011-01-04 06:27:10 +00:00
class BestPractice(ItfModel):
2010-12-06 22:34:49 +00:00
title = models.CharField(max_length=512)
story = models.TextField()
guideline = models.TextField(blank=True)
guidelines = models.ManyToManyField("Guideline", null=True, blank=True)
2010-12-06 22:34:49 +00:00
law = models.TextField(blank=True)
2011-03-06 12:42:33 +00:00
law_image = models.ImageField(upload_to='upload/images/bestpractices/law/', blank=True, null=True)
2010-12-06 22:34:49 +00:00
theatre = models.TextField(blank=True, help_text="Spotlight on Theatre text")
2010-12-07 14:51:24 +00:00
quick_howto = models.TextField(blank=True)
2010-12-06 22:34:49 +00:00
tags = TagField(blank=True, help_text="Enter as many tags as you like, separated by commas.")
2011-02-01 15:13:30 +00:00
category = models.ForeignKey("BestPracticeCategory", null=True)
2011-01-11 20:32:18 +00:00
added = models.DateTimeField(auto_now_add=True, null=True)
modified = models.DateTimeField(auto_now=True, null=True)
2011-04-10 02:00:43 +00:00
fts_fields = ['title', 'story', 'guideline', 'law', 'theatre', 'quick_howto']
2011-01-04 06:27:10 +00:00
fk_filters = ['category']
sort_fields = ['title']
2011-04-10 02:00:43 +00:00
2011-02-01 15:13:30 +00:00
add_form = "BestPracticeForm"
getters = ['info_dict', 'list_dict', 'no_of_stories']
2011-01-04 06:27:10 +00:00
2010-12-06 22:34:49 +00:00
def __unicode__(self):
2011-01-20 08:02:03 +00:00
return str(self.id) + ": " + self.title
2010-12-07 14:51:24 +00:00
2011-01-04 06:27:10 +00:00
def info_dict(self):
return {
2011-01-12 10:28:18 +00:00
'id': self.id,
2011-01-04 06:27:10 +00:00
'title': self.title,
'story': self.story,
'stories': map(lambda x: x.get_dict(), BestPracticeStory.objects.filter(bestpractice=self)),
2011-01-04 06:27:10 +00:00
'guideline': self.guideline,
'law': self.law,
'law_image': self.law_image.url if self.law_image.name != '' else '',
2011-01-04 06:27:10 +00:00
'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,
}
2011-01-11 14:26:36 +00:00
def preview_dict(self):
return {
'id': self.id,
'title': self.title,
'category': self.category.name,
'story': self.story,
'images': self.get_images()
2011-01-11 14:26:36 +00:00
}
def get_images(self):
images = []
for i in BestPracticeImage.objects.filter(bestpractice=self):
images.append({
'url': i.image.url,
2011-01-19 08:41:55 +00:00
'caption': i.caption,
'thumb': i.get_thumb()
})
return images
2011-01-04 06:27:10 +00:00
'''
Model get methods (getters)
'''
def no_of_stories(self):
return BestPracticeStory.objects.filter(bestpractice=self).count()
2011-04-18 13:36:11 +00:00
class BestPracticeDownload(models.Model):
language = models.CharField(max_length=255)
fil = models.FileField(upload_to="upload/bestpractices_downloads/")
2011-03-06 12:42:33 +00:00
class BestPracticeStory(models.Model):
text = models.TextField()
image = models.ImageField(upload_to='upload/images/bestpractices/stories/', blank=True, null=True)
2011-03-06 12:42:33 +00:00
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 BestPracticeFAQ(ItfModel):
2011-04-25 19:13:19 +00:00
question = models.TextField()
answer = models.TextField()
def __unicode__(self):
return self.question
2011-01-19 10:10:29 +00:00
2011-09-09 23:13:27 +00:00
def list_dict(self):
return {
'id': self.id,
'question': self.question,
'answer': self.answer,
}
2010-12-07 14:51:24 +00:00
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
}
2011-09-28 11:27:19 +00:00
class BestPracticeLink(ItfModel):
2010-12-07 14:51:24 +00:00
url = models.URLField()
text = models.TextField(blank=True)
bestpractice = models.ForeignKey(BestPractice)
def get_dict(self):
return {
'url': self.url,
'text': self.text
}
2011-09-28 11:27:19 +00:00
def list_dict(self):
return {
'id': self.id,
'text': self.text,
'url': self.url,
}
2010-12-07 14:51:24 +00:00
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,
}
2010-12-07 14:51:24 +00:00
def __unicode__(self):
2011-01-19 08:41:55 +00:00
return self.caption
def get_thumb(self, max_width='200'):
return self.image.url #FIXME!!
2010-12-07 14:51:24 +00:00
2011-09-09 23:13:27 +00:00
class Guideline(ItfModel):
title = models.CharField(max_length=512)
text = models.TextField()
def get_dict(self):
return {
'title': self.title,
'text': self.text
}
2011-09-09 23:13:27 +00:00
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
}
2011-09-09 23:13:27 +00:00
def list_dict(self):
return {
'id': self.id,
'term': self.term,
'definition': self.definition
}
def __unicode__(self):
return self.term
2011-02-01 15:13:30 +00:00