def display_xml(request, identifier): """ Used for displaying DataCite or Crossref XML """ d = {'menu_item': 'ui_manage.null'} r = _getLatestMetadata(identifier, request) if type(r) is str: django.contrib.messages.error(request, uic.formatError(r)) return redirect("/") s, id_metadata = r assert s.startswith("success:") d['identifier'] = id_metadata d['current_profile'] = metadata.getProfile(id_metadata['_profile']) if d['current_profile'].name == 'datacite' and 'datacite' in id_metadata: content = id_metadata["datacite"] elif d['current_profile'].name == 'crossref' and 'crossref' in id_metadata: content = id_metadata["crossref"] else: return uic.staticTextResponse("No XML metadata.") # By setting the content type ourselves, we gain control over the # character encoding and can properly set the content length. ec = content.encode("UTF-8") r = django.http.HttpResponse(ec, content_type="application/xml; charset=UTF-8") r["Content-Length"] = len(ec) return r
def details(request): """ ID Details page for a given ID """ d = {'menu_item': 'ui_manage.null'} d["testPrefixes"] = uic.testPrefixes identifier = request.path_info[len("/id/"):] r = _getLatestMetadata(identifier, request, prefixMatch=(request.GET.get( "prefix_match", "no").lower() == "yes")) if type(r) is str: django.contrib.messages.error( request, uic.formatError(r + ": " + identifier)) # ToDo: Pass details in from previous screen so we know where to send redirect back to if userauth.getUser(request) == None: return redirect("ui_search.index") else: return redirect("ui_home.index") s, id_metadata = r assert s.startswith("success:") if " in_lieu_of " in s: newid = s.split()[1] django.contrib.messages.info( request, "Identifier %s returned in lieu of %s." % (newid, identifier)) return redirect("/id/" + urllib.quote(newid, ":/")) d['allow_update'] = policy.authorizeUpdateLegacy( userauth.getUser(request, returnAnonymous=True), id_metadata["_owner"], id_metadata["_ownergroup"]) d['identifier'] = id_metadata d['id_text'] = s.split()[1] d['id_as_url'] = util2.urlForm(d['id_text']) d['is_test_id'] = _isTestId(d['id_text'], d['testPrefixes']) d['internal_profile'] = metadata.getProfile('internal') d['target'] = id_metadata['_target'] d['current_profile'] = metadata.getProfile(id_metadata['_profile']) or\ metadata.getProfile('erc') d['recent_creation'] = identifier.startswith('doi') and \ (time.time() - float(id_metadata['_created']) < 60 * 30) d['recent_update'] = identifier.startswith('doi') and \ (time.time() - float(id_metadata['_updated']) < 60 * 30) if d['current_profile'].name == 'datacite' and 'datacite' in id_metadata: r = datacite.dcmsRecordToHtml(id_metadata["datacite"]) if r: d['datacite_html'] = r if d['current_profile'].name == 'crossref' and 'crossref' in id_metadata and \ id_metadata['crossref'].strip() != "": d['has_crossref_metadata'] = True t_stat = [x.strip() for x in id_metadata['_status'].split("|", 1)] d['pub_status'] = t_stat[0] if t_stat[0] == 'unavailable' and len(t_stat) > 1: d['stat_reason'] = t_stat[1] if t_stat[0] == 'public' and identifier.startswith("ark:/"): d['schemaDotOrgMetadata'] = _schemaDotOrgMetadata( mapping.map(id_metadata), d['id_as_url']) d['has_block_data'] = uic.identifier_has_block_data(id_metadata) d['has_resource_type'] = True if (d['current_profile'].name == 'datacite' \ and 'datacite.resourcetype' in id_metadata \ and id_metadata['datacite.resourcetype'] != '') else False return uic.render(request, "manage/details", d)
def login(request): """ Renders the login page (GET) or processes a login form submission (POST). A successful login redirects to the URL specified by ?next=... or, failing that, the home page. """ d = {"menu_item": "ui_null.null"} if request.method == "GET": if "next" in request.GET: try: m = django.core.urlresolvers.resolve(request.GET["next"]) if m.app_name == "admin": messages.error( request, _("You must be logged in as an administrator to view this page." ), ) except django.core.urlresolvers.Resolver404: pass d["next"] = request.GET["next"] else: d["next"] = django.core.urlresolvers.reverse("ui_home.index") return uic.render(request, "account/login", d) elif request.method == "POST": if ("username" not in request.POST or "password" not in request.POST or "next" not in request.POST): return uic.badRequest(request) d.update(uic.extract(request.POST, ["username", "password", "next"])) user = userauth.authenticate(d["username"], d["password"], request) if type(user) is str: messages.error(request, uic.formatError(user)) return uic.render(request, "account/login", d) if user != None: # 'extra_tags' used for recording a Google Analytics event messages.add_message( request, messages.SUCCESS, _("Login successful."), extra_tags='Accounts Submit Login', ) if django.utils.http.is_safe_url(url=d["next"], host=request.get_host()): return redirect(d["next"]) else: return redirect("ui_home.index") else: messages.error(request, _("Login failed.")) return uic.render(request, "account/login", d) else: return uic.methodNotAllowed(request)
def edit(request, identifier): """ Edit page for a given ID """ d = {'menu_item': 'ui_manage.null'} d["testPrefixes"] = uic.testPrefixes r = _getLatestMetadata(identifier, request) if type(r) is str: django.contrib.messages.error(request, uic.formatError(r)) return redirect("ui_manage.index") s, id_metadata = r if not policy.authorizeUpdateLegacy( userauth.getUser(request, returnAnonymous=True), id_metadata["_owner"], id_metadata["_ownergroup"], ): django.contrib.messages.error( request, _( "You are not allowed to edit this identifier. " + "If this ID belongs to you and you'd like to edit, please log in." ), ) return redirect("/id/" + urllib.quote(identifier, ":/")) d['identifier'] = id_metadata t_stat = [x.strip() for x in id_metadata['_status'].split("|", 1)] d['pub_status'] = t_stat[0] d['orig_status'] = t_stat[0] d['stat_reason'] = None if t_stat[0] == 'unavailable' and len(t_stat) > 1: d['stat_reason'] = t_stat[1] d['export'] = id_metadata['_export'] if '_export' in id_metadata else 'yes' d['id_text'] = s.split()[1] d['id_as_url'] = util2.urlForm(d['id_text']) d['internal_profile'] = metadata.getProfile('internal') d['profiles'] = metadata.getProfiles()[1:] if request.method == "GET": d['is_test_id'] = _isTestId(d['id_text'], d['testPrefixes']) if '_profile' in id_metadata: d['current_profile'] = metadata.getProfile(id_metadata['_profile']) else: d['current_profile'] = metadata.getProfile('dc') if d['current_profile'].name == 'datacite' and 'datacite' in id_metadata: d = _assignManualTemplate(d) # Testing # xml = datacite_xml.temp_mockxml() # form_coll = datacite_xml.dataciteXmlToFormElements(xml) form_coll = datacite_xml.dataciteXmlToFormElements( d['identifier']['datacite'] ) # This is the only item from internal profile that needs inclusion in django form framework form_coll.nonRepeating['target'] = id_metadata['_target'] d['form'] = form_objects.getIdForm_datacite_xml(form_coll, request) if not form_objects.isValidDataciteXmlForm(d['form']): django.contrib.messages.error(request, FORM_VALIDATION_ERROR_ON_LOAD) else: if "form_placeholder" not in d: d['form_placeholder'] = None d['form'] = form_objects.getIdForm( d['current_profile'], d['form_placeholder'], id_metadata ) if not d['form'].is_valid(): django.contrib.messages.error(request, FORM_VALIDATION_ERROR_ON_LOAD) elif request.method == "POST": P = request.POST d['pub_status'] = P['_status'] if '_status' in P else d['pub_status'] d['stat_reason'] = P['stat_reason'] if 'stat_reason' in P else d['stat_reason'] d['export'] = P['_export'] if '_export' in P else d['export'] ''' Profiles could previously be switched in edit template, thus generating posibly two differing profiles (current vs original). So we previously did a check here to confirm current_profile equals original profile before saving.''' d['current_profile'] = metadata.getProfile( P.get('original_profile', d['identifier']['_profile']) ) if P['_status'] == 'unavailable': stts = P['_status'] + " | " + P['stat_reason'] else: stts = P['_status'] if d['current_profile'].name == 'datacite' and 'datacite' in id_metadata: d = _assignManualTemplate(d) d = ui_create.validate_adv_form_datacite_xml(request, d) if 'id_gen_result' in d: return uic.render(request, 'manage/edit', d) # ID Creation page else: assert 'generated_xml' in d to_write = { "_profile": 'datacite', '_target': P['target'], "_status": stts, "_export": d['export'], "datacite": d['generated_xml'], } s = ezid.setMetadata( P['identifier'], userauth.getUser(request, returnAnonymous=True), to_write, ) if s.startswith("success:"): _alertMessageUpdateSuccess(request) return redirect("/id/" + urllib.quote(identifier, ":/")) else: _alertMessageUpdateError(request, s) else: """ Even if converting from simple to advanced, let's make sure forms validate and update identifier first, else don't upgrade. """ d['form'] = form_objects.getIdForm(d['current_profile'], None, P) if d['form'].is_valid(): result = _updateEzid(request, d, stts) if not result.startswith("success:"): d['current_profile'] = metadata.getProfile(id_metadata['_profile']) _alertMessageUpdateError(request, result) return uic.render(request, "manage/edit", d) else: if 'simpleToAdvanced' in P and P['simpleToAdvanced'] == 'True': # Convert simple ID to advanced (datacite with XML) result = _updateEzid(request, d, stts, id_metadata) r = _getLatestMetadata(identifier, request) if type(r) is str: django.contrib.messages.error(request, uic.formatError(r)) return redirect("ui_manage.index") s, id_metadata = r if not result.startswith("success:"): # if things fail, just display same simple edit page with error _alertMessageUpdateError(request, result) else: _alertMessageUpdateSuccess(request) return redirect("/id/" + urllib.quote(identifier, ":/")) else: _alertMessageUpdateSuccess(request) return redirect("/id/" + urllib.quote(identifier, ":/")) else: return uic.methodNotAllowed(request) return uic.render(request, "manage/edit", d)
def tombstone(request): """ Renders a tombstone (i.e., unavailable identifier) page. """ if request.method != "GET": return uic.methodNotAllowed(request) assert request.path_info.startswith("/tombstone/id/") id = request.path_info[14:] if "auth" in request.session: r = ezid.getMetadata(id, userauth.getUser(request, returnAnonymous=True)) else: r = ezid.getMetadata(id) if type(r) is str: messages.error(request, uic.formatError(r)) return uic.redirect("/") s, m = r assert s.startswith("success:") id = s[8:].strip() if not m["_status"].startswith("unavailable"): return uic.redirect("/id/%s" % urllib.quote(id, ":/")) status = m["_status"] reason = tombstone_text if "|" in m["_status"]: status = m["_status"].split("|", 1)[0].strip() # Translators: Output for tombstone page (unavailable IDs) reason += " " + _("Reason:") + " " + m["_status"].split("|", 1)[1].strip() htmlMode = False if m["_profile"] == "datacite" and "datacite" in m: md = datacite.dcmsRecordToHtml(m["datacite"]) if md: htmlMode = True root = lxml.etree.fromstring(md) # Tack on an additional row displaying status row = lxml.etree.Element("tr", attrib={"class": "dcms_element"}) c1 = lxml.etree.SubElement(row, "th", attrib={"class": "dcms_label"}) c1.text = _("Status:") c2 = lxml.etree.SubElement(row, "td", attrib={"class": "dcms_value"}) c2.text = status root.append(row) md = lxml.etree.tostring(root) if not htmlMode: # This echoes the Merritt hack above. if m["_profile"] == "erc" and m.get("erc", "").strip() != "": md = [{"label": "ERC", "value": m["erc"].strip()}] else: p = metadata.getProfile(m["_profile"]) if not p: p = metadata.getProfile("erc") md = [] for e in p.elements: if "." not in e.name: continue v = m.get(e.name, "").strip() md.append({ "label": e.displayName, "value": v if v != "" else "(no value)" }) # Tack on an additional row displaying status md.append({"label": _("Status"), "value": status}) return uic.render( request, "tombstone", { "identifier": id, "identifierLink": "/id/%s" % urllib.quote(id, ":/"), "reason": reason, "htmlMode": htmlMode, "metadata": md })