Exemple #1
0
def dataset_view(self, siloname, id):
    # Only authorized users can view state information.
    if not granary.issilo(siloname):
        raise Http404
        
    ident = request.user
    if not ident.is_authenticated():
        return HttpResponseForbidden("Not Authorised")
        
    silos = authz(ident)
    if siloname not in silos:
        return HttpResponseForbidden("Forbidden")
    
    silos_admin = authz(ident, permission='administrator')
    silos_manager = authz(ident, permission='manager')
    
    rdfsilo = granary.get_rdf_silo(siloname)
    if not rdfsilo.exists(id):
        raise Http404

    item = rdfsilo.get_item(id)
        
    creator = None
    
    if item.manifest and item.manifest.state and 'metadata' in item.manifest.state and item.manifest.state['metadata'] and \
            'createdby' in item.manifest.state['metadata'] and item.manifest.state['metadata']['createdby']:
        creator = item.manifest.state['metadata']['createdby']
    
    if not (ident.username == creator or siloname in silos_admin or siloname in silos_manager):
        return HttpResponseForbidden("Forbidden")

    options = request.GET
    if 'version' in options and options['version']:
        if not options['version'] in item.manifest['versions']:
            raise Http404
        currentversion = str(item.currentversion)
        vnum = str(options['version'])
        if vnum and not vnum == currentversion:
            item.set_version_cursor(vnum) 

    parts = item.list_parts(detailed=True)

    dataset = {}
    dataset['parts'] = {}
    for part in parts:
        dataset['parts'][part] = serialisable_stat(parts[part])
        if item.manifest:
            dataset['state'] = item.manifest.state

    # Always return application/json
    return HttpResponse(json.dumps(dataset), mimetype='application/json; charset="UTF-8"')
Exemple #2
0
def silo_view(request, siloname):
    # Only authorized users can view state information.
    if not granary.issilo(siloname):
        raise Http404

    ident = request.user
    if not ident.is_authenticated():
        return HttpResponseForbidden("Not Authorised")
        
    silos = authz(ident)
    if siloname not in silos:
        return HttpResponseForbidden("Forbidden")
    
    silos_admin = authz(ident, permission='administrator')
    silos_manager = authz(ident, permission='manager')
    #if not ident.get('role') in ["admin", "manager"]:
    if not (siloname in silos_admin or siloname in silos_manager):
        return HttpResponseForbidden(403, "Forbidden")

    options = request.GET
    start = 0
    if 'start' in options and options['start']:
        try:
            start = int(options['start'])
        except:
            start = 0
    rows = 1000
    if 'rows' in options and options['rows']:
        try:
            rows = int(options['rows'])
        except:
            rows = 1000

    rdfsilo = granary.get_rdf_silo(siloname)
    state_info = granary.describe_silo(siloname)
    state_info['silo'] = siloname
    state_info['uri_base'] = ''
    if rdfsilo.state and rdfsilo.state['uri_base']:
        state_info['uri_base'] = rdfsilo.state['uri_base']
    state_info['number of data packages'] = get_datasets_count(siloname)
    state_info['params'] = {'start':start, 'rows':rows}
    items = {}
    #for item in rdfsilo.list_items():
    for item in get_datasets(siloname, start=start, rows=rows):
        items[item] = {}
        try:
            items[item]['embargo_info'] = is_embargoed(rdfsilo, item)
        except:
            pass
    state_info['datasets'] = items
    return HttpResponse(json.dumps(state_info), mimetype='application/json; charset="UTF-8"')
def view(request, siloname):
    c = create_context(request.user, siloname)
    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] = ()

    return render_html_or_json(request, c, 'siloview.html', c.embargos)
Exemple #4
0
def index(request):
    # logged in user
    c = create_context(request.user)
    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))

    return render_html_or_json(request, c, "list_of_silos.html", c.silos)
def index(request):
    # logged in user
    c = create_context(request.user)
    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))

    return render_html_or_json(request, c, 'list_of_silos.html', c.silos)
Exemple #6
0
def view(request, siloname):
    c = create_context(request.user, siloname)
    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] = ()

    return render_html_or_json(request, c, "siloview.html", c.embargos)
Exemple #7
0
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
Exemple #9
0
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
Exemple #10
0
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})