2012-08-19 17:43:15 +00:00
|
|
|
from django import forms
|
|
|
|
|
|
|
|
|
|
from django_select2 import *
|
|
|
|
|
|
2012-08-21 17:51:41 +00:00
|
|
|
from .models import Employee, Dept, ClassRoom, Lab
|
2012-08-19 17:43:15 +00:00
|
|
|
|
|
|
|
|
class EmployeeChoices(AutoModelSelect2Field):
|
|
|
|
|
queryset = Employee.objects
|
|
|
|
|
search_fields = ['name__icontains', ]
|
|
|
|
|
|
2012-08-21 17:51:41 +00:00
|
|
|
class ClassRoomChoices(AutoModelSelect2MultipleField):
|
|
|
|
|
queryset = ClassRoom.objects
|
|
|
|
|
search_fields = ['number__icontains', ]
|
|
|
|
|
|
2012-08-22 18:52:49 +00:00
|
|
|
class ClassRoomSingleChoices(AutoModelSelect2Field):
|
|
|
|
|
queryset = ClassRoom.objects
|
|
|
|
|
search_fields = ['number__icontains', ]
|
2012-08-21 17:51:41 +00:00
|
|
|
|
2012-08-19 17:43:15 +00:00
|
|
|
class EmployeeForm(forms.ModelForm):
|
2012-08-21 17:51:41 +00:00
|
|
|
manager = EmployeeChoices(required=False)
|
2012-08-19 17:43:15 +00:00
|
|
|
dept = ModelSelect2Field(queryset=Dept.objects)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Employee
|
2012-08-20 13:38:52 +00:00
|
|
|
|
2012-08-20 20:08:31 +00:00
|
|
|
class DeptForm(forms.ModelForm):
|
2012-08-21 17:51:41 +00:00
|
|
|
allotted_rooms = ClassRoomChoices()
|
|
|
|
|
allotted_labs = ModelSelect2MultipleField(queryset=Lab.objects, required=False)
|
2012-08-20 13:38:52 +00:00
|
|
|
|
2012-08-20 20:08:31 +00:00
|
|
|
class Meta:
|
|
|
|
|
model = Dept
|
2012-08-22 05:49:30 +00:00
|
|
|
|
2012-08-31 19:47:08 +00:00
|
|
|
class SelfChoices(AutoSelect2Field):
|
|
|
|
|
def get_results(self, request, term, page, context):
|
|
|
|
|
res = []
|
|
|
|
|
for i in range(1, 6):
|
|
|
|
|
res.append((i, term * i,))
|
|
|
|
|
self.choices = res
|
|
|
|
|
|
|
|
|
|
return (NO_ERR_RESP, False, res)
|
|
|
|
|
|
|
|
|
|
class SelfMultiChoices(AutoSelect2MultipleField):
|
2012-09-03 04:09:50 +00:00
|
|
|
big_data = {
|
|
|
|
|
1: "First", 2: "Second", 3: "Third",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def validate_value(self, value):
|
|
|
|
|
if value in [v for v in self.big_data]:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def coerce_value(self, value):
|
|
|
|
|
return int(value)
|
|
|
|
|
|
|
|
|
|
def get_val_txt(self, value):
|
|
|
|
|
return self.big_data.get(value, None)
|
|
|
|
|
|
2012-08-31 19:47:08 +00:00
|
|
|
def get_results(self, request, term, page, context):
|
2012-09-03 04:09:50 +00:00
|
|
|
res = [(v, self.big_data[v]) for v in self.big_data]
|
|
|
|
|
for i in range(len(res), 6):
|
2012-08-31 19:47:08 +00:00
|
|
|
res.append((i, term * i,))
|
|
|
|
|
|
|
|
|
|
return (NO_ERR_RESP, False, res)
|
|
|
|
|
|
2012-08-22 18:52:49 +00:00
|
|
|
class MixedForm(forms.Form):
|
|
|
|
|
emp1 = EmployeeChoices()
|
|
|
|
|
rooms1 = ClassRoomChoices()
|
|
|
|
|
emp2 = EmployeeChoices()
|
|
|
|
|
rooms2 = ClassRoomChoices()
|
|
|
|
|
rooms3 = ClassRoomSingleChoices()
|
2012-08-31 19:47:08 +00:00
|
|
|
self_choices = SelfChoices(label='Self copy choices')
|
|
|
|
|
self_multi_choices = SelfMultiChoices(label='Self copy multi-choices')
|
2012-08-22 18:52:49 +00:00
|
|
|
|
2012-08-22 05:49:30 +00:00
|
|
|
# These are just for testing Auto registration of fields
|
|
|
|
|
EmployeeChoices() # Should already be registered
|
|
|
|
|
EmployeeChoices(auto_id="EmployeeChoices_CustomAutoId") # Should get registered
|
2012-08-31 06:04:23 +00:00
|
|
|
|
|
|
|
|
class InitialValueForm(forms.Form):
|
|
|
|
|
select2Choice = Select2ChoiceField(initial=2, choices=((1, "First"), (2, "Second"), (3, "Third"), ))
|
|
|
|
|
select2MultipleChoice = Select2MultipleChoiceField(initial=[2,3], choices=((1, "First"), (2, "Second"), (3, "Third"), ))
|
|
|
|
|
heavySelect2Choice = AutoSelect2Field(initial=2, choices=((1, "First"), (2, "Second"), (3, "Third"), ))
|
|
|
|
|
heavySelect2MultipleChoice = AutoSelect2MultipleField(initial=[1,3], choices=((1, "First"), (2, "Second"), (3, "Third"), ))
|
2012-09-01 19:51:17 +00:00
|
|
|
self_choices = SelfChoices(label='Self copy choices', initial=2, choices=((1, "First"), (2, "Second"), (3, "Third"), ))
|
2012-09-03 04:09:50 +00:00
|
|
|
self_multi_choices = SelfMultiChoices(label='Self copy multi-choices', initial=[2,3])
|
2012-08-31 06:04:23 +00:00
|
|
|
|