Skip to content

Commit d45592d

Browse files
authored
Enhance login link and refactor NewQuestionForm category choices (#77)
* 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. * Refactor NewQuestionForm to preserve minute/second values on validation errors * Update login links in templates to retain original request path for improved user experience
1 parent 9025210 commit d45592d

4 files changed

Lines changed: 53 additions & 16 deletions

File tree

static/forums/templates/update-password.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</div>
3434
<div class="sign">
3535
<div class="need">Already Registered? </div>
36-
<div> <a href="/accounts/login/" class="btn btn-sm btn-success">Login </a></div>
36+
<div> <a href="{% url 'user_login' %}?next={{ request.get_full_path|urlencode }}" class="btn btn-sm btn-success">Login </a></div>
3737
</div>
3838
</div>
3939
</div>

static/website/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
</li> <!-- /li.dropdown -->
101101
{% else %}
102102
<li>
103-
<a href="{% url 'user_login' %}">
103+
<a href="{% url 'user_login' %}?next={{ request.get_full_path|urlencode }}">
104104
<span class="glyphicon glyphicon-user"></span>
105105
Login
106106
</a>

static/website/templates/get-question.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ <h4><u>Answers:</u></h4>
231231
</a>
232232
{% else %}
233233
<br>
234-
<a class="btn btn-xs btn-success vs" href="/accounts/login">
234+
<a class="btn btn-xs btn-success vs" href="{% url 'user_login' %}?next={{ request.get_full_path|urlencode }}">
235235
Login to add comment
236236
</a>
237237
{% endif %}
@@ -264,7 +264,7 @@ <h4><u>Answers:</u></h4>
264264
</form>
265265
{% else %}
266266
<h4>
267-
<a class="btn btn-xs btn-success" href="{% url 'user_login'%}"><b>Log-in</b></a> to answer to this question.
267+
<a class="btn btn-xs btn-success" href="{% url 'user_login' %}?next={{ request.get_full_path|urlencode }}"><b>Log-in</b></a> to answer to this question.
268268
</h4>
269269
{% endif %}
270270

website/forms.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,72 @@
99
seconds = ()
1010

1111

12+
def _get_category_choices():
13+
"""Distinct FOSS categories for new-question form, sorted alphabetically."""
14+
categories = list(
15+
TutorialResources.objects.filter(
16+
Q(status=1) | Q(status=2),
17+
language__name='English',
18+
tutorial_detail__foss__show_on_homepage=1,
19+
)
20+
.values_list('tutorial_detail__foss__foss', flat=True)
21+
.distinct()
22+
)
23+
# Remove empty/None and sort case-insensitively (A-Z)
24+
categories = [c for c in categories if c]
25+
categories = sorted(set(categories), key=lambda x: x.lower())
26+
return [('', 'Select a Category')] + [(c, c) for c in categories]
27+
28+
1229
class NewQuestionForm(forms.Form):
13-
category = forms.ChoiceField(choices=[('', 'Select a Category'), ] + list(TutorialResources.objects.filter(
14-
Q(status=1) | Q(status=2), language__name='English',tutorial_detail__foss__show_on_homepage=1).values_list('tutorial_detail__foss__foss',
15-
'tutorial_detail__foss__foss').distinct()),
16-
widget=forms.Select(attrs={}), required=True, error_messages={'required': 'State field is required.'})
30+
category = forms.ChoiceField(
31+
choices=[],
32+
widget=forms.Select(attrs={}),
33+
required=True,
34+
error_messages={'required': 'State field is required.'},
35+
)
1736
title = forms.CharField(max_length=200)
1837
body = forms.CharField(widget=forms.Textarea())
1938

2039
def __init__(self, *args, **kwargs):
40+
# Values that can be passed explicitly (e.g. from the spoken website)
2141
category = kwargs.pop('category', None)
2242
selecttutorial = kwargs.pop('tutorial', None)
23-
2443
select_min = kwargs.pop('minute_range', None)
2544
select_sec = kwargs.pop('second_range', None)
45+
2646
super(NewQuestionForm, self).__init__(*args, **kwargs)
47+
self.fields['category'].choices = _get_category_choices()
2748
tutorial_choices = (
2849
("Select a Tutorial", "Select a Tutorial"),
2950
)
30-
# check minute_range, secpnd_range coming from spoken website
31-
# user clicks on post question link through website
32-
if (select_min is None and select_sec is None):
51+
52+
# If no explicit minute/second values were provided (e.g. normal POST),
53+
# preserve any values that came from submitted form data so that
54+
# validation errors (like missing reCAPTCHA) do not wipe them out.
55+
data = args[0] if args else {}
56+
if select_min is None and data and 'minute_range' in data:
57+
select_min = data.get('minute_range')
58+
if select_sec is None and data and 'second_range' in data:
59+
select_sec = data.get('second_range')
60+
61+
# When a minute/second value is available (from the spoken website or a
62+
# previous form submission), show that value as the only selectable
63+
# option; otherwise show the default placeholders.
64+
if select_min:
3365
minutes = (
3466
(select_min, select_min),
3567
)
36-
seconds = (
37-
(select_sec, select_sec),
38-
)
3968
else:
4069
minutes = (
4170
("", "min"),
4271
)
72+
73+
if select_sec:
74+
seconds = (
75+
(select_sec, select_sec),
76+
)
77+
else:
4378
seconds = (
4479
("", "sec"),
4580
)
@@ -48,7 +83,9 @@ def __init__(self, *args, **kwargs):
4883
category = args[0]['category']
4984
if FossCategory.objects.filter(foss=category).exists():
5085
self.fields['category'].initial = category
51-
tutorials = TutorialDetails.objects.using('spoken').filter(foss__foss=category)
86+
tutorials = TutorialDetails.objects.using('spoken').filter(
87+
foss__foss=category
88+
).order_by('level', 'order')
5289
for tutorial in tutorials:
5390
tutorial_choices += ((tutorial.tutorial, tutorial.tutorial),)
5491
self.fields['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices))

0 commit comments

Comments
 (0)