Exemple #1
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, request=request)
        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(request=request)
    # 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(request=request)

    # 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,
    )
def core_explore_tree_index(request):
    """ Page that allows to see the exploration tree.

    Args:
        request:

    Returns:

    """
    context = {}
    error = None

    try:
        # get the active ontology
        active_ontology = query_ontology_api.get_active()

        # get the navigation from the cache
        nav_key = active_ontology.id
        if nav_key in navigation_cache:
            navigation = navigation_cache.get(nav_key)
        else:
            # create the navigation
            navigation = create_navigation_tree_from_owl_file(active_ontology.content)
            navigation_cache.set(nav_key, navigation)

        # get the tree from the cache
        tree_key = navigation.id
        if tree_key in html_tree_cache:
            html_tree = html_tree_cache.get(tree_key)
        else:
            # create the html tree
            html_tree = render_navigation_tree(navigation, active_ontology.template.id)
            html_tree_cache.set(tree_key, html_tree)

        context = {
            'navigation_tree': html_tree,
            'navigation_id': navigation.id
        }
    except exceptions.DoesNotExist:
        error = {"error": "An Ontology should be active to explore. Please contact an admin."}
    except Exception:
        error = {"error": "An error occurred during the generation of the navigation tree."}

    if error:
        context.update(error)

    assets = {
        "js": [
            {
                "path": 'core_explore_tree_app/user/js/load_view.js',
                "is_raw": False
            },
            {
                "path": 'core_explore_tree_app/user/js/tree.js',
                "is_raw": True
            },
            {
                "path": 'core_explore_tree_app/user/js/resize_tree_panel.js',
                "is_raw": True
            },
            {
                "path": 'core_explore_tree_app/user/js/mock.js',
                "is_raw": True
            },
        ],
        "css": ['core_explore_tree_app/user/css/tree.css',
                'core_explore_tree_app/user/css/loading_background.css']
    }

    modals = ['core_explore_tree_app/user/navigation/download_options.html']

    return render(request,
                  'core_explore_tree_app/user/navigation/explore_tree_wrapper.html',
                  assets=assets,
                  modals=modals,
                  context=context)
Exemple #3
0
    def get(self, request, template_id, query_id=None):
        """Page that allows to build and submit queries

        Args:
            request:
            template_id:
            query_id:

        Returns:

        """
        try:
            template = template_api.get(template_id)
            if template is None:
                return render(request,
                              'core_explore_example_app/user/errors.html',
                              assets={},
                              context={
                                  'errors':
                                  "The selected {0} does not exist".format(
                                      self.object_name)
                              })

            # Init variables
            saved_query_form = ""

            try:
                explore_data_structure = explore_data_structure_api.get_by_user_id_and_template_id(
                    str(request.user.id), template_id)
                # If custom fields form present, set it
                custom_form = explore_data_structure.selected_fields_html_tree
            except exceptions.DoesNotExist:
                custom_form = None

            # If new form
            if query_id is None:
                # empty session variables
                request.session['mapCriteriaExplore'] = dict()
                request.session['savedQueryFormExplore'] = ""
                # create new query object
                query = self._create_new_query(request, template)
            else:
                # if not a new form and a query form is present in session
                if 'savedQueryFormExplore' in request.session:
                    saved_query_form = request.session['savedQueryFormExplore']
                query = query_api.get_by_id(query_id)

            # Get saved queries of a user
            if '_auth_user_id' in request.session:
                user_id = request.session['_auth_user_id']
                user_queries = saved_query_api.get_all_by_user_and_template(
                    user_id=user_id, template_id=template_id)
            else:
                user_queries = []

            assets = {"js": self._get_js(), "css": self._get_css()}

            context = {
                'queries': user_queries,
                'template_id': template_id,
                'description': self.get_description(),
                'title': self.get_title(),
                'custom_form': custom_form,
                'query_form': saved_query_form,
                'query_id': str(query.id),
                "build_query_url": self.build_query_url,
                "results_url": self.results_url,
                "get_query_url": self.get_query_url,
                "save_query_url": self.save_query_url,
                "select_fields_url": self.select_fields_url,
                "data_sources_selector_template":
                self.data_sources_selector_template,
                "query_builder_interface": self.query_builder_interface
            }

            modals = [
                "core_explore_example_app/user/modals/custom_tree.html",
                "core_explore_example_app/user/modals/sub_elements_query_builder.html",
                "core_main_app/common/modals/error_page_modal.html",
                "core_explore_example_app/user/modals/delete_all_queries.html",
                "core_explore_example_app/user/modals/delete_query.html"
            ]

            return render(request,
                          'core_explore_example_app/user/build_query.html',
                          assets=assets,
                          context=context,
                          modals=modals)
        except Exception, e:
            return render(request,
                          'core_explore_example_app/user/errors.html',
                          assets={},
                          context={'errors': e.message})
Exemple #4
0
    def get(self, request, template_id, query_id):
        """Query results view

        Args:
            request:
            template_id:
            query_id:

        Returns:

        """
        context = {
            'template_id':
            template_id,
            'query_id':
            query_id,
            'exporter_app':
            False,
            'back_to_query_redirect':
            self.back_to_query_redirect,
            'get_shareable_link_url':
            reverse("core_explore_example_get_persistent_query_url")
        }

        assets = {
            "js": [
                {
                    "path": 'core_explore_common_app/user/js/results.js',
                    "is_raw": False
                },
                {
                    "path": 'core_explore_common_app/user/js/results.raw.js',
                    "is_raw": True
                },
                {
                    "path": 'core_main_app/common/js/XMLTree.js',
                    "is_raw": False
                },
                {
                    "path":
                    'core_main_app/common/js/modals/error_page_modal.js',
                    "is_raw": True
                },
                {
                    "path":
                    'core_explore_common_app/user/js/button_persistent_query.js',
                    "is_raw": False
                },
            ],
            "css": [
                "core_explore_common_app/user/css/query_result.css",
                "core_main_app/common/css/XMLTree.css",
                "core_explore_common_app/user/css/results.css"
            ],
        }

        modals = [
            "core_main_app/common/modals/error_page_modal.html",
            "core_explore_common_app/user/persistent_query/modals/persistent_query_modal.html"
        ]

        if 'core_exporters_app' in INSTALLED_APPS:
            # add all assets needed
            assets['js'].extend([{
                "path":
                'core_exporters_app/user/js/exporters/list/modals/list_exporters_selector.js',
                "is_raw": False
            }])
            # add the modal
            modals.extend([
                "core_exporters_app/user/exporters/list/modals/list_exporters_selector.html"
            ])
            # the modal need all selected template
            query = query_api.get_by_id(query_id)

            context['exporter_app'] = True
            context['templates_list'] = json.dumps(
                [str(template.id) for template in query.templates])

        return render(request,
                      'core_explore_example_app/user/results.html',
                      assets=assets,
                      modals=modals,
                      context=context)
Exemple #5
0
    def get(self,
            request,
            curate_data_structure_id,
            reload_unsaved_changes=False):
        """Load view to enter data.

        Args:
            request:
            curate_data_structure_id:
            reload_unsaved_changes:

        Returns:

        """
        curate_data_structure = None
        try:
            # Retrieve CurateDataStructure and lock the object for the current
            # user.
            curate_data_structure = _get_curate_data_structure_by_id(
                curate_data_structure_id, request)

            # Lock from database
            if curate_data_structure.data is not None:
                lock_api.set_lock_object(curate_data_structure.data,
                                         request.user)

            # Check if we need to change the user. Code executed only if the
            # data is unlocked. set_lock_object() raises LockError.
            if str(request.user.id) != curate_data_structure.user:
                curate_data_structure.user = str(request.user.id)
                curate_data_structure = curate_data_structure_api.upsert(
                    curate_data_structure, request.user)

            return render(
                request,
                "core_curate_app/user/data-entry/enter_data.html",
                assets=self.assets,
                context=self.build_context(request, curate_data_structure,
                                           reload_unsaved_changes),
                modals=self.modals,
            )
        except (LockError, AccessControlError, ModelError, DoesNotExist) as ex:
            return render(
                request,
                "core_curate_app/user/errors.html",
                assets={},
                context={"errors": str(ex)},
            )
        except Exception as e:
            try:
                # Unlock from database
                if (curate_data_structure is not None
                        and curate_data_structure.data is not None):
                    lock_api.remove_lock_on_object(curate_data_structure.data,
                                                   request.user)
            except Exception as lock_exc:
                # CurateDataStructure not found, continue search
                logger.warning(
                    "'EnterDataView.get' threw an exception: {0}".format(
                        str(lock_exc)))

            return render(
                request,
                "core_curate_app/user/errors.html",
                assets={},
                context={"errors": str(e)},
            )
Exemple #6
0
    def get(self, request, template_id, *args, **kwargs):
        """Loads view to customize exploration tree

        Args:
            request:
            template_id:

        Returns:

        """
        try:
            # Set the assets
            assets = {
                "js": [
                    {
                        "path": 'core_main_app/common/js/XMLTree.js',
                        "is_raw": False
                    },
                    {
                        "path": "core_parser_app/js/autosave.js",
                        "is_raw": False
                    },
                    {
                        "path": "core_parser_app/js/autosave_checkbox.js",
                        "is_raw": False
                    },
                    {
                        "path": "core_parser_app/js/autosave.raw.js",
                        "is_raw": True
                    },
                    {
                        "path": "core_parser_app/js/buttons.js",
                        "is_raw": False
                    },
                    {
                        "path":
                        "core_explore_example_app/user/js/buttons.raw.js",
                        "is_raw": True
                    },
                    {
                        "path": "core_parser_app/js/modules.js",
                        "is_raw": False
                    },
                    {
                        "path": "core_parser_app/js/choice.js",
                        "is_raw": False
                    },
                    {
                        "path":
                        "core_explore_example_app/user/js/choice.raw.js",
                        "is_raw": True
                    },
                    {
                        "path":
                        "core_explore_example_app/user/js/select_fields.js",
                        "is_raw": False
                    },
                    {
                        "path":
                        "core_explore_example_app/user/js/select_fields.raw.js",
                        "is_raw": True
                    },
                ],
                "css": [
                    'core_explore_example_app/user/css/xsd_form.css',
                    'core_explore_example_app/user/css/style.css'
                ]
            }

            template = template_api.get(template_id)
            # get data structure
            data_structure = explore_data_structure_api.create_and_get_explore_data_structure(
                template, request.user.id)
            root_element = data_structure.data_structure_element_root

            # renders the form
            xsd_form = render_form(request, root_element)

            # Set the context
            context = {
                "template_id": template_id,
                "build_query_url": self.build_query_url,
                "load_form_url": self.load_form_url,
                "generate_element_url": self.generate_element_url,
                "remove_element_url": self.remove_element_url,
                "generate_choice_url": self.generate_choice_url,
                "data_structure_id": str(data_structure.id),
                "xsd_form": xsd_form
            }

            return render(request,
                          'core_explore_example_app/user/select_fields.html',
                          assets=assets,
                          context=context)
        except Exception, e:
            return render(request,
                          'core_explore_example_app/user/errors.html',
                          assets={},
                          context={'errors': e.message})
Exemple #7
0
def choose_query(request):
    """
    Select a query from the one into the database to be able to use it.
    Used by the user.
    If there is no query into the database, an error message is shown to the user.
    If no query has been selected, an error message is shown to the user.
    Send to the query_step if a query has been selected.
    If history queries are available, they are show with the following option:
    _History deletion
    _History recovery

    Args:
        request:

    Returns:

    """
    formset = None
    formset_history = None
    history_queries = None
    # Come back from the form with the query selected
    if request.method == 'POST':
        # Get the forms variable
        query_id = request.POST.get('query', None)
        to_recover = request.POST.get('to_recover', None)
        number_forms = request.POST.get('form-TOTAL_FORMS', None)

        # if a variable is none, come back with an error message
        if query_id is None and to_recover is None and number_forms is None:
            messages.add_message(request, messages.INFO,
                                 "You have to select a query.")
        else:
            if query_id is not None:
                return redirect(
                    reverse("core_custom_queries_app_steps",
                            kwargs={'query_id': query_id}))
            # History selected
            elif to_recover is not None and to_recover != '':
                return redirect(
                    reverse("core_custom_queries_app_recover_steps",
                            kwargs={'history_id': to_recover}))
            # Delete history query
            else:
                number_forms = int(number_forms)
                formset_history = formset_factory(FormHistory,
                                                  extra=number_forms,
                                                  can_delete=True)
                formset = formset_history(request.POST, request.FILES)
                if formset.is_valid():
                    if len(formset.deleted_forms) == 0:
                        messages.add_message(request, messages.ERROR,
                                             "Please select a query first.")
                    for form in formset.deleted_forms:
                        data = form.cleaned_data
                        if 'DELETE' in data:
                            id_history = ObjectId(data['id_history'])
                            history = history_query_api.get_by_id(id_history)
                            temp_query_id = history.query_id
                            query = temp_user_query_api.get_by_id(
                                temp_query_id)
                            query.delete_database()
                    messages.add_message(request, messages.SUCCESS,
                                         "Queries successfully deleted.")

    # Create the query form from the ones into the database
    id_list = list()
    # Get the available templates
    available_template_version_managers = template_version_manager_api.get_active_global_version_manager(
    )
    for available_template_version_manager in available_template_version_managers:
        for available_template_id in available_template_version_manager.versions:
            id_list.append(str(available_template_id))

    # Get the queries available for the current user
    if request.user.is_staff:
        # FIXME: call specialized api
        queries = dyn_query_api.get_all().filter(
            schema__in=id_list).values_list('pk', 'name')
    else:
        # FIXME: call specialized api
        queries = dyn_query_api.get_all().filter(schema__in=id_list).filter(
            group='user').values_list('pk', 'name')

    number_queries = len(queries)

    form = None
    if number_queries != 0:
        form = FormChooseQuery(query=queries)
    else:
        # If no query available, set an error message
        message = "No query available."
        messages.add_message(request, messages.ERROR, message)

    try:
        # If histories exist load them
        history_queries = history_query_api.get_all_by_user_id(
            str(request.user.id))
        number_history = len(history_queries)
    except Exception:
        number_history = 0

    if number_history > 0:
        # Create the formset with the histories values
        formset_history = formset_factory(FormHistory,
                                          extra=number_history,
                                          can_delete=True)
        data = dict()
        data['form-TOTAL_FORMS'] = number_history
        data['form-INITIAL_FORMS'] = number_history
        data['form-MAX_NUM_FORMS'] = number_history
        position = 0
        for history_query in history_queries:
            try:
                query_id = str(history_query.query_id)
                data['form-' + str(position) + '-id_history'] = str(
                    history_query.id)
                query = temp_user_query_api.get_by_id(query_id)
                data['form-' + str(position) +
                     '-query_name'] = query.query.name
                data['form-' + str(position) +
                     '-query_last_modified'] = query.last_modified

                h_message = history_query.message
                if h_message == "Pending.":
                    redis_server = Redis.from_url(REDIS_URL)
                    try:
                        if redis_server.exists("current_id"):
                            current_id = redis_server.get(
                                "current_id").decode()
                            if current_id == query_id:
                                h_message = "Pending: The file creation will begin shortly."

                        if redis_server.exists("list_ids"):
                            list_id = redis_server.lrange('list_ids', 0, -1)
                            try:
                                position_id = list_id.index(query_id) + 1
                                h_message = "Pending - Waiting list: " + str(
                                    position_id) + "/" + str(len(list_id))
                            except ValueError as e:
                                logger.warning(
                                    "choose_query threw an exception: {0}".
                                    format(str(e)))

                    except ConnectionError as e:
                        log_file = LogFile(
                            application="Custom Queries",
                            message="Redis not reachable, is it running?",
                            additionalInformation={'message': str(e)},
                            timestamp=datetime.now())
                        log_file_api.upsert(log_file)

                data['form-' + str(position) + '-query_message'] = h_message
                data['form-' + str(position) +
                     '-status'] = history_query.status
                data['form-' + str(position) + '-DELETE'] = ''
                position += 1
            except DoesNotExist as e:
                logger.warning("choose_query threw an exception: {0}".format(
                    str(e)))

        formset = formset_history(data)

    context = {
        'number_queries': number_queries,
        'number_history': number_history,
        'form': form,
        'formset': formset,
        'formset_history': formset_history
    }

    assets = {
        "js": [
            {
                "path": 'core_custom_queries_app/user/js/custom_queries.js',
                "is_raw": False
            },
        ],
    }

    return render(request,
                  'core_custom_queries_app/user/select_query.html',
                  assets=assets,
                  context=context)
Exemple #8
0
def default_custom_login(request):
    """ Default custom login page.
    
        Parameters:
            request: 
    
        Returns:
    """
    def _login_redirect(to_page):
        if to_page is not None and to_page != "":
            return redirect(to_page)

        return redirect(reverse("core_main_app_homepage"))

    if request.method == "POST":
        username = request.POST["username"]
        password = request.POST["password"]
        next_page = request.POST["next_page"]

        try:
            user = authenticate(username=username, password=password)

            if not user.is_active:
                return render(request,
                              "core_main_app/user/login.html",
                              context={
                                  'login_form':
                                  LoginForm(initial={'next_page': next_page}),
                                  'login_locked':
                                  True,
                                  'with_website_features':
                                  "core_website_app" in INSTALLED_APPS
                              })

            login(request, user)

            return _login_redirect(next_page)
        except Exception as e:
            return render(request,
                          "core_main_app/user/login.html",
                          context={
                              'login_form':
                              LoginForm(initial={'next_page': next_page}),
                              'login_error':
                              True,
                              'with_website_features':
                              "core_website_app" in INSTALLED_APPS
                          })
    elif request.method == "GET":
        if request.user.is_authenticated():
            return redirect(reverse("core_main_app_homepage"))

        next_page = None
        if "next" in request.GET:
            next_page = request.GET["next"]

        # build the context
        context = {
            'login_form': LoginForm(initial={"next_page": next_page}),
            'with_website_features': "core_website_app" in INSTALLED_APPS
        }
        assets = {"css": ["core_main_app/user/css/login/login.css"]}

        # get the web page login if exist
        web_page_login = web_page_login_api.get()

        if web_page_login:
            # update the context
            context["login_message"] = web_page_login.content

            # if exist we build assets and modals collection
            assets.update({
                "js": [{
                    "path":
                    'core_main_app/user/js/web_page_login/web_page_login.js',
                    "is_raw": False
                }]
            })
            modals = [
                "core_main_app/user/web_page_login/modals/web_page_login_modal.html"
            ]

            # render the page with context, assets and modals
            return render(request,
                          "core_main_app/user/login.html",
                          context=context,
                          assets=assets,
                          modals=modals)

        # render the page with context, assets
        return render(request,
                      "core_main_app/user/login.html",
                      context=context,
                      assets=assets)
    else:
        return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED)
    def get(self, request, template_id, query_id=None):
        """Page that allows to build and submit queries

        Args:
            request:
            template_id:
            query_id:

        Returns:

        """
        try:
            template = template_api.get(template_id, request=request)
            if template is None:
                return render(
                    request,
                    "core_explore_example_app/user/errors.html",
                    assets={},
                    context={
                        "errors":
                        "The selected {0} does not exist".format(
                            self.object_name)
                    },
                )

            # Init variables
            saved_query_form = ""

            try:
                explore_data_structure = (
                    explore_data_structure_api.get_by_user_id_and_template_id(
                        str(request.user.id), template_id))
                # If custom fields form present, set it
                custom_form = explore_data_structure.selected_fields_html_tree
            except exceptions.DoesNotExist:
                custom_form = None

            # If new form
            if query_id is None:
                # empty session variables
                request.session["mapCriteriaExplore"] = dict()
                request.session["savedQueryFormExplore"] = ""
                # create new query object
                query = self._create_new_query(request, template)
            else:
                # if not a new form and a query form is present in session
                if "savedQueryFormExplore" in request.session:
                    saved_query_form = request.session["savedQueryFormExplore"]
                query = query_api.get_by_id(query_id, request.user)

            # Get saved queries of a user
            if "_auth_user_id" in request.session:
                user_id = request.session["_auth_user_id"]
                user_queries = saved_query_api.get_all_by_user_and_template(
                    user_id=user_id, template_id=template_id)
            else:
                user_queries = []

            assets = {"js": self._get_js(), "css": self._get_css()}

            context = {
                "queries": user_queries,
                "template_id": template_id,
                "description": self.get_description(),
                "title": self.get_title(),
                "data_sorting_fields": build_sorting_context_array(query),
                "default_data_sorting_fields": ",".join(DATA_SORTING_FIELDS),
                "custom_form": custom_form,
                "query_form": saved_query_form,
                "query_id": str(query.id),
                "build_query_url": self.build_query_url,
                "results_url": self.results_url,
                "get_query_url": self.get_query_url,
                "save_query_url": self.save_query_url,
                "select_fields_url": self.select_fields_url,
                "data_sources_selector_template":
                self.data_sources_selector_template,
                "query_builder_interface": self.query_builder_interface,
            }

            modals = [
                "core_explore_example_app/user/modals/custom_tree.html",
                "core_explore_example_app/user/modals/sub_elements_query_builder.html",
                "core_main_app/common/modals/error_page_modal.html",
                "core_explore_example_app/user/modals/delete_all_queries.html",
                "core_explore_example_app/user/modals/delete_query.html",
            ]

            return render(
                request,
                "core_explore_example_app/user/build_query.html",
                assets=assets,
                context=context,
                modals=modals,
            )
        except Exception as e:
            return render(
                request,
                "core_explore_example_app/user/errors.html",
                assets={},
                context={"errors": str(e)},
            )
Exemple #10
0
class EnterDataView(View):
    def __init__(self):
        super(EnterDataView, self).__init__()
        self.assets = {
            "js": [
                {
                    "path": "core_curate_app/user/js/enter_data.js",
                    "is_raw": False
                },
                {
                    "path": "core_curate_app/user/js/enter_data.raw.js",
                    "is_raw": True
                },
                {
                    "path": "core_parser_app/js/modules.js",
                    "is_raw": False
                },
                {
                    "path": "core_parser_app/js/modules.raw.js",
                    "is_raw": True
                },
                {
                    "path": 'core_main_app/common/js/XMLTree.js',
                    "is_raw": False
                },
                {
                    "path": "core_parser_app/js/autosave.js",
                    "is_raw": False
                },
                {
                    "path": "core_parser_app/js/autosave.raw.js",
                    "is_raw": True
                },
                {
                    "path": "core_parser_app/js/buttons.js",
                    "is_raw": False
                },
                {
                    "path": "core_curate_app/user/js/buttons.raw.js",
                    "is_raw": True
                },
                {
                    "path": "core_parser_app/js/choice.js",
                    "is_raw": False
                },
                {
                    "path": "core_curate_app/user/js/choice.raw.js",
                    "is_raw": True
                },
            ],
            "css": [
                'core_curate_app/user/css/xsd_form.css',
                'core_parser_app/css/use.css'
            ]
        }
        self.modals = [
            'core_curate_app/user/data-entry/modals/cancel-changes.html',
            'core_curate_app/user/data-entry/modals/cancel-form.html',
            'core_curate_app/user/data-entry/modals/clear-fields.html',
            'core_curate_app/user/data-entry/modals/download-options.html',
            'core_curate_app/user/data-entry/modals/save-form.html',
            'core_curate_app/user/data-entry/modals/use-validation.html',
            'core_curate_app/user/data-entry/modals/xml-error.html',
            'core_curate_app/user/data-entry/modals/xml-valid.html',
        ]

    def build_context(self, request, curate_data_structure,
                      reload_unsaved_changes):
        """ Build the context of the view

        Args:
            request:
            curate_data_structure:
            reload_unsaved_changes:

        Returns:

        """
        # get xsd string from the template
        xsd_string = curate_data_structure.template.content

        if reload_unsaved_changes:
            # get root element from the data structure
            root_element = curate_data_structure.data_structure_element_root
        else:
            # if form string provided, use it to generate the form
            xml_string = curate_data_structure.form_string

            # get the root element
            root_element = generate_form(xsd_string, xml_string)

            # save the root element in the data structure
            curate_data_structure_api.update_data_structure_root(
                curate_data_structure, root_element)

        # renders the form
        xsd_form = render_form(request, root_element)

        return {
            "edit": True if curate_data_structure.data is not None else False,
            "xsd_form": xsd_form,
            "data_structure": curate_data_structure,
        }

    @method_decorator(
        decorators.permission_required(
            content_type=rights.curate_content_type,
            permission=rights.curate_access,
            login_url=reverse_lazy("core_main_app_login")))
    def get(self,
            request,
            curate_data_structure_id,
            reload_unsaved_changes=False):
        """Load view to enter data.

        Args:
            request:
            curate_data_structure_id:
            reload_unsaved_changes:

        Returns:

        """
        try:
            # get data structure
            curate_data_structure = _get_curate_data_structure_by_id(
                curate_data_structure_id, request)

            # lock from database
            if curate_data_structure.data is not None:
                lock_api.set_lock_object(curate_data_structure.data,
                                         request.user)

            # Check if we need to change the user.
            # Code executed only if the data is unlocked. set_lock_object() raises LockError.
            if str(request.user.id) != curate_data_structure.user:
                curate_data_structure.user = str(request.user.id)
                curate_data_structure = curate_data_structure_api.upsert(
                    curate_data_structure)

            # Set the context
            context = self.build_context(request, curate_data_structure,
                                         reload_unsaved_changes)

            return render(request,
                          'core_curate_app/user/data-entry/enter_data.html',
                          assets=self.assets,
                          context=context,
                          modals=self.modals)
        except LockError, ler:
            return render(request,
                          'core_curate_app/user/errors.html',
                          assets={},
                          context={'errors': ler.message})
        except Exception, e:
            try:
                # unlock from database
                if curate_data_structure is not None and curate_data_structure.data is not None:
                    lock_api.remove_lock_on_object(curate_data_structure.data,
                                                   request.user)
            except:
                pass

            return render(request,
                          'core_curate_app/user/errors.html',
                          assets={},
                          context={'errors': e.message})
Exemple #11
0
 def common_render(self, request, template_name, modals=None, assets=None, context=None):
     return admin_render(request, template_name, modals, assets, context) if self.administration \
         else render(request, template_name, modals, assets, context)
Exemple #12
0
        'form': form,
        'formset': formset,
        'formset_history': formset_history
    }

    assets = {
        "js": [
            {
                "path": 'core_custom_queries_app/user/js/custom_queries.js',
                "is_raw": False
            },
        ],
    }

    return render(request,
                  'core_custom_queries_app/user/select_query.html',
                  assets=assets,
                  context=context)


@decorators.permission_required(
    content_type=rights.custom_queries_content_type,
    permission=rights.custom_queries_access,
    login_url=reverse_lazy("core_main_app_login"))
def query_steps(request, query_id):
    """
    Manage each step of the query.

    Create the query step in function of the step type. Validate the user choices and store each step to the query
    history. The last step validation store the query to the database to explore the files and get the data.

    Args:
Exemple #13
0
def sandbox_view(request, pk):
    """Loads view to customize sandbox tree

    Args:
        request:
        pk: template id

    Returns:

    """
    try:
        # Set the assets
        assets = {
            "js": [
                {
                    "path": 'core_main_app/common/js/XMLTree.js',
                    "is_raw": False
                },
                {
                    "path": "core_parser_app/js/autosave.js",
                    "is_raw": False
                },
                {
                    "path": "core_parser_app/js/autosave_checkbox.js",
                    "is_raw": False
                },
                {
                    "path": "core_parser_app/js/autosave.raw.js",
                    "is_raw": True
                },
                {
                    "path": "core_parser_app/js/buttons.js",
                    "is_raw": False
                },
                {
                    "path": "core_schema_viewer_app/user/js/buttons.raw.js",
                    "is_raw": True
                },
                {
                    "path": "core_parser_app/js/choice.js",
                    "is_raw": False
                },
                {
                    "path": "core_schema_viewer_app/user/js/choice.raw.js",
                    "is_raw": True
                },
                {
                    "path": "core_parser_app/js/modules.js",
                    "is_raw": False
                },
                {
                    "path": 'core_schema_viewer_app/user/js/sandbox.js',
                    "is_raw": False
                },
            ],
            "css": [
                'core_main_app/common/css/XMLTree.css',
                'core_schema_viewer_app/user/css/xsd_form.css',
                'core_parser_app/css/use.css'
            ]
        }

        template = template_api.get(pk)

        # create the data structure
        sandbox_data_structure = sandbox_data_structure_api.create_and_save(
            template, request.user.id)

        # renders the form
        xsd_form = render_form(
            request, sandbox_data_structure.data_structure_element_root)

        # Set the context
        context = {
            "data_structure_id": str(sandbox_data_structure.id),
            "xsd_form": xsd_form
        }

        return render(request,
                      'core_schema_viewer_app/user/sandbox.html',
                      assets=assets,
                      context=context)
    except Exception as e:
        return render(
            request,
            'core_main_app/common/commons/error.html',
            assets={},
            context={'errors': 'An error occurred while rendering the tree.'})
def edit_rights(request, workspace_id):
    """ Load page to edit the rights.

    Args:   request
            workspace_id
    Returns:
    """

    try:
        workspace = workspace_api.get_by_id(workspace_id)
    except DoesNotExist as e:
        return HttpResponseBadRequest("The workspace does not exist.")
    except:
        return HttpResponseBadRequest("Something wrong happened.")

    if workspace.owner != str(request.user.id):
        return HttpResponseForbidden(
            "Only the workspace owner can edit the rights.")

    try:
        # Users
        users_read_workspace = workspace_api.get_list_user_can_read_workspace(
            workspace, request.user)
        users_write_workspace = workspace_api.get_list_user_can_write_workspace(
            workspace, request.user)

        users_access_workspace = list(
            set(users_read_workspace + users_write_workspace))
        detailed_users = []
        for user in users_access_workspace:
            detailed_users.append({
                'object_id': user.id,
                'object_name': user.username,
                'can_read': user in users_read_workspace,
                'can_write': user in users_write_workspace,
            })
    except:
        detailed_users = []

    try:
        # Groups
        groups_read_workspace = workspace_api.get_list_group_can_read_workspace(
            workspace, request.user)
        groups_write_workspace = workspace_api.get_list_group_can_write_workspace(
            workspace, request.user)

        groups_access_workspace = list(
            set(groups_read_workspace + groups_write_workspace))
        group_utils.remove_list_object_from_list(
            groups_access_workspace,
            [group_api.get_anonymous_group(),
             group_api.get_default_group()])
        detailed_groups = []
        for group in groups_access_workspace:
            detailed_groups.append({
                'object_id':
                group.id,
                'object_name':
                group.name,
                'can_read':
                group in groups_read_workspace,
                'can_write':
                group in groups_write_workspace,
            })
    except:
        detailed_groups = []

    context = {
        'workspace': workspace,
        'user_data': detailed_users,
        'group_data': detailed_groups,
        'template': workspace_constants.EDIT_RIGHTS_TEMPLATE_TABLE,
        'action_read': workspace_constants.ACTION_READ,
        'action_write': workspace_constants.ACTION_WRITE,
        'user': workspace_constants.USER,
        'group': workspace_constants.GROUP,
    }

    assets = {
        "css": [
            'core_main_app/libs/datatables/1.10.13/css/jquery.dataTables.css',
            "core_main_app/libs/fSelect/css/fSelect.css"
        ],
        "js": [{
            "path":
            'core_main_app/libs/datatables/1.10.13/js/jquery.dataTables.js',
            "is_raw": True
        }, {
            "path": "core_main_app/libs/fSelect/js/fSelect.js",
            "is_raw": False
        }, {
            "path": 'core_main_app/common/js/backtoprevious.js',
            "is_raw": True
        }]
    }

    assets['js'].extend(copy.deepcopy(workspace_constants.JS_TABLES))
    assets['js'].extend(copy.deepcopy(workspace_constants.JS_ADD_USER))
    assets['css'].extend(copy.deepcopy(workspace_constants.CSS_SWITCH))
    assets['js'].extend(copy.deepcopy(workspace_constants.JS_SWITCH_RIGHT))
    assets['js'].extend(copy.deepcopy(workspace_constants.JS_REMOVE_RIGHT))
    assets['js'].extend(copy.deepcopy(workspace_constants.JS_ADD_GROUP))
    assets['js'].extend(copy.deepcopy(workspace_constants.JS_INIT))

    modals = [
        workspace_constants.MODAL_ADD_USER,
        workspace_constants.MODAL_SWITCH_RIGHT,
        workspace_constants.MODAL_REMOVE_RIGHTS,
        workspace_constants.MODAL_ADD_GROUP
    ]

    return render(request,
                  workspace_constants.EDIT_RIGHTS_TEMPLATE,
                  context=context,
                  assets=assets,
                  modals=modals)
Exemple #15
0
def recover_query_steps(request, history_id):
    """

    """
    user_step = None
    previous_step = False
    next_step = False
    history_step = False

    # Validate the user step
    if request.method == 'POST':
        # Get the form's value
        user_query_id = request.POST.get('user_query', None)
        step_back = request.POST.get('step_back', None)
        try:
            # Load the query
            user_query = temp_user_query_api.get_by_id(user_query_id)
        except DoesNotExist:
            # The query doesn't exist anymore.
            messages.add_message(
                request, messages.ERROR,
                "A problem occurred during your information's transportation."
                " Please do it again.")

            return redirect(reverse("core_custom_queries_app_index"))

        # Get the current step information
        user_step = user_query.get_current_step()
        form_type = user_step.form_type
        # If the user want to go back to the previous step
        if step_back == 'True':
            previous_step = True
        else:
            error = False
            # The step is a a single choice step
            if form_type == 'radio':
                choices = request.POST.get('choices', None)
                if choices:
                    user_step.choices.append(choices)
                else:
                    messages.add_message(request, messages.WARNING,
                                         'You have to choose one.')
                    error = True
            # The step is a a multiple choices step
            elif form_type == 'check':
                list_choices = request.POST.getlist('choices')
                # Get the step restrictions and validate the choices against the restrictions
                min_range = user_step.step.data_min_range
                if min_range != '' and min_range:
                    min_range = int(min_range)
                    max_range = user_step.step.data_max_range
                    # data_infinity = user_step.step.data_infinity
                    if max_range != '' and max_range:
                        max_range = int(max_range)
                        if max_range < len(list_choices) or len(
                                list_choices) < min_range:
                            error = True
                            messages.add_message(
                                request, messages.WARNING,
                                'You have to choose between ' +
                                str(min_range) + ' and ' + str(max_range) +
                                ' choice.')
                    else:
                        if len(list_choices) < min_range:
                            error = True
                            messages.add_message(
                                request, messages.WARNING,
                                'You have to choose at least one.')
                if not error:
                    user_step.choices = list_choices
            # The step is a a date step
            else:
                time_from = request.POST['timeFrom']
                time_to = request.POST['timeTo']
                if time_from == "" or time_to == "":
                    error = True
                    messages.add_message(request, messages.WARNING,
                                         'You have to fill both date.')
                else:
                    try:
                        time_from = datetime.strptime(time_from,
                                                      "%Y-%m-%d %H:%M")
                        time_to = datetime.strptime(time_to, "%Y-%m-%d %H:%M")

                        if time_from > time_to:
                            error = True
                            messages.add_message(
                                request, messages.WARNING,
                                'Date From has to be before date To')

                        date_range = user_step.step.date_range
                        if date_range and date_range != '':
                            date_range = int(date_range)
                            if (time_to - time_from).days > date_range:
                                error = True
                                messages.add_message(
                                    request, messages.WARNING,
                                    'The maximum time range you can query in is '
                                    + str(date_range) + ' day(s).')
                    except Exception as e:
                        error = True
                        messages.add_message(request, messages.ERROR, e)
                if not error:
                    user_step.choices = (request.POST['timeFrom'],
                                         request.POST['timeTo'])
            if not error:
                user_step.update_choices_to_db()
                next_step = True
    # Select and load the current step
    else:
        # Load history queries
        try:
            history_query = history_query_api.get_by_id(history_id)
            user_query = temp_user_query_api.get_by_id(history_query.query_id)
        except DoesNotExist:
            messages.add_message(request, messages.ERROR,
                                 "A problem occurred during the redirection.")
            return redirect(reverse("core_custom_queries_app_index"))
        history_step = True
    try:  # Load the current step
        if previous_step is True:
            # Load the previous step
            user_step = user_query.get_previous_query_able_step()
            user_query.update_current_position_into_the_db()
            user_step.transform_choices_files_list_to_dict()
            history_id = user_query.save_to_history(user_id=request.user.id,
                                                    history_id=history_id)
        elif next_step is True:
            # Load the next step
            user_step = user_query.get_and_load_choices_next_step()
            user_query.update_current_position_into_the_db()
            history_id = user_query.save_to_history(user_id=request.user.id,
                                                    history_id=history_id)
        elif history_step is True:
            if user_query.history.message == "Error during output file creation.":
                # An error occurred
                messages.add_message(
                    request, messages.ERROR,
                    "An internal problem occurred during the output file creation. "
                    "The administrator has been noticed.")
                return redirect(reverse("core_custom_queries_app_index"))
            else:
                # Load the history query
                user_step = user_query.handle_history()
                user_step.transform_choices_files_list_to_dict()
    except Exception as e:
        # Create the log file from the error
        log_file = LogFile(
            application="Custom Queries",
            message=str(e),
            additionalInformation={
                'query_name':
                user_query.query.name,
                'step_name':
                user_query.list_steps[user_query.current_position].step.name,
                'user_choices':
                user_query.get_previous_choices()
            },
            timestamp=datetime.now())
        log_file_api.upsert(log_file)
        try:
            user_query.update_message("Error during query step.")
        except ValidationError as e:
            logger.warning(
                "recover_query_steps threw an exception: {0}".format(str(e)))

        messages.add_message(
            request, messages.ERROR,
            "An internal problem occurred, the administrator has been notified."
        )
        return redirect(reverse("core_custom_queries_app_index"))

    if user_step is None:
        # Go to result page
        user_query.add_query_to_waiting_line()
        user_query.update_message("Pending.")
        user_query.update_status(1)
        messages.add_message(request, messages.SUCCESS,
                             "The output file will be created soon.")
        return redirect(reverse("core_custom_queries_app_index"))
    else:
        context = _create_step_context(user_query, user_step, history_id)

        assets = {
            "js": [
                {
                    "path":
                    'core_custom_queries_app/user/js/custom_queries.js',
                    "is_raw": False
                },
                {
                    "path":
                    'core_custom_queries_app/user/js/custom_queries_step.js',
                    "is_raw": False
                },
                {
                    "path":
                    "core_custom_queries_app/libs/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js",
                    "is_raw": False
                },
            ],
            'css': [
                'core_custom_queries_app/libs/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css'
            ]
        }

        # for the rendering
        return render(request,
                      'core_custom_queries_app/user/query_step.html',
                      assets=assets,
                      context=context)
Exemple #16
0
def custom_password_reset_confirm(
        request,
        uidb64=None,
        token=None,
        template_name='core_main_app/user/registration/password_reset_confirm.html',
        token_generator=default_token_generator,
        set_password_form=SetPasswordForm,
        post_reset_redirect=None,
        extra_context=None):
    """
    View that checks the hash in a password reset link and presents a
    form for entering a new password.

        Parameters:
        :param request:
        :param uidb64:
        :param token:
        :param template_name:
        :param token_generator:
        :param set_password_form:
        :param post_reset_redirect:
        :param extra_context:

        Returns:
        :return request
    """

    UserModel = get_user_model()

    assert uidb64 is not None and token is not None  # checked by URLconf
    if post_reset_redirect is None:
        post_reset_redirect = reverse('password_reset_complete')
    else:
        post_reset_redirect = resolve_url(post_reset_redirect)
    try:
        # urlsafe_base64_decode() decodes to bytestring on Python 3
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = UserModel._default_manager.get(pk=uid)
    except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
        user = None

    if user is not None and token_generator.check_token(user, token):
        validlink = True
        title = 'Enter new password'
        if request.method == 'POST':
            form = set_password_form(user, request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(post_reset_redirect)
        else:
            form = set_password_form(user)
    else:
        validlink = False
        form = None
        title = 'Password reset unsuccessful'
    context = {
        'form': form,
        'title': title,
        'validlink': validlink,
    }
    if extra_context is not None:
        context.update(extra_context)

    return render(request, template_name, context=context)
Exemple #17
0
def custom_reset_password(
    request,
    template_name="core_main_app/user/registration/password_reset_form.html",
    email_template_name="core_main_app/user/registration/password_reset_email.html",
    subject_template_name="core_main_app/user/registration/password_reset_subject.txt",
    password_reset_form=PasswordResetForm,
    token_generator=default_token_generator,
    post_reset_redirect=None,
    from_email=None,
    extra_context=None,
    html_email_template_name=None,
    extra_email_context=None,
):
    """Custom reset password page.

    Parameters:
    :param request:
    :param template_name:
    :param email_template_name:
    :param subject_template_name:
    :param password_reset_form:
    :param token_generator:
    :param post_reset_redirect:
    :param from_email:
    :param extra_context:
    :param extra_email_context:
    :param html_email_template_name:

    Returns:
    :return request
    """

    if post_reset_redirect is None:
        post_reset_redirect = reverse("password_reset_done")
    else:
        post_reset_redirect = resolve_url(post_reset_redirect)
    if request.method == "POST":
        form = password_reset_form(request.POST)
        if form.is_valid():
            opts = {
                "use_https": request.is_secure(),
                "token_generator": token_generator,
                "from_email": from_email,
                "email_template_name": email_template_name,
                "subject_template_name": subject_template_name,
                "request": request,
                "html_email_template_name": html_email_template_name,
                "extra_email_context": extra_email_context,
                "domain_override": PASSWORD_RESET_DOMAIN_OVERRIDE,
            }
            form.save(**opts)
            return HttpResponseRedirect(post_reset_redirect)
    else:
        form = password_reset_form()
    context = {
        "form": form,
        "title": "Password reset",
    }
    if extra_context is not None:
        context.update(extra_context)

    return render(request, template_name, context=context)
Exemple #18
0
 def get(self, request, *args, **kwargs):
     self.context.update({'upload_form': self.form_class()})
     return render(request, self.template_name, context=self.context)
Exemple #19
0
def request_new_account(request):
    """Page that allows to request a user account

        Parameters:
            request:

        Returns: Http response
    """
    assets = {
        "js": [
            {
                "path": "core_website_app/user/js/user_account_req.js",
                "is_raw": False
            }
        ],
        "css": ['core_website_app/user/css/list.css']
    }

    if request.method == 'POST':
        request_form = RequestAccountForm(request.POST)
        if request_form.is_valid():
            # Call the API
            try:
                user = User(username=request.POST['username'],
                            first_name=request.POST['firstname'],
                            last_name=request.POST['lastname'],
                            password=make_password(request.POST['password']),
                            email=request.POST['email'],
                            is_active=False)

                account_request_api.insert(user)

                messages.add_message(request, messages.INFO, 'User Account Request sent to the administrator.')
                return redirect(reverse("core_main_app_homepage"))
            except ApiError as e:
                error_message = e.message

                error_template = get_template("core_website_app/user/request_error.html")
                error_box = error_template.render({"error_message": error_message})

                return render(request, 'core_website_app/user/request_new_account.html',
                              assets=assets,
                              context={'request_form': request_form, 'action_result': error_box})
            except ValidationError as e:
                error_message = "The following error(s) occurred during validation:"
                error_items = [str(field).capitalize() + ": " + str(error) for field, error in e.errors.items()]

                error_template = get_template("core_website_app/user/request_error.html")
                error_box = error_template.render({"error_message": error_message, "error_items": error_items})

                return render(request, 'core_website_app/user/request_new_account.html',
                              assets=assets,
                              context={'request_form': request_form, 'action_result': error_box})
    else:
        request_form = RequestAccountForm()

    context = {
        "request_form": request_form
    }

    return render(request, 'core_website_app/user/request_new_account.html', assets=assets, context=context)