attempt to merge brainstorm with what was anthill.ideas

This commit is contained in:
James Turk 2009-08-19 12:18:57 -04:00
parent c91028d840
commit 104cf2a32a
5 changed files with 92 additions and 78 deletions

View File

@ -3,25 +3,29 @@ from brainstorm.models import Subsite
class SubsiteFeed(Feed): class SubsiteFeed(Feed):
description_template = 'feedback/idea_rss_description.html' title_template = 'brainstorm/feed_title.html'
description_template = 'brainstorm/feed_description.html'
def get_object(self, bits): def get_object(self, bits):
return Subsite.objects.get(slug__exact=bits[0]) return Subsite.objects.get(slug__exact=bits[0])
def title(self, obj): def title(self, obj):
return '%s' % obj.name return 'Latest ideas submitted for %s' % obj.name
def description(self, obj):
return 'Latest ideas submitted for %s' % obj.name
def link(self, obj): def link(self, obj):
if not obj: if not obj:
raise FeedDoesNotExist raise FeedDoesNotExist
return obj.get_absolute_url() return obj.get_absolute_url()
def description(self, obj):
return 'Latest ideas submitted for %s' % obj.name
def items(self, obj): def items(self, obj):
return obj.ideas.order_by('-submit_date')[:30] return obj.ideas.order_by('-submit_date')[:30]
def item_link(self, item):
return item.get_absolute_url()
def item_author_name(self, item): def item_author_name(self, item):
return item.user return item.user

View File

@ -3,7 +3,6 @@ from django.contrib.auth.models import User
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.contrib.contenttypes import generic from django.contrib.contenttypes import generic
from django.contrib.comments.models import Comment from django.contrib.comments.models import Comment
import secretballot
ALLOW_ALL, REQUIRE_LOGIN, DISALLOW_ALL = range(3) ALLOW_ALL, REQUIRE_LOGIN, DISALLOW_ALL = range(3)
SUBSITE_POST_STATUS = ( SUBSITE_POST_STATUS = (
@ -36,10 +35,16 @@ class Subsite(models.Model):
elif self.post_status == REQUIRE_LOGIN: elif self.post_status == REQUIRE_LOGIN:
return not user.is_anonymous() return not user.is_anonymous()
class IdeaManager(models.Manager):
def with_user_vote(self, user):
return self.extra(select={'user_vote':'SELECT value FROM brainstorm_vote WHERE idea_id=ideas_idea.id AND user_id=%s'}, select_params=[user.id])
class Idea(models.Model): class Idea(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
description = models.TextField() description = models.TextField()
score = models.IntegerField(default=0)
submit_date = models.DateTimeField(auto_now_add=True) submit_date = models.DateTimeField(auto_now_add=True)
@ -48,10 +53,28 @@ class Idea(models.Model):
comments = generic.GenericRelation(Comment, object_id_field='object_pk') comments = generic.GenericRelation(Comment, object_id_field='object_pk')
objects = IdeaManager()
def __unicode__(self): def __unicode__(self):
return self.title return self.title
def get_absolute_url(self): def get_absolute_url(self):
return reverse('idea', args=[self.subsite_id, self.id]) return reverse('idea', args=[self.subsite_id, self.id])
secretballot.enable_voting_on(Idea) class Vote(models.Model):
user = models.ForeignKey(User, related_name='idea_votes')
idea = models.ForeignKey(Idea, related_name='votes')
value = models.IntegerField()
timestamp = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return '%s %s on %s' % (self.user, self.value, self.idea)
class Meta:
unique_together = (('user', 'idea'),)
def update_idea_votes(sender, instance, created, **kwargs):
score = instance.idea.votes.aggregate(score=models.Sum('value'))['score']
instance.idea.score = score
instance.idea.save()
post_save.connect(update_idea_votes, sender=Vote)

View File

@ -3,6 +3,8 @@ from django.contrib.contenttypes.models import ContentType
from brainstorm.models import Idea from brainstorm.models import Idea
from brainstorm.feeds import SubsiteFeed from brainstorm.feeds import SubsiteFeed
BRAINSTORM_USE_SECRETBALLOT = False
feeds = { feeds = {
'latest': SubsiteFeed, 'latest': SubsiteFeed,
} }
@ -14,13 +16,14 @@ urlpatterns = patterns('',
) )
urlpatterns += patterns('brainstorm.views', urlpatterns += patterns('brainstorm.views',
url(r'^submit_comment/$', 'submit_comment', name='submit_idea_comment'), url(r'^(?P<slug>[\w-]+)/$', 'idea_list', {'ordering': 'most_popular'}, name='ideas_popular'),
url(r'^(?P<slug>[\w-]+)/$', 'idea_list', {'ordering': 'most_popular'}, name='subsite'), url(r'^(?P<slug>[\w-]+)/latest/$', 'idea_list', {'ordering': 'latest'}, name='ideas_latest'),
url(r'^(?P<slug>[\w-]+)/latest/$', 'idea_list', {'ordering': 'latest'}, name='subsite_latest'), url(r'^(?P<slug>[\w-]+)/(?P<id>\d+)/$', 'idea_detail', name='idea_detail'),
url(r'^(?P<slug>[\w-]+)/(?P<id>\d+)/$', 'idea_detail', name='idea'),
url(r'^(?P<slug>[\w-]+)/new_idea/$', 'new_idea', name='new_idea'), url(r'^(?P<slug>[\w-]+)/new_idea/$', 'new_idea', name='new_idea'),
url(r'^vote/$', 'vote', name='idea_vote'),
) )
if BRAINSTORM_USE_SECRETBALLOT:
urlpatterns = patterns('secretballot.views', urlpatterns = patterns('secretballot.views',
url(r'^vote_up/(?P<object_id>\d+)/$', 'vote', url(r'^vote_up/(?P<object_id>\d+)/$', 'vote',
{'content_type': ContentType.objects.get_for_model(Idea), 'vote': 1}, {'content_type': ContentType.objects.get_for_model(Idea), 'vote': 1},

View File

@ -1,45 +1,28 @@
import datetime import datetime
from django.template import RequestContext from django.template import RequestContext
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render_to_response from django.shortcuts import get_object_or_404, render_to_response, redirect
from django.http import HttpResponseRedirect from django.http import HttpResponse
from django.contrib.comments.models import Comment from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.views.generic import list_detail
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from brainstorm.models import Subsite, Idea from django.contrib.auth.decorators import login_required
from django.conf import settings
from anthill.ideas.models import Subsite, Idea, Vote
def idea_list(request, slug, ordering='-total_upvotes'): def idea_list(request, slug, ordering='-total_upvotes'):
subsite = get_object_or_404(Subsite, pk=slug) ordering_db = {'most_popular': '-score',
ordering_db = {'most_popular': '-total_upvotes',
'latest': '-submit_date'}[ordering] 'latest': '-submit_date'}[ordering]
paginator = Paginator(Idea.objects.from_request(request).filter(subsite=subsite).order_by(ordering_db), return list_detail.object_list(request,
subsite.ideas_per_page) queryset=Idea.objects.filter(subsite__slug=slug).with_user_vote(request.user).select_related().order_by(ordering_db),
extra_context={'ordering': ordering, 'subsite':slug}, paginate_by=10,
try: template_object_name='idea')
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
ideas = paginator.page(page)
except (EmptyPage, InvalidPage):
ideas = paginator.page(paginator.num_pages)
return render_to_response('brainstorm/index.html',
{'subsite':subsite, 'ideas': ideas,
'ordering': ordering,
'user_can_post': subsite.user_can_post(request.user)},
context_instance=RequestContext(request))
def idea_detail(request, slug, id): def idea_detail(request, slug, id):
subsite = get_object_or_404(Subsite, pk=slug) idea = get_object_or_404(Idea.objects.with_user_vote(request.user), pk=id, subsite__slug=slug)
idea = get_object_or_404(Idea.objects.from_request(request), return render_to_response('ideas/idea_detail.html',
subsite=slug, pk=id) {'idea': idea},
return render_to_response('brainstorm/idea.html',
{'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
@ -49,29 +32,30 @@ def new_idea(request, slug):
return HttpResponseRedirect(subsite.get_absolute_url()) 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():
user = None
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,
user=user, subsite=subsite) subsite=subsite)
return HttpResponseRedirect(idea.get_absolute_url()) return redirect(idea)
@require_POST @require_POST
def submit_comment(request): @login_required
from django.conf import settings def vote(request):
content_type = ContentType.objects.get_for_model(Idea).id idea_id = int(request.POST.get('idea'))
site = settings.SITE_ID score = int(request.POST.get('score'))
object_pk = request.POST['idea_id'] if score not in (0,1):
name = request.POST.get('name', 'anonymous') score = 0
email = request.POST.get('email', '') idea = get_object_or_404(Idea, pk=idea_id)
url = request.POST.get('url', '') score_diff = score
comment = request.POST['comment'] vote, created = Vote.objects.get_or_create(user=request.user, idea=idea,
date = datetime.datetime.now() defaults={'value':score})
ip = request.META['REMOTE_ADDR'] if not created:
c = Comment.objects.create(user_name=name, user_email=email, user_url=url, new_score = idea.score + (score-vote.value)
comment=comment, submit_date=date, ip_address=ip, vote.value = score
site_id=site, content_type_id=content_type, object_pk=object_pk) vote.save()
idea = Idea.objects.get(pk=object_pk) else:
linkback = '%s#c%s' % (idea.get_absolute_url(), c.id) new_score = idea.score
return HttpResponseRedirect(linkback)
if request.is_ajax():
return HttpResponse("{'score':%d}" % new_score)
return redirect(idea)

View File

@ -4,7 +4,7 @@ long_description = open('README.rst').read()
setup( setup(
name='django-brainstorm', name='django-brainstorm',
version="0.1", version="0.2.0",
package_dir={'brainstorm': 'brainstorm'}, package_dir={'brainstorm': 'brainstorm'},
packages=['brainstorm'], packages=['brainstorm'],
package_data={'brainstorm': ['templates/brainstorm/*.html']}, package_data={'brainstorm': ['templates/brainstorm/*.html']},