it/itf/bestpractices/models.py

253 lines
6.0 KiB
Python
Raw Permalink 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
from django.forms import ModelForm
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)
display_image = models.ImageField(upload_to="upload/images/bestpractices/main/", blank=True, null=True)
2010-12-06 22:34:49 +00:00
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-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,
2011-10-05 14:24:04 +00:00
'category_id': self.category.id,
2011-10-08 00:39:33 +00:00
'images': self.get_images(),
'links': BestPracticeLink.objects.filter(bestpractice=self)
2011-01-04 06:27:10 +00:00
}
2011-10-05 15:27:02 +00:00
def main_image(self):
2011-11-01 01:38:22 +00:00
if self.display_image:
return self.display_image
2011-10-06 11:33:38 +00:00
imgs = self.get_images()
if len(imgs) > 0:
2011-10-10 10:30:18 +00:00
img = imgs[0]
return img.image
2011-11-01 01:38:22 +00:00
return None
2011-10-05 15:27:02 +00:00
2011-01-04 06:27:10 +00:00
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):
2011-10-05 14:24:04 +00:00
images.append(i)
'''
images.append({
'url': i.image.url,
2011-01-19 08:41:55 +00:00
'caption': i.caption,
'thumb': i.get_thumb()
})
2011-10-05 14:24:04 +00:00
'''
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,
2011-10-05 14:24:04 +00:00
'image': self.image if self.image.name != '' else None
}
class BestPracticeFAQ(ItfModel):
2011-04-25 19:13:19 +00:00
question = models.TextField()
answer = models.TextField()
2011-10-24 00:19:04 +00:00
2011-10-21 01:50:01 +00:00
title_field = 'question'
2011-10-24 00:19:04 +00:00
fts_fields = ['question', 'answer']
2011-10-21 01:50:01 +00:00
2011-04-25 19:13:19 +00:00
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,
2011-10-21 01:52:41 +00:00
'title': self.question,
2011-09-09 23:13:27 +00:00
'answer': self.answer,
}
2011-10-21 01:50:01 +00:00
def info_dict(self):
return {
'id': self.id,
'question': self.question,
'answer': self.answer
}
2011-09-09 23:13:27 +00:00
2011-10-21 01:50:01 +00:00
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()
2011-10-24 00:19:04 +00:00
fts_fields = ['title', 'text']
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
2011-10-23 23:09:55 +00:00
class Glossary(ItfModel):
term = models.CharField(max_length=256)
definition = models.TextField()
2011-10-23 23:09:55 +00:00
title_field = 'term'
2011-10-24 00:19:04 +00:00
fts_fields = ['term', 'definition']
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,
2011-10-23 23:13:15 +00:00
'title': self.term,
2011-09-09 23:13:27 +00:00
'definition': self.definition
}
2011-10-23 23:09:55 +00:00
def info_dict(self):
return self.list_dict()
def __unicode__(self):
return self.term
2011-02-01 15:13:30 +00:00