|
| 1 | +from django.forms import ModelForm, Form, TypedChoiceField, CharField |
| 2 | +from crispy_forms.helper import FormHelper |
| 3 | +from crispy_forms.layout import Layout, Fieldset, Div, Field |
| 4 | +from .models import Item, Identifier, IdentifierType |
| 5 | + |
| 6 | + |
| 7 | +class SearchForm(Form): |
| 8 | + identifier_types = [(i.id, i.name) |
| 9 | + for i in IdentifierType.objects.all()] |
| 10 | + identifier_types.insert(0, (0, 'Name')) |
| 11 | + search_property = TypedChoiceField( |
| 12 | + choices=identifier_types, |
| 13 | + coerce=int, |
| 14 | + empty_value=0 |
| 15 | + ) |
| 16 | + search_keyword = CharField() |
| 17 | + |
| 18 | + def __init__(self, *args, **kwargs): |
| 19 | + self.helper = FormHelper() |
| 20 | + self.helper.form_tag = False |
| 21 | + self.helper.layout = Layout( |
| 22 | + Div( |
| 23 | + Div( |
| 24 | + Field('search_property', css_class='form-helper'), |
| 25 | + css_class='col-sm-6' |
| 26 | + ), |
| 27 | + Div( |
| 28 | + Field('search_keyword', css_class='form-helper'), |
| 29 | + css_class='col-sm-6' |
| 30 | + ), |
| 31 | + css_class='row' |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + super(SearchForm, self).__init__(*args, **kwargs) |
| 36 | + |
| 37 | + |
| 38 | +class ItemForm(ModelForm): |
| 39 | + |
| 40 | + class Meta: |
| 41 | + model = Item |
| 42 | + fields = ['name', 'desc', 'bucket'] |
| 43 | + |
| 44 | + def __init__(self, *args, **kwargs): |
| 45 | + self.helper = FormHelper() |
| 46 | + self.helper.form_tag = False |
| 47 | + self.helper.layout = Layout( |
| 48 | + Div( |
| 49 | + Div('name', css_class='col-md-6'), |
| 50 | + Div('bucket', css_class='col-md-6'), |
| 51 | + css_class='row' |
| 52 | + ), |
| 53 | + Field('desc', rows=2, cols=50) |
| 54 | + ) |
| 55 | + |
| 56 | + super(ItemForm, self).__init__(*args, **kwargs) |
| 57 | + self.fields['bucket'].required = True |
| 58 | + |
| 59 | + |
| 60 | +class BucketItemForm(ModelForm): |
| 61 | + |
| 62 | + class Meta: |
| 63 | + model = Item |
| 64 | + fields = ['name', 'desc', 'bucket'] |
| 65 | + |
| 66 | + def __init__(self, *args, **kwargs): |
| 67 | + self.helper = FormHelper() |
| 68 | + self.helper.form_tag = False |
| 69 | + self.helper.layout = Layout( |
| 70 | + Field('name'), |
| 71 | + Field('desc', rows=2, cols=50), |
| 72 | + Field('bucket', type='hidden') |
| 73 | + ) |
| 74 | + |
| 75 | + super(BucketItemForm, self).__init__(*args, **kwargs) |
| 76 | + |
| 77 | + |
| 78 | +class IdentifierForm(ModelForm): |
| 79 | + |
| 80 | + class Meta: |
| 81 | + model = Identifier |
| 82 | + fields = ['type', 'value'] |
| 83 | + |
| 84 | + def __init__(self, *args, **kwargs): |
| 85 | + self.helper = FormHelper() |
| 86 | + self.helper.form_tag = False |
| 87 | + self.helper.layout = Layout( |
| 88 | + Div( |
| 89 | + Div('type', css_class='col-md-6'), |
| 90 | + Div('value', css_class='col-md-6'), |
| 91 | + css_class='row' |
| 92 | + ) |
| 93 | + ) |
| 94 | + |
| 95 | + super(IdentifierForm, self).__init__(*args, **kwargs) |
0 commit comments