Permalink
Browse files

Initial

  • Loading branch information...
0 parents commit 6058e4e2b086b0e7cb7cf2e0bdfbdb48d50eaf19 @zgohr committed May 16, 2012
@@ -0,0 +1,2 @@
+*.swp
+*.pyc
@@ -0,0 +1,23 @@
+Copyright (c) Zach Gohr and individual contributors.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,11 @@
+## Mezzanine Page View
+
+Limit viewable CMS pages by group.
+
+To install, add ```mezzanine_pageview.middleware.PageViewMiddleware``` to your ```MIDDLEWARE_CLASSES``` and ```mezzanine_pageview``` to your ```INSTALLED_APPS``` somewhere above ```mezzanine.pages```. Finally, ```./manage.py syncdb```.
+
+### What's going on
+
+I needed the ability to limit the pages that any particular user can see. This application is a middleware to throw a 404 error if the logged user does not have access to a group required to view a particular page, and a templatetag as well as modified navigation templates so the links do not get displayed.
+
+Mezzanine has a concept called [page processors](http://mezzanine.jupo.org/docs/content-architecture.html#page-processors) that performs something between middelware and context processors for custom ```Page``` models. Unfortunately you cannot currently use page processors on the generic ```Page``` model.
No changes.
@@ -0,0 +1,7 @@
+from django.contrib import admin
+from models import PageViewGroup
+
+class PageViewGroupAdmin(admin.ModelAdmin):
+ list_display = ("group", "page")
+
+admin.site.register(PageViewGroup, PageViewGroupAdmin)
@@ -0,0 +1,22 @@
+from django.http import Http404
+from page_processors import can_access
+from mezzanine.pages.models import Page
+
+class PageViewMiddleware(object):
+ """
+ Mezzanine does not allow page_processors on a generic Page
+ so for the time being we're going to use middleware to
+ throw a 404 error if the user is not in the necessary group
+ """
+ def process_request(self, request):
+ slug = request.path
+ if slug != "/":
+ slug = slug.strip("/")
+ pages_for_user = Page.objects.published(request.user)
+ try:
+ page = pages_for_user.get(slug=slug)
+ if not can_access(request.user, page):
+ raise Http404
+ except Page.DoesNotExist:
+ pass
+ return {}
@@ -0,0 +1,97 @@
+# -*- coding: 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 model 'PageViewGroup'
+ db.create_table('page_view_pageviewgroup', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('group', self.gf('django.db.models.fields.related.ForeignKey')(related_name='viewablepages', to=orm['auth.Group'])),
+ ('page', self.gf('django.db.models.fields.related.ForeignKey')(related_name='viewgroup', to=orm['pages.Page'])),
+ ))
+ db.send_create_signal('page_view', ['PageViewGroup'])
+
+
+ def backwards(self, orm):
+ # Deleting model 'PageViewGroup'
+ db.delete_table('page_view_pageviewgroup')
+
+
+ models = {
+ 'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ 'auth.permission': {
+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'generic.assignedkeyword': {
+ 'Meta': {'ordering': "('_order',)", 'object_name': 'AssignedKeyword'},
+ '_order': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'keyword': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'assignments'", 'to': "orm['generic.Keyword']"}),
+ 'object_pk': ('django.db.models.fields.IntegerField', [], {})
+ },
+ 'generic.keyword': {
+ 'Meta': {'object_name': 'Keyword'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']"}),
+ 'slug': ('django.db.models.fields.CharField', [], {'max_length': '2000', 'null': 'True', 'blank': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '500'})
+ },
+ 'page_view.pageviewgroup': {
+ 'Meta': {'object_name': 'PageViewGroup'},
+ 'group': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'viewablepages'", 'to': "orm['auth.Group']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'viewgroup'", 'to': "orm['pages.Page']"})
+ },
+ 'pages.page': {
+ 'Meta': {'ordering': "('titles',)", 'object_name': 'Page'},
+ '_order': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+ 'content_model': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'expiry_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+ 'gen_description': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'in_footer': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'in_navigation': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'keywords': ('mezzanine.generic.fields.KeywordsField', [], {'object_id_field': "'object_pk'", 'to': "orm['generic.AssignedKeyword']"}),
+ 'keywords_string': ('django.db.models.fields.CharField', [], {'max_length': '500', 'blank': 'True'}),
+ 'login_required': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['pages.Page']"}),
+ 'publish_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+ 'short_url': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'}),
+ 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']"}),
+ 'slug': ('django.db.models.fields.CharField', [], {'max_length': '2000', 'null': 'True', 'blank': 'True'}),
+ 'status': ('django.db.models.fields.IntegerField', [], {'default': '2'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
+ 'titles': ('django.db.models.fields.CharField', [], {'max_length': '1000', 'null': 'True'})
+ },
+ 'sites.site': {
+ 'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"},
+ 'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ }
+ }
+
+ complete_apps = ['page_view']
@@ -0,0 +1,11 @@
+from django.db import models
+from django.contrib.auth.models import Group
+from mezzanine.pages.models import Page
+
+class PageViewGroup(models.Model):
+ class Meta:
+ verbose_name = "Page View Group"
+ verbose_name_plural = "Page View Groups"
+
+ group = models.ForeignKey(Group, related_name="viewablepages")
+ page = models.ForeignKey(Page, related_name="viewgroup")
@@ -0,0 +1,19 @@
+#from django.http import Http404
+#from mezzanine.pages.page_processors import processor_for
+#from mezzanine.pages.models import Page
+#from mezzanine.galleries.models import Gallery
+from models import PageViewGroup
+
+def can_access(user, page):
+ """
+ If the page has groups associated with it, the user
+ must have access to the group
+ """
+ page_groups = PageViewGroup.objects.filter(page=page)
+ groups = page_groups.filter(group__in=user.groups.all())
+ return page_groups.count() == 0 or groups.count() > 0
+
+"""
+@processor_for(Page)
+def page_view(request, page):
+"""
@@ -0,0 +1,40 @@
+{% load i18n pages_tags page_view_tags %}
+
+{% spaceless %}
+{% for page in page_branch %}
+ {% if page.is_primary %}
+ {% if forloop.first %}
+ <ul>
+ {% endif %}
+ {% if page.in_footer and user|can_access_link:page %}
+ <li><ul>
+ {% endif %}
+ {% endif %}
+
+ {% if page.in_footer and user|can_access_link:page %}
+ {% if forloop.first and not page.parent.in_footer and not page.is_primary and not user|can_access_link:page.parent %}
+ <li><ul>
+ {% endif %}
+ <li {% if page.is_current_or_ascendant %}class="active"{% endif %}>
+ <a href="{{ page.get_absolute_url }}">{{ page.title }}</a></li>
+ {% endif %}
+
+ {% page_menu page %}
+
+ {% if page.in_footer and user|can_access_link:page %}
+ {% if forloop.first and not page.parent.in_footer and not page.is_primary and not user|can_access_link:page.parent %}
+ </li></ul>
+ {% endif %}
+ {% endif %}
+
+ {% if page.is_primary %}
+ {% if page.in_footer and user|can_access_link:page %}
+ </ul></li>
+ {% endif %}
+ {% if forloop.last %}
+ </ul>
+ {% endif %}
+ {% endif %}
+
+{% endfor %}
+{% endspaceless %}
@@ -0,0 +1,21 @@
+{% load pages_tags i18n page_view_tags %}
+
+{% spaceless %}
+<ul id="primary-menu" class="nav pull-right">
+ {% for page in page_branch %}
+ {% if forloop.first %}
+ <li class="divider-vertical"></li>
+ <li id="primary-menu-home" class="first{% if on_home %} active{% endif %}">
+ <a href="{% url home %}">{% trans "Home" %}</a>
+ </li>
+ <li class="divider-vertical"></li>
+ {% endif %}
+ {% if page.in_navigation and user|can_access_link:page %} <li id="primary-menu-{{ page.html_id }}"
+ class="{% if page.is_current_or_ascendant %}active{% endif %}{% if forloop.last %} last{% endif %}">
+ <a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
+ </li>
+ <li class="divider-vertical"></li>
+ {% endif %}
+ {% endfor %}
+</ul>
+{% endspaceless %}
@@ -0,0 +1,28 @@
+{% load i18n pages_tags page_view_tags %}
+
+{% spaceless %}
+{% if page_branch_in_navigation %}
+<ul class="nav nav-list navlist-menu-level-{{ branch_level }}">
+ {% for page in page_branch %}
+ {% if page.is_primary and forloop.first %}
+ <li class="first{% if on_home %} active{% endif %}">
+ <a href="{% url home %}">{% trans "Home" %}</a>
+ </li>
+ {% endif %}
+ {% if page.in_navigation and user|can_access_link:page %}
+ <li class="
+ {% if page.is_current %} active{% endif %}
+ {% if not page.is_primary and forloop.first %} first{% endif %}
+ {% if forloop.last %} last{% endif %}"
+ id="tree-menu-{{ page.html_id }}">
+ <a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
+ {# remove this if tag to always show all nav items #}
+ {% if settings.PAGES_MENU_SHOW_ALL or page.is_current_or_ascendant %}
+ {% page_menu page %}
+ {% endif %}
+ </li>
+ {% endif %}
+ {% endfor %}
+</ul>
+{% endif %}
+{% endspaceless %}
@@ -0,0 +1,8 @@
+from django import template
+from .. page_processors import can_access
+
+register = template.Library()
+
+@register.filter
+def can_access_link(user, page):
+ return can_access(user, page)
@@ -0,0 +1,16 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.assertEqual(1 + 1, 2)
@@ -0,0 +1 @@
+# Create your views here.

0 comments on commit 6058e4e

Please sign in to comment.