def api_view(request, api_name): # logged in user c = Context() # duck-typed context object for Mako c.ident = request.user add_auth_info_to_context(request, c) if api_name not in ['silos', 'datasets', 'states', 'items']: # redirect(url(controller="api", action="apiview", api_name="silos")) return redirect("/api/silos") c.api_file = "%s_api.html"%api_name return render_to_response('/api.html', {'c':c})
def welcome(request): c = Context() # duck-typed context object for Mako c.came_from = request.GET.get('came_from', "/") c.ident = request.user if request.user.is_authenticated(): add_auth_info_to_context(request, c) request.session['user_id'] = request.user.username return redirect(c.came_from) else: destination = "/login?came_from=%s" % (c.came_from) return redirect(destination)
def login_handler(request): c = Context() # duck-typed context object for Mako c.came_from = request.GET.get('came_from', "/") if request.method == "POST": if request.POST.has_key('login') and request.POST.has_key('password'): username = request.POST['login'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) # Redirect to a success page. return redirect('/welcome') else: # Return a 'disabled account' error message request.session['login_flash'] = """Your account is currently disabled. Please contact the administrator.""" else: # Return an 'invalid login' error message. request.session['login_flash'] = """Wrong credentials. Have you been registered?""" return render_to_response("login.html", {'c':c, 'request':request})
def login_handler(request): c = Context() # duck-typed context object for Mako c.came_from = request.GET.get('came_from', "/") if request.method == "POST": if request.POST.has_key('login') and request.POST.has_key('password'): username = request.POST['login'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) # Redirect to a success page. return redirect('/welcome') else: # Return a 'disabled account' error message request.session[ 'login_flash'] = """Your account is currently disabled. Please contact the administrator.""" else: # Return an 'invalid login' error message. request.session[ 'login_flash'] = """Wrong credentials. Have you been registered?""" return render_to_response("login.html", {'c': c, 'request': request})
def index(request): # logged in user c = Context() # duck-typed context object for Mako c.ident = request.user add_auth_info_to_context(request, c) c.silos = list_silos() if settings.METADATA_EMBARGOED: #if not ident: # abort(401, "Not Authorised") if request.user.is_authenticated(): c.silos = authz(ident) else: return HttpResponseForbidden("Forbidden") c.silo_infos = {} for silo in c.silos: c.silo_infos[silo] = [] state_info = granary.describe_silo(silo) if 'title' in state_info and state_info['title']: c.silo_infos[silo].append(state_info['title']) else: c.silo_infos[silo].append(silo) c.silo_infos[silo].append(get_datasets_count(silo)) c.silo_infos[silo].append(getSiloModifiedDate(silo)) # conneg return accept_list = None if request.META.has_key('HTTP_ACCEPT'): try: accept_list = conneg_parse(request.META['HTTP_ACCEPT']) except: accept_list= [MT("text", "html")] if not accept_list: accept_list= [MT("text", "html")] mimetype = accept_list.pop(0) while(mimetype): if str(mimetype).lower() in ["text/html", "text/xhtml"]: return render_to_response('list_of_silos.html', {'c':c}) elif str(mimetype).lower() in ["text/plain", "application/json"]: response.content_type = 'application/json; charset="UTF-8"' response.status_int = 200 response.status = "200 OK" return HttpResponse(json.dumps(c.silos), mimetype='application/json; charset="UTF-8"') try: mimetype = accept_list.pop(0) except IndexError: mimetype = None return render_to_response('list_of_silos.html', {'c':c})
def dataset_view(request, siloname, id): if not granary.issilo(siloname): raise Http404 rdfsilo = granary.get_rdf_silo(siloname) if not rdfsilo.exists(id): raise Http404 c = Context() silo = siloname #tmpl_context variables needed: c.silo_name, c.zipfiles, c.ident, c.id, c.path c.silo_name = siloname c.id = id ident = request.user c.ident = ident.username dataset = rdfsilo.get_item(id) creator = None if dataset.manifest and dataset.manifest.state and 'metadata' in dataset.manifest.state and dataset.manifest.state['metadata'] and \ 'createdby' in dataset.manifest.state['metadata'] and dataset.manifest.state['metadata']['createdby']: creator = dataset.manifest.state['metadata']['createdby'] http_method = request.method if http_method == "GET": c.editor = False if settings.METADATA_EMBARGOED: if not ident: return HttpResponse("Not Authorised", status=401) silos = authz(ident) if silo not in silos: return HttpResponseForbidden("Forbidden") silos_admin = authz(ident, permission='administrator') silos_manager = authz(ident, permission='manager') if c.ident == creator or silo in silos_admin or silo in silos_manager: c.editor = True elif ident: silos = authz(ident) if silo in silos: silos_admin = authz(ident, permission='administrator') silos_manager = authz(ident, permission='manager') if ident['repoze.who.userid'] == creator or silo in silos_admin or silo in silos_manager: c.editor = True else: #identity management of item if not ident: return HttpResponse("Not Authorised", status=401) silos = authz(ident) if silo not in silos: return HttpResponseForbidden("Forbidden") silos_admin = authz(ident, permission='administrator') silos_manager = authz(ident, permission='manager') if not (c.ident == creator or silo in silos_admin or silo in silos_manager): return HttpResponseForbidden("Forbidden") if http_method == "GET": c.zipfiles = get_zipfiles_in_dataset(dataset) # conneg return accept_list = None if 'HTTP_ACCEPT' in request.META: try: accept_list = conneg_parse(request.META['HTTP_ACCEPT']) except: accept_list= [MT("text", "html")] if not accept_list: accept_list= [MT("text", "html")] mimetype = accept_list.pop(0) while(mimetype): if str(mimetype).lower() in ["text/html", "text/xhtml"]: return render_to_response("/list_of_zipfiles.html", {'c':c,}) elif str(mimetype).lower() in ["text/plain", "application/json"]: return HttpResponse(json.dumps(list(c.zipfiles.keys())), mimetype = 'application/json; charset="UTF-8"') try: mimetype = accept_list.pop(0) except IndexError: mimetype = None #Whoops nothing satisfies - return text/html return render_to_response("/list_of_zipfiles.html", {'c':c,}) elif http_method == "POST": params = request.POST if not (params.has_key("filename") and params['filename']): return HttpResponse("You must supply a filename to unpack", status=400) item_real_filepath = dataset.to_dirpath() target_filepath = "%s/%s"%(item_real_filepath, params['filename']) if not os.path.isfile(target_filepath): return HttpResponse("File to unpack not found", status=404) if not check_file_mimetype(target_filepath, 'application/zip'): return HttpResponse("File is not of type application/zip", status=415) if params.has_key("id") and params['id']: target_dataset_name = params['id'] else: target_dataset_name = id #step 1: Create / initialize target dataset if not rdfsilo.exists(target_dataset_name): if not allowable_id2(target_dataset_name): return HttpResponse("Data package name can contain only the following characters - %s and has to be more than 1 character"%ag.naming_rule_humanized, status=400, mimetype="text/plain") target_dataset = create_new(rdfsilo, target_dataset_name, c.ident) hr = HttpResponse("Created", status=201, mimetype="text/plain") hr["Content-Location"] = reverse("dataset_view", kwargs={'siloname': siloname, 'id': target_dataset_name}) else: hr = HttpResponse("204 Updated", status=204, mimetype="text/plain") target_dataset = rdfsilo.get_item(target_dataset_name) #step 2: Unpack zip item try: unpack_zip_item(target_dataset, dataset, params['filename'], rdfsilo, c.ident) except BadZipfile: return HttpResponse("BadZipfile: Couldn't unpack zipfile", status=400, mimetype="text/plain") # FIXME: err.. wat is this?! target_dataset.sync() target_dataset.sync() target_dataset.sync() if hr.status == 201: try: b.creation(silo, id, ident=c.ident) except: pass else: try: b.change(silo, id, ident=c.ident) except: pass # conneg return accept_list = None if 'HTTP_ACCEPT' in request.META: try: accept_list = conneg_parse(request.META['HTTP_ACCEPT']) except: accept_list= [MT("text", "html")] if not accept_list: accept_list= [MT("text", "html")] mimetype = accept_list.pop(0) while(mimetype): if str(mimetype).lower() in ["text/html", "text/xhtml"]: return redirect(reverse("dataset_view", kwargs={'silo': silo, 'id': id})) elif str(mimetype).lower() in ["text/plain", "application/json"]: hr.mimetype = "text/plain" return hr try: mimetype = accept_list.pop(0) except IndexError: mimetype = None # Whoops - nothing satisfies - return text/plain hr.mimetype = "text/plain" return hr
def home(request): # logged in user c = Context() # duck-typed context object for Mako c.ident = request.user add_auth_info_to_context(request, c) return render_to_response("home.html", {'c':c})
def api_index(request): # logged in user c = Context() # duck-typed context object for Mako c.ident = request.user return redirect("/api/silos")
def dataset_view(request, siloname, id): if not granary.issilo(siloname): raise Http404 rdfsilo = granary.get_rdf_silo(siloname) if not rdfsilo.exists(id): raise Http404 c = Context() silo = siloname #tmpl_context variables needed: c.silo_name, c.zipfiles, c.ident, c.id, c.path c.silo_name = siloname c.id = id ident = request.user c.ident = ident.username dataset = rdfsilo.get_item(id) creator = None if dataset.manifest and dataset.manifest.state and 'metadata' in dataset.manifest.state and dataset.manifest.state['metadata'] and \ 'createdby' in dataset.manifest.state['metadata'] and dataset.manifest.state['metadata']['createdby']: creator = dataset.manifest.state['metadata']['createdby'] http_method = request.method if http_method == "GET": c.editor = False if settings.METADATA_EMBARGOED: if not ident: return HttpResponse("Not Authorised", status=401) silos = authz(ident) if silo not in silos: return HttpResponseForbidden("Forbidden") silos_admin = authz(ident, permission='administrator') silos_manager = authz(ident, permission='manager') if c.ident == creator or silo in silos_admin or silo in silos_manager: c.editor = True elif ident: silos = authz(ident) if silo in silos: silos_admin = authz(ident, permission='administrator') silos_manager = authz(ident, permission='manager') if ident[ 'repoze.who.userid'] == creator or silo in silos_admin or silo in silos_manager: c.editor = True else: #identity management of item if not ident: return HttpResponse("Not Authorised", status=401) silos = authz(ident) if silo not in silos: return HttpResponseForbidden("Forbidden") silos_admin = authz(ident, permission='administrator') silos_manager = authz(ident, permission='manager') if not (c.ident == creator or silo in silos_admin or silo in silos_manager): return HttpResponseForbidden("Forbidden") if http_method == "GET": c.zipfiles = get_zipfiles_in_dataset(dataset) # conneg return accept_list = None if 'HTTP_ACCEPT' in request.META: try: accept_list = conneg_parse(request.META['HTTP_ACCEPT']) except: accept_list = [MT("text", "html")] if not accept_list: accept_list = [MT("text", "html")] mimetype = accept_list.pop(0) while (mimetype): if str(mimetype).lower() in ["text/html", "text/xhtml"]: return render_to_response("/list_of_zipfiles.html", { 'c': c, }) elif str(mimetype).lower() in ["text/plain", "application/json"]: return HttpResponse( json.dumps(list(c.zipfiles.keys())), mimetype='application/json; charset="UTF-8"') try: mimetype = accept_list.pop(0) except IndexError: mimetype = None #Whoops nothing satisfies - return text/html return render_to_response("/list_of_zipfiles.html", { 'c': c, }) elif http_method == "POST": params = request.POST if not (params.has_key("filename") and params['filename']): return HttpResponse("You must supply a filename to unpack", status=400) item_real_filepath = dataset.to_dirpath() target_filepath = "%s/%s" % (item_real_filepath, params['filename']) if not os.path.isfile(target_filepath): return HttpResponse("File to unpack not found", status=404) if not check_file_mimetype(target_filepath, 'application/zip'): return HttpResponse("File is not of type application/zip", status=415) if params.has_key("id") and params['id']: target_dataset_name = params['id'] else: target_dataset_name = id #step 1: Create / initialize target dataset if not rdfsilo.exists(target_dataset_name): if not allowable_id2(target_dataset_name): return HttpResponse( "Data package name can contain only the following characters - %s and has to be more than 1 character" % ag.naming_rule_humanized, status=400, mimetype="text/plain") target_dataset = create_new(rdfsilo, target_dataset_name, c.ident) hr = HttpResponse("Created", status=201, mimetype="text/plain") hr["Content-Location"] = reverse("dataset_view", kwargs={ 'siloname': siloname, 'id': target_dataset_name }) else: hr = HttpResponse("204 Updated", status=204, mimetype="text/plain") target_dataset = rdfsilo.get_item(target_dataset_name) #step 2: Unpack zip item try: unpack_zip_item(target_dataset, dataset, params['filename'], rdfsilo, c.ident) except BadZipfile: return HttpResponse("BadZipfile: Couldn't unpack zipfile", status=400, mimetype="text/plain") # FIXME: err.. wat is this?! target_dataset.sync() target_dataset.sync() target_dataset.sync() if hr.status == 201: try: b.creation(silo, id, ident=c.ident) except: pass else: try: b.change(silo, id, ident=c.ident) except: pass # conneg return accept_list = None if 'HTTP_ACCEPT' in request.META: try: accept_list = conneg_parse(request.META['HTTP_ACCEPT']) except: accept_list = [MT("text", "html")] if not accept_list: accept_list = [MT("text", "html")] mimetype = accept_list.pop(0) while (mimetype): if str(mimetype).lower() in ["text/html", "text/xhtml"]: return redirect( reverse("dataset_view", kwargs={ 'silo': silo, 'id': id })) elif str(mimetype).lower() in ["text/plain", "application/json"]: hr.mimetype = "text/plain" return hr try: mimetype = accept_list.pop(0) except IndexError: mimetype = None # Whoops - nothing satisfies - return text/plain hr.mimetype = "text/plain" return hr
def view(request, siloname): c = Context() # duck-typed context object for Mako add_auth_info_to_context(request, c) if not granary.issilo(siloname): raise Http404 ident = request.user c.ident = ident c.silo_name = siloname c.user_permissions = list_user_permissions(ident.username, siloname) c.editor = False if settings.METADATA_EMBARGOED: if request.user.is_authenticated(): c.silos = authz(ident) if siloname in silos: c.editor = True else: return HttpResponseForbidden("Forbidden") else: return HttpResponseForbidden("Forbidden") else: c.silos = authz(ident) if siloname in c.silos: c.editor = True # FIXME: MAJOR issue - very specific 'if' here # TODO: Add a flag to the Silo model to mark whether a silo is # listable. if siloname in ['ww1archives', 'digitalbooks']: return HttpResponse("The silo %s contains too many data packages to list"%siloname, mimetype="text/plain") rdfsilo = granary.get_rdf_silo(siloname) state_info = granary.describe_silo(siloname) if 'title' in state_info and state_info['title']: c.title = state_info['title'] c.embargos = {} c.items = [] for item in rdfsilo.list_items(): c.embargos[item] = None try: c.embargos[item] = is_embargoed(rdfsilo, item) except: pass c.items.append(item) #c.embargos[item] = () # conneg return accept_list = None if request.META.has_key('HTTP_ACCEPT'): try: accept_list = conneg_parse(request.META['HTTP_ACCEPT']) except: accept_list= [MT("text", "html")] if not accept_list: accept_list= [MT("text", "html")] mimetype = accept_list.pop(0) while(mimetype): if str(mimetype).lower() in ["text/html", "text/xhtml"]: return render_to_response('siloview.html', {'c':c}) elif str(mimetype).lower() in ["text/plain", "application/json"]: response.content_type = 'application/json; charset="UTF-8"' response.status_int = 200 response.status = "200 OK" return simplejson.dumps(c.embargos) try: mimetype = accept_list.pop(0) except IndexError: mimetype = None #Whoops nothing satisfies - return text/html return render_to_response('siloview.html', {'c':c})