from django import forms from django.db.models import Q from django.forms.models import inlineformset_factory from mptt.forms import TreeNodeChoiceField from pprint import pprint, pformat import itertools from .models import Project, ProjectFile, Portfolio, Answer, Realty, PortfolioPhoto, Stage, Specialization from common.models import Location from users.models import User class ProjectFilterForm(forms.ModelForm): PROJECT_ORDER_CHOICES = ( # "Упорядочить по"... ('name', 'названию'), ('budget', 'цене'), ('created', 'дате размещения'), ('views', 'просмотрам'), ) order_by = forms.ChoiceField(required=False, choices=PROJECT_ORDER_CHOICES) last_order_by = forms.ChoiceField(required=False, choices=PROJECT_ORDER_CHOICES) reverse_order = forms.BooleanField(required=False) keywords = forms.CharField(required=False, max_length=255) class Meta: model = Project fields = ( 'cro', 'specialization', 'work_type', ) widgets = { 'work_type': forms.Select(attrs={'class': 'selectpicker'}), } def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) self.fields['work_type'].choices = tuple(itertools.chain((('',''),), self.fields['work_type'].choices)) self.fields['work_type'].required = False self.fields['work_type'].initial = '' self.fields['specialization'].required = False self.fields['specialization'].queryset = Specialization.objects.root_nodes()[0].get_descendants() class ProjectFilterRealtyForm(forms.ModelForm): class Meta: model = Realty fields = ( 'building_classification', 'construction_type', 'location', ) widgets = { 'building_classification': forms.Select(attrs={'class': 'selectpicker'}), 'construction_type': forms.Select(attrs={'class': 'selectpicker'}), } def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) self.fields['building_classification'].empty_label = '' self.fields['building_classification'].required = False self.fields['construction_type'].empty_label = '' self.fields['construction_type'].required = False self.fields['location'].queryset = Location.objects.root_nodes()[0].get_descendants() class CustomerProjectEditForm(forms.ModelForm): files = forms.ModelMultipleChoiceField( queryset=ProjectFile.objects.none(), widget=forms.CheckboxSelectMultiple, required=False, ) class Meta: model = Project fields = ( 'budget', 'budget_by_agreement', 'cro', 'currency', 'deal_type', 'files', 'name', 'price_and_term_required', 'realty', 'specialization', 'term_type', 'text', 'work_type', ) widgets = { 'currency': forms.Select(attrs={'class': 'selectpicker2 valul'}), 'realty': forms.Select(attrs={'class': 'selectpicker'}), 'term_type': forms.Select(attrs={'class': 'selectpicker'}), } def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) self.fields['realty'].empty_label = 'Создать новый' self.fields['specialization'].queryset = Specialization.objects.root_nodes()[0].get_descendants() if self.instance.pk: self.fields['files'].queryset = self.instance.files class RealtyForm(forms.ModelForm): class Meta: model = Realty fields = ( 'building_classification', 'construction_type', 'location', 'name', ) widgets = { 'construction_type': forms.Select(attrs={'class':'selectpicker'}), 'building_classification': forms.Select(attrs={'class':'selectpicker'}), } def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) self.fields['location'].queryset = Location.objects.root_nodes()[0].get_descendants() class Realty1Form(forms.Form): pass class PortfolioForm(forms.ModelForm): class Meta: model = Portfolio fields = '__all__' widgets = { 'construction_type': forms.Select(attrs={'class':'selectpicker'}), 'building_classification': forms.Select(attrs={'class':'selectpicker'}), 'currency': forms.Select(attrs={'class':'selectpicker'}), 'term_type': forms.Select(attrs={'class':'selectpicker'}), } class ContractorProjectAnswerForm(forms.ModelForm): # def __init__(self, *args, **kwargs): # # import code; code.interact(local=dict(globals(), **locals())) # self.project_id = kwargs.pop('project_id') # super().__init__(*args, **kwargs) # self.fields["project"].initial = self.project_id class Meta: model = Answer fields = ( 'budget', 'currency', 'term', 'term_type', 'text', ) widgets = { 'currency': forms.Select(attrs={'class':'selectpicker'}), 'term_type': forms.Select(attrs={'class':'selectpicker'}), } class StageForm(forms.ModelForm): class Meta: model = Stage fields = ( # 'cost', # 'cost_type', # 'name', # 'result', # 'term', # 'term_type', ) PortfolioPhotoFormSet = inlineformset_factory(Portfolio, PortfolioPhoto, fields=('img',)) class CustomerProjectTrashForm(forms.Form): pk = forms.ModelChoiceField(queryset=Project.objects.none()) def __init__(self, *args, **kwargs): self.req = kwargs.pop('req') super().__init__(*args, **kwargs) self.fields['pk'].queryset = self.req.user.projects.filter(state='active') class CustomerProjectRestoreForm(forms.Form): pk = forms.ModelChoiceField(queryset=Project.objects.none()) def __init__(self, *args, **kwargs): self.req = kwargs.pop('req') super().__init__(*args, **kwargs) self.fields['pk'].queryset = self.req.user.projects.filter(state='trashed') class CustomerProjectDeleteForm(forms.Form): pk = forms.ModelChoiceField(queryset=Project.objects.none()) def __init__(self, *args, **kwargs): self.req = kwargs.pop('req') super().__init__(*args, **kwargs) self.fields['pk'].queryset = self.req.user.projects.filter(Q(state='active') | Q(state='trashed')) # import code; code.interact(local=dict(globals(), **locals()))