Skip to content

Instantly share code, notes, and snippets.

@notanumber
Created October 8, 2009 16:55
Show Gist options
  • Save notanumber/205157 to your computer and use it in GitHub Desktop.
Save notanumber/205157 to your computer and use it in GitHub Desktop.
diff --git a/haystack/models.py b/haystack/models.py
index ab5f401..7777bf8 100644
--- a/haystack/models.py
+++ b/haystack/models.py
@@ -3,6 +3,7 @@ from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils.encoding import force_unicode
from django.utils.text import capfirst
+from django.utils.translation import ugettext_lazy as _
# Not a Django model, but tightly tied to them and there doesn't seem to be a
@@ -118,3 +119,18 @@ class SearchResult(object):
self._stored_fields[fieldname] = getattr(self, fieldname, u'')
return self._stored_fields
+
+
+class StaticPage(models.Model):
+ url = models.URLField(_('url'), max_length=255)
+ content = models.TextField(_('content'))
+
+ class Meta:
+ verbose_name = _('static page')
+ verbose_name_plural = _('static pages')
+
+ def __unicode__(self):
+ return url
+
+ def get_absolute_url(self):
+ return self.url
\ No newline at end of file
diff --git a/haystack/sites.py b/haystack/sites.py
index 7ed76ac..bea6c07 100644
--- a/haystack/sites.py
+++ b/haystack/sites.py
@@ -56,6 +56,22 @@ class SearchSite(object):
self._teardown(model, self._registry[model])
del(self._registry[model])
+ def static_pages(self, *args):
+ from haystack.models import StaticPage
+ from django.core.urlresolvers import reverse
+ from django.template.loader import render_to_string
+
+ for (url, kwargs) in args:
+ try:
+ page = StaticPage.objects.get(url=reverse(url))
+ except StaticPage.DoesNotExist:
+ page = StaticPage(url=reverse(url))
+
+ page.content = render_to_string(dict(kwargs)['template'])
+ page.save()
+
+ self.register(StaticPage)
+
def _setup(self, model, index):
index._setup_save(model)
index._setup_delete(model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment