From 0a8b9fd4976f5d67c2b9701eb52ac4c6a309eec7 Mon Sep 17 00:00:00 2001 From: Jayaram Pai Date: Sat, 3 May 2014 18:22:10 +0530 Subject: added General FOSS category, fixed user-login --- forums/forms.py | 14 ++++ forums/views.py | 38 +++++----- static/forums/templates/user-login.html | 10 +++ static/website/js/custom.js | 100 +++++++++++++++++++-------- static/website/templates/ajax-tutorials.html | 2 +- static/website/templates/new-question.html | 2 +- website/forms.py | 1 + website/views.py | 2 - 8 files changed, 114 insertions(+), 55 deletions(-) diff --git a/forums/forms.py b/forums/forms.py index 2e3a6d1..5a93b5f 100644 --- a/forums/forms.py +++ b/forums/forms.py @@ -1,6 +1,20 @@ from django import forms +from django.contrib.auth import login, logout, authenticate class UserLoginForm(forms.Form): username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput()) + def clean(self): + cleaned_data = self.cleaned_data + username = cleaned_data.get('username') + password = cleaned_data.get('password') + if username is None or password is None: + raise forms.ValidationError("Invalid username or password") + user = authenticate(username=username, password=password) + if not user: + raise forms.ValidationError("Invalid username or password") + if not user.is_active: + raise forms.ValidationError("User is blocked") + cleaned_data['user'] = user + return cleaned_data diff --git a/forums/views.py b/forums/views.py index a38d6b7..b2dae90 100644 --- a/forums/views.py +++ b/forums/views.py @@ -8,29 +8,25 @@ from forums.forms import UserLoginForm def user_login(request): if request.user.is_anonymous(): if request.method == 'POST': - username = request.POST['username'] - password = request.POST['password'] - user = authenticate(username=username, password=password) - if user is not None: - if user.is_active: - login(request, user) - if 'next' in request.POST: - next_url = request.POST.get('next') - return HttpResponseRedirect(next_url) - return HttpResponseRedirect('/') - else: - return HttpResponse('you are blocked') - else: - return HttpResponse('Invalid username or password') + form = UserLoginForm(request.POST) + if form.is_valid(): + cleaned_data = form.cleaned_data + user = cleaned_data.get("user") + login(request, user) + if 'next' in request.POST: + next_url = request.POST.get('next') + return HttpResponseRedirect(next_url) + return HttpResponseRedirect('/') else: form = UserLoginForm() - next_url = request.GET.get('next') - context = { - 'form': form, - 'next': next_url - } - context.update(csrf(request)) - return render_to_response('forums/templates/user-login.html', context) + + next_url = request.GET.get('next') + context = { + 'form': form, + 'next': next_url + } + context.update(csrf(request)) + return render_to_response('forums/templates/user-login.html', context) else: return HttpResponseRedirect('/') diff --git a/static/forums/templates/user-login.html b/static/forums/templates/user-login.html index 21c4551..ef62ffe 100644 --- a/static/forums/templates/user-login.html +++ b/static/forums/templates/user-login.html @@ -6,6 +6,12 @@

Please login with your
Spoken-Tutorial.org account.

{% csrf_token %} + {% if form.errors %} +
+ + Invalid username or password +
+ {% endif %} {% with WIDGET_ERROR_CLASS='field_error' %}
@@ -39,3 +45,7 @@
{% endblock %} + +{% block javascript %} + $(".alert").alert(); +{% endblock %} diff --git a/static/website/js/custom.js b/static/website/js/custom.js index 49b4053..5565683 100644 --- a/static/website/js/custom.js +++ b/static/website/js/custom.js @@ -4,45 +4,86 @@ $(document).ready(function() { $minute_range = $("#id_minute_range"); $second_range = $("#id_second_range"); + function reset() { + for (var i = 0, l = arguments.length; i < l; i ++) { + switch(arguments[i]) { + case "tutorial": + $tutorial.html(""); + $tutorial.attr("disabled", true); + break; + + case "minute_range": + $minute_range.html(""); + $minute_range.attr("disabled", true); + break; + + case "second_range": + $second_range.html(""); + $second_range.attr("disabled", true); + break; + + } + } + } + $category.change(function() { $("#similar-link").hide(); - var category = $(this).val(); + /* resetting dropdowns */ + reset("tutorial", "minute_range", "second_range"); /* see thread-user.js */ $("#question-details-ok").show(); - $.ajax({ - url: "/ajax-tutorials/", - type: "POST", - data: { - category: category - }, - success: function(data) { - $("#id_tutorial").html(data); - $("#id_tutorial").removeAttr("disabled"); - console.log("response = " + data); - } - }); + var category = $(this).val(); + if(category == "General") { + /* disabling all other fields */ + $tutorial.html(""); + $tutorial.removeAttr("disabled"); + $minute_range.html(""); + $minute_range.removeAttr("disabled"); + $second_range.html(""); + $second_range.removeAttr("disabled"); + } else { + $.ajax({ + url: "/ajax-tutorials/", + type: "POST", + data: { + category: category + }, + success: function(data) { + $("#id_tutorial").html(data); + $("#id_tutorial").removeAttr("disabled"); + } + }); + } }); $tutorial.change(function() { - console.log("tut changed"); + /* resetting dropdowns */ + reset("minute_range", "second_range"); var category = $category.val(); var tutorial = $(this).val(); - $.ajax({ - url: "/ajax-duration/", - type: "POST", - data: { - category: category, + if(tutorial == "General") { + /* disabling all other fields */ + $minute_range.html(""); + $minute_range.removeAttr("disabled"); + $second_range.html(""); + $second_range.removeAttr("disabled"); + } else { + $.ajax({ + url: "/ajax-duration/", + type: "POST", + data: { + category: category, tutorial: tutorial - }, - success: function(data){ - var $response = $(data); - console.log($response.html()); - $minute_range.html($response.find("#minutes").html()) - $minute_range.removeAttr("disabled"); - $second_range.html($response.find("#seconds").html()) - $second_range.removeAttr("disabled"); - } - }); + }, + success: function(data){ + var $response = $(data); + $minute_range.html($response.find("#minutes").html()) + $minute_range.removeAttr("disabled"); + $second_range.html($response.find("#seconds").html()) + $second_range.removeAttr("disabled"); + } + }); + } }); $second_range.change(function() { @@ -57,7 +98,6 @@ $(document).ready(function() { }, dataType: "html", success: function(data) { - console.log(data); $response = $(data); var similar_count= $response.find("#similar-count").text(); $("#similar-link").show().html(similar_count); diff --git a/static/website/templates/ajax-tutorials.html b/static/website/templates/ajax-tutorials.html index 63e6000..c19fe3c 100644 --- a/static/website/templates/ajax-tutorials.html +++ b/static/website/templates/ajax-tutorials.html @@ -2,4 +2,4 @@ {% for tutorial in tutorials %} {% endfor %} - + diff --git a/static/website/templates/new-question.html b/static/website/templates/new-question.html index 16c6021..28fc707 100644 --- a/static/website/templates/new-question.html +++ b/static/website/templates/new-question.html @@ -11,7 +11,7 @@ {% with WIDGET_ERROR_CLASS='field_error' %}

- Please enter the tutorial details. {{ category }} + Please enter the tutorial details.

diff --git a/website/forms.py b/website/forms.py index c8f3578..b982d54 100644 --- a/website/forms.py +++ b/website/forms.py @@ -4,6 +4,7 @@ from website.models import * categories = ( ("None", "Select a Category"), + ("General", "General"), ('Advanced-C++', 'Advanced-C++'), ('BASH', 'BASH'), ('Blender', 'Blender'), diff --git a/website/views.py b/website/views.py index 0665b0f..e349daa 100644 --- a/website/views.py +++ b/website/views.py @@ -245,13 +245,11 @@ def new_question(request): question.tutorial, 'http://forums.spoken-tutorial.org/question/'+str(question.id) ) - email = EmailMultiAlternatives( subject,'', 'forums', ['team@spoken-tutorial.org', 'team@fossee.in'], headers={"Content-type":"text/html;charset=iso-8859-1"} ) - email.attach_alternative(message, "text/html") email.send(fail_silently=True) # End of email send -- cgit