Skip to content

OSQA-823 - Show question list even for non-existing tags & prepopulate tags field for new questions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions forum/forms/qanda.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def clean(self, value):


class TagNamesField(forms.CharField):
def __init__(self, user=None, *args, **kwargs):
def __init__(self, user=None, initial='', *args, **kwargs):
super(TagNamesField, self).__init__(*args, **kwargs)

self.required = True
Expand All @@ -82,7 +82,7 @@ def __init__(self, user=None, *args, **kwargs):
self.help_text = _('Tags are short keywords, with no spaces within. At least %(min)s and up to %(max)s tags can be used.') % {
'min': settings.FORM_MIN_NUMBER_OF_TAGS, 'max': settings.FORM_MAX_NUMBER_OF_TAGS
}
self.initial = ''
self.initial = initial
self.user = user

def clean(self, value):
Expand Down Expand Up @@ -175,10 +175,10 @@ class AskForm(forms.Form):
title = TitleField()
text = QuestionEditorField()

def __init__(self, data=None, user=None, *args, **kwargs):
def __init__(self, data=None, user=None, default_tag='', *args, **kwargs):
super(AskForm, self).__init__(data, *args, **kwargs)

self.fields['tags'] = TagNamesField(user)
self.fields['tags'] = TagNamesField(user, initial=default_tag)

if int(user.reputation) < settings.CAPTCHA_IF_REP_LESS_THAN and not (user.is_superuser or user.is_staff):
spam_fields = call_all_handlers('create_anti_spam_field')
Expand Down
8 changes: 8 additions & 0 deletions forum/settings/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@
label = _("Limit related tags block"),
help_text = _("Limit related tags block size in questions list pages. Set to 0 to display all all tags.")))

DISPLAY_EMPTY_LIST_FOR_NONEXISTENT_TAGS = Setting('DISPLAY_EMPTY_LIST_FOR_NONEXISTENT_TAGS', False, VIEW_SET, dict(
label = _("Display empty question list for nonexistent tags"), required=False,
help_text = _("Display an empty question list (instead of an error page) for nonexistent tags")))

AUTO_SET_TAG_ON_QUESTION = Setting('AUTO_SET_TAG_ON_QUESTION', False, VIEW_SET, dict(
label = _("Automatically set tag on questions asked from tag page"), required=False,
help_text = _("Automatically set the tag on new questions asked from the tag page")))

2 changes: 1 addition & 1 deletion forum/skins/default/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% loopregistry page_top_tabs %}{% spaceless %}
<a id="nav_{{ tab_name }}"{% ifequal tab tab_name %} class="on"{% endifequal %} href="{{ tab_url }}" >{{ tab_title }}</a>
{% endspaceless %}{% endloopregistry %}
<a id="nav_ask" href="{% url ask %}" class="special">{% trans "ask a question" %}</a>
<a id="nav_ask" href="{% url ask %}{% if settings.AUTO_SET_TAG_ON_QUESTION and tag %}?tag={{ tag }}{% endif %}" class="special">{% trans "ask a question" %}</a>
</div>

<div id="searchBar">
Expand Down
20 changes: 14 additions & 6 deletions forum/views/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,19 @@ def unanswered(request):
def questions(request):
return question_list(request, Question.objects.all(), _('questions'), show_summary=True)

@decorators.render('questions.html')
def tag(request, tag):
tag_obj = None
try:
tag = Tag.active.get(name=unquote(tag))
tag_obj = Tag.active.get(name=unquote(tag))
except Tag.DoesNotExist:
raise Http404
if django_settings.DISPLAY_EMPTY_LIST_FOR_NONEXISTENT_TAGS:
tag_obj = Tag(name=unquote(tag), used_count=0, created_by_id=-1)
tag_obj.id = -1
else:
raise Http404

# Getting the questions QuerySet
questions = Question.objects.filter(tags__id=tag.id)
questions = Question.objects.filter(tags__id=tag_obj.id)

if request.method == "GET":
user = request.GET.get('user', None)
Expand All @@ -117,7 +121,7 @@ def tag(request, tag):

# The extra tag context we need to pass
tag_context = {
'tag' : tag,
'tag' : tag_obj,
}

# The context returned by the question_list function, contains info about the questions
Expand All @@ -137,7 +141,11 @@ def tag(request, tag):
# Create the combined context
context = dict(question_context.items() + tag_context.items())

return context
result = render_to_response('questions.html', context, context_instance = RequestContext(request))
if tag_obj.id < 0:
# prevent search engines indexing these pages / link spam attacks
result.status_code = 418 # I'm a teapot
return result

@decorators.render('questions.html', 'questions', tabbed=False)
def user_questions(request, mode, user, slug):
Expand Down
7 changes: 5 additions & 2 deletions forum/views/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ def ask(request):
return HttpResponseRedirect(reverse('auth_signin'))
elif "go" in request.POST:
form = AskForm({'title': request.POST['q']}, user=request.user)


default_tag = ''
if settings.AUTO_SET_TAG_ON_QUESTION and 'tag' in request.GET:
default_tag = request.GET['tag']
if not form:
form = AskForm(user=request.user)
form = AskForm(user=request.user, default_tag=default_tag)

return render_to_response('ask.html', {
'form' : form,
Expand Down