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
|
from django.contrib.comments.models import Comment
|
||||||
import secretballot
|
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):
|
class Subsite(models.Model):
|
||||||
slug = models.SlugField(max_length=50, primary_key=True)
|
slug = models.SlugField(max_length=50, primary_key=True)
|
||||||
name = models.CharField(max_length=50)
|
name = models.CharField(max_length=50)
|
||||||
@ -14,6 +21,7 @@ class Subsite(models.Model):
|
|||||||
|
|
||||||
ideas_per_page = models.IntegerField(default=10)
|
ideas_per_page = models.IntegerField(default=10)
|
||||||
allow_anonymous_ideas = models.BooleanField(default=False)
|
allow_anonymous_ideas = models.BooleanField(default=False)
|
||||||
|
post_status = models.IntegerField(default=ALLOW_ALL, choices=SUBSITE_POST_STATUS)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@ -21,6 +29,14 @@ class Subsite(models.Model):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('subsite', args=[self.slug])
|
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):
|
class Idea(models.Model):
|
||||||
|
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
{% if user_can_post %}
|
||||||
<form action="{% url new_idea subsite.slug %}" method="post">
|
<form action="{% url new_idea subsite.slug %}" method="post">
|
||||||
<p>
|
<p>
|
||||||
<label for="title">Title</label>
|
<label for="title">Title</label>
|
||||||
@ -9,5 +10,6 @@
|
|||||||
</p>
|
</p>
|
||||||
<button class="btnSubmit" type="submit">Submit</button>
|
<button class="btnSubmit" type="submit">Submit</button>
|
||||||
</form>
|
</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',
|
return render_to_response('brainstorm/index.html',
|
||||||
{'subsite':subsite, 'ideas': ideas,
|
{'subsite':subsite, 'ideas': ideas,
|
||||||
'ordering': ordering},
|
'ordering': ordering,
|
||||||
|
'user_can_post': subsite.user_can_post(request.user)},
|
||||||
context_instance=RequestContext(request))
|
context_instance=RequestContext(request))
|
||||||
|
|
||||||
def idea_detail(request, slug, id):
|
def idea_detail(request, slug, id):
|
||||||
@ -37,19 +38,21 @@ def idea_detail(request, slug, id):
|
|||||||
subsite=slug, pk=id)
|
subsite=slug, pk=id)
|
||||||
|
|
||||||
return render_to_response('brainstorm/idea.html',
|
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))
|
context_instance=RequestContext(request))
|
||||||
|
|
||||||
@require_POST
|
@require_POST
|
||||||
def new_idea(request, slug):
|
def new_idea(request, slug):
|
||||||
subsite = get_object_or_404(Subsite, pk=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']
|
title = request.POST['title']
|
||||||
description = request.POST['description']
|
description = request.POST['description']
|
||||||
if request.user.is_anonymous():
|
if request.user.is_anonymous():
|
||||||
user = None
|
user = None
|
||||||
else:
|
else:
|
||||||
user = request.user
|
user = request.user
|
||||||
|
|
||||||
idea = Idea.objects.create(title=title, description=description,
|
idea = Idea.objects.create(title=title, description=description,
|
||||||
user=user, subsite=subsite)
|
user=user, subsite=subsite)
|
||||||
return HttpResponseRedirect(idea.get_absolute_url())
|
return HttpResponseRedirect(idea.get_absolute_url())
|
||||||
|
Loading…
Reference in New Issue
Block a user