From 8a3043c012ebb748d57f55c6c52bd4672ace4568 Mon Sep 17 00:00:00 2001 From: Jayaram Pai Date: Sun, 4 May 2014 16:49:50 +0530 Subject: added pretty urls to questions --- website/helpers.py | 11 +++++++++++ website/templatetags/helpers.py | 9 +++++++++ website/urls.py | 1 + website/views.py | 14 +++++++++----- 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 website/templatetags/helpers.py (limited to 'website') diff --git a/website/helpers.py b/website/helpers.py index 0d67da0..bf5217b 100644 --- a/website/helpers.py +++ b/website/helpers.py @@ -1,3 +1,5 @@ +import re + def get_video_info(path): """Uses ffmpeg to determine information about a video. This has not been broadly tested and your milage may vary""" @@ -27,3 +29,12 @@ def get_video_info(path): info_m['width'] = int(info_m['width']) info_m['height'] = int(info_m['height']) return info_m + +def prettify(string): + string = string.lower() + string = string.replace('-', ' ') + string = string.strip() + string = string.replace(' ', '-') + string = re.sub('[^A-Za-z0-9\-]+', '', string) + string = re.sub('-+', '-', string) + return string diff --git a/website/templatetags/helpers.py b/website/templatetags/helpers.py new file mode 100644 index 0000000..a0f65be --- /dev/null +++ b/website/templatetags/helpers.py @@ -0,0 +1,9 @@ +from django import template + +from website.models import Question, Answer, Notification +from website.helpers import prettify + +register = template.Library() + +# imported from website/helpers +register.simple_tag(prettify) diff --git a/website/urls.py b/website/urls.py index 1184044..5d7a654 100644 --- a/website/urls.py +++ b/website/urls.py @@ -4,6 +4,7 @@ urlpatterns = patterns('', url(r'^$', 'website.views.home', name='home'), url(r'^questions/$', 'website.views.questions', name='questions'), url(r'^question/(?P\d+)/$', 'website.views.get_question', name='get_question'), + url(r'^question/(?P\d+)/(?P.+)/$', 'website.views.get_question', name='get_question'), url(r'^question-answer/$', 'website.views.question_answer', name='question_answer'), url(r'^answer-comment/$', 'website.views.answer_comment', name='answer_comment'), url(r'^filter/(?P[^/]+)/$', 'website.views.filter', name='filter'), diff --git a/website/views.py b/website/views.py index 221a265..69c5a41 100644 --- a/website/views.py +++ b/website/views.py @@ -8,15 +8,16 @@ from django.contrib.auth.decorators import login_required from django.db.models import Q, Max from django.core.mail import EmailMultiAlternatives from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger +from django.core.urlresolvers import reverse from django.contrib.auth import get_user_model User = get_user_model() from website.models import Question, Answer, Notification, TutorialDetails, TutorialResources, AnswerComment from website.forms import NewQuestionForm, AnswerQuesitionForm -from website.helpers import get_video_info +from website.helpers import get_video_info, prettify admins = ( - 9, 4376, 4915, 14595, 12329, 22467, 5518 + 9, 4376, 4915, 14595, 12329, 22467, 5518, 30705 ) categories = ( @@ -60,8 +61,11 @@ def questions(request): } return render(request, 'website/templates/questions.html', context) -def get_question(request, question_id=None): +def get_question(request, question_id=None, pretty_url=None): question = get_object_or_404(Question, id=question_id) + pretty_title = prettify(question.title) + if pretty_url != pretty_title: + return HttpResponseRedirect('/question/'+ question_id + '/' + pretty_title) answers = question.answer_set.all() form = AnswerQuesitionForm() context = { @@ -121,8 +125,8 @@ def question_answer(request): email.attach_alternative(message, "text/html") email.send(fail_silently=True) # End of email send - - return HttpResponseRedirect('/question/'+str(qid) + "#answer" + str(answer.id)) + return HttpResponseRedirect('/question/'+ str(qid) + "#answer" + str(answer.id)) + return HttpResponseRedirect('/') @login_required def answer_comment(request): -- cgit