from django import forms from django.forms import ModelForm import itertools import pydash as _; _.map = _.map_; _.filter = _.filter_ from .models import User, UserFinancialInfo from common.models import Location from projects.models import Project, Realty, BuildingClassfication, ConstructionType from specializations.models import Specialization class UserEditForm(ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if kwargs.get('instance'): if kwargs.get('instance').is_contractor(): self.fields['contractor_specializations'].queryset = kwargs.get('instance').contractor_specializations.all() class Meta: model = User fields = ( 'first_name', 'last_name', 'patronym', 'location', 'date_of_birth', 'website', 'skype', 'avatar', 'contractor_specializations', 'cro', 'phone', 'gender', ) widgets = { 'date_of_birth': forms.SelectDateWidget(years=range(1940, 2015)), # 'contractor_specializations': forms.Select(), } class ContractorFilterForm(forms.Form): CONTRACTOR_ORDER_CHOICES = ( ('', 'Сортировать по...'), ('name', 'Названию'), ('created', 'Дате размещения'), ('views', 'Просмотрам'), ('reviews', 'Отзывам'), ('rating', 'Рейтингу'), # ('project_number', 'Кол-ву выполн. проектов'), ) PARTY_TYPES = ( ('all', 'Все'), ('teams', 'Группы'), ('contractors', 'Исполнители'), ) order_by = forms.ChoiceField(required=False, choices=CONTRACTOR_ORDER_CHOICES) last_order_by = forms.ChoiceField(required=False, choices=CONTRACTOR_ORDER_CHOICES) reverse_order = forms.BooleanField(required=False) specialization = forms.ModelChoiceField( queryset=Specialization.objects.root_nodes()[0].get_descendants(), # queryset=Specialization.objects, # Migrate with this enabled required=False, ) location = forms.ModelChoiceField( queryset=Location.objects.root_nodes()[0].get_descendants(), # queryset=Location.objects, # Migrate with this enabled required=False, ) building_classification = forms.ModelChoiceField( queryset=BuildingClassfication.objects, widget=forms.Select(attrs={'class': 'selectpicker'}), required=False, empty_label='', ) work_type = forms.ChoiceField( choices=tuple(itertools.chain((('',''),), Project.WORK_TYPES)), widget=forms.Select(attrs={'class': 'selectpicker'}), required=False, ) construction_type = forms.ModelChoiceField( queryset=ConstructionType.objects, widget=forms.Select(attrs={'class': 'selectpicker'}), required=False, empty_label='', ) cro = forms.BooleanField(required=False) party_types = forms.ChoiceField( choices=PARTY_TYPES, required=False, ) last_party_types = forms.ChoiceField( choices=PARTY_TYPES, required=False, ) def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) class CustomerProfileProjectRealtyForm(forms.Form): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') self.customer = kwargs.pop('customer') super().__init__(*args, **kwargs) realties = _.uniq(tuple(p.realty for p in self.customer.projects.all())) self.fields['realty'] = forms.ChoiceField( widget=forms.Select(attrs={ 'class': 'selectpicker', 'onchange': "$(this).closest('form').submit()", }), choices=(('', 'Все объекты'),) + tuple((r.pk, r.name) for r in realties), required=False, ) class UserFinancicalInfoForm(ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['residency'].choices = self.fields['residency'].choices[1:] self.fields['legal_status'].choices = self.fields['legal_status'].choices[1:] # self.fields['residency'].empty_label = None # self.fields['residency'].widget.choices = self.fields['residency'].choices class Meta: model = UserFinancialInfo fields = ( 'fio', 'date_of_birth', 'phone', 'residency', 'legal_status', 'passport_series', 'passport_number', 'subdivision_code', 'passport_issued_by', 'passport_issue_date', 'inn', 'yandex_money', 'credit_card_number', 'passport_scan', ) widgets = { 'residency': forms.RadioSelect(), 'legal_status': forms.RadioSelect(), }