-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathforms.py
More file actions
executable file
·87 lines (69 loc) · 3.31 KB
/
forms.py
File metadata and controls
executable file
·87 lines (69 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from django import forms
from spoken_auth.models import TutorialDetails, TutorialResources, FossCategory
from django.db.models import Q
tutorials = (
("Select a Tutorial", "Select a Tutorial"),
)
minutes = ()
seconds = ()
# Pre-fetch FOSS ids shown on homepage
foss_ids = list(
FossCategory.objects.using('spoken')
.filter(show_on_homepage=1)
.values_list('id', flat=True) # get ids only
)
class NewQuestionForm(forms.Form):
category = forms.ChoiceField(
choices=[('', 'Select a Category')] +
list(
TutorialResources.objects.using('spoken')
.filter(
Q(status__in=[1, 2]), # cleaner than Q(status=1)|Q(status=2)
language_id=22, # use id instead of name
tutorial_detail__foss_id__in=foss_ids
)
.values('tutorial_detail__foss__foss')
.order_by('tutorial_detail__foss__foss')
.values_list('tutorial_detail__foss__foss', 'tutorial_detail__foss__foss')
.distinct()
),
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())
def __init__(self, *args, **kwargs):
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)
tutorial_choices = (("Select a Tutorial", "Select a Tutorial"),)
# Set minutes & seconds
if select_min is None and select_sec is None:
minutes = ((select_min, select_min),)
seconds = ((select_sec, select_sec),)
else:
minutes = (("", "min"),)
seconds = (("", "sec"),)
# Handle category logic
if not category and args and 'category' in args[0] and args[0]['category']:
category = args[0]['category']
if FossCategory.objects.using('spoken').filter(foss=category).exists():
self.fields['category'].initial = category
tutorials = TutorialDetails.objects.using('spoken').filter(foss__foss=category)
for tutorial in tutorials:
tutorial_choices += ((tutorial.tutorial, tutorial.tutorial),)
self.fields['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices))
if TutorialDetails.objects.using('spoken').filter(tutorial=selecttutorial).exists():
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['tutorial'] = forms.CharField(widget=forms.Select(choices=tutorial_choices))
self.fields['minute_range'] = forms.CharField(widget=forms.Select(choices=minutes))
self.fields['second_range'] = forms.CharField(widget=forms.Select(choices=seconds))
class AnswerQuesitionForm(forms.Form):
question = forms.IntegerField(widget=forms.HiddenInput())
body = forms.CharField(widget=forms.Textarea())