From fff3f3cc5623ea5fdd42da87ea1e54fb9a3d7aa7 Mon Sep 17 00:00:00 2001 From: ashwinishinde Date: Tue, 23 Jun 2015 13:05:21 +0530 Subject: Subject: Fixed bug from Registration page. Description: 1) Integrity Error. --- forums/forms.py | 26 ++++++++++++++++++++++---- forums/views.py | 5 ++--- static/website/templates/get-question.html | 1 + static/website/templates/questions.html | 13 ++++++++++++- website/views.py | 13 +++++++++---- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/forums/forms.py b/forums/forms.py index 3404ac2..90a147d 100644 --- a/forums/forms.py +++ b/forums/forms.py @@ -15,24 +15,33 @@ from website.models import Profile class UserLoginForm(forms.Form): + username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput()) + db = forms.ChoiceField(choices=[('forum', 'forum'), ('scilab', 'scilab'), ('client2', 'client2')]) def clean(self): cleaned_data = self.cleaned_data username = cleaned_data.get('username') - print username password = cleaned_data.get('password') - print password + db = cleaned_data.get('db') + if username is None or password is None: raise forms.ValidationError("Invalid username or password") - user = authenticate(username=username, password=password) - + + + if db == 'forum': + user = authenticate(username=username, password=password) + print "default" , "*******************" + else: + user = authenticate(username=username, password=password, db=db) + print db , "*******************" 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 class ProfileForm(forms.ModelForm): @@ -95,3 +104,12 @@ class RegisterForm(forms.Form): required=True ) captcha = ReCaptchaField() + + + def clean_username(self): + try: + User.objects.get(username=self.cleaned_data['username']) + raise forms.ValidationError(_("This username has already existed.")) + except User.DoesNotExist: + pass + return username diff --git a/forums/views.py b/forums/views.py index f1f3096..80410b4 100644 --- a/forums/views.py +++ b/forums/views.py @@ -49,7 +49,6 @@ def account_register(request): } context.update(csrf(request)) return render_to_response('forums/templates/user-register.html', context) - def confirm(request, confirmation_code, username): try: user = User.objects.get(username=username) @@ -60,11 +59,11 @@ def confirm(request, confirmation_code, username): user.save() user.backend='django.contrib.auth.backends.ModelBackend' login(request,user) - print "In if" + messages.success(request, "Your account has been activated!. Please update your profile to complete your registration") return HttpResponseRedirect('/accounts/profile/'+user.username) else: - print "In else" + messages.success(request, "Something went wrong!. Please try again!") return HttpResponseRedirect('/') except Exception, e: diff --git a/static/website/templates/get-question.html b/static/website/templates/get-question.html index 7c93803..67ac33a 100644 --- a/static/website/templates/get-question.html +++ b/static/website/templates/get-question.html @@ -34,6 +34,7 @@ {{ net_count }} + {% ifequal thisUserDownvote 0 %} diff --git a/static/website/templates/questions.html b/static/website/templates/questions.html index 96f596d..81c0d56 100644 --- a/static/website/templates/questions.html +++ b/static/website/templates/questions.html @@ -13,6 +13,17 @@ Question Date Views +
+ {% if num_votes == 0 %} + vote + {% elif num_votes == 1 %} + vote + {% elif num_votes == -1 %} + vote + {% else %} + votes + {% endif %} +
Answers User {% for question in questions %} @@ -49,7 +60,7 @@ {{ question.answer_set.count }} - + {{ question.user|truncatechars:10 }} diff --git a/website/views.py b/website/views.py index c9a2a5f..5974433 100644 --- a/website/views.py +++ b/website/views.py @@ -36,7 +36,9 @@ def questions(request): questions = Question.objects.all().order_by('date_created').reverse() paginator = Paginator(questions, 20) page = request.GET.get('page') - + for q in questions: + print q.title + print q.num_votes try: questions = paginator.page(page) except PageNotAnInteger: @@ -46,6 +48,7 @@ def questions(request): context = { 'questions': questions } + return render(request, 'website/templates/questions.html', context) def get_question(request, question_id=None, pretty_url=None): @@ -59,19 +62,21 @@ def get_question(request, question_id=None, pretty_url=None): thisuserdownvote = question.userDownVotes.filter(id=request.user.id).count() net_count = question.userUpVotes.count() - question.userDownVotes.count() + context = { 'question': question, 'answers': answers, 'form': form, 'thisUserUpvote': thisuserupvote, 'thisUserDownvote': thisuserdownvote, - 'net_count': net_count + 'net_count': net_count, + 'num_votes':question.num_votes } - context.update(csrf(request)) # updating views count question.views += 1 question.save() + return render(request, 'website/templates/get-question.html', context) @login_required @@ -343,7 +348,7 @@ def vote_post(request): num_votes = cur_post.userUpVotes.count() - cur_post.userDownVotes.count() cur_post.num_votes = num_votes cur_post.save() - + print "Num Votes: %s" % num_votes return HttpResponse(num_votes) -- cgit