From cb4c7ae9319ff59a0e6c0eeebb0e7094c7da7df5 Mon Sep 17 00:00:00 2001 From: James Turk Date: Thu, 7 May 2009 13:27:21 -0400 Subject: [PATCH] added post_status field to subsites to control posting --- brainstorm/models.py | 16 ++++++++++++++++ brainstorm/templates/brainstorm/idea_form.html | 8 +++++--- brainstorm/views.py | 9 ++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/brainstorm/models.py b/brainstorm/models.py index 26fd3fa..e18bc6e 100644 --- a/brainstorm/models.py +++ b/brainstorm/models.py @@ -5,6 +5,13 @@ from django.contrib.contenttypes import generic from django.contrib.comments.models import Comment import secretballot +ALLOW_ALL, REQUIRE_LOGIN, DISALLOW_ALL = range(3) +SUBSITE_POST_STATUS = ( + (ALLOW_ALL, 'Allow All Posts'), + (REQUIRE_LOGIN, 'Require Login'), + (DISALLOW_ALL, 'Allow No Posts'), +) + class Subsite(models.Model): slug = models.SlugField(max_length=50, primary_key=True) name = models.CharField(max_length=50) @@ -14,6 +21,7 @@ class Subsite(models.Model): ideas_per_page = models.IntegerField(default=10) allow_anonymous_ideas = models.BooleanField(default=False) + post_status = models.IntegerField(default=ALLOW_ALL, choices=SUBSITE_POST_STATUS) def __unicode__(self): return self.name @@ -21,6 +29,14 @@ class Subsite(models.Model): def get_absolute_url(self): return reverse('subsite', args=[self.slug]) + def user_can_post(self, user): + if self.post_status == DISALLOW_ALL: + return False + elif self.post_status == ALLOW_ALL: + return True + elif self.post_status == REQUIRE_LOGIN: + return not user.is_anonymous() + class Idea(models.Model): title = models.CharField(max_length=100) diff --git a/brainstorm/templates/brainstorm/idea_form.html b/brainstorm/templates/brainstorm/idea_form.html index 885cc1a..c6c6f77 100644 --- a/brainstorm/templates/brainstorm/idea_form.html +++ b/brainstorm/templates/brainstorm/idea_form.html @@ -1,3 +1,4 @@ +{% if user_can_post %}

@@ -7,7 +8,8 @@

- +
- - +{% else %} +

Posting new ideas has been disabled for this site.

+{% endif %} diff --git a/brainstorm/views.py b/brainstorm/views.py index f2197c4..8682afa 100644 --- a/brainstorm/views.py +++ b/brainstorm/views.py @@ -28,7 +28,8 @@ def idea_list(request, slug, ordering='-total_upvotes'): return render_to_response('brainstorm/index.html', {'subsite':subsite, 'ideas': ideas, - 'ordering': ordering}, + 'ordering': ordering, + 'user_can_post': subsite.user_can_post(request.user)}, context_instance=RequestContext(request)) def idea_detail(request, slug, id): @@ -37,19 +38,21 @@ def idea_detail(request, slug, id): subsite=slug, pk=id) return render_to_response('brainstorm/idea.html', - {'subsite':subsite, 'idea': idea}, + {'subsite':subsite, 'idea': idea, + 'user_can_post': subsite.user_can_post(request.user)}, context_instance=RequestContext(request)) @require_POST def new_idea(request, slug): subsite = get_object_or_404(Subsite, pk=slug) + if not subsite.user_can_post(request.user): + return HttpResponseRedirect(subsite.get_absolute_url()) title = request.POST['title'] description = request.POST['description'] if request.user.is_anonymous(): user = None else: user = request.user - idea = Idea.objects.create(title=title, description=description, user=user, subsite=subsite) return HttpResponseRedirect(idea.get_absolute_url())