Esempio n. 1
0
    def post(self, request):
        """ POST request. Try to save the configuration.

        Args:
            request:

        Returns:

        """
        form = self.form_class(request.POST)

        if form.is_valid():
            # Call the API
            content = request.POST['content']
            page = self.api.get()

            markdown_content = markdown(content)
            if markdown_content != stripjs(markdown_content):
                return self.get(request, current_content=content, error_id=constants.MARKDOWN_UNSAFE)

            try:
                parse_html(markdown_content, 'div')
            except HTMLError:
                return self.get(request, current_content=content, error_id=constants.MARKDOWN_GENERATION_FAILED)

            if page is None:
                page = WebPage(self.web_page_type, content)
            else:
                page.content = content

            self.api.upsert(page)
            messages.add_message(request, messages.INFO, 'Information saved with success.')

            return redirect(reverse(self.post_redirect))
Esempio n. 2
0
 def test_web_page_upsert_type_exist(self, mock_save):
     # Arrange
     web_page_type = WEB_PAGE_TYPES["login"]
     content = "content"
     web_page = WebPage(web_page_type, content)
     mock_save.return_value = WebPage(web_page_type, content)
     # Act
     result = web_page_api.upsert(web_page)
     # Assert
     self.assertEquals(content, result.content)
Esempio n. 3
0
def post(request):
    """ Create the privacy policy

    Parameters:

        {
            "content": "new_content"
        }

    Returns:

        - code: 200
          content: Privacy policy page
        - code: 400
          content: Validation error
    """
    try:
        # Get parameters
        privacy_policy_content = request.DATA['content']
        privacy_policy_page = privacy_policy_api.get()

        if privacy_policy_page is None:
            privacy_policy_page = WebPage(WEB_PAGE_TYPES["privacy_policy"],
                                          privacy_policy_content)
        else:
            privacy_policy_page.content = privacy_policy_content

        try:
            privacy_policy_page = privacy_policy_api.upsert(
                privacy_policy_page)

            # Serialize the request
            serializer = WebPageSerializer(privacy_policy_page.content)

            if serializer.is_valid():
                return Response(serializer.data, status=status.HTTP_200_OK)
            else:
                content = {'message': 'Serialization failed'}
                return Response(content, status=status.HTTP_400_BAD_REQUEST)

        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)

    except Exception as e:
        content = {'message': 'Expected parameters not provided.'}
        logger.exception(e.message)
        return Response(content, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 4
0
 def create(self, validated_data):
     """Create and return a new `WebPage` instance, given the validated data"""
     # Create data
     web_page = WebPage(
         content=validated_data["content"],
         type=validated_data["type"],
     )
     # Save the web page
     return web_page_api.upsert(web_page)
Esempio n. 5
0
def delete_by_type(page_type):
    """Delete the web pages with the given type

    Args:
        page_type (int): page type

    Returns: Web page

    """
    return WebPage.delete_by_type(page_type)
Esempio n. 6
0
def get(page_type):
    """Get the web page of a given type

    Args:
        page_type: type of the web page

    Returns: web page corresponding to the given id
    """
    if page_type not in list(WEB_PAGE_TYPES.keys()):
        return None
    try:
        return WebPage.get_by_type(page_type)
    except exceptions.DoesNotExist:
        return None
Esempio n. 7
0
 def test_web_page_upsert_type_does_not_exist(self):
     # Arrange
     web_page = WebPage(4, "test")
     # Act # Assert
     with self.assertRaises(ApiError):
         web_page_api.upsert(web_page)
Esempio n. 8
0
def _get_mock_web_page():
    """ Returns a Web page
    """
    type_default_for_test = 0
    return WebPage(type=type_default_for_test, content="web page")