From 99bbf8ec16fca5eeec9d0ddad5f0a5ae169e4218 Mon Sep 17 00:00:00 2001 From: Jayaram Pai Date: Wed, 23 Apr 2014 17:18:27 +0530 Subject: changed slick in index page changed login:next_url modified the forms.py for initial value --- website/forms.py | 22 +++++++++++++++++++--- website/models.py | 3 +++ website/templatetags/notify.py | 13 +++++++++++++ website/views.py | 14 ++++++++------ 4 files changed, 43 insertions(+), 9 deletions(-) (limited to 'website') diff --git a/website/forms.py b/website/forms.py index f1e29be..7d60ffa 100644 --- a/website/forms.py +++ b/website/forms.py @@ -1,6 +1,6 @@ from django import forms -from website.models import Question +from website.models import * categories = ( ("None", "Select a Category"), @@ -64,8 +64,24 @@ seconds= ( ) class NewQuestionForm(forms.Form): - category = forms.CharField(widget=forms.Select(choices=categories)) - tutorial = forms.CharField(widget=forms.Select(choices=tutorials)) + #fix dirty code + def __init__(self, *args, **kwargs): + category = kwargs.pop('category') + super(NewQuestionForm, self).__init__(*args, **kwargs) + self.fields['category'] = forms.CharField(widget=forms.Select(choices=categories)) + self.fields['category'].initial = category + + tutorial_choices = ( + ("None", "Select a Tutorial"), + ) + if (category, category) in categories: + tutorials = TutorialDetails.objects.using('spoken').filter(foss_category=category) + for tutorial in tutorials: + tutorial_choices += ((tutorial.tutorial_name, tutorial.tutorial_name),) + self.fields['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices)) + else: + self.fields['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices)) + minute_range = forms.CharField(widget=forms.Select(choices=minutes)) second_range = forms.CharField(widget=forms.Select(choices=seconds)) title = forms.CharField(max_length=200) diff --git a/website/models.py b/website/models.py index c280d2d..7a845c5 100644 --- a/website/models.py +++ b/website/models.py @@ -22,6 +22,9 @@ class Question(models.Model): user = User.objects.get(id=self.uid) return user.username + class Meta: + get_latest_by = "date_created" + class QuestionVote(models.Model): uid = models.IntegerField() question = models.ForeignKey(Question) diff --git a/website/templatetags/notify.py b/website/templatetags/notify.py index 44465ea..c39e658 100644 --- a/website/templatetags/notify.py +++ b/website/templatetags/notify.py @@ -20,3 +20,16 @@ def notification_count(user_id): count = Notification.objects.filter(uid=user_id).count() return count register.simple_tag(notification_count) + +# retriving the latest post of a category +def latest_question(category): + question = None + try: + question = Question.objects.filter(category=category).order_by('-date_created')[0] + except: + pass + context = { + 'question': question + } + return context +register.inclusion_tag('website/templates/latest_question.html')(latest_question) diff --git a/website/views.py b/website/views.py index cd1665b..925fc2c 100644 --- a/website/views.py +++ b/website/views.py @@ -5,7 +5,7 @@ from django.shortcuts import render, get_object_or_404 from django.core.context_processors import csrf from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required -from django.db.models import Q +from django.db.models import Q, Max from django.core.mail import EmailMultiAlternatives from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.contrib.auth import get_user_model @@ -217,6 +217,7 @@ def filter(request, category=None, tutorial=None, minute_range=None, second_ran @login_required def new_question(request): + context = {} if request.method == 'POST': form = NewQuestionForm(request.POST) if form.is_valid(): @@ -257,11 +258,12 @@ def new_question(request): return HttpResponseRedirect('/') else: - form = NewQuestionForm() - - context = { - 'form': form - } + #fix dirty code + category = request.GET.get('category') + form = NewQuestionForm(category=category) + context['category'] = category + + context['form'] = form context.update(csrf(request)) return render(request, 'website/templates/new-question.html', context) -- cgit