vkk.workhours.accounting.costs.forms
1from django import forms 2from vkk.workhours.models import ( 3 GeneralCosts, DepartmentDate, DepartmentCosts, 4 SalaryLevelDate, SalaryLevelCosts, ReceiptTemplate, 5 Department, SalaryLevel 6) 7from vkk.workhours.forms import CustomDateInput 8 9class DepartmentCostForm(forms.ModelForm): 10 class Meta: 11 model = DepartmentDate 12 fields = ['date'] 13 widgets = { 14 'date': CustomDateInput(), 15 } 16 17 def __init__(self, *args, **kwargs): 18 super().__init__(*args, **kwargs) 19 20 # Collect all associated departments to create fields 21 self._departments = Department.objects.all().order_by('name') 22 field_class = DepartmentCosts.equivalents_per_hour.field.formfield 23 for department in self._departments: 24 self.fields[ 25 'dep_' + str(department.accounting_entry) 26 ] = field_class( 27 label=department.name 28 ) 29 30 # Look for model instance and fill associated fields accordingly 31 if self.instance is not None: 32 department_cost_instances = DepartmentCosts.objects.filter( 33 start=self.instance 34 ).select_related('department') 35 for department_cost_instance in department_cost_instances: 36 self.fields[ 37 'dep_' + str( 38 department_cost_instance.department.accounting_entry 39 ) 40 ].initial = department_cost_instance.equivalents_per_hour 41 42 def save(self, commit=True): 43 # save instance 44 super().save(commit) 45 # save all associated instances 46 if self.is_valid() and self.has_changed(): 47 department_costs_list = [] 48 for department in self._departments: 49 equivalents_per_hour = self.cleaned_data.get( 50 'dep_' + str(department.accounting_entry) 51 ) 52 department_costs_list.append( 53 DepartmentCosts( 54 department=department, 55 start=self.instance, 56 equivalents_per_hour=equivalents_per_hour, 57 ) 58 ) 59 DepartmentCosts.objects.bulk_create( 60 department_costs_list, 61 update_conflicts=True, 62 update_fields=['equivalents_per_hour'], 63 unique_fields=['department_id', 'start_id'] 64 ) 65 return self.instance 66 67class SalaryLevelCostForm(forms.ModelForm): 68 class Meta: 69 model = SalaryLevelDate 70 fields = ['date'] 71 widgets = { 72 'date': CustomDateInput(), 73 } 74 75 def __init__(self, *args, **kwargs): 76 super().__init__(*args, **kwargs) 77 78 # Collect all associated departments to create fields 79 self._salary_levels = SalaryLevel.objects.all() 80 field_class = SalaryLevelCosts.brutto_per_hour.field.formfield 81 for salary_level in self._salary_levels: 82 self.fields[ 83 'sal_' + str(salary_level.salary_code) 84 ] = field_class( 85 label=salary_level.salary_code 86 ) 87 88 # Look for model instance and fill associated fields accordingly 89 if self.instance is not None: 90 salary_level_cost_instances = SalaryLevelCosts.objects.filter( 91 start=self.instance 92 ).select_related('salary_level') 93 for salary_level_cost_instance in salary_level_cost_instances: 94 self.fields[ 95 'sal_' + str( 96 salary_level_cost_instance.salary_level.salary_code 97 ) 98 ].initial = salary_level_cost_instance.brutto_per_hour 99 100 def save(self, commit=True): 101 # save instance 102 super().save(commit) 103 # save all associated instances 104 if self.is_valid() and self.has_changed(): 105 salary_level_costs_list = [] 106 for salary_level in self._salary_levels: 107 brutto_per_hour = self.cleaned_data.get( 108 'sal_' + str(salary_level.salary_code) 109 ) 110 salary_level_costs_list.append( 111 SalaryLevelCosts( 112 salary_level=salary_level, 113 start=self.instance, 114 brutto_per_hour=brutto_per_hour, 115 ) 116 ) 117 SalaryLevelCosts.objects.bulk_create( 118 salary_level_costs_list, 119 update_conflicts=True, 120 update_fields=['brutto_per_hour'], 121 unique_fields=['salary_level_id', 'start_id'] 122 ) 123 return self.instance
10class DepartmentCostForm(forms.ModelForm): 11 class Meta: 12 model = DepartmentDate 13 fields = ['date'] 14 widgets = { 15 'date': CustomDateInput(), 16 } 17 18 def __init__(self, *args, **kwargs): 19 super().__init__(*args, **kwargs) 20 21 # Collect all associated departments to create fields 22 self._departments = Department.objects.all().order_by('name') 23 field_class = DepartmentCosts.equivalents_per_hour.field.formfield 24 for department in self._departments: 25 self.fields[ 26 'dep_' + str(department.accounting_entry) 27 ] = field_class( 28 label=department.name 29 ) 30 31 # Look for model instance and fill associated fields accordingly 32 if self.instance is not None: 33 department_cost_instances = DepartmentCosts.objects.filter( 34 start=self.instance 35 ).select_related('department') 36 for department_cost_instance in department_cost_instances: 37 self.fields[ 38 'dep_' + str( 39 department_cost_instance.department.accounting_entry 40 ) 41 ].initial = department_cost_instance.equivalents_per_hour 42 43 def save(self, commit=True): 44 # save instance 45 super().save(commit) 46 # save all associated instances 47 if self.is_valid() and self.has_changed(): 48 department_costs_list = [] 49 for department in self._departments: 50 equivalents_per_hour = self.cleaned_data.get( 51 'dep_' + str(department.accounting_entry) 52 ) 53 department_costs_list.append( 54 DepartmentCosts( 55 department=department, 56 start=self.instance, 57 equivalents_per_hour=equivalents_per_hour, 58 ) 59 ) 60 DepartmentCosts.objects.bulk_create( 61 department_costs_list, 62 update_conflicts=True, 63 update_fields=['equivalents_per_hour'], 64 unique_fields=['department_id', 'start_id'] 65 ) 66 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.
18 def __init__(self, *args, **kwargs): 19 super().__init__(*args, **kwargs) 20 21 # Collect all associated departments to create fields 22 self._departments = Department.objects.all().order_by('name') 23 field_class = DepartmentCosts.equivalents_per_hour.field.formfield 24 for department in self._departments: 25 self.fields[ 26 'dep_' + str(department.accounting_entry) 27 ] = field_class( 28 label=department.name 29 ) 30 31 # Look for model instance and fill associated fields accordingly 32 if self.instance is not None: 33 department_cost_instances = DepartmentCosts.objects.filter( 34 start=self.instance 35 ).select_related('department') 36 for department_cost_instance in department_cost_instances: 37 self.fields[ 38 'dep_' + str( 39 department_cost_instance.department.accounting_entry 40 ) 41 ].initial = department_cost_instance.equivalents_per_hour
43 def save(self, commit=True): 44 # save instance 45 super().save(commit) 46 # save all associated instances 47 if self.is_valid() and self.has_changed(): 48 department_costs_list = [] 49 for department in self._departments: 50 equivalents_per_hour = self.cleaned_data.get( 51 'dep_' + str(department.accounting_entry) 52 ) 53 department_costs_list.append( 54 DepartmentCosts( 55 department=department, 56 start=self.instance, 57 equivalents_per_hour=equivalents_per_hour, 58 ) 59 ) 60 DepartmentCosts.objects.bulk_create( 61 department_costs_list, 62 update_conflicts=True, 63 update_fields=['equivalents_per_hour'], 64 unique_fields=['department_id', 'start_id'] 65 ) 66 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
68class SalaryLevelCostForm(forms.ModelForm): 69 class Meta: 70 model = SalaryLevelDate 71 fields = ['date'] 72 widgets = { 73 'date': CustomDateInput(), 74 } 75 76 def __init__(self, *args, **kwargs): 77 super().__init__(*args, **kwargs) 78 79 # Collect all associated departments to create fields 80 self._salary_levels = SalaryLevel.objects.all() 81 field_class = SalaryLevelCosts.brutto_per_hour.field.formfield 82 for salary_level in self._salary_levels: 83 self.fields[ 84 'sal_' + str(salary_level.salary_code) 85 ] = field_class( 86 label=salary_level.salary_code 87 ) 88 89 # Look for model instance and fill associated fields accordingly 90 if self.instance is not None: 91 salary_level_cost_instances = SalaryLevelCosts.objects.filter( 92 start=self.instance 93 ).select_related('salary_level') 94 for salary_level_cost_instance in salary_level_cost_instances: 95 self.fields[ 96 'sal_' + str( 97 salary_level_cost_instance.salary_level.salary_code 98 ) 99 ].initial = salary_level_cost_instance.brutto_per_hour 100 101 def save(self, commit=True): 102 # save instance 103 super().save(commit) 104 # save all associated instances 105 if self.is_valid() and self.has_changed(): 106 salary_level_costs_list = [] 107 for salary_level in self._salary_levels: 108 brutto_per_hour = self.cleaned_data.get( 109 'sal_' + str(salary_level.salary_code) 110 ) 111 salary_level_costs_list.append( 112 SalaryLevelCosts( 113 salary_level=salary_level, 114 start=self.instance, 115 brutto_per_hour=brutto_per_hour, 116 ) 117 ) 118 SalaryLevelCosts.objects.bulk_create( 119 salary_level_costs_list, 120 update_conflicts=True, 121 update_fields=['brutto_per_hour'], 122 unique_fields=['salary_level_id', 'start_id'] 123 ) 124 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.
76 def __init__(self, *args, **kwargs): 77 super().__init__(*args, **kwargs) 78 79 # Collect all associated departments to create fields 80 self._salary_levels = SalaryLevel.objects.all() 81 field_class = SalaryLevelCosts.brutto_per_hour.field.formfield 82 for salary_level in self._salary_levels: 83 self.fields[ 84 'sal_' + str(salary_level.salary_code) 85 ] = field_class( 86 label=salary_level.salary_code 87 ) 88 89 # Look for model instance and fill associated fields accordingly 90 if self.instance is not None: 91 salary_level_cost_instances = SalaryLevelCosts.objects.filter( 92 start=self.instance 93 ).select_related('salary_level') 94 for salary_level_cost_instance in salary_level_cost_instances: 95 self.fields[ 96 'sal_' + str( 97 salary_level_cost_instance.salary_level.salary_code 98 ) 99 ].initial = salary_level_cost_instance.brutto_per_hour
101 def save(self, commit=True): 102 # save instance 103 super().save(commit) 104 # save all associated instances 105 if self.is_valid() and self.has_changed(): 106 salary_level_costs_list = [] 107 for salary_level in self._salary_levels: 108 brutto_per_hour = self.cleaned_data.get( 109 'sal_' + str(salary_level.salary_code) 110 ) 111 salary_level_costs_list.append( 112 SalaryLevelCosts( 113 salary_level=salary_level, 114 start=self.instance, 115 brutto_per_hour=brutto_per_hour, 116 ) 117 ) 118 SalaryLevelCosts.objects.bulk_create( 119 salary_level_costs_list, 120 update_conflicts=True, 121 update_fields=['brutto_per_hour'], 122 unique_fields=['salary_level_id', 'start_id'] 123 ) 124 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