Skip to content
Browse files

added custom markup support through AGORA_PARSER

  • Loading branch information...
1 parent 77f43dc commit ed86929babb481739c08d06d3bd1eca306c6d187 @brosner brosner committed Dec 16, 2011
Showing with 38 additions and 1 deletion.
  1. +10 −1 agora/models.py
  2. +4 −0 agora/settings.py
  3. +24 −0 agora/utils.py
View
11 agora/models.py
@@ -1,13 +1,17 @@
import datetime
+import functools
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import post_save, post_delete, pre_delete
from django.utils import simplejson as json
+from django.utils.html import conditional_escape
from django.contrib.auth.models import User
from agora.managers import ForumThreadManager
+from agora.settings import PARSER
+from agora.utils import load_path_attr
# this is the glue to the activity events framework, provided as a no-op here
@@ -234,13 +238,18 @@ def restore(cls, in_):
class ForumPost(models.Model):
author = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_related")
- # @@@ support markup
content = models.TextField()
+ content_html = models.TextField()
created = models.DateTimeField(default=datetime.datetime.now, editable=False)
class Meta:
abstract = True
+ def save(self, **kwargs):
+ render_func = functools.partial(load_path_attr(PARSER[0], **PARSER[1]))
+ self.content_html = conditional_escape(render_func(self.content))
+ super(ForumPost, self).save(**kwargs)
+
# allow editing for short period after posting
def editable(self, user):
if user == self.author:
View
4 agora/settings.py
@@ -0,0 +1,4 @@
+from django.conf import settings
+
+
+PARSER = getattr(settings, "AGORA_PARSER", ["agora.utils.default_text", {}])
View
24 agora/utils.py
@@ -0,0 +1,24 @@
+from django.core.exceptions import ImproperlyConfigured
+from django.utils.html import urlize, linebreaks
+try:
+ from django.utils.importlib import import_module
+except ImportError:
+ from importlib import import_module
+
+
+def load_path_attr(path):
+ i = path.rfind(".")
+ module, attr = path[:i], path[i+1:]
+ try:
+ mod = import_module(module)
+ except ImportError, e:
+ raise ImproperlyConfigured("Error importing %s: '%s'" % (module, e))
+ try:
+ attr = getattr(mod, attr)
+ except AttributeError:
+ raise ImproperlyConfigured("Module '%s' does not define a '%s'" % (module, attr))
+ return attr
+
+
+def default_text(text):
+ return linebreaks(urlize(text))

0 comments on commit ed86929

Please sign in to comment.
Something went wrong with that request. Please try again.