from django.utils import simplejson

from django.db import models
from django.contrib.auth.models import User, Group
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from samklang_utils import markdown

class Page(models.Model):
    url = models.CharField(_('URL'), max_length=100, db_index=True)
    name = models.CharField(_('name'), max_length=50, help_text=_("Used for page list. Remember to create a heading in the content field."))
    content = models.TextField(_('content'), blank=True)
    content_html = models.TextField(null=True, blank=True)
    site = models.ForeignKey(Site, verbose_name=_('site'))
    user = models.ForeignKey(User, verbose_name=_('user'))
    group = models.ForeignKey(Group, related_name='pages', verbose_name=_('read permission'), null=True, blank=True)
    admingroup = models.ForeignKey(Group, related_name='administers_pages', verbose_name=_('write permission'))
    #document_class = models.SlugField(max_length=20, blank=True)

    class Meta:
        verbose_name = _('page')
        verbose_name_plural = _('pages')
        ordering = ('url',)
        db_table = 'samklang_page'

    def __unicode__(self):
        return u"%s -- %s" % (self.url, self.name)

    @models.permalink
    def get_absolute_url(self):
        return self.url

    def save(self, *args, **kwargs):
        self.content_html = markdown(self.content.strip())
        super(Page, self).save(*args, **kwargs)


@@ -1470,8 +1470,998 @@
 wargs)%0A%0A
+class PageWidget(models.Model):%0A    page = models.ForeignKey(Page, verbose_name=_('page'), related_name='widget')%0A    widget_name = models.CharField(max_length=30)%0A    position = models.IntegerField(verbose_name=_('position'))%0A    options = models.TextField(blank=True, verbose_name=_('options'))%0A%0A    class Meta:%0A        verbose_name = _('Page widget')%0A        verbose_name_plural = _('Page widgets')%0A        ordering = ('position', 'widget_name')%0A        db_table = 'samklang_pagewidget'%0A%0A    def __unicode__(self):%0A        return u%22%25s: %25s%22 %25 (self.widget_name, self.page.name)%0A%0A    def widget(self):%0A        app, widget_name = self.widget_name.rsplit(%22.%22, 1)%0A        imp = __import__(app + %22.pagewidgets%22, globals(), locals(), %5Bwidget_name%5D)%0A        widget_class = getattr(imp, widget_name)%0A        try:%0A            options = simplejson.loads(self.options)%0A        except simplejson.JSONDecodeError:%0A            options = %7B%7D%0A        widget = widget_class(options)%0A        return widget%0A%0A

Revert removal of pagewidgets; needed for migration