def __init__(self, data, person, learning_unit_year=None, proposal=None, proposal_type=None, default_ac_year=None): self.person = person self.learning_unit_year = learning_unit_year self.proposal = proposal if proposal_type: self.proposal_type = proposal_type initial = self._get_initial() ac_year = default_ac_year or learning_unit_year.academic_year if not learning_unit_year or learning_unit_year.subtype == learning_unit_year_subtypes.FULL: learning_unit = learning_unit_year.learning_unit if learning_unit_year else None start_year = default_ac_year.year if default_ac_year else None self.learning_unit_form_container = FullForm( person, ac_year, learning_unit_instance=learning_unit, data=data, start_year=start_year, proposal=True, proposal_type=proposal_type) else: self.learning_unit_form_container = PartimForm( person, learning_unit_year.parent.learning_unit, ac_year, learning_unit_instance=learning_unit_year.learning_unit, data=data, proposal=True) self.form_proposal = ProposalLearningUnitForm(data, person=person, instance=proposal, initial=initial)
def _get_learning_unit_base_form(self, ac_year, learning_unit_instance=None, data=None, start_year=None): form_kwargs = { 'person': self.person, 'learning_unit_instance': learning_unit_instance, 'academic_year': ac_year, 'start_year': start_year, 'data': data.copy() if data else None, 'learning_unit_full_instance': self.learning_unit_full_instance, 'postposal': not data } if self.external: return ExternalLearningUnitBaseForm(**form_kwargs) return FullForm(**form_kwargs) if self.subtype == learning_unit_year_subtypes.FULL else \ PartimForm(**form_kwargs)
def test_save_method_create_new_instance_with_start_anac(self): partim_acronym = FULL_ACRONYM + "B" start_year = AcademicYearFactory(year=self.current_academic_year.year + 2) a_new_learning_unit_partim = LearningUnitYearFactory.build( academic_year=start_year, acronym=FULL_ACRONYM, subtype=learning_unit_year_subtypes.PARTIM, language=self.learning_unit_year_full.language) post_data = get_valid_form_data(a_new_learning_unit_partim) person = PersonFactory() form = PartimForm( person, self.learning_unit_year_full.learning_unit, self.learning_unit_year_full.academic_year, data=post_data, learning_unit_instance=None, start_anac=self.learning_unit_year_full.learning_unit.start_year, end_year=self.learning_unit_year_full.learning_unit.end_year) self.assertTrue(form.is_valid(), form.errors) form.save() # Check all related object is created self.assertEqual( LearningUnitYear.objects.filter( acronym=partim_acronym, academic_year=self.current_academic_year).count(), 1) self.assertEqual( LearningUnit.objects.filter( learningunityear__acronym=partim_acronym).count(), 1) learning_unit = LearningUnit.objects.filter( learningunityear__acronym=partim_acronym).first() self.assertEqual(learning_unit.start_year, self.learning_unit_year_full.learning_unit.start_year) self.assertEqual(learning_unit.end_year, self.learning_unit_year_full.learning_unit.end_year)
class ProposalBaseForm: # Default values proposal_type = ProposalType.MODIFICATION.name # TODO :: set academic_year as mandatory param and use a kwarg learning_unit_instance (like FullForm and PartimForm) def __init__(self, data, person, learning_unit_year=None, proposal=None, proposal_type=None, default_ac_year=None): self.person = person self.learning_unit_year = learning_unit_year self.proposal = proposal if proposal_type: self.proposal_type = proposal_type initial = self._get_initial() ac_year = default_ac_year or learning_unit_year.academic_year if not learning_unit_year or learning_unit_year.subtype == learning_unit_year_subtypes.FULL: learning_unit = learning_unit_year.learning_unit if learning_unit_year else None start_year = default_ac_year.year if default_ac_year else None self.learning_unit_form_container = FullForm( person, ac_year, learning_unit_instance=learning_unit, data=data, start_year=start_year, proposal=True, proposal_type=proposal_type) else: self.learning_unit_form_container = PartimForm( person, learning_unit_year.parent.learning_unit, ac_year, learning_unit_instance=learning_unit_year.learning_unit, data=data, proposal=True) self.form_proposal = ProposalLearningUnitForm(data, person=person, instance=proposal, initial=initial) def is_valid(self): return all([ self.learning_unit_form_container.is_valid() and self.form_proposal.is_valid() ]) @property def errors(self): return self.learning_unit_form_container.errors + [ self.form_proposal.errors ] @property def fields(self): return OrderedDict( chain(self.form_proposal.fields.items(), self.learning_unit_form_container.fields.items())) @transaction.atomic def save(self): # First save to calculate ProposalType proposal = self.form_proposal.save() self.learning_unit_form_container.save() learning_unit_year = self.learning_unit_form_container.instance proposal.type = compute_proposal_type(proposal, learning_unit_year) proposal.save() return proposal def _get_initial(self): initial = { 'learning_unit_year': self.learning_unit_year, 'type': self.proposal_type, 'state': compute_proposal_state(self.person), 'author': self.person } if self.proposal: initial['type'] = self.proposal.type initial['state'] = self.proposal.state return initial def get_context(self): context = self.learning_unit_form_container.get_context() context['learning_unit_year'] = self.learning_unit_year context['person'] = self.person context['form_proposal'] = self.form_proposal return context