def test_detach_when_element_is_in_clipboard(self, mock_permission, mock_delete): ElementCache(self.person.user).save_element_selected( self.group_element_year.child_branch, source_link_id=self.group_element_year.id) self.client.post(self.url, follow=True, HTTP_X_REQUESTED_WITH='XMLHttpRequest') error_msg = "The clipboard should be cleared if detached element is in clipboard" self.assertFalse(ElementCache(self.person.user).cached_data, error_msg)
def test_detach_when_clipboard_filled_with_different_detached_element( self, mock_permission, mock_delete): element_cached = EducationGroupYearFactory() ElementCache(self.person.user).save_element_selected(element_cached, ) self.client.post(self.url, follow=True, HTTP_X_REQUESTED_WITH='XMLHttpRequest') error_msg = "The clipboard should not be cleared if element in clipboard is not the detached element" self.assertEqual( ElementCache(self.person.user).cached_data['id'], element_cached.id, error_msg)
def form_valid(self, form): """ If the form is valid, save the associated model. """ # Clear cache. ElementCache(self.request.user).clear() return super().form_valid(form)
def test_move(self): ElementCache(self.person.user).save_element_selected( self.selected_egy, source_link_id=self.group_element_year.id) self.client.post(self.url, data={"link_type": LinkTypes.REFERENCE.name}) self.assertFalse( GroupElementYear.objects.filter(id=self.group_element_year.id))
def _cache_object_and_redirect(request, object_to_cache, redirect_to): ElementCache(request.user).save_element_selected(object_to_cache) success_message = get_clipboard_content_display(object_to_cache, ElementCache.ElementCacheAction.COPY.value) if request.is_ajax(): return build_success_json_response(success_message) else: messages.add_message(request, messages.INFO, success_message) return redirect(redirect_to)
def test_select_case_education_group(self): response = self.client.post(self.url_management, data=self.select_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') data_cached = ElementCache(self.person.user).cached_data self.assertEquals(response.status_code, HTTPStatus.OK) self.assertDictEqual( data_cached, {'modelname': management.EDUCATION_GROUP_YEAR, 'id':self.child_education_group_year.id} )
def test_context_data(self): ElementCache(self.person.user).save_element_selected( self.selected_egy, source_link_id=self.group_element_year.id) response = self.client.get(self.url) context = response.context self.assertEqual(context["object_to_attach"], self.selected_egy) self.assertEqual(context["source_link"], self.group_element_year) self.assertEqual(context["education_group_year_parent"], self.group_element_year.child_branch)
def learning_unit_select(request, learning_unit_year_id): learning_unit_year = get_object_or_404(LearningUnitYear, pk=learning_unit_year_id) ElementCache(request.user).save_element_selected(learning_unit_year) success_message = build_success_message(learning_unit_year) if request.is_ajax(): return build_success_json_response(success_message) else: messages.add_message(request, messages.INFO, success_message) return redirect(reverse('learning_unit', args=[learning_unit_year_id]))
def extract_child_from_cache(parent, user): selected_data = ElementCache(user).cached_data if not selected_data: raise ObjectDoesNotExist kwargs = {'parent': parent} if selected_data['modelname'] == LEARNING_UNIT_YEAR: kwargs['child_leaf'] = LearningUnitYear.objects.get( pk=selected_data['id']) elif selected_data['modelname'] == EDUCATION_GROUP_YEAR: kwargs['child_branch'] = EducationGroupYear.objects.get( pk=selected_data['id']) if selected_data.get('source_link_id'): kwargs['source_link'] = GroupElementYear.objects.select_related('parent') \ .get(pk=selected_data['source_link_id']) return kwargs
def _get_elements_selected(request_parameters, user): object_ids = request_parameters.getlist("id", []) content_type = request_parameters.get("content_type") if object_ids and content_type: selected_data = [{"id": object_id, "modelname": content_type} for object_id in object_ids] elif object_ids or content_type: selected_data = [] else: cached_data = ElementCache(user).cached_data selected_data = [cached_data] if cached_data else [] return selected_data
def test_select_ajax_case_learning_unit_year(self): response = self.client.post( self.url_select_learning_unit, HTTP_X_REQUESTED_WITH='XMLHttpRequest', ) data_cached = ElementCache(self.person.user).cached_data self.assertEquals(response.status_code, HTTPStatus.OK) self.assertDictEqual( data_cached, {'modelname': management.LEARNING_UNIT_YEAR, 'id': self.learning_unit_year.id} )
def get_redirect_url(self, *args, **kwargs): self.pattern_name = 'group_element_year_create' try: perms.can_change_education_group(self.request.user, self.education_group_year) except PermissionDenied as e: display_warning_messages(self.request, str(e)) cached_data = ElementCache(self.request.user).cached_data if cached_data: action_from_cache = cached_data.get('action') if action_from_cache == ElementCache.ElementCacheAction.CUT.value: kwargs['group_element_year_id'] = fetch_source_link( self.request.GET, self.request.user).id self.pattern_name = 'group_element_year_move' return super().get_redirect_url(*args, **kwargs)
def test_attach_a_not_valid_case(self, mock_attach_strategy): ElementCache(self.person.user).save_element_selected( self.child_education_group_year) response = self.client.get( reverse( "group_element_year_create", args=[self.root.pk, self.new_parent_education_group_year.pk]), ) self.assertEqual(response.status_code, 200) messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), _("Dummy message"))
def education_group_select(request, root_id=None, education_group_year_id=None): education_group_year = get_object_or_404(EducationGroupYear, pk=request.POST['element_id']) ElementCache(request.user).save_element_selected(education_group_year) success_message = build_success_message(education_group_year) if request.is_ajax(): return build_success_json_response(success_message) else: messages.add_message(request, messages.INFO, success_message) return redirect( reverse('education_group_read', args=[ root_id, education_group_year_id, ]))
def test_attach_without_selecting_gives_warning(self): ElementCache(self.person.user).clear() expected_absent_group_element_year = GroupElementYear.objects.filter( parent=self.new_parent_education_group_year, child_branch=self.child_education_group_year).exists() self.assertFalse(expected_absent_group_element_year) response = self.client.get( reverse( "group_element_year_create", args=[self.root.pk, self.new_parent_education_group_year.pk]), ) self.assertEqual(response.status_code, 200) messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), _("Please select an item before attach it"))
def test_attach_case_child_learning_unit_year(self): expected_absent_group_element_year = GroupElementYear.objects.filter( parent=self.new_parent_education_group_year, child_leaf=self.learning_unit_year).exists() self.assertFalse(expected_absent_group_element_year) data_cached = ElementCache(self.person.user).save_element_selected( self.learning_unit_year) response = self.client.post(reverse( "group_element_year_create", args=[self.root.pk, self.new_parent_education_group_year.pk]), data={}) self.assertEqual(response.status_code, 302) expected_group_element_year_count = GroupElementYear.objects.filter( parent=self.new_parent_education_group_year, child_leaf=self.learning_unit_year).count() self.assertEqual(expected_group_element_year_count, 1)
def test_move(self): AuthorizedRelationshipFactory( parent_type=self.selected_egy.education_group_type, child_type=self.group_element_year.child_branch.education_group_type, min_count_authorized=0, max_count_authorized=None ) ElementCache(self.person.user).save_element_selected( self.group_element_year.child_branch, source_link_id=self.group_element_year.id ) self.client.post(self.url, data={ 'form-TOTAL_FORMS': '1', 'form-INITIAL_FORMS': '0', 'form-MAX_NUM_FORMS': '1', "link_type": LinkTypes.REFERENCE.name }) self.assertFalse(GroupElementYear.objects.filter(id=self.group_element_year.id)) self.mocked_perm.assert_any_call(self.person, self.selected_egy, raise_exception=True) self.mocked_perm.assert_any_call(self.person, self.group_element_year.parent, raise_exception=True)
def _remove_element_from_clipboard_if_stored(self, obj_detached): element_cache = ElementCache(self.request.user) obj_detached = obj_detached.child_branch or obj_detached.child_leaf if element_cache.equals(obj_detached): element_cache.clear()
def _select(request, group_element_year, *args, **kwargs): element = kwargs['element'] ElementCache(request.user).save_element_selected(element) success_msg = build_success_message(element) return build_success_json_response(success_msg)
def form_valid(self, form): ElementCache(self.request.user).clear() return super().form_valid(form)
def get_selected_element_for_clipboard(self): cached_data = ElementCache(self.request.user).cached_data if cached_data: obj = self._get_instance_object_from_cache(cached_data) return get_clipboard_content_display(obj, cached_data['action']) return None
def _select(request, group_element_year, *args, **kwargs): element = kwargs['element'] group_element_year_pk = group_element_year.pk if group_element_year else None ElementCache(request.user).save_element_selected(element, source_link_id=group_element_year_pk) success_msg = build_success_message(element) return build_success_json_response(success_msg)
def _cache_object(user, group_element_year, object_to_cache, action): group_element_year_pk = group_element_year.pk if group_element_year else None ElementCache(user).save_element_selected(object_to_cache, source_link_id=group_element_year_pk, action=action) success_msg = get_clipboard_content_display(object_to_cache, action) return build_success_json_response(success_msg)
def clear_clipboard(request): if request.is_ajax(): ElementCache(request.user).clear() return JsonResponse({}) return HttpResponseBadRequest()