Permalink
Browse files

Merge pull request #44 from nbadoux/master

Added field to ContentItem model
  • Loading branch information...
2 parents 6257be1 + 2b4f49b commit 6a796257c0b983401f4ca0835657281923bf99e5 @dbunskoek dbunskoek committed Jan 10, 2012
Showing with 98 additions and 4 deletions.
  1. +83 −0 fiber/migrations/0011_auto__add_field_contentitem_used_on_pages_data.py
  2. +15 −4 fiber/models.py
@@ -0,0 +1,83 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding field 'ContentItem.used_on_pages_data'
+ db.add_column('fiber_contentitem', 'used_on_pages_data', self.gf('fiber.utils.json.JSONField')(null=True, blank=True), keep_default=False)
+
+
+ def backwards(self, orm):
+
+ # Deleting field 'ContentItem.used_on_pages_data'
+ db.delete_column('fiber_contentitem', 'used_on_pages_data')
+
+
+ models = {
+ 'fiber.contentitem': {
+ 'Meta': {'object_name': 'ContentItem'},
+ 'content_html': ('fiber.utils.fields.FiberHTMLField', [], {}),
+ 'content_markup': ('fiber.utils.fields.FiberMarkupField', [], {}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'metadata': ('fiber.utils.json.JSONField', [], {'null': 'True', 'blank': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'protected': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'template_name': ('django.db.models.fields.CharField', [], {'max_length': '70', 'blank': 'True'}),
+ 'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'used_on_pages_data': ('fiber.utils.json.JSONField', [], {'null': 'True', 'blank': 'True'})
+ },
+ 'fiber.file': {
+ 'Meta': {'ordering': "('file',)", 'object_name': 'File'},
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'file': ('django.db.models.fields.files.FileField', [], {'max_length': '255'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
+ },
+ 'fiber.image': {
+ 'Meta': {'ordering': "('image',)", 'object_name': 'Image'},
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'height': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '255'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'width': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'})
+ },
+ 'fiber.page': {
+ 'Meta': {'ordering': "('tree_id', 'lft')", 'object_name': 'Page'},
+ 'content_items': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['fiber.ContentItem']", 'through': "orm['fiber.PageContentItem']", 'symmetrical': 'False'}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'mark_current_regexes': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'metadata': ('fiber.utils.json.JSONField', [], {'null': 'True', 'blank': 'True'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'subpages'", 'null': 'True', 'to': "orm['fiber.Page']"}),
+ 'protected': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'redirect_page': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'redirected_pages'", 'null': 'True', 'to': "orm['fiber.Page']"}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'show_in_menu': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'template_name': ('django.db.models.fields.CharField', [], {'max_length': '70', 'blank': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'url': ('fiber.utils.fields.FiberURLField', [], {'max_length': '255', 'blank': 'True'})
+ },
+ 'fiber.pagecontentitem': {
+ 'Meta': {'object_name': 'PageContentItem'},
+ 'block_name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'content_item': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'page_content_items'", 'to': "orm['fiber.ContentItem']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'page_content_items'", 'to': "orm['fiber.Page']"}),
+ 'sort': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'})
+ }
+ }
+
+ complete_apps = ['fiber']
View
@@ -26,6 +26,7 @@ class ContentItem(models.Model):
protected = models.BooleanField(_('protected'), default=False)
metadata = JSONField(_('metadata'), blank=True, null=True, schema=METADATA_CONTENT_SCHEMA, prefill_from='fiber.models.ContentItem')
template_name = models.CharField(_('template name'), blank=True, max_length=70)
+ used_on_pages_data = JSONField(_('used on pages'), blank=True, null=True)
objects = managers.ContentItemManager()
@@ -51,15 +52,21 @@ def get_change_url(self):
named_url = 'fiber_admin:%s_%s_change' % (self._meta.app_label, self._meta.object_name.lower())
return reverse(named_url, args=(self.id, ))
- def get_used_on_pages_json(self):
- page_content_items = self.page_content_items.all()
+ def set_used_on_pages_json(self):
json_pages = []
- for page_content_item in page_content_items:
+ for page_content_item in self.page_content_items.all():
json_pages.append({
'title': page_content_item.page.title,
'url': page_content_item.page.get_absolute_url(),
})
- return simplejson.dumps(json_pages)
+ self.used_on_pages_data = json_pages
+ self.save()
+
+ def get_used_on_pages_json(self):
+ if not self.used_on_pages_data:
+ self.set_used_on_pages_json()
+
+ return simplejson.dumps(self.used_on_pages_data)
class Page(MPTTModel):
@@ -206,6 +213,10 @@ class PageContentItem(models.Model):
objects = managers.PageContentItemManager()
+ def save(self, *args, **kwargs):
+ super(PageContentItem, self).save(*args, **kwargs)
+ self.content_item.set_used_on_pages_json()
+
class Image(models.Model):
created = models.DateTimeField(_('created'), auto_now_add=True)

0 comments on commit 6a79625

Please sign in to comment.