Пример #1
0
    def generate_data_collection(self):
        """Generate a Data collection.

        Returns:

        """

        # NOTE: no xml_content to avoid using unsupported GridFS mock
        self.data = Data(template=self.template,
                         user_id="1",
                         dict_content=None,
                         title="title").save()

        self.data_structure_1 = CurateDataStructure(user="******",
                                                    template=self.template,
                                                    name="data_structure_1",
                                                    data=self.data).save()

        self.data_structure_2 = CurateDataStructure(
            user="******", template=self.template, name="data_structure_2").save()

        self.data_structure_3 = CurateDataStructure(
            user="******", template=self.template, name="data_structure_3").save()

        self.data_collection = [
            self.data_structure_1,
            self.data_structure_2,
            self.data_structure_3,
            self.data,
        ]
Пример #2
0
def _start_curate_post(request):
    """Start curate POST request.

    Args:
        request:

    Returns:

    """
    template_id = str(request.POST["hidden_value"])
    selected_option = request.POST["curate_form"]
    user_id = str(request.user.id)
    if selected_option == "new" or selected_option == "upload":
        if selected_option == "new":
            new_form = users_forms.NewForm(request.POST)
            if new_form.is_valid():
                name = new_form.data["document_name"]
                curate_data_structure = CurateDataStructure(
                    user=user_id, template=template_id, name=name)
            else:
                raise CurateAjaxError(
                    "An error occurred during the validation " +
                    get_form_label())
        else:
            upload_form = users_forms.UploadForm(request.POST, request.FILES)
            if upload_form.is_valid():
                xml_file = request.FILES["file"]
                xml_data = read_xsd_file(xml_file)
                well_formed = is_well_formed_xml(xml_data)
                name = xml_file.name
                if not well_formed:
                    raise CurateAjaxError(
                        "An error occurred during the file upload: the file is "
                        "not well formed XML")
                else:
                    curate_data_structure = CurateDataStructure(
                        user=user_id,
                        template=template_id,
                        name=name,
                        form_string=xml_data,
                    )
            else:
                raise CurateAjaxError(
                    "An error occurred during the validation " +
                    get_form_label())

        curate_data_structure_api.upsert(curate_data_structure, request.user)
    else:
        open_form = users_forms.OpenForm(request.POST)
        curate_data_structure = curate_data_structure_api.get_by_id(
            open_form.data["forms"], request.user)

    url = reverse("core_curate_enter_data", args=(curate_data_structure.id, ))
    return HttpResponse(url)
Пример #3
0
 def test_curate_data_structure_get_all_by_user_return_collection_of_curate_data_structure(
         self, mock_list):
     # Arrange
     mock_data_1 = CurateDataStructure(user="******",
                                       template=_get_template(),
                                       name="name_title_1")
     mock_data_2 = CurateDataStructure(user="******",
                                       template=_get_template(),
                                       name="name_title_2")
     mock_list.return_value = [mock_data_1, mock_data_2]
     # Act
     result = curate_data_structure_api.get_all_by_user(create_mock_user(1))
     # Assert
     self.assertTrue(
         all(isinstance(item, CurateDataStructure) for item in result))
Пример #4
0
def get_all(user):
    """Returns all curate data structure api

    Returns:

    """
    return CurateDataStructure.get_all()
Пример #5
0
def get_none():
    """Returns None object, used by forms

    Returns:

    """
    return CurateDataStructure.get_none()
Пример #6
0
    def get(self, request):
        """Get all user Curate Data Structure

        Args:

            request: HTTP request

        Returns:

            - code: 200
              content: List of curate data structure
            - code: 403
              content: Forbidden
            - code: 500
              content: Internal server error
        """
        if not request.user.is_superuser:
            return Response(status=status.HTTP_403_FORBIDDEN)

        try:
            # Get object
            object_list = CurateDataStructure.get_all()

            # Serialize object
            serializer = self.serializer(object_list, many=True)

            # Return response
            return Response(serializer.data, status=status.HTTP_200_OK)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Пример #7
0
def get_all_with_no_data(user):
    """Returns all curate data structure api with no link to a data.

    Returns:

    """
    return CurateDataStructure.get_all_with_no_data()
Пример #8
0
def get_all_except_user_id_with_no_data(user_id):
    """ Returns all the curate date structure except the one of the user, with no data.

    Args: user_id:
    Returns:
    """
    return CurateDataStructure.get_all_except_user_id_with_no_data(user_id)
Пример #9
0
    def test_curate_data_structure_get_all_except_user_id_with_no_data_return_curate_data_structure(
            self, mock_list):
        # Arrange
        mock_data_1 = CurateDataStructure(user="******",
                                          template=_get_template(),
                                          name="name_title_1")
        mock_data_2 = CurateDataStructure(user="******",
                                          template=_get_template(),
                                          name="name_title_2")
        mock_list.return_value = [mock_data_1, mock_data_2]
        mock_user = create_mock_user("1", is_staff=True, is_superuser=True)

        # Act
        result = curate_data_structure_api.get_all_except_user_id_with_no_data(
            2, mock_user)
        # Assert
        self.assertTrue(
            all(isinstance(item, CurateDataStructure) for item in result))
Пример #10
0
def get_by_id(curate_data_structure_id):
    """ Returns the curate data structure with the given id

    Args:
        curate_data_structure_id:

    Returns:

    """
    return CurateDataStructure.get_by_id(curate_data_structure_id)
Пример #11
0
 def test_curate_data_structure_get_all_by_user_id_and_template_id_with_no_data_return_curate_data_structure(
         self, mock_list):
     # Arrange
     template = _get_template()
     mock_data_1 = CurateDataStructure(user="******",
                                       template=template,
                                       name="name_title_1")
     mock_data_2 = CurateDataStructure(user="******",
                                       template=template,
                                       name="name_title_2")
     mock_list.return_value = [mock_data_1, mock_data_2]
     mock_user = create_mock_user("1")
     # Act
     result = (curate_data_structure_api.
               get_all_by_user_id_and_template_id_with_no_data(
                   "1", template.id))
     # Assert
     self.assertTrue(
         all(isinstance(item, CurateDataStructure) for item in result))
Пример #12
0
def get_all_by_user(user):
    """Get all curate data that belong to user.

    Args:
        user: User

    Returns:

    """
    return CurateDataStructure.get_all_by_user(user.id)
Пример #13
0
def get_by_data_id(data_id, user):
    """Return the curate data structure with the given data id

    Args:
        data_id:

    Returns:

    """
    return CurateDataStructure.get_by_data_id(data_id)
Пример #14
0
 def test_curate_data_structure_get_by_id_return_data_if_found(
         self, mock_get):
     # Arrange
     mock_data_structure = CurateDataStructure(user="******",
                                               template=Template(),
                                               name="name")
     mock_get.return_value = mock_data_structure
     mock_user = create_mock_user("1")
     # Act
     result = curate_data_structure_api.get_by_id(1, mock_user)
     # Assert
     self.assertIsInstance(result, CurateDataStructure)
Пример #15
0
def get_all_by_user_id_and_template_id(user_id, template_id):
    """Returns object with the given user id and template id

    Args:
        user_id:
        template_id:

    Returns:

    """
    return CurateDataStructure.get_all_by_user_id_and_template_id(
        user_id, template_id)
Пример #16
0
def get_all_by_user_id_and_template_id_with_no_data(user_id, template_id):
    """Return all the curate data structure by user and template, with no link to a data

    Args:
        user_id:
        template_id:

    Returns:

    """
    return CurateDataStructure.get_all_by_user_id_and_template_id_with_no_data(
        user_id, template_id)
Пример #17
0
 def test_curate_data_structure_get_by_user_and_template_and_name_return_curate_data_structure(
         self, mock_get):
     # Arrange
     mock_data_1 = CurateDataStructure(user="******",
                                       template=_get_template(),
                                       name="name_title_1")
     mock_get.return_value = mock_data_1
     # Act
     result = curate_data_structure_api.get_by_user_id_and_template_id_and_name(
         1, 1, "name_title_1")
     # Assert
     self.assertIsInstance(result, CurateDataStructure)
Пример #18
0
def get_by_data_structure_element_root_id(data_structure_element_root, user):
    """Return the curate data structure with the given data structure element root id

    Args:
        data_structure_element_root:
        user:

    Returns:

    """
    return CurateDataStructure.get_by_data_structure_element_root(
        data_structure_element_root)
Пример #19
0
def edit_record(request):
    """ Edit a record.

    Args:
        request:

    Returns:
    """
    try:
        data = data_api.get_by_id(request.POST['id'], request.user)
    except DoesNotExist:
        message = Message(
            messages.ERROR, "It seems a " + get_data_label() +
            " is missing. Please refresh the page.")
        return HttpResponseBadRequest(json.dumps({
            'message': message.message,
            'tags': message.tags
        }),
                                      content_type='application/json')

    # Check if the data is locked
    if lock_api.is_object_locked(data.id, request.user):
        message = Message(
            messages.ERROR,
            "The " + get_data_label() + " is locked. You can't edit it.")
        return HttpResponseBadRequest(json.dumps({
            'message': message.message,
            'tags': message.tags
        }),
                                      content_type='application/json')

    try:
        # Check if a curate data structure already exists
        curate_data_structure = curate_data_structure_api.get_by_data_id(
            data.id)
    except DoesNotExist:
        # Create a new curate data structure
        curate_data_structure = CurateDataStructure(
            user=str(request.user.id),
            template=str(data.template.id),
            name=data.title,
            form_string=data.xml_content,
            data=data)
        curate_data_structure = curate_data_structure_api.upsert(
            curate_data_structure)
    except Exception, e:
        message = Message(messages.ERROR, "A problem occurred while editing.")
        return HttpResponseBadRequest(json.dumps({
            'message': message.message,
            'tags': message.tags
        }),
                                      content_type='application/json')
Пример #20
0
def get_by_user_id_and_template_id_and_name(user_id, template_id, name):
    """Returns object with the given user id and template id and name

    Args:
        user_id:
        template_id:
        name:

    Returns:

    """
    return CurateDataStructure.get_by_user_id_and_template_id_and_name(
        user_id, template_id, name)
Пример #21
0
 def test_curate_data_structure_upsert_return_data_structure_element(
         self, mock_save):
     # Arrange
     mock_data_structure = CurateDataStructure(user="******",
                                               template=Template(),
                                               name="name")
     mock_save.return_value = mock_data_structure
     mock_user = create_mock_user("1")
     # Act
     result = curate_data_structure_api.upsert(mock_data_structure,
                                               mock_user)
     # Assert
     self.assertIsInstance(result, CurateDataStructure)
Пример #22
0
 def create(self, validated_data):
     """
     Create and return a new `CurateDataStructure` instance, given the validated data.
     """
     # Create data
     curate_data_structure = CurateDataStructure(
         user=validated_data["user"],
         name=validated_data["name"],
         template=validated_data["template"],
         form_string=validated_data["form_string"]
         if "form_string" in validated_data else None,
         data=validated_data["data"] if "data" in validated_data else None,
         data_structure_element_root=validated_data[
             "data_structure_element_root"]
         if "data_structure_element_root" in validated_data else None,
     )
     return curate_data_structure_api.upsert(curate_data_structure,
                                             self.context["request"].user)
Пример #23
0
def _start_curate_post(request):
    """Start curate POST request.

    Args:
        request:

    Returns:

    """
    try:
        template_id = str(request.POST['hidden_value'])
        selected_option = request.POST['curate_form']
        user_id = str(request.user.id)
        if selected_option == "new" or selected_option == "upload":
            if selected_option == "new":
                new_form = users_forms.NewForm(request.POST)
                if new_form.is_valid():
                    name = new_form.data['document_name']
                    curate_data_structure = CurateDataStructure(
                        user=user_id, template=template_id, name=name)
                else:
                    raise exceptions.CurateAjaxError(
                        'Error occurred during the validation ' +
                        get_form_label())
            else:
                try:  # check XML data or not?
                    upload_form = users_forms.UploadForm(
                        request.POST, request.FILES)
                    if upload_form.is_valid():
                        xml_file = request.FILES['file']
                        xml_file.seek(
                            0)  # put the cursor at the beginning of the file
                        xml_data = xml_file.read(
                        )  # read the content of the file
                        well_formed = is_well_formed_xml(xml_data)
                        name = xml_file.name
                        if not well_formed:
                            raise exceptions.CurateAjaxError(
                                'Uploaded File is not well formed XML')
                        else:
                            curate_data_structure = CurateDataStructure(
                                user=user_id,
                                template=template_id,
                                name=name,
                                form_string=xml_data)
                    else:
                        raise exceptions.CurateAjaxError(
                            'Error occurred during the validation ' +
                            get_form_label())
                except Exception as e:
                    raise exceptions.CurateAjaxError(
                        'Error during file uploading')

            curate_data_structure_api.upsert(curate_data_structure)
        else:
            open_form = users_forms.OpenForm(request.POST)
            curate_data_structure = curate_data_structure_api.get_by_id(
                open_form.data['forms'])
        url = reverse("core_curate_enter_data",
                      args=(curate_data_structure.id, ))
        return HttpResponse(url)
    except Exception as e:
        raise exceptions.CurateAjaxError(e.message)