From 0505017ad502c3592429c17a07e50c2ec80dbb5d Mon Sep 17 00:00:00 2001 From: Kushagra Tiwari Date: Sat, 28 Feb 2026 11:22:52 +0530 Subject: [PATCH 1/3] Enhance login link and refactor NewQuestionForm category choices - Updated the login link to retain the original request path for better user experience. - Refactored the NewQuestionForm to dynamically populate category choices from the database, ensuring they are distinct and sorted alphabetically. - Improved the tutorial retrieval process by ordering results based on level and order. --- static/website/templates/base.html | 2 +- website/forms.py | 32 +++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/static/website/templates/base.html b/static/website/templates/base.html index 0152830..578adc2 100644 --- a/static/website/templates/base.html +++ b/static/website/templates/base.html @@ -100,7 +100,7 @@ {% else %}
  • - + Login diff --git a/website/forms.py b/website/forms.py index cd32f52..b9493ab 100755 --- a/website/forms.py +++ b/website/forms.py @@ -9,11 +9,30 @@ seconds = () +def _get_category_choices(): + """Distinct FOSS categories for new-question form, sorted alphabetically.""" + categories = list( + TutorialResources.objects.filter( + Q(status=1) | Q(status=2), + language__name='English', + tutorial_detail__foss__show_on_homepage=1, + ) + .values_list('tutorial_detail__foss__foss', flat=True) + .distinct() + ) + # Remove empty/None and sort case-insensitively (A-Z) + categories = [c for c in categories if c] + categories = sorted(set(categories), key=lambda x: x.lower()) + return [('', 'Select a Category')] + [(c, c) for c in categories] + + class NewQuestionForm(forms.Form): - category = forms.ChoiceField(choices=[('', 'Select a Category'), ] + list(TutorialResources.objects.filter( - Q(status=1) | Q(status=2), language__name='English',tutorial_detail__foss__show_on_homepage=1).values_list('tutorial_detail__foss__foss', - 'tutorial_detail__foss__foss').distinct()), - widget=forms.Select(attrs={}), required=True, error_messages={'required': 'State field is required.'}) + category = forms.ChoiceField( + choices=[], + widget=forms.Select(attrs={}), + required=True, + error_messages={'required': 'State field is required.'}, + ) title = forms.CharField(max_length=200) body = forms.CharField(widget=forms.Textarea()) @@ -24,6 +43,7 @@ def __init__(self, *args, **kwargs): select_min = kwargs.pop('minute_range', None) select_sec = kwargs.pop('second_range', None) super(NewQuestionForm, self).__init__(*args, **kwargs) + self.fields['category'].choices = _get_category_choices() tutorial_choices = ( ("Select a Tutorial", "Select a Tutorial"), ) @@ -48,7 +68,9 @@ def __init__(self, *args, **kwargs): category = args[0]['category'] if FossCategory.objects.filter(foss=category).exists(): self.fields['category'].initial = category - tutorials = TutorialDetails.objects.using('spoken').filter(foss__foss=category) + tutorials = TutorialDetails.objects.using('spoken').filter( + foss__foss=category + ).order_by('level', 'order') for tutorial in tutorials: tutorial_choices += ((tutorial.tutorial, tutorial.tutorial),) self.fields['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices)) From 326909b0fe4a3a3c97c7d24dd02442a49c5ff289 Mon Sep 17 00:00:00 2001 From: Kushagra Tiwari Date: Mon, 2 Mar 2026 11:31:49 +0530 Subject: [PATCH 2/3] Refactor NewQuestionForm to preserve minute/second values on validation errors --- website/forms.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/website/forms.py b/website/forms.py index b9493ab..1d9391f 100755 --- a/website/forms.py +++ b/website/forms.py @@ -37,29 +37,44 @@ class NewQuestionForm(forms.Form): body = forms.CharField(widget=forms.Textarea()) def __init__(self, *args, **kwargs): + # Values that can be passed explicitly (e.g. from the spoken website) category = kwargs.pop('category', None) selecttutorial = kwargs.pop('tutorial', None) - select_min = kwargs.pop('minute_range', None) select_sec = kwargs.pop('second_range', None) + super(NewQuestionForm, self).__init__(*args, **kwargs) self.fields['category'].choices = _get_category_choices() tutorial_choices = ( ("Select a Tutorial", "Select a Tutorial"), ) - # check minute_range, secpnd_range coming from spoken website - # user clicks on post question link through website - if (select_min is None and select_sec is None): + + # If no explicit minute/second values were provided (e.g. normal POST), + # preserve any values that came from submitted form data so that + # validation errors (like missing reCAPTCHA) do not wipe them out. + data = args[0] if args else {} + if select_min is None and data and 'minute_range' in data: + select_min = data.get('minute_range') + if select_sec is None and data and 'second_range' in data: + select_sec = data.get('second_range') + + # When a minute/second value is available (from the spoken website or a + # previous form submission), show that value as the only selectable + # option; otherwise show the default placeholders. + if select_min: minutes = ( (select_min, select_min), ) - seconds = ( - (select_sec, select_sec), - ) else: minutes = ( ("", "min"), ) + + if select_sec: + seconds = ( + (select_sec, select_sec), + ) + else: seconds = ( ("", "sec"), ) From a52a05d21413b8a45cae7794b5845a739527d552 Mon Sep 17 00:00:00 2001 From: Kushagra Tiwari Date: Mon, 2 Mar 2026 13:44:46 +0530 Subject: [PATCH 3/3] Update login links in templates to retain original request path for improved user experience --- static/forums/templates/update-password.html | 2 +- static/website/templates/get-question.html | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/static/forums/templates/update-password.html b/static/forums/templates/update-password.html index b54e95c..f510884 100755 --- a/static/forums/templates/update-password.html +++ b/static/forums/templates/update-password.html @@ -33,7 +33,7 @@
    Already Registered?
    - +
    diff --git a/static/website/templates/get-question.html b/static/website/templates/get-question.html index 7bc8bc0..9025017 100755 --- a/static/website/templates/get-question.html +++ b/static/website/templates/get-question.html @@ -231,7 +231,7 @@

    Answers:

    {% else %}
    - + Login to add comment {% endif %} @@ -264,7 +264,7 @@

    Answers:

    {% else %}

    - Log-in to answer to this question. + Log-in to answer to this question.

    {% endif %}