Skip to content

Commit

Permalink
[bug 731183] Noindex questions older than 30 days without answers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlr committed Mar 2, 2012
1 parent 3caee71 commit 1c4e291
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions apps/questions/templates/questions/answers.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
{% if answers.number > 1 %}
{% set canonical_url = canonical_url|urlparams(page=answers.number) %}
{% endif %}
{% if robots_noindex %}
{% set meta = (('robots', 'noindex'),) %}
{% endif %}

{% block content %}
<article class="main">
Expand Down
28 changes: 27 additions & 1 deletion apps/questions/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

from questions.events import QuestionReplyEvent, QuestionSolvedEvent
from questions.models import Question, Answer, QuestionVote, VoteMetadata
from questions.tests import TestCaseBase, TaggingTestCaseBase, tags_eq
from questions.tests import (TestCaseBase, TaggingTestCaseBase, tags_eq,
question, answer)
from questions.views import UNAPPROVED_TAG, NO_TAG
from questions.cron import cache_top_contributors
from sumo.helpers import urlparams
Expand Down Expand Up @@ -689,6 +690,31 @@ def test_links_nofollow(self):
eq_('nofollow', doc('#question div.content a')[0].attrib['rel'])
eq_('nofollow', doc('li.answer div.content a')[0].attrib['rel'])

def test_robots_noindex(self):
"""Verify noindex on unanswered questions over 30 days old."""
q = question(save=True)

# A brand new questions shouldn't be noindex'd...
response = get(self.client, 'questions.answers', args=[q.id])
eq_(200, response.status_code)
doc = pq(response.content)
eq_(0, len(doc('meta[name=robots]')))

# But a 31 day old question should be noindexed...
q.created = datetime.now() - timedelta(days=31)
q.save()
response = get(self.client, 'questions.answers', args=[q.id])
eq_(200, response.status_code)
doc = pq(response.content)
eq_(1, len(doc('meta[name=robots]')))

# Except if it has answers.
answer(question=q, save=True)
response = get(self.client, 'questions.answers', args=[q.id])
eq_(200, response.status_code)
doc = pq(response.content)
eq_(0, len(doc('meta[name=robots]')))


class TaggedQuestionsTestCase(TaggingTestCaseBase):
"""Questions/answers template tests that require tagged questions."""
Expand Down
13 changes: 11 additions & 2 deletions apps/questions/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import json
import logging
import random
Expand Down Expand Up @@ -160,9 +160,18 @@ def answers(request, question_id, form=None, watch_form=None,
"""View the answers to a question."""
ans_ = _answers_data(request, question_id, form, watch_form,
answer_preview)
question = ans_['question']

if request.user.is_authenticated():
ans_['images'] = ans_['question'].images.filter(creator=request.user)
ans_['images'] = question.images.filter(creator=request.user)

extra_kwargs.update(ans_)

# Add noindex to questions without answers that are > 30 days old.
no_answers = ans_['answers'].paginator.count == 0
if no_answers and question.created < datetime.now() - timedelta(days=30):
extra_kwargs.update(robots_noindex=True)

return jingo.render(request, 'questions/answers.html', extra_kwargs)


Expand Down

0 comments on commit 1c4e291

Please sign in to comment.