vkk.workhours.accounting.projects.project.export.funded_staff.forms
1from django import forms 2from vkk.workhours.models import ( 3 ProjectFundedStaff, ProjectFundedStaffDate, SalaryLevel, 4 Project 5) 6from vkk.workhours.forms import CustomDateInput 7 8class ProjectFundedStaffForm(forms.ModelForm): 9 class Meta: 10 model = ProjectFundedStaffDate 11 fields = ['date'] 12 widgets = { 13 'date': CustomDateInput(), 14 } 15 16 def __init__(self, *args, invoice_number=None, **kwargs): 17 super().__init__(*args, **kwargs) 18 self._invoice_number = invoice_number 19 20 # Collect all salary levels to create Fields 21 self._salary_levels = SalaryLevel.objects.all() 22 field_class = ProjectFundedStaff.hours.field.formfield 23 for salary_level in self._salary_levels: 24 self.fields[ 25 'fun_' + str(salary_level.salary_code) 26 ] = field_class( 27 label=salary_level.salary_code 28 ) 29 # Look for model instance and fill associated fields accordingly 30 if self.instance is not None: 31 project_funded_staff_instances = ProjectFundedStaff.objects.filter( 32 start=self.instance 33 ).select_related('salary_level') 34 for project_funded_staff_instance in project_funded_staff_instances: 35 self.fields[ 36 'fun_' + str(project_funded_staff_instance.salary_level.salary_code) 37 ].initial = project_funded_staff_instance.hours 38 39 def save(self, commit=True): 40 if self.instance.pk is None: 41 self.instance.project = Project.objects.get(invoice_number=self._invoice_number) 42 # save instance 43 super().save(commit) 44 # save all associated instances 45 if self.is_valid() and self.has_changed(): 46 project_funded_staff_list = [] 47 for salary_level in self._salary_levels: 48 hours = self.cleaned_data.get('fun_' + str(salary_level.salary_code)) 49 project_funded_staff_list.append( 50 ProjectFundedStaff( 51 salary_level=salary_level, 52 start=self.instance, 53 hours=hours 54 ) 55 ) 56 ProjectFundedStaff.objects.bulk_create( 57 project_funded_staff_list, 58 update_conflicts=True, 59 update_fields=['hours'], 60 unique_fields=['salary_level_id', 'start_id'] 61 ) 62 return self.instance
class
ProjectFundedStaffForm(django.forms.models.ModelForm):
9class ProjectFundedStaffForm(forms.ModelForm): 10 class Meta: 11 model = ProjectFundedStaffDate 12 fields = ['date'] 13 widgets = { 14 'date': CustomDateInput(), 15 } 16 17 def __init__(self, *args, invoice_number=None, **kwargs): 18 super().__init__(*args, **kwargs) 19 self._invoice_number = invoice_number 20 21 # Collect all salary levels to create Fields 22 self._salary_levels = SalaryLevel.objects.all() 23 field_class = ProjectFundedStaff.hours.field.formfield 24 for salary_level in self._salary_levels: 25 self.fields[ 26 'fun_' + str(salary_level.salary_code) 27 ] = field_class( 28 label=salary_level.salary_code 29 ) 30 # Look for model instance and fill associated fields accordingly 31 if self.instance is not None: 32 project_funded_staff_instances = ProjectFundedStaff.objects.filter( 33 start=self.instance 34 ).select_related('salary_level') 35 for project_funded_staff_instance in project_funded_staff_instances: 36 self.fields[ 37 'fun_' + str(project_funded_staff_instance.salary_level.salary_code) 38 ].initial = project_funded_staff_instance.hours 39 40 def save(self, commit=True): 41 if self.instance.pk is None: 42 self.instance.project = Project.objects.get(invoice_number=self._invoice_number) 43 # save instance 44 super().save(commit) 45 # save all associated instances 46 if self.is_valid() and self.has_changed(): 47 project_funded_staff_list = [] 48 for salary_level in self._salary_levels: 49 hours = self.cleaned_data.get('fun_' + str(salary_level.salary_code)) 50 project_funded_staff_list.append( 51 ProjectFundedStaff( 52 salary_level=salary_level, 53 start=self.instance, 54 hours=hours 55 ) 56 ) 57 ProjectFundedStaff.objects.bulk_create( 58 project_funded_staff_list, 59 update_conflicts=True, 60 update_fields=['hours'], 61 unique_fields=['salary_level_id', 'start_id'] 62 ) 63 return self.instance
The main implementation of all the Form logic. Note that this class is different than Form. See the comments by the Form class for more info. Any improvements to the form API should be made to this class, not to the Form class.
ProjectFundedStaffForm(*args, invoice_number=None, **kwargs)
17 def __init__(self, *args, invoice_number=None, **kwargs): 18 super().__init__(*args, **kwargs) 19 self._invoice_number = invoice_number 20 21 # Collect all salary levels to create Fields 22 self._salary_levels = SalaryLevel.objects.all() 23 field_class = ProjectFundedStaff.hours.field.formfield 24 for salary_level in self._salary_levels: 25 self.fields[ 26 'fun_' + str(salary_level.salary_code) 27 ] = field_class( 28 label=salary_level.salary_code 29 ) 30 # Look for model instance and fill associated fields accordingly 31 if self.instance is not None: 32 project_funded_staff_instances = ProjectFundedStaff.objects.filter( 33 start=self.instance 34 ).select_related('salary_level') 35 for project_funded_staff_instance in project_funded_staff_instances: 36 self.fields[ 37 'fun_' + str(project_funded_staff_instance.salary_level.salary_code) 38 ].initial = project_funded_staff_instance.hours
def
save(self, commit=True):
40 def save(self, commit=True): 41 if self.instance.pk is None: 42 self.instance.project = Project.objects.get(invoice_number=self._invoice_number) 43 # save instance 44 super().save(commit) 45 # save all associated instances 46 if self.is_valid() and self.has_changed(): 47 project_funded_staff_list = [] 48 for salary_level in self._salary_levels: 49 hours = self.cleaned_data.get('fun_' + str(salary_level.salary_code)) 50 project_funded_staff_list.append( 51 ProjectFundedStaff( 52 salary_level=salary_level, 53 start=self.instance, 54 hours=hours 55 ) 56 ) 57 ProjectFundedStaff.objects.bulk_create( 58 project_funded_staff_list, 59 update_conflicts=True, 60 update_fields=['hours'], 61 unique_fields=['salary_level_id', 'start_id'] 62 ) 63 return self.instance
Save this form's self.instance object if commit=True. Otherwise, add a save_m2m() method to the form which can be called after the instance is saved manually at a later time. Return the model instance.
Inherited Members
- django.forms.models.BaseModelForm
- clean
- validate_unique
- django.forms.forms.BaseForm
- order_fields
- errors
- is_valid
- add_prefix
- add_initial_prefix
- get_context
- non_field_errors
- add_error
- has_error
- full_clean
- has_changed
- changed_data
- is_multipart
- visible_fields
- get_initial_for_field
- django.forms.utils.RenderableFormMixin
- as_p
- as_table
- as_ul
- as_div
- django.forms.utils.RenderableMixin
- render
class
ProjectFundedStaffForm.Meta: