changed model structure to have ModelExtra to be able to add more options to a model class

This commit is contained in:
Sanj 2011-08-27 16:24:45 +05:30
parent 77d376a997
commit 54b69c6d5e
6 changed files with 61 additions and 14 deletions

View File

@ -6,7 +6,7 @@ from ox.django.fields import DictField
from django.core.paginator import Paginator, InvalidPage, EmptyPage from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from insidepages.models import ModuleTab from insidepages.models import ModuleTab, ModelExtra
def splitSearch(string): def splitSearch(string):
ret = [] ret = []
@ -38,7 +38,8 @@ class ItfModel(models.Model):
def get_tab(self): def get_tab(self):
ctype = ContentType.objects.get(model=self.__class__._meta.module_name) ctype = ContentType.objects.get(model=self.__class__._meta.module_name)
tab = ModuleTab.objects.filter(model=ctype)[0] modelextra = ModelExtra.objects.filter(model=ctype)[0]
tab = ModuleTab.objects.filter(model=modelextra)[0]
return tab return tab
def get_absolute_url(self): def get_absolute_url(self):

View File

@ -8,9 +8,23 @@ class TabsInline(admin.StackedInline):
class ModuleAdmin(admin.ModelAdmin): class ModuleAdmin(admin.ModelAdmin):
inlines = [TabsInline] inlines = [TabsInline]
prepopulated_fields = {'slug': ('title',)}
# list_display = ('name', 'order',) # list_display = ('name', 'order',)
# list_editable = ['order'] # list_editable = ['order']
class ButtonsInline(admin.StackedInline):
model = ModelExtra.buttons.through
extra = 3
class SortInline(admin.StackedInline):
model = ModelSort
extra = 4
class ModelExtraAdmin(admin.ModelAdmin):
inlines = [SortInline, ButtonsInline]
admin.site.register(Module, ModuleAdmin) admin.site.register(Module, ModuleAdmin)
admin.site.register(ModelExtra, ModelExtraAdmin)
admin.site.register(ModelButton)
#admin.site.register(SliderBox, SliderBoxAdmin) #admin.site.register(SliderBox, SliderBoxAdmin)

View File

@ -17,7 +17,7 @@ class ModuleTab(models.Model):
module = models.ForeignKey(Module) module = models.ForeignKey(Module)
title = models.CharField(max_length=64) title = models.CharField(max_length=64)
text = models.TextField() text = models.TextField()
model = models.ForeignKey(ContentType) model = models.ForeignKey("ModelExtra")
is_default = models.BooleanField(default=False) is_default = models.BooleanField(default=False)
order = models.IntegerField(default=1) order = models.IntegerField(default=1)
is_displayed = models.BooleanField(default=True) is_displayed = models.BooleanField(default=True)
@ -28,6 +28,43 @@ class ModuleTab(models.Model):
def __unicode__(self): def __unicode__(self):
return self.title return self.title
def get_list(self, options): def model_class(self):
return self.model.model_class().get_list(options) return self.model.model.model_class()
def get_list(self, options):
return self.model_class().get_list(options)
class ModelExtra(models.Model):
model = models.ForeignKey(ContentType)
friendly_name = models.CharField(max_length=128)
has_comments = models.BooleanField(default=False)
buttons = models.ManyToManyField('ModelButton', through='ModelExtraButton', blank=True)
def __unicode__(self):
return self.friendly_name
class ModelSort(models.Model):
model = models.ForeignKey(ModelExtra)
operator = models.CharField(max_length=1)
field_name = models.CharField(max_length=64)
friendly_name = models.CharField(max_length=128)
def __unicode__(self):
return self.friendly_name
class ModelButton(models.Model):
name = models.CharField(max_length=64)
template = models.TextField(blank=True)
def __unicode__(self):
return self.name
class ModelExtraButton(models.Model):
modelextra = models.ForeignKey(ModelExtra)
modelbutton = models.ForeignKey(ModelButton)
parameters = models.CharField(max_length=64, blank=True)

View File

@ -9,7 +9,7 @@ def main(request, module_slug):
tabs = m.moduletab_set.all() tabs = m.moduletab_set.all()
default_tab = tabs[0] default_tab = tabs[0]
list_options = {} #get some options as GET params, etc. to potentially pass to get_list list_options = {} #get some options as GET params, etc. to potentially pass to get_list
default_tab_list = default_tab.model.model_class().get_list(list_options) default_tab_list = default_tab.model_class().get_list(list_options)
context = RequestContext(request, { context = RequestContext(request, {
'title': m.title, 'title': m.title,
'default_tab': tabs[0], 'default_tab': tabs[0],
@ -34,7 +34,7 @@ def get_list(request):
def get_details(request): def get_details(request):
tab_id = request.GET.get("tab_id", 0) tab_id = request.GET.get("tab_id", 0)
tab = get_object_or_404(ModuleTab, pk=tab_id) tab = get_object_or_404(ModuleTab, pk=tab_id)
model_class = tab.model.model_class() model_class = tab.model_class()
object_id = request.GET.get("object_id", 0) object_id = request.GET.get("object_id", 0)
obj = get_object_or_404(model_class, pk=object_id) obj = get_object_or_404(model_class, pk=object_id)
return render_to_json_response(obj.insidepage_dict()) return render_to_json_response(obj.insidepage_dict())

View File

@ -148,7 +148,7 @@ INSTALLED_APPS = (
'tagging', 'tagging',
'app', 'app',
'api', 'api',
'boxes', # 'boxes',
'frontpage', 'frontpage',
# 'solango', # 'solango',
'multilingual', 'multilingual',

View File

@ -24,12 +24,7 @@ function getLi(item) {
var $li = $('<li />').addClass("tabListItem"); var $li = $('<li />').addClass("tabListItem");
var $a = $('<a />').attr("href", '#').attr("data-id", item.id).appendTo($li); var $a = $('<a />').attr("href", '#').attr("data-id", item.id).appendTo($li);
// console.log(item.title.length); // console.log(item.title.length);
if (item.title.length > titleLength) { var $span = $('<span />').text(item.title).appendTo($a);
var title = item.title.substring(0,titleLength) + "...";
} else {
var title = item.title;
}
var $span = $('<span />').text(title).appendTo($a);
return $li; return $li;
} }