Esempio n. 1
0
def manage_types(request):
    """View that allows type management.

    Args:
        request:

    Returns:

    """
    # get all types
    type_version_managers = type_version_manager_api.get_global_version_managers(
        request=request)
    # get buckets
    buckets = bucket_api.get_all()

    context = {
        "object_name":
        "Type",
        "available": [
            type_version_manager
            for type_version_manager in type_version_managers
            if not type_version_manager.is_disabled
        ],
        "disabled": [
            type_version_manager
            for type_version_manager in type_version_managers
            if type_version_manager.is_disabled
        ],
        "buckets":
        buckets,
    }

    assets = {
        "js": [
            {
                "path": "core_main_app/common/js/templates/list/restore.js",
                "is_raw": False,
            },
            {
                "path":
                "core_main_app/common/js/templates/list/modals/disable.js",
                "is_raw": False,
            },
            EditTemplateVersionManagerView.get_modal_js_path(),
        ],
        "css": ["core_composer_app/common/css/bucket.css"],
    }

    modals = [
        "core_main_app/admin/templates/list/modals/disable.html",
        EditTemplateVersionManagerView.get_modal_html_path(),
    ]

    return admin_render(
        request,
        "core_composer_app/admin/types/list.html",
        assets=assets,
        context=context,
        modals=modals,
    )
Esempio n. 2
0
def manage_type_buckets(request, version_manager_id):
    """Manage buckets of a type.

    Args:
        request:
        version_manager_id:

    Returns:

    """
    # get the version manager
    version_manager = None

    try:
        version_manager = version_manager_api.get(version_manager_id,
                                                  request=request)
    except ModelError as e:
        logger.error("manage_type_buckets threw a ModelError: {0}".format(
            str(e)))
        return admin_render(
            request,
            "core_main_app/common/commons/error.html",
            context={"error": str(e)},
        )
    except DoesNotExist as e:
        # TODO: catch exception, redirect to error page
        logger.warning(
            "manage_type_buckets threw a DoesNotExist exception: {0}".format(
                str(e)))

    context = {
        "version_manager": version_manager,
        "buckets": bucket_api.get_all()
    }

    assets = {
        "css": ["core_composer_app/common/css/bucket.css"],
        "js": [{
            "path": "core_main_app/common/js/backtoprevious.js",
            "is_raw": True
        }],
    }
    if request.method == "POST":
        form = EditTypeBucketsForm(request.POST)
        if form.is_valid():
            buckets = request.POST.getlist("buckets")
            bucket_api.update_type_buckets(version_manager, buckets)
            return redirect(reverse("admin:core_composer_app_types"))
    else:
        form = EditTypeBucketsForm()

    context["form"] = form
    return admin_render(
        request,
        "core_composer_app/admin/buckets/edit.html",
        assets=assets,
        context=context,
    )
Esempio n. 3
0
class EditTypeBucketsForm(forms.Form):
    """
    Form to edit buckets of a Type.
    """
    buckets = BucketDataModelChoiceField(
        label='Select new buckets',
        queryset=bucket_api.get_all(),
        required=False,
        widget=forms.SelectMultiple(attrs={'class': 'form-control'}))
Esempio n. 4
0
    def test_get_all_buckets_returns_buckets(self, mock_get_all):
        # Arrange
        mock_bucket1 = _create_mock_bucket()
        mock_bucket2 = _create_mock_bucket()

        mock_get_all.return_value = [mock_bucket1, mock_bucket2]

        # Act
        result = bucket_api.get_all()

        # Assert
        self.assertTrue(all(isinstance(item, Bucket) for item in result))
Esempio n. 5
0
def manage_type_buckets(request, version_manager_id):
    """Manage buckets of a type.

    Args:
        request:
        version_manager_id:

    Returns:

    """
    # get the version manager
    version_manager = None

    try:
        version_manager = version_manager_api.get(version_manager_id)
    except:
        # TODO: catch good exception, redirect to error page
        pass

    context = {
        'version_manager': version_manager,
        'buckets': bucket_api.get_all()
    }

    assets = {
        "css": ['core_composer_app/common/css/bucket.css'],
        "js": [{
            "path": 'core_main_app/common/js/backtoprevious.js',
            "is_raw": True
        }]
    }
    if request.method == 'POST':
        form = EditTypeBucketsForm(request.POST)
        if form.is_valid():
            buckets = request.POST.getlist('buckets')
            bucket_api.update_type_buckets(version_manager, buckets)
            return redirect(reverse('admin:core_composer_app_types'))
    else:
        form = EditTypeBucketsForm()

    context['form'] = form
    return admin_render(request,
                        'core_composer_app/admin/buckets/edit.html',
                        assets=assets,
                        context=context)
Esempio n. 6
0
class UploadTypeForm(forms.Form):
    """
    Form to upload a new Type.
    """
    name = forms.CharField(
        label='Enter Type name',
        max_length=100,
        required=True,
        widget=forms.TextInput(attrs={'class': 'form-control'}))
    xsd_file = forms.FileField(
        label='Select a file',
        required=True,
        widget=forms.FileInput(attrs={'class': 'form-control'}))
    buckets = BucketDataModelChoiceField(
        label='Select buckets',
        queryset=bucket_api.get_all(),
        required=False,
        widget=forms.SelectMultiple(attrs={'class': 'form-control'}))
Esempio n. 7
0
def get_no_buckets_types(request):
    """Get list of available types not inside a bucket.

    Returns:

    """
    # build list of types
    bucket_types = []
    for bucket in bucket_api.get_all():
        bucket_types += bucket.types

    all_types = get_global_version_managers(request=request)
    no_bucket_types = [
        type_version_manager
        for type_version_manager in all_types
        if type_version_manager not in bucket_types
    ]
    return no_bucket_types
Esempio n. 8
0
def manage_buckets(request):
    """Manage buckets view.

    Args:
        request:

    Returns:

    """

    context = {"object_name": "Bucket", "buckets": bucket_api.get_all()}

    assets = {
        "js": [
            {
                "path": "core_composer_app/admin/js/bucket.js",
                "is_raw": False
            },
            {
                "path": "core_composer_app/admin/js/bucket.raw.js",
                "is_raw": True
            },
            {
                "path": "core_main_app/common/js/backtoprevious.js",
                "is_raw": True
            },
            EditBucketView.get_modal_js_path(),
        ],
        "css": ["core_composer_app/common/css/bucket.css"],
    }

    modals = [
        "core_composer_app/admin/buckets/modals/delete.html",
        EditBucketView.get_modal_html_path(),
    ]

    return admin_render(
        request,
        "core_composer_app/admin/buckets/list.html",
        assets=assets,
        modals=modals,
        context=context,
    )
Esempio n. 9
0
    def get(self, request):
        """ Get all buckets

        Url Parameters:

            label: label

        Examples:

            ../bucket/
            ../bucket?label=[label]

        Args:

            request: HTTP request

        Returns:

            - code: 200
              content: List of buckets
            - code: 500
              content: Internal server error
        """
        try:
            # Get objects
            object_list = bucket_api.get_all()

            # Apply filters
            label = self.request.query_params.get('label', None)
            if label is not None:
                object_list = object_list.filter(label=label)

            # Serialize object
            serializer = BucketSerializer(object_list, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Esempio n. 10
0
def build_template(request, template_id):
    """View that allows to build the Template.

    Args:
        request:
        template_id:

    Returns:

    """
    if template_id == "new":
        base_template_path = finders.find(join('core_composer_app', 'user', 'xsd', 'new_base_template.xsd'))
        xsd_string = read_file_content(base_template_path)
    else:
        template = template_api.get(template_id)
        xsd_string = template.content

    request.session['newXmlTemplateCompose'] = xsd_string
    request.session['includedTypesCompose'] = []

    # store the current includes/imports
    xsd_tree = XSDTree.build_tree(xsd_string)
    includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE))
    for el_include in includes:
        if 'schemaLocation' in el_include.attrib:
            request.session['includedTypesCompose'].append(el_include.attrib['schemaLocation'])
    imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE))
    for el_import in imports:
        if 'schemaLocation' in el_import.attrib:
            request.session['includedTypesCompose'].append(el_import.attrib['schemaLocation'])

    # remove annotations from the tree
    remove_annotations(xsd_tree)
    xsd_string = XSDTree.tostring(xsd_tree)

    # loads XSLT
    xslt_path = finders.find(join('core_composer_app', 'user', 'xsl', 'xsd2html.xsl'))
    # reads XSLT
    xslt_string = read_file_content(xslt_path)
    # transform XML to HTML
    xsd_to_html_string = xsl_transform(xsd_string, xslt_string)

    # 1) Get user defined types
    user_types = type_version_manager_api.get_version_managers_by_user(str(request.user.id))
    # 2) Get buckets
    buckets = bucket_api.get_all()

    # 3) no_buckets_types: list of types that are not assigned to a specific bucket
    no_buckets_types = type_version_manager_api.get_no_buckets_types()

    # 4) Build list of built-in types
    built_in_types = []
    for built_in_type in get_xsd_types():
        built_in_types.append({'current': 'built_in_type', 'title': built_in_type})

    assets = {
        "js": [
            {
                "path": 'core_composer_app/user/js/build_template.js',
                "is_raw": False
            },
            {
                "path": 'core_composer_app/user/js/build_template.raw.js',
                "is_raw": True
            },
            {
                "path": 'core_composer_app/user/js/xpath.js',
                "is_raw": False
            },
            {
                "path": 'core_composer_app/user/js/menus.js',
                "is_raw": False
            },
            {
                "path": 'core_composer_app/user/js/xsd_tree.js',
                "is_raw": False
            },
        ],
        "css": ['core_main_app/common/css/XMLTree.css',
                'core_composer_app/common/css/bucket.css',
                'core_composer_app/user/css/menu.css',
                'core_composer_app/user/css/style.css',
                'core_composer_app/user/css/xsd_tree.css']
    }
    context = {
        'buckets': buckets,
        'built_in_types': built_in_types,
        'no_buckets_types': no_buckets_types,
        'user_types': user_types,
        'xsd_form': xsd_to_html_string,
        'template_id': template_id,
    }

    modals = [
        'core_composer_app/user/builder/menus/sequence.html',
        'core_composer_app/user/builder/menus/element.html',
        'core_composer_app/user/builder/menus/element_root.html',

        'core_composer_app/user/builder/modals/root_type_name.html',
        'core_composer_app/user/builder/modals/element_name.html',
        'core_composer_app/user/builder/modals/insert_element.html',
        'core_composer_app/user/builder/modals/delete_element.html',
        'core_composer_app/user/builder/modals/change_type.html',
        'core_composer_app/user/builder/modals/save_template.html',
        'core_composer_app/user/builder/modals/save_type.html',
        'core_composer_app/user/builder/modals/save_success.html',
        'core_composer_app/user/builder/modals/occurrences.html',
        'core_composer_app/user/builder/modals/errors.html',
    ]

    return render(request,
                  'core_composer_app/user/build_template.html',
                  assets=assets,
                  context=context,
                  modals=modals)