added post_status field to subsites to control posting
This commit is contained in:
parent
7f1bbaa641
commit
cb4c7ae931
@ -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)
|
||||
|
@ -1,3 +1,4 @@
|
||||
{% if user_can_post %}
|
||||
<form action="{% url new_idea subsite.slug %}" method="post">
|
||||
<p>
|
||||
<label for="title">Title</label>
|
||||
@ -7,7 +8,8 @@
|
||||
<label for="description">Description</label>
|
||||
<textarea id="description" name="description"></textarea>
|
||||
</p>
|
||||
<button class="btnSubmit" type="submit">Submit</button>
|
||||
<button class="btnSubmit" type="submit">Submit</button>
|
||||
</form>
|
||||
|
||||
|
||||
{% else %}
|
||||
<p>Posting new ideas has been disabled for this site.</p>
|
||||
{% endif %}
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user