diff --git a/website/forms.py b/website/forms.py index 1d9391f..81222b2 100755 --- a/website/forms.py +++ b/website/forms.py @@ -61,46 +61,50 @@ def __init__(self, *args, **kwargs): # 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), - ) + if select_min not in (None, ''): + minutes = ((select_min, select_min),) else: - minutes = ( - ("", "min"), - ) - - if select_sec: - seconds = ( - (select_sec, select_sec), - ) + minutes = (("", "min"),) + if select_sec not in (None, ''): + seconds = ((select_sec, select_sec),) else: - seconds = ( - ("", "sec"), - ) + seconds = (("", "sec"),) - if not category and args and 'category' in args[0] and args[0]['category']: - category = args[0]['category'] - if FossCategory.objects.filter(foss=category).exists(): + if not category and args and len(args) > 0 and hasattr(args[0], 'get') and args[0].get('category'): + category = args[0].get('category') + if category: self.fields['category'].initial = category + if FossCategory.objects.filter(foss=category).exists(): tutorials = TutorialDetails.objects.using('spoken').filter( foss__foss=category ).order_by('level', 'order') for tutorial in tutorials: tutorial_choices += ((tutorial.tutorial, tutorial.tutorial),) + # Include submitted tutorial in choices so it displays after captcha error + if selecttutorial and not any(c[0] == selecttutorial for c in tutorial_choices): + tutorial_choices += ((selecttutorial, selecttutorial),) self.fields['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices)) - if TutorialDetails.objects.using('spoken').filter(tutorial=selecttutorial).exists(): + if selecttutorial: self.fields['tutorial'].initial = selecttutorial - - self.fields['minute_range'] = forms.CharField(widget=forms.Select(choices=minutes)) - self.fields['second_range'] = forms.CharField(widget=forms.Select(choices=seconds)) - else: - self.fields['minute_range'] = forms.CharField(widget=forms.Select(choices=minutes)) - self.fields['second_range'] = forms.CharField(widget=forms.Select(choices=seconds)) + self.fields['minute_range'] = forms.CharField(widget=forms.Select(choices=minutes)) + self.fields['second_range'] = forms.CharField(widget=forms.Select(choices=seconds)) + if select_min not in (None, ''): + self.fields['minute_range'].initial = select_min + if select_sec not in (None, ''): + self.fields['second_range'].initial = select_sec else: + # Category missing or not in DB: still show submitted tutorial if any + if selecttutorial: + tutorial_choices += ((selecttutorial, selecttutorial),) self.fields['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices)) + if selecttutorial: + self.fields['tutorial'].initial = selecttutorial self.fields['minute_range'] = forms.CharField(widget=forms.Select(choices=minutes)) self.fields['second_range'] = forms.CharField(widget=forms.Select(choices=seconds)) + if select_min not in (None, ''): + self.fields['minute_range'].initial = select_min + if select_sec not in (None, ''): + self.fields['second_range'].initial = select_sec class AnswerQuesitionForm(forms.Form): diff --git a/website/views.py b/website/views.py index e4ad525..44bcbb1 100755 --- a/website/views.py +++ b/website/views.py @@ -651,9 +651,7 @@ def new_question(request): # If form not valid -> re-render with errors context['form'] = form context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY - # check if user needs to complete captcha - user_has_role = request.user.is_authenticated and request.user.groups.exists() - context['require_recaptcha'] = not user_has_role + context['require_recaptcha'] = not has_role(request.user) context.update(csrf(request)) return render(request, 'website/templates/new-question.html', context)