Exemplo n.º 1
0
    def generate_template_xsl_rendering_collection(self):
        """Generate a TemplateXslRendering collection.

        Returns:

        """
        # NOTE: no real file to avoid using unsupported GridFS mock

        self.template_1 = Template(filename="template_1.xsd",
                                   content="content1",
                                   hash="hash1").save()
        self.template_2 = Template(filename="template_2.xsd",
                                   content="content2",
                                   hash="hash2").save()
        self.template_xsl_rendering_1 = TemplateXslRendering(
            template=str(self.template_1.id)).save()
        self.xsl_transformation_3 = XslTransformation(
            name="xsl_transformation_3",
            filename="xsl_transformation_3",
            content="content_3",
        ).save()

        self.template_xsl_rendering_2 = TemplateXslRendering(
            template=str(self.template_2.id),
            default_detail_xslt=str(self.xsl_transformation_3.id),
            list_detail_xslt=[str(self.xsl_transformation_3.id)],
        ).save()

        self.template_xsl_rendering_collection = [
            self.template_xsl_rendering_1,
            self.template_xsl_rendering_2,
        ]
Exemplo n.º 2
0
def get_by_template_hash(template_hash):
    """Get TemplateXslRendering by its template hash.

    Args:
        template_hash: Template hash.

    Returns:
        The TemplateXslRendering instance.

    """
    instance = None
    # Get list of templates with the given hash
    templates = template_api.get_all_by_hash(template_hash)
    # Check if one of the templates has a xslt. Stop when found.
    # FIXME: Check if this process is okay (no solutions to distinguish templates from same hash)
    for template in templates:
        try:
            instance = TemplateXslRendering.get_by_template_id(template.id)
            break
        except exceptions.DoesNotExist:
            pass

    if instance is None:
        raise exceptions.DoesNotExist(
            "No TemplateXslRendering found with the given template hash")

    return instance
Exemplo n.º 3
0
def add_or_delete(template_id,
                  list_xslt,
                  detail_xslt,
                  template_xsl_rendering_id=None):
    """ Manage the saving of a TemplateXslRendering. If no XSLTs have been given, deletes the instance.

    Args:
        template_id: TemplateXslRendering.
        list_xslt: XSLT.
        detail_xslt: XSLT.
        template_xsl_rendering_id: Instance id.

    Returns:

    """
    # Boolean to know if we need this instance in database, i.e there are XSLTs information.
    need_to_be_kept = list_xslt is not None or detail_xslt is not None

    try:
        # Update the configuration.
        template_xsl_rendering = get_by_id(template_xsl_rendering_id)
        if need_to_be_kept:
            template_xsl_rendering.list_xslt = list_xslt
            template_xsl_rendering.detail_xslt = detail_xslt
            _upsert(template_xsl_rendering)
        else:
            delete(template_xsl_rendering)
    except (Exception, exceptions.DoesNotExist):
        # If no configuration, create a new one.
        if need_to_be_kept:
            template_xsl_rendering = TemplateXslRendering(
                template=template_id,
                list_xslt=list_xslt,
                detail_xslt=detail_xslt)
            _upsert(template_xsl_rendering)
Exemplo n.º 4
0
def upsert(template_id,
           list_xslt,
           detail_xslt,
           template_xsl_rendering_id=None):
    """ Update or create a XSL Template rendering object

    Args:
        template_id:
        list_xslt:
        detail_xslt:
        template_xsl_rendering_id:

    Returns:
        TemplateXSLRendering - The updated/created object.
    """
    try:
        template_xsl_rendering = get_by_id(template_xsl_rendering_id)
        template_xsl_rendering.list_xslt = list_xslt
        template_xsl_rendering.detail_xslt = detail_xslt
    except Exception as exception:
        logger.warning(
            "Exception when saving TemplateXSLRendering object: %s" %
            str(exception))
        template_xsl_rendering = TemplateXslRendering(template=template_id,
                                                      list_xslt=list_xslt,
                                                      detail_xslt=detail_xslt)

    return _upsert(template_xsl_rendering)
Exemplo n.º 5
0
def get_all():
    """Get all TemplateXslRendering.

    Returns:
        List of TemplateXslRendering.

    """
    return TemplateXslRendering.get_all()
Exemplo n.º 6
0
def get_by_template_id(template_id):
    """Get TemplateXslRendering by its template id.

    Args:
        template_id: Template id.

    Returns:
        The TemplateXslRendering instance.

    """
    return TemplateXslRendering.get_by_template_id(template_id)
Exemplo n.º 7
0
def _create_template_xsl_rendering():
    """ Mocks an TemplateXslRendering.

    Returns:
        TemplateXslRendering mock.

    """
    template_xsl_rendering = TemplateXslRendering()
    template_xsl_rendering = _set_template_xsl_rendering_fields(template_xsl_rendering)

    return template_xsl_rendering
Exemplo n.º 8
0
def get_by_template_hash(template_hash):
    """Get TemplateXslRendering by its template hash.

    Args:
        template_hash: Template hash.

    Returns:
        The TemplateXslRendering instance.

    """
    # FIXME: Check if this process is okay (no solutions to distinguish templates from same hash)
    return TemplateXslRendering.get_by_template_hash(template_hash)
Exemplo n.º 9
0
def get_by_id(template_xsl_rendering_id):
    """Get an TemplateXslRendering document by its id.

    Args:
        template_xsl_rendering_id: Id.

    Returns:
        TemplateXslRendering object.

    Raises:
        DoesNotExist: The TemplateXslRendering doesn't exist.
        ModelError: Internal error during the process.

    """
    return TemplateXslRendering.get_by_id(template_xsl_rendering_id)