def show_captcha(request, year, month, day, slug): story = get_by_date_or_404(Article, year, month, day, slug=slug) key = 'comment:%s' % story.get_absolute_url() try: comment = request.session[key] except KeyError: raise Http404 url = "http://api.recaptcha.net/%s?k=" + settings.RECAPTCHA_PUBLIC_KEY if "recaptcha_response_field" in request.POST: result = recaptcha.submit(request.POST.get('recaptcha_challenge_field', None), request.POST.get('recaptcha_response_field', None), settings.RECAPTCHA_PRIVATE_KEY, get_ip(request)) if result.is_valid: del request.session[key] comment.mark_as_ham() comment.save() if request.is_ajax(): # we're not (yet) doing this via ajax, so it's ok raise NotImplemented else: return HttpResponseRedirect(comment.get_absolute_url()) else: url += "&error=%s" % response.error_code rc = RequestContext(request, { 'challenge_captcha_url': url % 'challenge', 'noscript_captcha_url': url % 'noscript' }) return render_to_response('stories/captcha_form.html', context_instance=rc)
def show_captcha(request, year, month, day, slug): story = get_by_date_or_404(Article, year, month, day, slug=slug) key = 'comment:%s' % story.get_absolute_url() try: comment = request.session[key] except KeyError: raise Http404 url = "http://api.recaptcha.net/%s?k=" + settings.RECAPTCHA_PUBLIC_KEY if "recaptcha_response_field" in request.POST: result = recaptcha.submit( request.POST.get('recaptcha_challenge_field', None), request.POST.get('recaptcha_response_field', None), settings.RECAPTCHA_PRIVATE_KEY, get_ip(request)) if result.is_valid: del request.session[key] comment.mark_as_ham() comment.save() if request.is_ajax(): # we're not (yet) doing this via ajax, so it's ok raise NotImplemented else: return HttpResponseRedirect(comment.get_absolute_url()) else: url += "&error=%s" % response.error_code rc = RequestContext( request, { 'challenge_captcha_url': url % 'challenge', 'noscript_captcha_url': url % 'noscript' }) return render_to_response('stories/captcha_form.html', context_instance=rc)
def article(request, slug, year, month, day, num=None, form=None, print_view=False): "Base function to call for displaying a given article." kwargs = { 'slug': slug[:100] } # for very-long old slugs if not request.user.is_staff: kwargs['status'] = 'p' # allow previews for staff story = get_by_date_or_404(Article, year, month, day, **kwargs) return specific_article(request, story, num, form, print_view)
def article(request, slug, year, month, day, num=None, form=None, print_view=False): "Base function to call for displaying a given article." kwargs = {'slug': slug[:100]} # for very-long old slugs if not request.user.is_staff: kwargs['status'] = 'p' # allow previews for staff story = get_by_date_or_404(Article, year, month, day, **kwargs) return specific_article(request, story, num, form, print_view)
def post_comment(request, slug, year, month, day): story = get_by_date_or_404(Article, year, month, day, slug=slug) if not story.comments_allowed: raise Http404 # semantically incorrect, but whatever logged_in = request.user.is_authenticated() staff = logged_in and get_user_profile(request).staff_status() form = make_comment_form(data=request.POST, logged_in=logged_in, staff=staff) if form.is_valid(): data = form.cleaned_data args = { 'subject': story, 'text': escape(data['text']).replace("\n", "<br/>"), 'ip_address': get_ip(request), 'user_agent': request.META.get('HTTP_USER_AGENT', '') } if logged_in: args['user'] = get_user_profile(request) if data['anonymous']: # and data['name'] != request.user.get_full_name(): args['name'] = data['name'] args['speaking_officially'] = data['speaking_officially'] else: args['name'] = data['name'] args['email'] = data['email'] try: comment = PublicComment.objects.new(**args) except CommentIsSpam, e: # put data in the session, because we're silly like that url = e.comment.subject.get_absolute_url() request.session.set_expiry(0) request.session['comment:%s' % url] = e.comment # NOTE: coupling with url for comment captchas redirect = request.build_absolute_uri(url + 'comment/captcha') if request.is_ajax(): return HttpResponse('redirect: %s' % redirect) else: return HttpResponseRedirect(redirect) if request.is_ajax(): return HttpResponse('success') else: return HttpResponseRedirect(comment.get_absolute_url())
def comments_for_article(request, slug, year, month, day, num=None): """ Returns the comments for the specified article, rendered as they are on article view pages, starting after number `num`. Used for after you've posted an AJAX comment. """ story = get_by_date_or_404(Article, year, month, day, slug=slug) user = get_user_profile(request) ip = get_ip(request) spec = Q(number__gt=num) if num else Q() comments = PublicComment.objects.for_article(story, user, ip, spec=spec) rc = RequestContext(request, { 'comments': comments, 'new': True }) return render_to_response("stories/comments.html", context_instance=rc)
def comments_for_article(request, slug, year, month, day, num=None): """ Returns the comments for the specified article, rendered as they are on article view pages, starting after number `num`. Used for after you've posted an AJAX comment. """ story = get_by_date_or_404(Article, year, month, day, slug=slug) user = get_user_profile(request) ip = get_ip(request) spec = Q(number__gt=num) if num else Q() comments = PublicComment.objects.for_article(story, user, ip, spec=spec) rc = RequestContext(request, {'comments': comments, 'new': True}) return render_to_response("stories/comments.html", context_instance=rc)
def post_comment(request, slug, year, month, day): story = get_by_date_or_404(Article, year, month, day, slug=slug) if not story.comments_allowed: raise Http404 # semantically incorrect, but whatever logged_in = request.user.is_authenticated() staff = logged_in and get_user_profile(request).staff_status() form = make_comment_form(data=request.POST, logged_in=logged_in, staff=staff) if form.is_valid(): data = form.cleaned_data args = { 'subject': story, 'text': escape(data['text']).replace("\n", "<br/>"), 'ip_address': get_ip(request), 'user_agent': request.META.get('HTTP_USER_AGENT', '') } if logged_in: args['user'] = get_user_profile(request) if data['anonymous']:# and data['name'] != request.user.get_full_name(): args['name'] = data['name'] args['speaking_officially'] = data['speaking_officially'] else: args['name'] = data['name'] args['email'] = data['email'] try: comment = PublicComment.objects.new(**args) except CommentIsSpam, e: # put data in the session, because we're silly like that url = e.comment.subject.get_absolute_url() request.session.set_expiry(0) request.session['comment:%s' % url] = e.comment # NOTE: coupling with url for comment captchas redirect = request.build_absolute_uri(url + 'comment/captcha') if request.is_ajax(): return HttpResponse('redirect: %s' % redirect) else: return HttpResponseRedirect(redirect) if request.is_ajax(): return HttpResponse('success') else: return HttpResponseRedirect(comment.get_absolute_url())
def issue_for_date(request, year, month, day, plain=False): issue = get_by_date_or_404(Issue, year, month, day, field='date') return show_issue(request, issue, plain)
def issue_for_date(request, year, month, day, plain=False): issue = get_by_date_or_404(Issue, year, month, day, field="date") return show_issue(request, issue, plain)