from django import forms from django.forms import ModelForm, HiddenInput, Form, Select from django.forms.models import inlineformset_factory from mptt.forms import TreeNodeChoiceField from .models import Project, Portfolio, Answer, Realty, PortfolioPhoto, Stage, Specialization from users.models import User # RealtyFormSet = inlineformset_factory(Project, Realty) class ProjectForm(ModelForm): specialization = TreeNodeChoiceField(queryset=Specialization.objects.exclude(id=1)) class Meta: model = Project fields = ( 'budget', 'budget_by_agreement', 'cro', 'currency', 'name', 'price_and_term_required', 'deal_type', 'term_type', 'text', 'work_type', 'specialization', ) widgets = { 'specialization': Select(attrs={'class':'selectpicker'}), 'currency': Select(attrs={'class':'selectpicker2 valul'}), 'term_type': Select(attrs={'class':'selectpicker'}), } class ProjectsForm(Form): name = forms.CharField(max_length=255) class RealtyForm(ModelForm): class Meta: model = Realty fields = ( 'country', 'city', 'building_classification', 'construction_type', 'name', ) widgets = { 'construction_type': Select(attrs={'class':'selectpicker'}), 'building_classification': Select(attrs={'class':'selectpicker'}), 'city': Select(attrs={'class':'selectpicker'}), 'country': Select(attrs={'class':'selectpicker'}), } class Realty1Form(Form): pass class PortfolioForm(ModelForm): class Meta: model = Portfolio fields = '__all__' class AnswerForm(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': Select(attrs={'class':'selectpicker'}), 'term_type': Select(attrs={'class':'selectpicker'}), } class StageForm(ModelForm): class Meta: model = Stage fields = ( # 'cost', # 'cost_type', # 'name', # 'result', # 'term', # 'term_type', ) PortfolioPhotoFormSet = inlineformset_factory(Portfolio, PortfolioPhoto, fields=('img',)) class ProjectTrashForm(Form): pk = forms.ModelChoiceField(queryset=Project.objects.none()) def __init__(self, *args, **kwargs): self.req = kwargs.pop('req') super().__init__(*args, **kwargs) if self.req.user.is_authenticated(): self.fields['pk'].queryset = self.req.user.projects def clean(self): cleaned_data = super().clean() if not self.req.user.is_authenticated(): raise forms.ValidationError('Пользователь не залогинен') return cleaned_data