Example #1
0
def q_publication(request, project_name=None, ontology_key=None, document_type=None, guid=None, version=None):

    # validate arguments...
    # (if this view is invoked from the above feed, these checks are superfluous)
    # (but if a user accesses this view directly, they are needed)
    validity, project, ontology, proxy, msg = validate_view_arguments(
        project_name=project_name,
        ontology_key=ontology_key,
        document_type=document_type
    )
    if not validity:
        return q_error(request, msg)

    # try to get the actual model...
    try:
        model = MetadataModel.objects.get(
            project=project,
            version=ontology,
            proxy=proxy,
            guid=guid,
        )
    except (ValueError, MetadataModel.DoesNotExist):
        msg = "Unable to find specified model."
        return q_error(request, msg)
    if not model.is_published:
        msg = "This model is not yet published"
        return q_error(request, msg)

    publications = QPublication.objects.filter(model=model).order_by("-version")
    if version:
        try:
            publication = publications.get(version=version)
        except QPublication.DoesNotExist:
            msg = "Unable to find model published at version {0}".format(version)
            return q_error(request, msg)
    else:
        publication = publications[0]

    return HttpResponse(publication.content, content_type="text/xml")
Example #2
0
    def get_object(self, request, project_name=None, ontology_key=None, document_type=None):
        """
        get_object parses the request sent from urls.py
        it sets some internal variables so that items() knows which models to expose
        """

        # TODO: THIS DOES NOT HANDLE INVALID ARGUMENTS CORRECTLY
        # TODO: SEE http://stackoverflow.com/questions/25817904/django-generate-error-on-atom-feed-request
        # check the arguments...
        validity, self.project, self.ontology, self.proxy, msg = validate_view_arguments(
            project_name=project_name,
            ontology_key=ontology_key,
            document_type=document_type
        )
        if not validity:
            return q_error(request, msg)

        self.title = "Published CIM Documents"
        if self.project:
            self.title += " project={0}".format(self.project.title)
            if self.ontology:
                self.title += ", ontology={0}".format(self.ontology)
                if self.proxy:
                    self.title += ", document_type={0}".format(self.proxy.name.title())
def q_customize_new(request, project_name=None, ontology_key=None, document_type=None):

    # save any request parameters...
    # (in case of redirection)
    context = add_parameters_to_context(request)

    # check the arguments...
    validity, project, ontology, proxy, msg = validate_view_arguments(
        project_name=project_name,
        ontology_key=ontology_key,
        document_type=document_type
    )
    if not validity:
        return q_error(request, msg)

    # check authentication...
    # (not using "@login_required" b/c some projects ignore authentication)
    if project.authenticated:
        current_user = request.user
        if not current_user.is_authenticated():
            next_page = "/login/?next=%s" % request.path
            return HttpResponseRedirect(next_page)
        if not is_admin_of(current_user, project):
            next_page = "/%s/" % project_name
            msg = "You have tried to view a restricted resource for this project.  Please consider joining."
            messages.add_message(request, messages.WARNING, msg)
            return HttpResponseRedirect(next_page)

    # get the set of vocabularies that apply to this project/ontology/proxy...
    vocabularies = project.vocabularies.filter(document_type__iexact=document_type)

    # get (or set) customization objects from the cache...
    session_key = get_key_from_request(request)
    cached_customization_set_key = "{0}_customization_set".format(session_key)
    customization_set = get_or_create_cached_object(request.session, cached_customization_set_key,
        get_new_customization_set,
        **{
            "project": project,
            "ontology": ontology,
            "proxy": proxy,
            "vocabularies": vocabularies,
        }
    )

    model_customization = customization_set["model_customization"]

    # I am only generating the model_customization_form at this top-level
    # all other forms (and formsets) are genearted as needed via the "load_section" view
    # called by the "section" directive according to the load-on-demand paradigm
    model_customization_form = QModelCustomizationForm(
        instance=model_customization,
        form_name="model_customization_form",
        # prefix=?!?,
        scope_prefix="model_customization",
    )

    # else:  # request.method == "POST"
    #
    #     # IN THEORY, I NEVER ENTER THIS BRANCH B/C ALL FORM SUBMISSION IS DONE VIA REST / ANGULAR
    #     # BUT I'M KEEPING THIS CODE HERE IN-CASE I NEED TO REFER TO IT LATER
    #
    #     data = request.POST.copy()  # sometimes I need to alter the data for unloaded forms;
    #                                 # this cannot be done on the original (immutable) QueryDict
    #
    #     model_customization_form = QModelCustomizationForm(
    #         data,
    #         instance=customization_set["model_customization"],
    #         # prefix=?!?,
    #         scope_prefix="model_customization",
    #         form_name="model_customization_form",
    #     )
    #
    #     if model_customization_form.is_valid():
    #         customization = model_customization_form.save()
    #         messages.add_message(request, messages.SUCCESS, "Successfully saved customization '%s'." % customization.name)
    #         customize_existing_url = reverse("customize_existing", kwargs={
    #             "project_name": project_name,
    #             "ontology_key": ontology_key,
    #             "document_type": document_type,
    #             "customizer_name": customization.name,
    #         })
    #         return HttpResponseRedirect(customize_existing_url)
    #
    #     else:
    #
    #         messages.add_message(request, messages.ERROR, "Failed to save customization.")

    # work out the various paths,
    # so that angular can reload things as needed
    view_url = request.path
    view_url_sections = [section for section in view_url.split('/') if section]
    view_url_dirname = '/'.join(view_url_sections[:])
    api_url = reverse("customization-list", kwargs={})
    api_url_sections = [section for section in api_url.split('/') if section]
    api_url_dirname = '/'.join(api_url_sections[:])

    # gather all the extra information required by the template
    _dict = {
        "session_key": session_key,
        "view_url_dirname": "/{0}/".format(view_url_dirname),
        "api_url_dirname": "/{0}/".format(api_url_dirname),
        "ontology": ontology,
        "proxy": proxy,
        "project": project,
        "vocabularies": vocabularies,
        "customization": model_customization,
        "model_customization_form": model_customization_form,
    }

    return render_to_response('questionnaire/q_customize.html', _dict, context_instance=context)
def q_customize_existing(request, project_name=None, ontology_key=None, document_type=None, customization_name=None):

    # save any request parameters...
    # (in case of redirection)
    context = add_parameters_to_context(request)

    # check the arguments...
    validity, project, ontology, proxy, msg = validate_view_arguments(
        project_name=project_name,
        ontology_key=ontology_key,
        document_type=document_type
    )
    if not validity:
        return q_error(request, msg)

    # check authentication...
    # (not using "@login_required" b/c some projects ignore authentication)
    if project.authenticated:
        current_user = request.user
        if not current_user.is_authenticated():
            next_page = "/login/?next=%s" % request.path
            return HttpResponseRedirect(next_page)
        if not is_admin_of(current_user, project):
            next_page = "/%s/" % project_name
            msg = "You have tried to view a restricted resource for this project.  Please consider joining."
            messages.add_message(request, messages.WARNING, msg)
            return HttpResponseRedirect(next_page)

    # get (or set) customization objects from the cache...
    # note that unlike in "q_customize_new" above, this bit is enclosed in a try/catch block
    # this is to deal w/ the possibility of an invalid customization_name
    try:
        session_key = get_key_from_request(request)
        cached_customization_set_key = "{0}_customization_set".format(session_key)
        customization_set = get_or_create_cached_object(request.session, cached_customization_set_key,
            get_existing_customization_set,
            **{
                "project": project,
                "ontology": ontology,
                "proxy": proxy,
                "customization_name": customization_name,
            }
        )
    except ObjectDoesNotExist:
        msg = "Cannot find the customization '{0}' for that project/ontology/model combination.".format(customization_name)
        return q_error(request, msg)

    model_customization = customization_set["model_customization"]

    # I am only generating the model_customization_form at this top-level
    # all other forms (and formsets) are generated as needed via the "load_section" view
    # called by the "section" directive according to the load-on-demand paradigm
    model_customization_form = QModelCustomizationForm(
        instance=model_customization,
        form_name="model_customization_form",
        # prefix=?!?,
        scope_prefix="model_customization",
    )

    # work out the various paths,
    # so that angular can reload things as needed
    view_url = request.path
    view_url_sections = [section for section in view_url.split('/') if section]
    view_url_dirname = '/'.join(view_url_sections[:-1])
    api_url = reverse("customization-detail", kwargs={"pk": model_customization.pk})
    api_url_sections = [section for section in api_url.split('/') if section]
    api_url_dirname = '/'.join(api_url_sections[:-1])

    # gather all the extra information required by the template
    _dict = {
        "session_key": session_key,
        "view_url_dirname": "/{0}/".format(view_url_dirname),
        "api_url_dirname": "/{0}/".format(api_url_dirname),
        "ontology": ontology,
        "proxy": proxy,
        "project": project,
        "vocabularies": [v.vocabulary for v in customization_set["vocabulary_customizations"]],
        "customization": model_customization,
        "model_customization_form": model_customization_form,
    }

    return render_to_response('questionnaire/q_customize.html', _dict, context_instance=context)
Example #5
0
def q_customize_new(request, project_name=None, ontology_key=None, document_type=None):

    # save any request parameters...
    # (in case of redirection)
    context = add_parameters_to_context(request)

    # check the arguments...
    validity, project, ontology, model_proxy, msg = validate_view_arguments(
        project_name=project_name,
        ontology_key=ontology_key,
        document_type=document_type
    )
    if not validity:
        return q_error(request, msg)

    # check authentication...
    # (not using "@login_required" b/c some projects ignore authentication)
    current_user = request.user
    if project.authenticated:
        if not current_user.is_authenticated():
            next_page = "/login/?next=%s" % request.path
            return HttpResponseRedirect(next_page)
        if not is_admin_of(current_user, project):
            next_page = "/%s/" % project_name
            msg = "You have tried to view a restricted resource for this project.  Please consider joining."
            messages.add_message(request, messages.WARNING, msg)
            return HttpResponseRedirect(next_page)

    # get (or set) customization objects from the cache...
    session_key = get_key_from_request(request)
    cached_customizations_key = "{0}_customizations".format(session_key)
    model_customization = get_or_create_cached_object(request.session, cached_customizations_key,
        get_new_customizations,
        **{
            "project": project,
            "ontology": ontology,
            "model_proxy": model_proxy,
            "key": model_proxy.name,
        }
    )
    model_customization_key = model_customization.get_key()

    if current_user.is_authenticated():
        set_owner(model_customization, evaluate_lazy_object(current_user))

    # I generate the model_customization_form at this top-level
    # all other forms are generated as needed via the "load_section" view
    # which is called by the "section" directive according to the load-on-demand paradigm
    model_customization_form_class = MODEL_CUSTOMIZATION_FORM_MAP["form_class"]
    model_customization_form = model_customization_form_class(
        instance=model_customization,
        form_name=MODEL_CUSTOMIZATION_FORM_MAP["form_name"].format(safe_key=model_customization_key.replace('-', '_')),
        # prefix=?!?
        scope_prefix=MODEL_CUSTOMIZATION_FORM_MAP["form_scope_prefix"],
    )

    # work out the various paths,
    # so that ng can reload things as needed
    view_url = request.path
    view_url_sections = [section for section in view_url.split('/') if section]
    view_url_dirname = '/'.join(view_url_sections[:])
    api_url = reverse("customization-list", kwargs={})
    api_url_sections = [section for section in api_url.split('/') if section]
    api_url_dirname = '/'.join(api_url_sections[:])

    # gather all the extra information required by the template
    _dict = {
        "session_key": session_key,
        "view_url_dirname": "/{0}/".format(view_url_dirname),
        "api_url_dirname": "/{0}/".format(api_url_dirname),
        "project": project,
        "ontology": ontology,
        "proxy": model_proxy,
        "customization": model_customization,
        "form": model_customization_form,
    }

    return render_to_response('questionnaire/q_customize.html', _dict, context_instance=context)
def q_customize_new(request, project_name=None, ontology_key=None, document_type=None):

    # save any request parameters...
    # (in case of redirection)
    context = add_parameters_to_context(request)

    # check the arguments...
    validity, project, ontology, model_proxy, msg = validate_view_arguments(
        project_name=project_name,
        ontology_key=ontology_key,
        document_type=document_type
    )
    if not validity:
        return q_error(request, msg)

    # check authentication...
    # (not using "@login_required" b/c some projects ignore authentication)
    current_user = request.user
    if project.authenticated:
        if not current_user.is_authenticated():
            next_page = add_parameters_to_url(reverse("account_login"), next=request.path)
            return HttpResponseRedirect(next_page)
        if not is_admin_of(current_user, project):
            next_page = reverse("project", kwargs={"project_name": project_name})
            msg = "You have tried to view a restricted resource for this project.  Please consider joining."
            messages.add_message(request, messages.WARNING, msg)
            return HttpResponseRedirect(next_page)

    # get (or set) customization objects from the cache...
    session_key = get_key_from_request(request)
    cached_customizations_key = "{0}_customizations".format(session_key)
    model_customization = get_or_create_cached_object(request.session, cached_customizations_key,
        get_new_customizations,
        **{
            "project": project,
            "ontology": ontology,
            "model_proxy": model_proxy,
            # "key": model_proxy.name,
            "key": model_proxy.key,
        }
    )

    if current_user.is_authenticated():
        set_owner(model_customization, evaluate_lazy_object(current_user))

    # setup top-level form...
    # (subforms are handled by the load-on-demand paradigm)
    model_customization_form_class = MODEL_CUSTOMIZATION_FORM_MAP["form_class"]
    model_customization_form = model_customization_form_class(
        instance=model_customization,
        form_name=MODEL_CUSTOMIZATION_FORM_MAP["form_name"].format(safe_key=model_customization.key.replace('-', '_')),
        scope_prefix=MODEL_CUSTOMIZATION_FORM_MAP["form_scope_prefix"],
        # prefix=?!?
    )

    # work out various paths, so that ng can reload things as needed...
    view_url_dirname = request.path.rsplit('/', 1)[0]
    api_url_dirname = reverse("customization-list").rsplit('/', 1)[0]

    # gather all the extra information required by the template...
    template_context = {
        "project": project,
        "ontology": ontology,
        "proxy": model_proxy,
        "view_url_dirname": view_url_dirname,
        "api_url_dirname": api_url_dirname,
        "session_key": session_key,
        "customization": model_customization,
        "form": model_customization_form,
    }
    return render_to_response('questionnaire/q_customize.html', template_context, context_instance=context)