def recordview(request): context = {} if request.GET.has_key('id'): #query = 'silo:"DataFinder" AND id:"' + request.GET['id'] + '"' query = 'id:"' + request.GET['id'] + '"' solr_query = SolrQuery(q=query) context['solr_response'] = solr_query.get_solrresponse() context['docs'] = context['solr_response'].get('docs',None) if len(context['docs'])<1: return redirect("/search/detailed") #id = list(set([id.text for id in sid if id.text.startswith("ww")]))[0] context['doi'] ="" if len(context['docs']) >0 and ('identifier' in context['docs'][0]) and (len(context['docs'][0]['identifier']) > 1): context['doi'] = context['docs'][0]['identifier'][1] if len(context['docs']) >0 and 'creator' in context['docs'][0]: creators = context['docs'][0]['creator'] context['creators_list'] = "" for creator in creators: context['creators_list'] = context['creators_list'] + creator + ', ' context['creators_list'] = context['creators_list'][:-2] context['subjects_list'] = "" if 'subject' in context['docs'][0]: subjects = context['docs'][0]['subject'] for subject in subjects: context['subjects_list'] = context['subjects_list'] + subject + ', ' context['subjects_list'] = context['subjects_list'][:-2] context['keywords_list'] = "" if 'keyword' in context['docs'][0]: keywords = context['docs'][0]['keyword'] for keyword in keywords: context['keywords_list'] = context['keywords_list'] + keyword + ', ' context['keywords_list'] = context['keywords_list'][:-2] #if context['creator'].endswith("rdf"): # context['creator'] = context['creator'].replace('https://databank.ora.ox.ac.uk/ww1archives/datasets/person/','') # context['creator'] = context['creator'].replace('.rdf','') # creator_list = context['creator'].split('-',1) # context['creator'] = creator_list[1] + ' ' + creator_list[0] #list(set([id for id in context['docs']['identifier'] if id.startswith("doi:")]))[0] context['doi'] = context['doi'].replace("doi:",'') if context['doi'].endswith('pdf'): context['doi'] = context['doi'] else: context['doi'] = "http://dx.doi.org/" + context['doi'] return render_to_response('record_view.html',context, context_instance=RequestContext(request))
def home(request): context = {} end = date.today() start = end-timedelta(days=7) #SolrQuery(self, query_filter = "" , q = "*:*" , req_format="json") q1 = "*:*" solr_query = SolrQuery(q=q1) context['numFound'] = solr_query.get_NumRecordsFound() q2 = "timestamp:["+ str(start) + "T00:00:00Z" + " TO "+ str(end) + "T00:00:00Z" + "]" solr_query = SolrQuery(q=q2) context['numFoundThisWeek'] = solr_query.get_NumRecordsFound() return render_to_response('home.html',context, context_instance=RequestContext(request))
def myrecords(request): context={} try: # A user needs to be authenticated to be able to view my records page in the DataFinder # Test if the user is now a university authenticated user if 'DF_USER_SSO_ID' not in request.session: return redirect("/login?redirectPath=myrecords") status="" if request.GET.has_key('status'): status = request.GET['status'] http_method = request.environ['REQUEST_METHOD'] if http_method == "GET": if status == 'All' or status == "": query = 'depositor:"' + request.session['DF_USER_SSO_ID'] + '"' else: query = 'depositor:"' + request.session['DF_USER_SSO_ID'] + '"' + ' AND ' 'status:"' + status + '"' solr_query = SolrQuery(q=query) context['solr_response'] = solr_query.get_solrresponse() except Exception, e: raise
def administration(request): # A user needs to be authenticated and authorized to be able to administer the DataFinder # Test if the user is now a university authenticated user if 'DF_USER_SSO_ID' not in request.session: return redirect("/login?redirectPath=admin") # Test if the user is Data Finder authorised user if request.session['DF_USER_ROLE'] != "admin" : return redirect("/") context = { 'registered_sources':[], 'unregistered_sources':[], } status = "" if request.GET.has_key('status'): status = request.GET['status'] http_method = request.environ['REQUEST_METHOD'] if status == 'All' or status == "": query = 'silo:"DataFinder"' else: query = 'silo:"DataFinder" AND status:"' + status + '"' solr_query = SolrQuery(q=query) context['solr_response'] = solr_query.get_solrresponse() if request.GET.has_key('message'): context["message"]=request.GET['message'] if request.GET.has_key('status'): context["status"]=request.GET['status'] # Need to have an 'admin' role within DF to be able to administer the DataFinder #src = settings.get("main:granary.uri_root") #host = settings.get("main:granary.host") #user_name = 'admin' #password = '******' #host = "192.168.2.230" #datastore = HTTPRequest(endpointhost=host) #datastore.setRequestUserPass(endpointuser=user_name, endpointpass=password) #(resp, respdata) = datastore.doHTTP_GET(resource="/silos", expect_type="application/JSON") #sources = json.loads(respdata) #registered_sources ={} #for source in sources: # (resp, respdata) = datastore.doHTTP_GET(resource='/' + source + '/states', expect_type="application/JSON") # state_info = json.loads(respdata) # registered_sources[source] = [source, len(state_info['datasets'])] #context['source_infos']=source_infos #unregistered_sources ={} print context try: #s_q= meta.Session.query(SourceInfo.silo).filter(SourceInfo.activate == False) sources= SourceInfo.objects.all() for src in sources: if src.activate == True: context['registered_sources'].append(src) else: context['unregistered_sources'].append(src) #print "Unregistered sources" #print context['unregistered_sources'] except Exception: raise #logger.exception("Failed to mark submission as failed") return render_to_response('administration.html',context, context_instance=RequestContext(request)) #print context return render_to_response('administration.html',context, context_instance=RequestContext(request))
def contribute(request): try: # A user needs to be authenticated to be able to contribute a record the DataFinder # Test if the user is now a university authenticated user if 'DF_USER_SSO_ID' not in request.session: return redirect("/login?redirectPath=contribute") context = {} literals = {} resources = {} project = {} person = {} if request.GET.has_key('message'): context["message"]=request.GET['message'] if request.GET.has_key('status'): context["status"]=request.GET['status'] http_method = request.environ['REQUEST_METHOD'] referrer = request.environ['HTTP_REFERER'] if http_method == "GET": if request.GET.has_key('id'): context['id'] = request.GET['id'] context['referrer'] = referrer query = 'id:"' + request.GET['id'] + '"' solr_query = SolrQuery(q=query) context['solr_response'] = solr_query.get_solrresponse() #context['docs'] = context['solr_response'].get('docs',None) return render_to_response('contribute.html', context, context_instance=RequestContext(request)) elif http_method == "POST": if request.POST.has_key('record_title'): BASE = settings.get("main:manifest.about") identifier = request.POST['record_title'] subject = URIRef(BASE+identifier) literals[DCTERMS['title']]=request.POST['record_title'] literals[DCTERMS['alternative']]=request.POST['alt_title'] literals[DCTERMS['description']]=request.POST['data_description'] project[DCTERMS['title']]=request.POST['project_name'] project[DCTERMS['description']]=request.POST['project_desc'] project[DCTERMS['URI']]=request.POST['project_website'] subjects = request.POST.getlist('subject_area') #for subj in subjects: literals[DCTERMS['subject']]= subjects keywords = request.POST.getlist('record_keyword') #for keyword in keywords: literals[DCTERMS['keywords']]=keywords literals[DCTERMS['language']]=request.POST['main_language'] if request.POST.has_key('ChangeRecordStatus'): literals[DCTERMS['status']]=request.POST['ChangeRecordStatus'] else: literals[DCTERMS['status']]='AwaitingReview' literals[OXDS['depositor']] = request.session['DF_USER_SSO_ID'] i = 1 while True: if request.GET.has_key('author_firstname_' + str(i)): person_title = request.POST['author_firstname_' + str(i)] + '-' +request.POST['author_middlename_' + str(i)] + '-' +request.POST['author_lastname_' + str(i)] person[FOAF["givenName"]]=request.POST['author_firstname_' + str(i)] person[FOAF["middleName"]]=request.POST['author_middlename_' + str(i)] person[OXDS["DisplayEmail"]]=request.POST['author_middlename_check_' + str(i)] person[FOAF["familyName"]]=request.POST['author_lastname_' + str(i)] person[FOAF["mbox"]]=request.POST['author_email_' + str(i)] person[OXDS['role']]=request.POST['author_role_' + str(i)] author_member_of_oxford = request.POST['author_mof_' + str(i)] person[FOAF["Organization"]]=request.POST['author_institution_' + str(i)] if author_member_of_oxford == True: person[OXDS["Faculty"]]=request.POST['author_faculty_' + str(i)] person[OXDS["OxfordCollege"]]=request.POST['author_oxfordcollege_'+ str(i)] person[OXDS["scheme"]]=request.POST['author_scheme_' + str(i)] person[OXDS["AUTHOR_ID"]]=request.POST['author_id_' + str(i)] else: person[OXDS["Faculty"]]=request.POST['author_faculty_text_' + str(i)] people_path = settings.get("main:granary.people_path") people_manifest_filename = os.path.join(people_path, 'people.rdf') people_manifest = bind_namespaces(rdflib.ConjunctiveGraph()) try: with open(people_manifest_filename, 'r') as f: people_manifest.parse(f, base=people_manifest_filename) except IOError, e: pass if people_manifest.value(FOAF[person_title], None, None) == None: people_manifest.add((FOAF[person_title], RDF.type, FOAF.Person)) for key, value in person.items(): people_manifest.add((FOAF[person_title], key, Literal(value))) with open(people_manifest_filename, 'w') as f: people_manifest.serialize(f, 'better-pretty-xml', base=people_manifest_filename) os.chown(people_manifest_filename, getpwnam('www-data').pw_uid, getpwnam('www-data').pw_gid) i = i + 1 else: break #literals[OXDS['contact']] = request.POST['record_contact'] #Save the contact details person_or_role = request.POST['person_or_role'] person_title="" if person_or_role == "is_role": person_title = request.POST['record_title'] else: person_title = request.POST['contact_firstname'] + '-' +request.POST['contact_middlename'] + '-' +request.POST['contact_lastname'] person[FOAF["givenName"]]=request.POST['contact_firstname'] person[FOAF["middleName"]]=request.POST['contact_middlename'] if request.GET.has_key('contact_middlename_check'): person[OXDS["DisplayEmail"]]=request.POST['contact_middlename_check'] person[FOAF["familyName"]]=request.POST['contact_lastname'] person[FOAF["mbox"]]=request.POST['contact_email'] person[OXDS['role']]=request.POST['contact_role'] member_of_oxford = request.POST['contact_mof'] person[FOAF["Organization"]]=request.POST['contact_institution'] if member_of_oxford == True: person[OXDS["Faculty"]]=request.POST['contact_faculty'] person[OXDS["OxfordCollege"]]=request.POST['contact_oxfordcollege'] person[OXDS["scheme"]]=request.POST['contact_scheme'] person[OXDS["AUTHOR_ID"]]=request.POST['contact_id'] else: person[OXDS["Faculty"]]=request.POST['contact_faculty_text'] people_path = settings.get("main:granary.people_path") people_manifest_filename = os.path.join(people_path, 'people.rdf') people_manifest = bind_namespaces(rdflib.ConjunctiveGraph()) try: with open(people_manifest_filename, 'r') as f: people_manifest.parse(f, base=people_manifest_filename) except IOError, e: pass if people_manifest.value(FOAF[person_title], None, None) == None: people_manifest.add((FOAF[person_title], RDF.type, FOAF.Person)) for key, value in person.items(): people_manifest.add((FOAF[person_title], key, Literal(value))) with open(people_manifest_filename, 'w') as f: people_manifest.serialize(f, 'better-pretty-xml', base=people_manifest_filename) os.chown(people_manifest_filename, getpwnam('www-data').pw_uid, getpwnam('www-data').pw_gid) #end of saving contact details #literals[OXDS['isEmbargoed']] = 'False' if request.POST.has_key('data-format'): literals[OXDS['isDigital']]=request.POST['data-format'] literals[OXDS['loc-format']]=request.POST['loc-format'] if request.POST['loc-format'] =="URL": resources[OXDS['DataLocation']]=request.POST['data_location'] else: literals[OXDS['DataLocation']]=request.POST['data_location'] if request.POST['doc-format'] =="URL": resources[OXDS['methodology']]=request.POST['digital_publisher_doc'] else: literals[OXDS['methodology']]=request.POST['digital_publisher_doc'] literals[DCTERMS['publisher']]=request.POST['publisher'] literals[DCTERMS['issued']]=request.POST['publication_year'] literals[OXDS['temporal_choice']]=request.POST['temporal_choice'] if request.POST['temporal_choice'] == 'single_date': literals[OXDS['dataTemporalCoverage']]=request.POST['single_temporal_date'] if request.POST['temporal_choice'] == 'date_range': literals[OXDS['dataCoverageStart']]=request.POST['start_date_range'] literals[OXDS['dataCoverageEnd']]=request.POST['end_date_range'] literals[OXDS['data_collected_temporal_choice']]=request.POST['data_collected_temporal_choice'] if request.POST['data_collected_temporal_choice'] == 'data_collected_single_date': literals[OXDS['dataCollectedCoverage']]=request.POST['data_collected_single_temporal_date'] if request.POST['data_collected_temporal_choice'] == 'data_collected_date_range': literals[OXDS['dataCollectedStart']]=request.POST['data_collected_start_date_range'] literals[OXDS['dataCollectedEnd']]=request.POST['data_collected_end_date_range'] literals[DCTERMS['Location']]=request.POST['geo_location'] literals[GEO['lat']]=request.POST['lat'] literals[GEO['lng']]=request.POST['lng'] if literals[OXDS['isDigital']] == "yes": literals[BIBO['doi']]=request.POST['digital_object_identifier'] literals[DCTERMS['type']]=request.POST['digital_resource_type'] literals[OXDS['Filesize']]=request.POST['digital_filesize'] literals[DCTERMS['format']]=request.POST['digital_format'] literals[OXDS['currentVersion']]=request.POST['digital_version'] funded_research = request.POST['funded_research'] literals[OXDS['funded_research']]=request.POST['funded_research'] if funded_research == "yes": funding_agencies = request.POST.getlist('funding_agency') literals[FUND['FundingBody']]=funding_agencies grant_numbers = request.POST.getlist('grant_number') literals[FUND['grantNumber']]=grant_numbers # #if request.POST['dmp_loc'] =="URL": # resources[OXDS['data_management_plan_location']]=request.POST['data_management_plan_location'] #else: # literals[OXDS['data_management_plan_location']]=request.POST['data_management_plan_location'] #context["publication_relationship_of_data_1"]=request.GET['publication_relationship_of_data_1'] #context["related_publication_url_1"]=request.GET['related_publication_url_1'] #context["dataset_relationship_of_data_1"]=request.GET['dataset_relationship_of_data_1'] #context["dataset_related_url_1"]=request.GET['dataset_related_url_1'] #context["deposit_data_file_upload"]=request.POST['deposit_data_file_upload'] #context["deposit_data_embargo_options"]=request.POST['deposit_data_embargo_options'] #context["deposit_data_embrago_release_date"]=request.POST['deposit_data_embrago_release_date'] #context["deposit_data_embargo_options_type"]=request.POST['deposit_data_embargo_options_type'] #context["deposit_data_embargo_reason"]=request.POST['deposit_data_embargo_reason'] literals[DCTERMS['rights']]=request.POST['terms_and_conditions'] #resources[OXDS['DataLocation']]=request.POST['data_location'] #prepared_licence - urls to licenses stored elsewhere #select_a_licence - urls of licens selted fromt he select dropdown #bespoke_licence - license pasted in as free text #other_licence - eg: none ie. when not associated with any license license = request.POST['license_radio'] literals[OXDS['license_radio']]=license if license =="prepared_licence" or license == 'select_a_licence': resources[DCTERMS['license']]=request.POST['data_standard_licence_URI'] else : literals[DCTERMS['license']]=request.POST['data_standard_licence_text'] if request.POST.has_key('embargo_options'): embargo_options = request.POST['embargo_options'] # embargo_options_list = ['embargo_status_unknown','embargoed_indefinitely'] literals[OXDS['embargoedUntil']]=request.POST['embargo_end_date'] #context["embargo-options"]=request.GET['embargo-options'] #context["subject_specific_metadata_file"]=request.GET['subject_specific_metadata_file'] #context["xml_schema_type"]=request.GET['xml_schema_type'] # Project RDF #projects_package = rdflib.URIRef(projects_path) projects_path = settings.get("main:granary.projects_path") projects_manifest_filename = os.path.join(projects_path, 'projects.rdf') projects_manifest = bind_namespaces(rdflib.ConjunctiveGraph()) # try: with open(projects_manifest_filename, 'r') as f: projects_manifest.parse(f, base=projects_manifest_filename) except IOError, e: pass for key, value in project.items(): # Will raise UniquenessError if >1 value returned, must be one or None if projects_manifest.value(None, DCTERMS['title'], Literal(value)) == None: projects_manifest.add((FUND[project[DCTERMS['title']]], key, Literal(value))) with open(projects_manifest_filename, 'w') as f: projects_manifest.serialize(f, 'better-pretty-xml', base=projects_manifest_filename) os.chown(projects_manifest_filename, getpwnam('www-data').pw_uid, getpwnam('www-data').pw_gid) resources[FUND['project']] = FUND[project[DCTERMS['title']]] # Record Manifest RDF records_path = settings.get("main:granary.records_path") records_package = rdflib.URIRef(records_path) #manifest_name = 'df_manifest_'+literals[DCTERMS['title']] +'.rdf' df_manifest_name = 'df_manifest.rdf' manifest_filename = os.path.join(records_path, df_manifest_name) main_manifest_filename = os.path.join(records_path, 'manifest.rdf') manifest = bind_namespaces(rdflib.ConjunctiveGraph()) #try: # with open(manifest_filename, 'w') as f: # manifest.parse(f, base=manifest_filename) #except IOError, e: # pass #for x in list(literals.keys()): # if literals[x] == "": # del literals[x] keys_with_valuelist = [ DCTERMS['subject'],DCTERMS['keywords'], FUND['FundingBody'],FUND['grantNumber'] ] for key, values in literals.items(): if key in keys_with_valuelist: for value in values: manifest.add((subject, key, Literal(value))) else: manifest.add((subject, key, Literal(values))) for key, res_uri in resources.items(): manifest.add((subject, key, res_uri)) with open(manifest_filename, 'w') as f: manifest.serialize(f, 'better-pretty-xml', base=manifest_filename) # try: # with open(main_manifest_filename, 'w') as f: # manifest.parse(f, base=main_manifest_filename) # except IOError, e: # pass user_name = settings.get("main:granary.uri_root_name") password = settings.get("main:granary.uri_root_pass") datastore = HTTPRequest(endpointhost=settings.get("main:granary.host")) datastore.setRequestUserPass(endpointuser=user_name, endpointpass=password) #(reqtype, reqdata) = SparqlQueryTestCase.encode_multipart_formdata([], []) # Create a dataset #(resp,respdata) = datastore.doHTTP_POST(reqdata, reqtype, resource="/DataFinder/datasets/" + identifier) #Submit the df_manefest as a file fields=[] df_manifest = open(manifest_filename).read() files = [("file", df_manifest_name, df_manifest, "application/rdf+xml")] (reqtype, reqdata) = SparqlQueryTestCase.encode_multipart_formdata(fields, files) (resp,respdata) = datastore.doHTTP_POST(reqdata, reqtype, resource="/DataFinder/datasets/" + identifier +"/") silo = settings.get("main:mainsilo") #solr_conn, b = settings.getSolrConnection() b = BroadcastToRedis('localhost', 'silochanges') b.creation(silo, identifier, ident=request.session['DF_USER_SSO_ID']) #Submit the main manifest file which as the see also fields=[] main_manifest = open(main_manifest_filename).read() files = [("file", "manifest.rdf", main_manifest, "application/rdf+xml")] (reqtype, reqdata) = SparqlQueryTestCase.encode_multipart_formdata(fields, files) (resp,respdata) = datastore.doHTTP_POST(reqdata, reqtype, resource="/DataFinder/datasets/" + identifier +"/") os.chown(manifest_filename, getpwnam('www-data').pw_uid, getpwnam('www-data').pw_gid) return render_to_response('contribute.html', context, context_instance=RequestContext(request))