vkk.workhours.accounting.projects.project.export.funded_staff.forms
A collection of forms used in this module.
1""" 2A collection of forms used in this module. 3""" 4 5from django import forms 6from vkk.workhours.models import ( 7 ProjectFundedStaff, ProjectFundedStaffDate, SalaryLevel, 8 Project 9) 10from vkk.workhours.forms import CustomDateInput 11 12class ProjectFundedStaffForm(forms.ModelForm): 13 """ 14 A class based `ModelForm` providing utilities for administrating `ProjectFundedStaff`. 15 """ 16 class Meta: 17 model = ProjectFundedStaffDate 18 fields = ['date'] 19 widgets = { 20 'date': CustomDateInput(), 21 } 22 23 def __init__(self, *args, invoice_number=None, **kwargs): 24 """ 25 Initializes and returns an object of this class. An invoice_number of a project must be provided. 26 """ 27 super().__init__(*args, **kwargs) 28 self._invoice_number = invoice_number 29 30 # Collect all salary levels to create Fields 31 self._salary_levels = SalaryLevel.objects.all() 32 field_class = ProjectFundedStaff.hours.field.formfield 33 for salary_level in self._salary_levels: 34 self.fields[ 35 'fun_' + str(salary_level.salary_code) 36 ] = field_class( 37 label=salary_level.salary_code 38 ) 39 # Look for model instance and fill associated fields accordingly 40 if self.instance is not None: 41 project_funded_staff_instances = ProjectFundedStaff.objects.filter( 42 start=self.instance 43 ).select_related('salary_level') 44 for project_funded_staff_instance in project_funded_staff_instances: 45 self.fields[ 46 'fun_' + str(project_funded_staff_instance.salary_level.salary_code) 47 ].initial = project_funded_staff_instance.hours 48 49 def save(self, commit=True): 50 """ 51 Tries to save and return the `ProjectFundedStaffDate` instance of this class and 52 all associated instances of `ProjectFundedStaff` to the database. 53 """ 54 if self.instance.pk is None: 55 self.instance.project = Project.objects.get(invoice_number=self._invoice_number) 56 # save instance 57 super().save(commit) 58 # save all associated instances 59 if self.is_valid() and self.has_changed(): 60 project_funded_staff_list = [] 61 for salary_level in self._salary_levels: 62 hours = self.cleaned_data.get('fun_' + str(salary_level.salary_code)) 63 project_funded_staff_list.append( 64 ProjectFundedStaff( 65 salary_level=salary_level, 66 start=self.instance, 67 hours=hours 68 ) 69 ) 70 ProjectFundedStaff.objects.bulk_create( 71 project_funded_staff_list, 72 update_conflicts=True, 73 update_fields=['hours'], 74 unique_fields=['salary_level_id', 'start_id'] 75 ) 76 return self.instance
class
ProjectFundedStaffForm(django.forms.models.ModelForm):
13class ProjectFundedStaffForm(forms.ModelForm): 14 """ 15 A class based `ModelForm` providing utilities for administrating `ProjectFundedStaff`. 16 """ 17 class Meta: 18 model = ProjectFundedStaffDate 19 fields = ['date'] 20 widgets = { 21 'date': CustomDateInput(), 22 } 23 24 def __init__(self, *args, invoice_number=None, **kwargs): 25 """ 26 Initializes and returns an object of this class. An invoice_number of a project must be provided. 27 """ 28 super().__init__(*args, **kwargs) 29 self._invoice_number = invoice_number 30 31 # Collect all salary levels to create Fields 32 self._salary_levels = SalaryLevel.objects.all() 33 field_class = ProjectFundedStaff.hours.field.formfield 34 for salary_level in self._salary_levels: 35 self.fields[ 36 'fun_' + str(salary_level.salary_code) 37 ] = field_class( 38 label=salary_level.salary_code 39 ) 40 # Look for model instance and fill associated fields accordingly 41 if self.instance is not None: 42 project_funded_staff_instances = ProjectFundedStaff.objects.filter( 43 start=self.instance 44 ).select_related('salary_level') 45 for project_funded_staff_instance in project_funded_staff_instances: 46 self.fields[ 47 'fun_' + str(project_funded_staff_instance.salary_level.salary_code) 48 ].initial = project_funded_staff_instance.hours 49 50 def save(self, commit=True): 51 """ 52 Tries to save and return the `ProjectFundedStaffDate` instance of this class and 53 all associated instances of `ProjectFundedStaff` to the database. 54 """ 55 if self.instance.pk is None: 56 self.instance.project = Project.objects.get(invoice_number=self._invoice_number) 57 # save instance 58 super().save(commit) 59 # save all associated instances 60 if self.is_valid() and self.has_changed(): 61 project_funded_staff_list = [] 62 for salary_level in self._salary_levels: 63 hours = self.cleaned_data.get('fun_' + str(salary_level.salary_code)) 64 project_funded_staff_list.append( 65 ProjectFundedStaff( 66 salary_level=salary_level, 67 start=self.instance, 68 hours=hours 69 ) 70 ) 71 ProjectFundedStaff.objects.bulk_create( 72 project_funded_staff_list, 73 update_conflicts=True, 74 update_fields=['hours'], 75 unique_fields=['salary_level_id', 'start_id'] 76 ) 77 return self.instance
A class based ModelForm
providing utilities for administrating ProjectFundedStaff
.
ProjectFundedStaffForm(*args, invoice_number=None, **kwargs)
24 def __init__(self, *args, invoice_number=None, **kwargs): 25 """ 26 Initializes and returns an object of this class. An invoice_number of a project must be provided. 27 """ 28 super().__init__(*args, **kwargs) 29 self._invoice_number = invoice_number 30 31 # Collect all salary levels to create Fields 32 self._salary_levels = SalaryLevel.objects.all() 33 field_class = ProjectFundedStaff.hours.field.formfield 34 for salary_level in self._salary_levels: 35 self.fields[ 36 'fun_' + str(salary_level.salary_code) 37 ] = field_class( 38 label=salary_level.salary_code 39 ) 40 # Look for model instance and fill associated fields accordingly 41 if self.instance is not None: 42 project_funded_staff_instances = ProjectFundedStaff.objects.filter( 43 start=self.instance 44 ).select_related('salary_level') 45 for project_funded_staff_instance in project_funded_staff_instances: 46 self.fields[ 47 'fun_' + str(project_funded_staff_instance.salary_level.salary_code) 48 ].initial = project_funded_staff_instance.hours
Initializes and returns an object of this class. An invoice_number of a project must be provided.
def
save(self, commit=True):
50 def save(self, commit=True): 51 """ 52 Tries to save and return the `ProjectFundedStaffDate` instance of this class and 53 all associated instances of `ProjectFundedStaff` to the database. 54 """ 55 if self.instance.pk is None: 56 self.instance.project = Project.objects.get(invoice_number=self._invoice_number) 57 # save instance 58 super().save(commit) 59 # save all associated instances 60 if self.is_valid() and self.has_changed(): 61 project_funded_staff_list = [] 62 for salary_level in self._salary_levels: 63 hours = self.cleaned_data.get('fun_' + str(salary_level.salary_code)) 64 project_funded_staff_list.append( 65 ProjectFundedStaff( 66 salary_level=salary_level, 67 start=self.instance, 68 hours=hours 69 ) 70 ) 71 ProjectFundedStaff.objects.bulk_create( 72 project_funded_staff_list, 73 update_conflicts=True, 74 update_fields=['hours'], 75 unique_fields=['salary_level_id', 'start_id'] 76 ) 77 return self.instance
Tries to save and return the ProjectFundedStaffDate
instance of this class and
all associated instances of ProjectFundedStaff
to the database.
Inherited Members
- django.forms.models.BaseModelForm
- clean
- validate_unique
- django.forms.forms.BaseForm
- default_renderer
- field_order
- prefix
- use_required_attribute
- template_name_div
- template_name_p
- template_name_table
- template_name_ul
- template_name_label
- is_bound
- data
- files
- auto_id
- initial
- error_class
- label_suffix
- empty_permitted
- fields
- renderer
- order_fields
- errors
- is_valid
- add_prefix
- add_initial_prefix
- template_name
- 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:
17 class Meta: 18 model = ProjectFundedStaffDate 19 fields = ['date'] 20 widgets = { 21 'date': CustomDateInput(), 22 }
model =
<class 'vkk.workhours.models.ProjectFundedStaffDate'>
widgets =
{'date': <vkk.workhours.forms.CustomDateInput object>}