From 5a7a6a99b6434a5bcff3f64a27a6e0dac7e15c1a Mon Sep 17 00:00:00 2001 From: Sanj Date: Fri, 18 Mar 2011 10:19:27 +0530 Subject: [PATCH] models meta changes (db change - ModelsModel -> ModelBox) --- itf/app/admin.py | 6 +- itf/app/models.py | 77 ++++++++++++++++++- itf/bestpractices/models.py | 46 +++++++++++ itf/static/js/itf/widgets.js | 2 +- .../bestpractices/BestPractice/preview.html | 1 - 5 files changed, 124 insertions(+), 8 deletions(-) diff --git a/itf/app/admin.py b/itf/app/admin.py index 847b57d..b40a577 100644 --- a/itf/app/admin.py +++ b/itf/app/admin.py @@ -1,15 +1,15 @@ from django.contrib import admin -from models import ModelsModel, ModelSort, ModelExtraButton +from models import ModelBox, ModelSort, ModelExtraButton class ButtonsInline(admin.StackedInline): model = ModelExtraButton extra = 2 -class ModelsModelAdmin(admin.ModelAdmin): +class ModelBoxAdmin(admin.ModelAdmin): inlines = [ButtonsInline] -admin.site.register(ModelsModel, ModelsModelAdmin) +admin.site.register(ModelBox, ModelBoxAdmin) admin.site.register(ModelSort) admin.site.register(ModelExtraButton) diff --git a/itf/app/models.py b/itf/app/models.py index 5641b68..2093ddc 100644 --- a/itf/app/models.py +++ b/itf/app/models.py @@ -16,6 +16,7 @@ def splitSearch(string): class ItfModel(models.Model): fts_fields = [] fk_filters = [] + related_models = [] sort_fields = [] hasComments = True @@ -26,8 +27,75 @@ class ItfModel(models.Model): return {} 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 def fts(kls, qset, search): terms = splitSearch(search) @@ -108,6 +176,7 @@ class ItfModel(models.Model): ret.append(r.list_dict()) return ret + def getField(fields, name): for f in fields: if f.name == name: @@ -115,7 +184,9 @@ def getField(fields, name): return False -class ModelsModel(models.Model): + + +class ModelBox(models.Model): model = models.ForeignKey(ContentType) friendly_name = models.CharField(max_length=256) 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) mouseover = models.CharField(max_length=512, blank=True, null=True) dialog_text = models.TextField() - model = models.ForeignKey(ModelsModel) + model = models.ForeignKey(ModelBox) def __unicode__(self): return self.mouseover diff --git a/itf/bestpractices/models.py b/itf/bestpractices/models.py index e42dfca..4b0f1d1 100644 --- a/itf/bestpractices/models.py +++ b/itf/bestpractices/models.py @@ -20,9 +20,18 @@ class BestPractice(ItfModel): modified = models.DateTimeField(auto_now=True, null=True) 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'] sort_fields = ['title'] add_form = "BestPracticeForm" + getters = ['info_dict', 'list_dict', 'no_of_stories'] def __unicode__(self): return str(self.id) + ": " + self.title @@ -69,6 +78,13 @@ class BestPractice(ItfModel): }) return images + ''' + Model get methods (getters) + ''' + def no_of_stories(self): + return BestPracticeStory.objects.filter(bestpractice=self).count() + + class BestPracticeStory(models.Model): text = models.TextField() image = models.ImageField(upload_to='upload/images/bestpractices/stories/', blank=True, null=True) @@ -91,11 +107,23 @@ class BestPracticeCategory(models.Model): def __unicode__(self): return self.name + def get_dict(self): + return { + 'name': self.name, + 'description': self.description + } + class BestPracticeLink(models.Model): url = models.URLField() text = models.TextField(blank=True) bestpractice = models.ForeignKey(BestPractice) + def get_dict(self): + return { + 'url': self.url, + 'text': self.text + } + def __unicode__(self): return self.url @@ -104,6 +132,12 @@ class BestPracticeImage(models.Model): 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 @@ -114,6 +148,12 @@ class Guideline(models.Model): title = models.CharField(max_length=512) text = models.TextField() + def get_dict(self): + return { + 'title': self.title, + 'text': self.text + } + def __unicode__(self): return self.title @@ -122,6 +162,12 @@ 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 __unicode__(self): return self.term diff --git a/itf/static/js/itf/widgets.js b/itf/static/js/itf/widgets.js index 4ed95b5..95fad80 100644 --- a/itf/static/js/itf/widgets.js +++ b/itf/static/js/itf/widgets.js @@ -274,7 +274,7 @@ Ox.ItfBox = function(options, self) { // 'itemHeight': 16, // 'itemWidth': 250, // 'orientation': 'horizontal', - 'request': function(data, callback) { + 'items': function(data, callback) { return app.api.find(getQueryParams(data), callback) }, 'id': options.id + 'List', diff --git a/itf/templates/bestpractices/BestPractice/preview.html b/itf/templates/bestpractices/BestPractice/preview.html index 9a36606..9427bef 100644 --- a/itf/templates/bestpractices/BestPractice/preview.html +++ b/itf/templates/bestpractices/BestPractice/preview.html @@ -1,6 +1,5 @@
${title}

- Category: ${category}

{{each images}} {{/each}}