Permalink
Browse files

Adding initial files.

  • Loading branch information...
0 parents commit 7fbc67c9af3b020e7527ceec17b50b7931320ae9 @danawoodman committed Jan 29, 2011
@@ -0,0 +1,4 @@
+*.py[co]
+*.db
+.DS_store
+*.egg-info
No changes.
@@ -0,0 +1,30 @@
+from django.contrib import admin
+from django.utils.translation import ugettext_lazy as _
+
+from flatpages_plus.forms import FlatpageForm
+from flatpages_plus.models import FlatPage
+
+
+class FlatPageAdmin(admin.ModelAdmin):
+ form = FlatpageForm
+ fieldsets = (
+ (None, {'fields': (
+ 'url',
+ 'title',
+ 'content',
+ 'sites'
+ )}),
+ (_('Advanced options'), {
+ 'classes': ('collapse',),
+ 'fields': (
+ 'enable_comments',
+ 'registration_required',
+ 'template_name'
+ )
+ }),
+ )
+ list_display = ('url', 'title')
+ list_filter = ('sites', 'enable_comments', 'registration_required')
+ search_fields = ('url', 'title')
+
+admin.site.register(FlatPage, FlatPageAdmin)
@@ -0,0 +1,15 @@
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
+from flatpages_plus.models import FlatPage
+
+
+class FlatpageForm(forms.ModelForm):
+ url = forms.RegexField(label=_("URL"), max_length=100, regex=r'^[-\w/\.~]+$',
+ help_text = _("Example: '/about/contact/'. Make sure to have leading"
+ " and trailing slashes."),
+ error_message = _("This value must contain only letters, numbers,"
+ " dots, underscores, dashes, slashes or tildes."))
+
+ class Meta:
+ model = FlatPage
@@ -0,0 +1,143 @@
+import datetime
+
+from django.db import models
+
+
+class FlatpagesManager(models.Manager):
+ """
+ Allows flatpages to be listed and sorted by various criteria.
+ """
+
+ def get_flatpages(self, sort='recent', tags=None, user=None,
+ limit=None, remove=None):
+ """
+ The main function to return flatpages based on various criteria.
+
+ This function is used by all the functions below it.
+
+ All fields are optional. If nothing is passed to this manager, it will
+ return all flatpages, sorted by most recent.
+
+ sort= What to sort the flatpages by.
+ 'recently_modified' Returns newest flatpages first.
+ '-recently_modified' Returns oldest plug ins first.
+ 'recently_created' Returns newest flatpages first.
+ '-recently_created' Returns oldest plug ins first.
+ 'popular' Returns most popular flatpages first.
+ Popular flatpages are determined by their
+ overall rating and views.
+ '-popular' Returns least popular flatpages first.
+ 'views' Returns the most viewed flatpages first.
+ '-views' Returns the least viewed flatpages first.
+ 'random' Returns random flatpages.
+
+ tags='foo,bar,baz' Returns all flatpages tagged with either 'foo',
+ 'bar', or 'baz'.
+
+ author=1 Returns all flatpages by an author with ID 1.
+
+ limit=10 Limits the number of flatpages that are
+ returned to 10 results.
+
+ remove=1 Removes a given plugin ID or list of IDs from
+ the results list.
+
+ """
+ # Get the initial queryset
+ query_set = self.get_query_set()
+
+ # Get all the filtering sort types.
+ sort_types = {
+ 'modified': query_set.order_by('modified'),
+ '-modified': query_set.order_by('-modified'),
+ 'created': query_set.order_by('created'),
+ '-created': query_set.order_by('-created'),
+ # TODO: Add popular filters....
+ # 'popular': query_set.order_by('recent'),
+ # '-popular': query_set.order_by('recent'),
+ 'views': query_set.order_by('-views'),
+ '-views': query_set.order_by('views'),
+ 'random': query_set.order_by('?')
+ }
+
+ # Try to get the type of sorting the user wants. Default to most
+ # recently modified.
+ try:
+ query_set = sort_types.get(sort, query_set)
+ except Exception, e:
+ raise e
+
+ if tags:
+ def get_tag_name(tag):
+ """Get the name of a tag."""
+ return tag.name
+ tag_list = map(get_tag_name, tags)
+ query_set = query_set.filter(tags__name__in=tag_list).distinct()
+
+ if author:
+ query_set = query_set.filter(author__pk=author)
+
+ if remove:
+ remove = str(remove)
+ remove_list = remove.split()
+ query_set = query_set.exclude(pk__in=remove_list)
+
+ # Limit the length of the result.
+ if limit:
+ query_set = query_set[:limit]
+
+ return query_set
+
+ def most_recently_modified(self, limit=None):
+ """
+ Get the most recently modified flatpages.
+ """
+ return self.get_flatpages(self, 'modified', limit=limit)
+
+ def least_recently_modified(self, limit=None):
+ """
+ Get the least recently modified flatpages.
+ """
+ return self.get_flatpages(self, '-modified', limit=limit)
+
+ def most_recently_created(self, limit=None):
+ """
+ Get the most recently created flatpages.
+ """
+ return self.get_flatpages(self, 'created', limit=limit)
+
+ def least_recently_created(self, limit=None):
+ """
+ Get the least recently created flatpages.
+ """
+ return self.get_flatpages(self, '-created', limit=limit)
+
+ def most_popular(self, limit=None):
+ """
+ Get the most popular flatpages.
+ """
+ return self.get_flatpages(self, 'popular', limit=limit)
+
+ def least_popular(self, limit=None):
+ """
+ Get the least popular flatpages.
+ """
+ return self.get_flatpages(self, '-popular', limit=limit)
+
+ def most_viewed(self, limit=None):
+ """
+ Get most viewed flatpages.
+ """
+ return self.get_flatpages(self, 'views', limit=limit)
+
+ def least_viewed(self, limit=None):
+ """
+ Get least viewed flatpages.
+ """
+ return self.get_flatpages(self, '-views', limit=limit)
+
+ def random(self, limit=None):
+ """
+ Get random flatpages.
+ """
+ return self.get_flatpages(self, 'random', limit=limit)
@@ -0,0 +1,20 @@
+from django.conf import settings
+from django.http import Http404
+
+from flatpages_plus.views import flatpage
+
+
+class FlatpageFallbackMiddleware(object):
+ def process_response(self, request, response):
+ if response.status_code != 404:
+ return response # No need to check for a flatpage for non-404 responses.
+ try:
+ return flatpage(request, request.path_info)
+ # Return the original response if any errors happened. Because this
+ # is a middleware, we can't assume the errors will be caught elsewhere.
+ except Http404:
+ return response
+ except:
+ if settings.DEBUG:
+ raise
+ return response
@@ -0,0 +1,47 @@
+from django.contrib.sites.models import Site
+from django.db import models
+from django.db.models import permalink
+from django.utils.translation import ugettext_lazy as _
+
+from taggit.managers import TaggableManager
+
+# from flatpages_plus.managers import FlatpagesManager
+
+
+class FlatPage(models.Model):
+ """
+ A static page.
+ """
+ url = models.CharField(_('URL'), max_length=100, db_index=True)
+ title = models.CharField(_('title'), max_length=200)
+ content = models.TextField(_('content'), blank=True)
+ views = models.IntegerField(_('views'), default=0, blank=True, null=True)
+ tags = TaggableManager()
+ enable_comments = models.BooleanField(_('enable comments'))
+ template_name = models.CharField(_('template name'), max_length=70, blank=True,
+ help_text=_("Example: 'flatpages/contact_page.html'. If this isn't \
+ provided, the system will use 'flatpages/default.html'."))
+ registration_required = models.BooleanField(_('registration required'),
+ help_text=_("If this is checked, only logged-in users will be able \
+ to view the page."))
+ sites = models.ManyToManyField(Site) # TODO: Make the default site, use SITE_ID (usually 1).
+ created = models.DateTimeField(_('created'), auto_now_add=True,
+ blank=True, null=True)
+ modified = models.DateTimeField(_('modified'), auto_now=True,
+ blank=True, null=True)
+
+ # objects = FlatpagesManager()
+
+ class Meta:
+ verbose_name = _('flat page')
+ verbose_name_plural = _('flat pages')
+ ordering = ('url',)
+
+ def __unicode__(self):
+ return u"%s -- %s" % (self.url, self.title)
+
+ @permalink
+ def get_absolute_url(self):
+ return ('flatpage', None, {
+ 'url': self.url
+ })
@@ -0,0 +1 @@
+{% extends "base.html" %}
@@ -0,0 +1,9 @@
+{% extends "flatpages_plus/base.html" %}
+
+{% block title %}{{ flatpage.title }}{% endblock %}
+
+{% block content %}
+
+ {{ flatpage.content|safe }}
+
+{% endblock %}
No changes.
Oops, something went wrong.

0 comments on commit 7fbc67c

Please sign in to comment.