def _get_or_create_branch(child_education_group_type, title_initial_value, partial_acronym_initial_value, parent_egy): existing_grp_ele = utils.get_object_or_none( GroupElementYear, parent=parent_egy, child_branch__education_group_type=child_education_group_type) if existing_grp_ele: return existing_grp_ele academic_year = parent_egy.academic_year previous_grp_ele = utils.get_object_or_none( GroupElementYear, parent__education_group=parent_egy.education_group, parent__academic_year__year__in=[ academic_year.year - 1, academic_year.year ], child_branch__education_group_type=child_education_group_type) edy_acronym = "{child_title}{parent_acronym}".format( child_title=title_initial_value.replace(" ", "").upper(), parent_acronym=parent_egy.acronym) if not previous_grp_ele: ed = EducationGroup.objects.filter( educationgroupyear__acronym=edy_acronym, ) if ed.exists(): child_eg = ed.first() else: child_eg = EducationGroup.objects.create(start_year=academic_year, end_year=academic_year) else: child_eg = previous_grp_ele.child_branch.education_group child_egy, _ = EducationGroupYear.objects.update_or_create( academic_year=parent_egy.academic_year, education_group=child_eg, defaults={ 'main_teaching_campus': parent_egy.main_teaching_campus, 'management_entity': parent_egy.management_entity, 'education_group_type': child_education_group_type, 'title': "{child_title} {parent_acronym}".format( child_title=title_initial_value, parent_acronym=parent_egy.acronym), 'partial_acronym': _generate_child_partial_acronym(parent_egy, partial_acronym_initial_value, child_education_group_type), 'acronym': edy_acronym, }) gey, _ = GroupElementYear.objects.get_or_create(parent=parent_egy, child_branch=child_egy) return gey
def _get_neighbouring_achievements(achievement): kwargs = { 'learning_unit_year': achievement.learning_unit_year, 'language': achievement.language } previous_achievement = get_object_or_none(LearningAchievement, order=achievement.order - 1, **kwargs) next_achievement = get_object_or_none(LearningAchievement, order=achievement.order + 1, **kwargs) return previous_achievement, achievement, next_achievement
def find_learning_unit_achievement(consistency_id, learning_unit_yr, a_language_code, position): return get_object_or_none(LearningAchievement, consistency_id=consistency_id, learning_unit_year=learning_unit_yr, language__code=a_language_code, order=position)
def _generate_child_partial_acronym(parent, child_initial_value, child_type): previous_grp_ele = utils.get_object_or_none( GroupElementYear, parent__education_group=parent.education_group, parent__academic_year__year__in=[ parent.academic_year.year - 1, parent.academic_year.year ], child_branch__education_group_type=child_type) if previous_grp_ele: return previous_grp_ele.child_branch.partial_acronym reg_parent_partial_acronym = re.compile(REGEX_TRAINING_PARTIAL_ACRONYM) # FIXME : Sometimes parent does not have a partial acronym, it is a dirty situation. We have to clean the DB. if not parent.partial_acronym: return "" match_result = reg_parent_partial_acronym.search(parent.partial_acronym) sigle_ele = match_result.group("sigle_ele") reg_child_initial_value = re.compile( REGEX_GROUP_PARTIAL_ACRONYM_INITIAL_VALUE) cnum, subdivision = _get_cnum_subdivision(child_initial_value, reg_child_initial_value) partial_acronym = "{}{}{}".format(sigle_ele, cnum, subdivision) while EducationGroupYear.objects.filter( partial_acronym=partial_acronym).exists(): cnum = "{:0{width}d}".format((int(cnum) + 1) % MAX_CNUM, width=WIDTH_CNUM) partial_acronym = "{}{}{}".format(sigle_ele, cnum, subdivision) return partial_acronym
def _get_or_create_branch(child_education_group_type, title_initial_value, partial_acronym_initial_value, parent_egy): existing_grp_ele = utils.get_object_or_none( GroupElementYear, parent=parent_egy, child_branch__education_group_type=child_education_group_type) if existing_grp_ele: return existing_grp_ele year = parent_egy.academic_year.year child_eg = EducationGroup.objects.create(start_year=year, end_year=year) child_egy = EducationGroupYear.objects.create( academic_year=parent_egy.academic_year, main_teaching_campus=parent_egy.main_teaching_campus, management_entity=parent_egy.management_entity, education_group_type=child_education_group_type, title="{child_title} {parent_acronym}".format( child_title=title_initial_value, parent_acronym=parent_egy.acronym), partial_acronym=_generate_child_partial_acronym( parent_egy, partial_acronym_initial_value, child_education_group_type), acronym="{child_title}{parent_acronym}".format( child_title=title_initial_value.replace(" ", "").upper(), parent_acronym=parent_egy.acronym), education_group=child_eg) grp_ele = GroupElementYear.objects.create(parent=parent_egy, child_branch=child_egy) return grp_ele
def _get_validation_rule(field_name, education_group_type): egy_title_reference = ValidationRuleMixin._field_reference( EducationGroupYear, field_name, education_group_type.external_id ) return get_object_or_none(ValidationRule, pk=egy_title_reference)
def _generate_child_partial_acronym(parent, child_initial_value, child_type): previous_grp_ele = utils.get_object_or_none( GroupElementYear, parent__education_group=parent.education_group, parent__academic_year__year__in=[parent.academic_year.year - 1, parent.academic_year.year], child_branch__education_group_type=child_type ) if previous_grp_ele: return previous_grp_ele.child_branch.partial_acronym reg_parent_partial_acronym = re.compile(REGEX_TRAINING_PARTIAL_ACRONYM) # FIXME : Sometimes parent does not have a partial acronym, it is a dirty situation. We have to clean the DB. if not parent.partial_acronym: return "" match_result = reg_parent_partial_acronym.search(parent.partial_acronym) sigle_ele = match_result.group("sigle_ele") reg_child_initial_value = re.compile(REGEX_GROUP_PARTIAL_ACRONYM_INITIAL_VALUE) cnum, subdivision = _get_cnum_subdivision(child_initial_value, reg_child_initial_value) partial_acronym = "{}{}{}".format(sigle_ele, cnum, subdivision) while EducationGroupYear.objects.filter(partial_acronym=partial_acronym).exists(): cnum = "{:0{width}d}".format( (int(cnum) + 1) % MAX_CNUM, width=WIDTH_CNUM ) partial_acronym = "{}{}{}".format(sigle_ele, cnum, subdivision) return partial_acronym
def management(request): root_id = _get_data_from_request(request, 'root_id') group_element_year_id = _get_data_from_request(request, 'group_element_year_id') or 0 group_element_year = get_object_or_none(GroupElementYear, pk=group_element_year_id) element_id = _get_data_from_request(request, 'element_id') element = _get_concerned_object(element_id, group_element_year) _check_perm_for_management(request, element, group_element_year) action_method = _get_action_method(request) source = _get_data_from_request(request, 'source') http_referer = request.META.get('HTTP_REFERER') response = action_method( request, group_element_year, root_id=root_id, element=element, source=source, http_referer=http_referer, ) if response: return response return redirect(http_referer)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['current_academic_year'] = self.current_academic_year context['is_person_linked_to_entity'] = self.person.is_linked_to_entity_in_charge_of_learning_unit_year( self.object ) context['warnings'] = self.object.warnings context['learning_container_year_partims'] = self.object.get_partims_related() context.update(get_all_attributions(self.object)) components = get_components_identification(self.object) context['components'] = components.get('components') context['REQUIREMENT_ENTITY'] = components.get('REQUIREMENT_ENTITY') context['ADDITIONAL_REQUIREMENT_ENTITY_1'] = components.get('ADDITIONAL_REQUIREMENT_ENTITY_1') context['ADDITIONAL_REQUIREMENT_ENTITY_2'] = components.get('ADDITIONAL_REQUIREMENT_ENTITY_2') proposal = get_object_or_none(ProposalLearningUnit, learning_unit_year__learning_unit=self.object.learning_unit) context['proposal'] = proposal context['proposal_folder_entity_version'] = get_by_entity_and_date( proposal.entity, None) if proposal else None context['differences'] = get_difference_of_proposal(proposal, self.object) \ if proposal and proposal.learning_unit_year == self.object else {} context.update(self.get_context_permission(proposal)) context["versions"] = self.get_versions() context["has_partim"] = self.object.get_partims_related().exists() return context
def _get_calendar(academic_yr, an_entity_version): """ Try to fetch the academic calendar for the entity. If it is not found, return the academic calendar. """ a_calendar = get_entity_calendar(an_entity_version, academic_yr) # TODO slow method... if a_calendar is None: a_calendar = get_object_or_none( AcademicCalendar, reference=SUMMARY_COURSE_SUBMISSION, academic_year=academic_yr ) return a_calendar
def _get_common_context(request, offer_year_id): offer_year = get_object_or_none(OfferYear, pk=offer_year_id) return { 'offer_year': offer_year, 'countries': Country.objects.all(), 'is_program_manager': mdl.program_manager.is_program_manager(request.user, offer_year=offer_year), 'entity_versions': score_encoding_sheet.get_entity_version_choices(offer_year), }
def _get_or_create_branch(child_education_group_type, title_initial_value, partial_acronym_initial_value, parent_egy): existing_grp_ele = utils.get_object_or_none( GroupElementYear, parent=parent_egy, child_branch__education_group_type=child_education_group_type ) if existing_grp_ele: return existing_grp_ele year = parent_egy.academic_year.year previous_grp_ele = utils.get_object_or_none( GroupElementYear, parent__education_group=parent_egy.education_group, parent__academic_year__year__in=[year - 1, year], child_branch__education_group_type=child_education_group_type ) if not previous_grp_ele: child_eg = EducationGroup.objects.create(start_year=year, end_year=year) else: child_eg = previous_grp_ele.child_branch.education_group child_egy, _ = EducationGroupYear.objects.update_or_create( academic_year=parent_egy.academic_year, education_group=child_eg, defaults={ 'main_teaching_campus': parent_egy.main_teaching_campus, 'management_entity': parent_egy.management_entity, 'education_group_type': child_education_group_type, 'title': "{child_title} {parent_acronym}".format( child_title=title_initial_value, parent_acronym=parent_egy.acronym ), 'partial_acronym': _generate_child_partial_acronym( parent_egy, partial_acronym_initial_value, child_education_group_type ), 'acronym': "{child_title}{parent_acronym}".format( child_title=title_initial_value.replace(" ", "").upper(), parent_acronym=parent_egy.acronym ), } ) return GroupElementYear.objects.create(parent=parent_egy, child_branch=child_egy)
def get_context_data(self, **kwargs): context = super().get_context_data() context["prerequisite"] = get_object_or_none(Prerequisite, learning_unit_year=context["learning_unit_year"], education_group_year=context["root"]) context["can_modify_prerequisite"] = perms.is_eligible_to_change_education_group(context['person'], context["root"]) return context
def get_contacts_intro_text(education_group_year, language_code): introduction = get_object_or_none(TranslatedText, text_label__entity=OFFER_YEAR, text_label__label=CONTACT_INTRO_KEY, language=language_code, entity=OFFER_YEAR, reference=education_group_year.id) if introduction: return introduction.text return None
def load_initial(self): self.value = None for code, label in settings.LANGUAGES: value = get_object_or_none( LearningAchievement, learning_unit_year__id=self.luy.id, consistency_id=self.consistency_id, language=Language.objects.get(code=code[:2].upper())) if value: self.value = value self.fields['text_{}'.format( code[:2])].initial = self.value.text self.fields['code_name'].initial = self.value.code_name
def get_context_data(self, **kwargs): context = super().get_context_data() luy = self.object root = context["root"] context["prerequisite"] = get_object_or_none(Prerequisite, learning_unit_year=luy, education_group_year=root) context[ "can_modify_prerequisite"] = perms.is_eligible_to_change_education_group( context['person'], context["root"]) context['is_prerequisites_list'] = Prerequisite.objects.filter( prerequisiteitem__learning_unit=luy.learning_unit, education_group_year=root) return context
def create_education_group(request, category, education_group_type_pk, root_id=None, parent_id=None): parent = get_object_or_none(EducationGroupYear, pk=parent_id) education_group_type = get_object_or_404(EducationGroupType, pk=education_group_type_pk) request_cache = RequestCache(request.user, reverse('education_groups')) cached_data = request_cache.cached_data or {} academic_year = cached_data.get('academic_year') if not academic_year: cached_data['academic_year'] = starting_academic_year() initial_academic_year = parent.academic_year_id if parent else cached_data.get( 'academic_year') form_education_group_year = FORMS_BY_CATEGORY[category]( request.POST or None, parent=parent, user=request.user, education_group_type=education_group_type, initial={ 'academic_year': initial_academic_year }) if form_education_group_year.is_valid(): return _common_success_redirect(request, form_education_group_year, root_id) data = { "form_education_group_year": form_education_group_year.forms[forms.ModelForm], "form_education_group": form_education_group_year.forms[EducationGroupModelForm], "parent": parent, 'root_pk': root_id, } if category == education_group_categories.TRAINING: data.update({ "form_hops": form_education_group_year.hops_form, }) return render(request, TEMPLATES_BY_CATEGORY.get(category), data)
def parent_by_training(self): """ Return the parent, only if the education group and its parent are a training. In our structure, it is forbidden to have 2 training parents for a training. """ if self.is_training(): try: return get_object_or_none( EducationGroupYear, groupelementyear__child_branch=self, education_group_type__category=education_group_categories.TRAINING ) except EducationGroupYear.MultipleObjectsReturned: raise MaximumOneParentAllowedException('Only one training parent is allowed')
def academic_calendar_form(request, academic_calendar_id): academic_calendar = get_object_or_none(AcademicCalendar, pk=academic_calendar_id) if request.method == 'GET': academic_cal_form = AcademicCalendarForm(instance=academic_calendar) else: academic_cal_form = AcademicCalendarForm(data=request.POST, instance=academic_calendar) if academic_cal_form.is_valid(): academic_cal_form.save() return academic_calendar_read(request, academic_cal_form.instance.id) return render(request, "academic_calendar/academic_calendar_form.html", { 'form': academic_cal_form, })
def _postpone_operation(neighbouring_achievements, next_luy, operation_str, curr_order): _, achievement, _ = neighbouring_achievements while next_luy.get_learning_unit_next_year(): current_luy = next_luy next_luy = next_luy.get_learning_unit_next_year() next_luy_achievement = get_object_or_none( LearningAchievement, learning_unit_year=next_luy, consistency_id=achievement.consistency_id, language=achievement.language) if next_luy_achievement and \ _operation_is_postponable(next_luy_achievement, neighbouring_achievements, operation_str, curr_order): getattr(next_luy_achievement, operation_str)() else: return current_luy.academic_year return next_luy.academic_year
def academic_calendar_form(request, academic_calendar_id): if not request.user.is_superuser: raise PermissionDenied academic_calendar = get_object_or_none(AcademicCalendar, pk=academic_calendar_id) if request.method == 'GET': academic_cal_form = AcademicCalendarForm(instance=academic_calendar) else: academic_cal_form = AcademicCalendarForm(data=request.POST, instance=academic_calendar) if academic_cal_form.is_valid(): academic_cal_form.save() return academic_calendar_read(request, academic_cal_form.instance.id) return render(request, "academic_calendar/academic_calendar_form.html", {'form': academic_cal_form})
def _duplicate_branch(child_education_group_type, parent_egy, last_child): existing_grp_ele = utils.get_object_or_none( GroupElementYear, parent=parent_egy, child_branch__education_group_type=child_education_group_type) if existing_grp_ele: return existing_grp_ele year = parent_egy.academic_year.year child_eg = EducationGroup.objects.create(start_year=year, end_year=year) child_egy = model.duplicate_object(last_child) child_egy.education_group = child_eg child_egy.academic_year = parent_egy.academic_year child_egy.save() grp_ele = GroupElementYear.objects.create(parent=parent_egy, child_branch=child_egy) return grp_ele
def _duplicate_branch(child_education_group_type, parent_egy, last_child): existing_grp_ele = utils.get_object_or_none( GroupElementYear, parent=parent_egy, child_branch__education_group_type=child_education_group_type ) if existing_grp_ele: return existing_grp_ele year = parent_egy.academic_year.year # child_eg = EducationGroup.objects.create(start_year=year, end_year=year) last_child.education_group.end_year = year last_child.education_group.save() child_egy = model.duplicate_object(last_child) child_egy.education_group = last_child.education_group child_egy.academic_year = parent_egy.academic_year child_egy.save() return GroupElementYear.objects.create(parent=parent_egy, child_branch=child_egy)
def get_context_data(self, **kwargs): context = super().get_context_data() luy = self.object root = context["root"] context["prerequisite"] = get_object_or_none(Prerequisite, learning_unit_year=luy, education_group_year=root) context[ "can_modify_prerequisite"] = perms.is_eligible_to_change_education_group( context['person'], context["root"]) context["learning_unit_years_parent"] = {} for grp in self.hierarchy.included_group_element_years: if not grp.child_leaf: continue context["learning_unit_years_parent"].setdefault( grp.child_leaf.id, grp) context['is_prerequisites_list'] = Prerequisite.objects.filter( prerequisiteitem__learning_unit=luy.learning_unit, education_group_year=root).select_related('learning_unit_year') return context
def get_by_internal_id(internal_id): return get_object_or_none(Entity, id__exact=internal_id)
def find_by_id(an_id): return get_object_or_none(Entity, pk=an_id)
def get_by_external_id(external_id): return get_object_or_none(Entity, external_id__exact=external_id)
def get_by_reference_and_data_year(a_reference, data_year): return get_object_or_none(AcademicCalendar, reference=a_reference, data_year=data_year)
def get_by_reference_and_academic_year(a_reference, an_academic_year): return get_object_or_none(AcademicCalendar, reference=a_reference, academic_year=an_academic_year)
def find_by_id(person_id): return get_object_or_none(Person, id=person_id)
def find_by_learning_unit(a_learning_unit): return get_object_or_none(ProposalLearningUnit, learning_unit_year__learning_unit=a_learning_unit)
def find_by_id(structure_id): return get_object_or_none(Structure, pk=structure_id)
def find_by_learning_unit(a_learning_unit): return get_object_or_none( ProposalLearningUnit, learning_unit_year__learning_unit=a_learning_unit)