def post(request, slug, error_messages=None): context = RequestContext(request) if not error_messages: comment_form = CommentForm(initial={'slug': slug}) post = get_object_or_404(Post, slug=slug) if post.deleted: raise Http404 post.created_on_humanize = arrow.get(post.created_on).humanize() if request.user.is_authenticated(): can_comment = get_user_permissions(request)['can_comment'] try: post.vote = PostVote.objects.get(post=post, user=request.user).vote except PostVote.DoesNotExist: post.vote = 0 else: can_comment = None post.score = PostVote.objects.filter(post=post).aggregate(Sum('vote'))['vote__sum'] if post.post_type == 'link': post.domain = domain_name(post.url) post.is_recent = (timezone.now() - post.created_on) < timedelta(days=1) all_comments = Comment.objects.filter(post=post).order_by('-created_on') for comment in all_comments: comment.created_on_humanize = arrow.get(comment.created_on).humanize() comment.delete_allowed = comment.can_delete(request.user) if request.user.is_authenticated(): try: comment.vote = CommentVote.objects.get(comment=comment, user=request.user).vote except CommentVote.DoesNotExist: comment.vote = 0 comment.score = CommentVote.objects.filter(comment=comment).aggregate(Sum('vote'))['vote__sum'] context_dict = { 'post': post, 'user': request.user, 'can_comment': can_comment, 'can_delete': post.can_delete(request.user), 'comments': all_comments, 'comment_form': comment_form, 'moderator': request.user.username in MODERATORS } return render_to_response('content/post.html', context_dict, context)
def add_comment(request): context = RequestContext(request) if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): post_slug = comment_form.cleaned_data['slug'] post = get_object_or_404(Post, slug=post_slug) text = comment_form.cleaned_data['text'] # Get commenting permission for the specific post permissions = get_user_permissions(request) # Create comment slug comment_slug = orig_slug = slugify(text)[:SLUG_MAX_LENGTH].strip('-') for x in itertools.count(1): if not Comment.objects.filter(post=post, slug=comment_slug).exists(): break comment_slug = '%s-%d' % (orig_slug[:SLUG_MAX_LENGTH - len(str(x)) - 1], x) if permissions['can_comment']: comment = Comment.objects.create( post=post, user=request.user, slug=comment_slug, text=text ) comment.save() else: print comment_form.errors['text'][0] post_slug = comment_form.cleaned_data['slug'] return HttpResponseRedirect( reverse('content.views.post', args=(), kwargs={'slug': post_slug})) else: return redirect('/c/')
def submit(request): context = RequestContext(request) if request.method == 'POST': post_form = PostForm(request.POST) permissions = get_user_permissions(request) if post_form.is_valid() and permissions['can_post']: user = request.user data = post_form.cleaned_data post_type = data['post_type'] symbol = data['symbol'] trend = data['trend'] title = data['title'] stock = Stock.objects.get(symbol=symbol) # Create slug slug = orig_slug = slugify(title)[:SLUG_MAX_LENGTH].strip('-') for x in itertools.count(1): if not Post.objects.filter(slug=slug).exists(): break slug = '%s-%d' % (orig_slug[:SLUG_MAX_LENGTH - len(str(x)) - 1], x) if post_type == 'link': url = data['url'] text = data['summary'] post = Post.objects.create( user=user, post_type=post_type, trend=trend, stock=stock, title=title, slug=slug, url=url, text=text ) elif post_type == 'article': text = data['text'] post = Post.objects.create( user=user, post_type=post_type, trend=trend, stock=stock, title=title, slug=slug, text=text ) post.save() return redirect(post) else: # Add message to be displayed if not permissions['email_confirmed']: messages.error( request, 'Please confirm your email before making a submission' ) elif not permissions['can_post']: messages.error( request, 'You have reached your daily or monthly limit on submissions' ) print post_form.errors else: post_form = PostForm() context_dict = {'form': post_form} return render_to_response('content/submit.html', context_dict, context)