models meta changes (db change - ModelsModel -> ModelBox)

This commit is contained in:
Sanj 2011-03-18 10:19:27 +05:30
parent 51e5cace14
commit 5a7a6a99b6
5 changed files with 124 additions and 8 deletions

View File

@ -1,15 +1,15 @@
from django.contrib import admin from django.contrib import admin
from models import ModelsModel, ModelSort, ModelExtraButton from models import ModelBox, ModelSort, ModelExtraButton
class ButtonsInline(admin.StackedInline): class ButtonsInline(admin.StackedInline):
model = ModelExtraButton model = ModelExtraButton
extra = 2 extra = 2
class ModelsModelAdmin(admin.ModelAdmin): class ModelBoxAdmin(admin.ModelAdmin):
inlines = [ButtonsInline] inlines = [ButtonsInline]
admin.site.register(ModelsModel, ModelsModelAdmin) admin.site.register(ModelBox, ModelBoxAdmin)
admin.site.register(ModelSort) admin.site.register(ModelSort)
admin.site.register(ModelExtraButton) admin.site.register(ModelExtraButton)

View File

@ -16,6 +16,7 @@ def splitSearch(string):
class ItfModel(models.Model): class ItfModel(models.Model):
fts_fields = [] fts_fields = []
fk_filters = [] fk_filters = []
related_models = []
sort_fields = [] sort_fields = []
hasComments = True hasComments = True
@ -26,7 +27,74 @@ class ItfModel(models.Model):
return {} return {}
def info_dict(self): def info_dict(self):
return {} return self.get_dict()
def get_dict(self):
return self.get(self._get_fields().keys() + self.related_models)
def get(self, props):
typ = type(props)
if typ == list:
ret = {}
for p in props:
ret[p] = self._get_property(p)
return ret
elif typ == str:
return _get_property(props)
else:
return False
def _get_property(self, prop):
fields = self._get_fields()
if prop in fields.keys():
field_type = fields[prop]
if field_type in ["TextField", "CharField", "IntegerField"]:
return self.__getattribute__(prop)
elif field_type == "ImageField":
imagefield = self.__getattribute__(prop)
return imagefield.url if imagefield.name != '' else ''
elif field_type == "ForeignKey":
return self._get_fk(prop)
elif field_type == "ManyToMany":
return self._get_m2m(prop)
elif prop.lower() in self.related_models:
return self._get_related(prop)
else:
try:
val = self.__getattribute__(prop)
if type(val).__name__ == 'instancemethod' and prop in self.getters:
return val()
else:
return val
except:
return False #FIXME
def _get_fk(self, prop):
prop = prop.replace("_id", "")
return self.__getattribute__(prop).get_dict()
# def _get_related_objects(self, fk_field):
def _get_m2m(self, prop):
ret = []
for o in self.__getattribute__(prop).all():
ret.append(o.get_dict())
return ret
def _get_related(self, prop):
attr = prop.lower() + "_set"
ret = []
for o in self.__getattribute__(attr).all():
ret.append(o.get_dict())
return ret
@classmethod
def _get_fields(kls):
ret = {}
for f in kls._meta.fields:
ret[f.get_attname()] = type(f).__name__
return ret
@classmethod @classmethod
def fts(kls, qset, search): def fts(kls, qset, search):
@ -108,6 +176,7 @@ class ItfModel(models.Model):
ret.append(r.list_dict()) ret.append(r.list_dict())
return ret return ret
def getField(fields, name): def getField(fields, name):
for f in fields: for f in fields:
if f.name == name: if f.name == name:
@ -115,7 +184,9 @@ def getField(fields, name):
return False return False
class ModelsModel(models.Model):
class ModelBox(models.Model):
model = models.ForeignKey(ContentType) model = models.ForeignKey(ContentType)
friendly_name = models.CharField(max_length=256) friendly_name = models.CharField(max_length=256)
friendly_name_singular = models.CharField(max_length=256, blank=True, null=True) friendly_name_singular = models.CharField(max_length=256, blank=True, null=True)
@ -156,7 +227,7 @@ class ModelExtraButton(models.Model):
icon = models.CharField(max_length=64) icon = models.CharField(max_length=64)
mouseover = models.CharField(max_length=512, blank=True, null=True) mouseover = models.CharField(max_length=512, blank=True, null=True)
dialog_text = models.TextField() dialog_text = models.TextField()
model = models.ForeignKey(ModelsModel) model = models.ForeignKey(ModelBox)
def __unicode__(self): def __unicode__(self):
return self.mouseover return self.mouseover

View File

@ -20,9 +20,18 @@ class BestPractice(ItfModel):
modified = models.DateTimeField(auto_now=True, null=True) modified = models.DateTimeField(auto_now=True, null=True)
fts_fields = ['title', 'story', 'guideline', 'law', 'theatre', 'quick_howto'] fts_fields = ['title', 'story', 'guideline', 'law', 'theatre', 'quick_howto']
related_models = ["bestpracticestory", "bestpracticelink", "bestpracticeimage"]
extra_buttons = [
{
'title': 'Foo button',
'mouseover': 'blah',
'dialog': ''
}
]
fk_filters = ['category'] fk_filters = ['category']
sort_fields = ['title'] sort_fields = ['title']
add_form = "BestPracticeForm" add_form = "BestPracticeForm"
getters = ['info_dict', 'list_dict', 'no_of_stories']
def __unicode__(self): def __unicode__(self):
return str(self.id) + ": " + self.title return str(self.id) + ": " + self.title
@ -69,6 +78,13 @@ class BestPractice(ItfModel):
}) })
return images return images
'''
Model get methods (getters)
'''
def no_of_stories(self):
return BestPracticeStory.objects.filter(bestpractice=self).count()
class BestPracticeStory(models.Model): class BestPracticeStory(models.Model):
text = models.TextField() text = models.TextField()
image = models.ImageField(upload_to='upload/images/bestpractices/stories/', blank=True, null=True) image = models.ImageField(upload_to='upload/images/bestpractices/stories/', blank=True, null=True)
@ -91,11 +107,23 @@ class BestPracticeCategory(models.Model):
def __unicode__(self): def __unicode__(self):
return self.name return self.name
def get_dict(self):
return {
'name': self.name,
'description': self.description
}
class BestPracticeLink(models.Model): class BestPracticeLink(models.Model):
url = models.URLField() url = models.URLField()
text = models.TextField(blank=True) text = models.TextField(blank=True)
bestpractice = models.ForeignKey(BestPractice) bestpractice = models.ForeignKey(BestPractice)
def get_dict(self):
return {
'url': self.url,
'text': self.text
}
def __unicode__(self): def __unicode__(self):
return self.url return self.url
@ -104,6 +132,12 @@ class BestPracticeImage(models.Model):
caption = models.CharField(max_length=512, blank=True) caption = models.CharField(max_length=512, blank=True)
bestpractice = models.ForeignKey(BestPractice) bestpractice = models.ForeignKey(BestPractice)
def get_dict(self):
return {
'image': self.image.url,
'caption': self.caption,
}
def __unicode__(self): def __unicode__(self):
return self.caption return self.caption
@ -114,6 +148,12 @@ class Guideline(models.Model):
title = models.CharField(max_length=512) title = models.CharField(max_length=512)
text = models.TextField() text = models.TextField()
def get_dict(self):
return {
'title': self.title,
'text': self.text
}
def __unicode__(self): def __unicode__(self):
return self.title return self.title
@ -122,6 +162,12 @@ class Glossary(models.Model):
term = models.CharField(max_length=256) term = models.CharField(max_length=256)
definition = models.TextField() definition = models.TextField()
def get_dict(self):
return {
'term': self.term,
'definition': self.definition
}
def __unicode__(self): def __unicode__(self):
return self.term return self.term

View File

@ -274,7 +274,7 @@ Ox.ItfBox = function(options, self) {
// 'itemHeight': 16, // 'itemHeight': 16,
// 'itemWidth': 250, // 'itemWidth': 250,
// 'orientation': 'horizontal', // 'orientation': 'horizontal',
'request': function(data, callback) { 'items': function(data, callback) {
return app.api.find(getQueryParams(data), callback) return app.api.find(getQueryParams(data), callback)
}, },
'id': options.id + 'List', 'id': options.id + 'List',

View File

@ -1,6 +1,5 @@
<div class="itfPreview"> <div class="itfPreview">
<span class="itfPreviewTitle">${title}</span><br /><br /> <span class="itfPreviewTitle">${title}</span><br /><br />
<span class="itfPreviewSub">Category: </span><span class="itfPreviewText">${category}</span> <br /><br />
<div class="itfPreviewImages"> <div class="itfPreviewImages">
{{each images}} <img src="${$value.thumb}" width="200" title="${$value.caption}" /> {{/each}} {{each images}} <img src="${$value.thumb}" width="200" title="${$value.caption}" /> {{/each}}
</div> </div>