def q_password(request, user_name=None): current_user = request.user try: user = User.objects.get(username=user_name) except User.DoesNotExist: if not user_name: msg = u"Please specify a user." else: msg = u"Unable to locate user '%s'" % (user_name) return q_error(request, error_msg=msg) if current_user.username != user_name: msg = u"You do not have permission to edit this user." return q_error(request, error_msg=msg) if not user.is_active: msg = u"This user's account has been disabled." return q_error(request, error_msg=msg) context = add_parameters_to_context(request) next_page = context.get("next", u"/users/%s" % user.username) return password_change_view( request, template_name='questionnaire/q_password.html', post_change_redirect=next_page, password_change_form=QUserPasswordForm, )
def q_view_new(request, project_name=None, ontology_key=None, document_type=None): """ this is never exposed by templates but a user might still try to navigate explicitly to this URL just return an error telling them not to try that :param request: :param project_name: :param ontology_key: :param document_type: :return: """ # save any request parameters... # (in case of redirection) context = add_parameters_to_context(request) # check the arguments... validity, project, ontology, model_proxy, model_customization, msg = validate_view_arguments( project_name=project_name, ontology_key=ontology_key, document_type=document_type ) if not validity: return q_error(request, msg) # and then let the user know that they can't vew a _new_ document... msg = "The ES-DOC Questionnaire only supports viewing of <em>existing</em> documents." return q_error(request, msg)
def q_profile(request, username=None): context = add_parameters_to_context(request) next_page = context.get("next") current_user = request.user try: user = User.objects.get(username=username) except User.DoesNotExist: if not username: msg = "Please specify a user." else: msg = "Unable to locate user '{0}'".format(username) return q_error(request, error_msg=msg) read_only = False if current_user.username != username: if not current_user.is_superuser: read_only = True # msg = "You do not have permission to edit this user." # return q_error(request, error_msg=msg) if user.is_superuser: msg = "You can't view the details of the site administrator. What were you thinking?!?." return q_error(request, error_msg=msg) if not user.is_active: msg = "This user's account has been disabled." return q_error(request, error_msg=msg) user_form = QUserProfileForm(instance=user.profile) # if request.method == "GET": # # user_form = QUserProfileForm(instance=user.profile) # # else: # request.method == "POST": # # import ipdb; ipdb.set_trace() # data = request.POST # user_form = QUserProfileForm(instance=user_profile, data=data) # if user_form.is_valid(): # # msg = "Successfully changed user details." # messages.add_message(request, messages.SUCCESS, msg) # # user_profile = user_form.save() # if user_profile is not None: # if next_page: # return redirect(next_page) # # else: # msg = "Error changing user details." # messages.add_message(request, messages.WARNING, msg) context = { "read_only": read_only, "user": user, "form": user_form, } return render(request, 'questionnaire/q_profile.html', context)
def q_user(request, user_name=None): current_user = request.user try: user = User.objects.get(username=user_name) except User.DoesNotExist: if not user_name: msg = u"Please specify a user." else: msg = u"Unable to locate user '%s'" % (user_name) return q_error(request, error_msg=msg) if current_user.username != user_name and not current_user.is_superuser: msg = u"You do not have permission to edit this user." return q_error(request, error_msg=msg) if user.is_superuser: msg = u"You can't edit the details of the site administrator. Sheesh." return q_error(request, error_msg=msg) if not user.is_active: msg = u"This user's account has been disabled." return q_error(request, error_msg=msg) user_profile = user.profile context = add_parameters_to_context(request) next_page = context.get("next") if request.method == "GET": user_form = QUserProfileForm(instance=user_profile) else: # request.method == "POST": data = request.POST user_form = QUserProfileForm(instance=user_profile, data=data) if user_form.is_valid(): msg = "Successfully changed user details." messages.add_message(request, messages.SUCCESS, msg) user_profile = user_form.save() if user_profile is not None: if next_page: return HttpResponseRedirect(next_page) else: msg = "Error changing user details." messages.add_message(request, messages.WARNING, msg) # gather all the extra information required by the template _dict = { "user": user, "projects": user_profile.projects.all(), "form": user_form } return render_to_response('questionnaire/q_user.html', _dict, context_instance=RequestContext(request))
def q_publication(request, project_name=None, ontology_key=None, document_type=None, publication_name=None, publication_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 = QModel.objects.get( project=project, ontology=ontology, proxy=proxy, guid=publication_name, ) except (ValueError, QModel.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.order_by("created") if publication_version: # if a version was specified, look for that specific publication... try: publication = publications.get(version=publication_version) except QPublication.DoesNotExist: msg = "Unable to find model published at version {0}".format(publication_version) return q_error(request, msg) else: # if no version was specified, just get the latest one... publication = publications.last() return HttpResponse(publication.content, content_type="text/xml")
def q_project(request, project_name=None): context = add_parameters_to_context(request) try: project = QProject.objects.get(name=project_name) except QProject.DoesNotExist: if not project_name: msg = u"Please specify a project name." else: msg = u"Unable to locate project '%s'" % (project_name) return q_error(request, error_msg=msg) if not project.is_active: msg = u"This project has been disabled." return q_error(request, error_msg=msg) # work out user roles... project_authenticated = project.authenticated current_user = request.user can_view = True # is_member_of(current_user, project) or not project_authenticated can_edit = not project_authenticated or (is_user_of(current_user, project) or is_admin_of(current_user, project)) can_customize = not project_authenticated or is_admin_of(current_user, project) can_join = current_user.is_authenticated() and not (is_member_of(current_user, project) and is_user_of(current_user, project) and is_admin_of(current_user, project)) can_delete = is_admin_of(current_user, project) # TODO: # has_published = project.models.published_documents().count() > 0 has_published = project.models_bak.filter(is_document=True, is_root=True, is_published=True).count() > 0 # gather all the extra information required by the template _dict = { "project": project, "can_customize": can_customize, "can_edit": can_edit, "can_view": can_view, "can_join": can_join, "can_delete": can_delete, "has_published": has_published, } return render_to_response('questionnaire/q_project.html', _dict, context_instance=context)
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")
def __call__(self, request, *args, **kwargs): """ this lil wrapper fn just exists to catch any validation errors in get_object below (otherwise, "super.__call__" will ignore the error and still generate a feed) :param request: :param args: :param kwargs: :return: """ try: response = super(QFeed, self).__call__(request, *args, **kwargs) return response except QError as e: return q_error(request, e.msg)
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_edit_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, model_customization, 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_user_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) realization objects from the cache... session_key = get_key_from_request(request) cached_realizations_key = "{0}_realizations".format(session_key) model_realization = get_or_create_cached_object(request.session, cached_realizations_key, get_new_realizations, **{ "project": project, "ontology": ontology, "model_proxy": model_proxy, "key": model_proxy.name, } ) if current_user.is_authenticated(): set_owner(model_realization, evaluate_lazy_object(current_user)) model_realization.is_root = True # TODO: COME UP W/ A BETTER WAY OF DEALING W/ "is_root" # no forms are created here, # instead the load-on-demand paradigm is used, # work out various paths, so that ng can reload things as needed... view_url_dirname = request.path.rsplit('/', 1)[0] api_url_dirname = reverse("realization-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, "realization": model_realization, "read_only": "false", # passing "false" instead of False b/c this is a JS variable } return render_to_response('questionnaire/q_edit.html', template_context, context_instance=context)
def q_edit_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, model_customization, 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_user_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) realization objects from the cache... session_key = get_key_from_request(request) # # no need to cache customizations; I access them as needed during form creation # # cached_customizations_key = "{0}_customizations".format(session_key) # # model_customization = get_or_create_cached_object(request.session, cached_customizations_key, # # get_existing_customizations, # # **{ # # "project": project, # # "ontology": ontology, # # "model_proxy": model_proxy, # # "customization_id": customization.id, # # } # # ) cached_realizations_key = "{0}_realizations".format(session_key) model_realization = get_or_create_cached_object(request.session, cached_realizations_key, get_new_realizations, **{ "project": project, "ontology": ontology, "model_proxy": model_proxy, "key": model_proxy.name, "customization": model_customization, } ) if current_user.is_authenticated(): set_owner(model_realization, evaluate_lazy_object(current_user)) # TODO: THIS IS A ONE-OFF TO GET ME THROUGH THE MEDIUM-TERM # TODO: IN THE LONG-TERM I OUGHT TO FIGURE OUT HOW TO AUTOMATICALLY WORK OUT HOW/WHEN TO SET "is_root" # TODO: (MOST LIKELY IT SHOULD BE IN "Q.questionnaire.models.models_realizations.QModel#reset") model_realization.is_root = True # no need to generate any forms or formsets; I do that all via the load-on-demand paradigm # 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("realization-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, "realization": model_realization, "customization": model_customization, "read_only": "false", } return render_to_response('questionnaire/q_edit.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, 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_edit_existing(request, project_name=None, ontology_key=None, document_type=None, realization_pk=None): # save any request parameters... # (in case of redirection) context = add_parameters_to_context(request) # check the arguments... validity, project, ontology, model_proxy, model_customization, 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_user_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) realization objects from the cache... # note that unlike in "q_edit_new" above, this bit is enclosed in a try/catch block try: session_key = get_key_from_request(request) cached_realizations_key = "{0}_realizations".format(session_key) model_realization = get_or_create_cached_object(request.session, cached_realizations_key, get_existing_realizations, **{ "project": project, "ontology": ontology, "model_proxy": model_proxy, "model_id": realization_pk } ) except ObjectDoesNotExist: msg = "Cannot find a document with an id of '{0}' for that project/ontology/document type combination.".format(realization_pk) return q_error(request, msg) # no forms are created here, # instead the load-on-demand paradigm is used, # work out various paths, so that ng can reload things as needed... # (notice these are slightly different than in "q_edit_new" above view_url_dirname = request.path.rsplit('/', 1)[0] api_url_dirname = reverse("realization-detail", kwargs={"pk": model_realization.pk}).rsplit('/', 2)[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, "realization": model_realization, "read_only": "false", # passing "false" instead of False b/c this is a JS variable } return render_to_response('questionnaire/q_edit.html', template_context, 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)
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)
def q_view_existing(request, project_name=None, ontology_key=None, document_type=None, realization_pk=None): # save any request parameters... # (in case of redirection) context = add_parameters_to_context(request) # check the arguments... validity, project, ontology, model_proxy, model_customization, msg = validate_view_arguments( project_name=project_name, ontology_key=ontology_key, document_type=document_type ) if not validity: return q_error(request, msg) # no need to check authentication # get (or set) realization objects from the cache... # note that unlike in "q_edit_new" above, this bit is enclosed in a try/catch block # this is to deal w/ the possibility of an invalid realization_pk try: session_key = get_key_from_request(request) cached_realizations_key = "{0}_realizations".format(session_key) model_realization = get_or_create_cached_object(request.session, cached_realizations_key, get_existing_realizations, **{ "project": project, "ontology": ontology, "model_proxy": model_proxy, "model_id": realization_pk, } ) except ObjectDoesNotExist: msg = "Cannot find a document with an id of '{0}' for that project/ontology/model combination.".format( realization_pk) return q_error(request, msg) # no need to generate any forms or formsets; I do that all via the load-on-demand paradigm # work out the various paths, # so that ng can reload things as needed # (notice these are slightly different than in "q_edit_new" above 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("realization-detail", kwargs={"pk": model_realization.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), "project": project, "ontology": ontology, "proxy": model_proxy, "realization": model_realization, "customization": model_customization, "read_only": "true", } return render_to_response('questionnaire/q_view.html', _dict, context_instance=context)
def q_get_existing(request, project_name=None, ontology_key=None, document_type=None): """ this is meant to be used from external requests (ie: further_info_url) where uniquely identifying model fields (including pk) are passed if a unique realization cannot be found then an error is returned otherwise the response is routed to "q_edit_existing" :param request: :param project_name: :param ontology_key: :param document_type: :param realization_pk: :return: """ # check the arguments... validity, project, ontology, model_proxy, model_customization, msg = validate_view_arguments( project_name=project_name, ontology_key=ontology_key, document_type=document_type ) if not validity: return q_error(request, msg) model_realizations = QModelRealization.objects.filter(project=project, proxy=model_proxy) additional_parameters = request.GET.copy() for key, value in additional_parameters.iteritems(): if key == "pk" or key == "guid": try: return HttpResponseRedirect(reverse("edit_existing", kwargs={ "project_name": project_name, "ontology_key": ontology_key, "document_type": document_type, "realization_pk": model_realizations.get(**{key: value}).pk })) except (ObjectDoesNotExist, ValueError): msg = "There is no '{0}' document with a {1} of '{2}'".format(model_proxy, key, value) return q_error(request, msg) else: try: property_proxy = model_proxy.property_proxies.get(name=key) if property_proxy.field_type == "ATOMIC": model_realizations = model_realizations.filter(properties__proxy=property_proxy).has_atomic_value(value) elif property_proxy.field_type == "ENUMERATION": formatted_values = [fv for fv in map(lambda v: v.strip(), value.split(',')) if fv] model_realizations = model_realizations.filter(properties__proxy=property_proxy).has_enumeration_values(formatted_values) else: # property_proxy_field_type == "RELATIONSHIP" # TODO: msg = "Unable to support getting a document by relationship_field" return q_error(request, msg) except ObjectDoesNotExist: msg = "There is no '{0}' property for the '{0}' document_type".format(key, model_proxy) return q_error(request, msg) if model_realizations.count() != 1: msg = "Unable to uniquely identify '{0}' document_type with the following properties: '{1}'".format( model_proxy, ", ".join(["{0}: {1}".format(p[0], p[1]) for p in additional_parameters.items()]) ) return q_error(request, msg) return HttpResponseRedirect(reverse("edit_existing", kwargs={ "project_name": project_name, "ontology_key": ontology_key, "document_type": document_type, "realization_pk": model_realizations.first().pk }))
def q_view_existing(request, project_name=None, ontology_key=None, document_type=None, realization_pk=None): """ this is exactly the same as "q_edit_existing" except: there are no authentication checks, the template_context & template are different. :param request: :param project_name: :param ontology_key: :param document_type: :param realization_pk: :return: """ # save any request parameters... # (in case of redirection) context = add_parameters_to_context(request) # check the arguments... validity, project, ontology, model_proxy, model_customization, msg = validate_view_arguments( project_name=project_name, ontology_key=ontology_key, document_type=document_type ) if not validity: return q_error(request, msg) # no need to check authentication # get (or set) realization objects from the cache... # note that unlike in "q_edit_new" above, this bit is enclosed in a try/catch block try: session_key = get_key_from_request(request) cached_realizations_key = "{0}_realizations".format(session_key) model_realization = get_or_create_cached_object(request.session, cached_realizations_key, get_existing_realizations, **{ "project": project, "ontology": ontology, "model_proxy": model_proxy, "model_id": realization_pk } ) except ObjectDoesNotExist: msg = "Cannot find a document with an id of '{0}' for that project/ontology/document type combination.".format(realization_pk) return q_error(request, msg) # no forms are created here, # instead the load-on-demand paradigm is used, # work out various paths, so that ng can reload things as needed... # (notice these are slightly different than in "q_edit_new" above view_url_dirname = request.path.rsplit('/', 1)[0] api_url_dirname = reverse("realization-detail", kwargs={"pk": model_realization.pk}).rsplit('/', 2)[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, "realization": model_realization, "read_only": "true", # passing "true" instead of True b/c this is a JS variable } return render_to_response('questionnaire/q_view.html', template_context, context_instance=context)