示例#1
0
def ingest_form(request):
    """Display or process the file ingest form. On GET, display the form. On
    valid POST, reposit the submitted file in a new digital object.
    """
    if request.method == 'POST':
        form = IngestForm(request.POST, request.FILES)
        if form.is_valid():
            # TODO: set label/dc:title based on filename;
            # set file mimetype in dc:format
            # TODO: file checksum?
            repo = Repository(request=request)
            fobj = repo.get_object(type=FileObject)
            st = (fobj.uriref, relsext.isMemberOfCollection,
                  URIRef(form.cleaned_data['collection']))
            fobj.rels_ext.content.add(st)
            fobj.master.content = request.FILES['file']
            # pre-populate the object label and dc:title with the uploaded filename
            fobj.label = fobj.dc.content.title = request.FILES['file'].name
            fobj.save('ingesting user content')

            messages.success(request, 'Successfully ingested <a href="%s"><b>%s</b></a>' % \
                             (reverse('file:view', args=[fobj.pid]), fobj.pid))
            return HttpResponseSeeOtherRedirect(reverse('site-index'))
    else:
        initial_data = {}
        # if collection is specified in url parameters, pre-select the
        # requested collection on the form via initial data
        if 'collection' in request.GET:
            initial_data['collection'] = request.GET['collection']
        form = IngestForm(initial=initial_data)
    return render_to_response('file/ingest.html', {'form': form},
                              request=request)
示例#2
0
def view_metadata(request, pid):
    repo = Repository(request=request)
    obj = repo.get_object(pid, type=FileObject)
    # if the object doesn't exist or user doesn't have sufficient
    # permissions to know that it exists, 404
    if not obj.exists:
        raise Http404
    return render_to_response('file/view.html', {'obj': obj}, request=request)
示例#3
0
def edit_metadata(request, pid):
    """View to edit the metadata for an existing
    :class:`~genrepo.file.models.FileObject` .

    On GET, display the form.  When valid form data is POSTed, updates
    thes object.
    """
    status_code = None
    repo = Repository(request=request)
    # get the object (if pid is not None), or create a new instance
    obj = repo.get_object(pid, type=FileObject)

    # on GET, instantiate the form with existing object data (if any)
    if request.method == 'GET':
        form = DublinCoreEditForm(instance=obj.dc.content)

    # on POST, create a new collection object, update DC from form
    # data (if valid), and save
    elif request.method == 'POST':
        form = DublinCoreEditForm(request.POST, instance=obj.dc.content)
        if form.is_valid():
            form.update_instance()
            # also use dc:title as object label
            obj.label = obj.dc.content.title
            try:
                result = obj.save('updated metadata')
                messages.success(request,
                'Successfully updated <a href="%s"><b>%s</b></a>' % \
                         (reverse('file:view', args=[obj.pid]), obj.pid))

                # maybe redirect to file view page when we have one
                return HttpResponseSeeOtherRedirect(reverse('site-index'))
            except (DigitalObjectSaveFailure, RequestFailed) as rf:
                # do we need a different error message for DigitalObjectSaveFailure?
                if isinstance(rf, PermissionDenied):
                    msg = 'You don\'t have permission to modify this object in the repository.'
                else:
                    msg = 'There was an error communicating with the repository.'
                messages.error(request,
                               msg + ' Please contact a site administrator.')

                # pass the fedora error code back in the http response
                status_code = getattr(rf, 'code', None)

    # if form is not valid, fall through and re-render the form with errors
    response = render_to_response('file/edit.html', {
        'form': form,
        'obj': obj
    },
                                  request=request)
    # if a non-standard status code is set, set it in the response before returning
    if status_code is not None:
        response.status_code = status_code
    return response
示例#4
0
def download_file(request, pid):
    '''Download the master file datastream associated with a
    :class:`~genrepo.file.models.FileObject`'''
    repo = Repository(request=request)
    # FIXME: what should the default download filename be?
    extra_headers = {'Content-Disposition': "attachment; filename=%s" % (pid)}
    # use generic raw datastream view from eulcore
    return raw_datastream(request,
                          pid,
                          FileObject.master.id,
                          type=FileObject,
                          repo=repo,
                          headers=extra_headers)
示例#5
0
def view_collection(request, pid):
    '''view an existing
    :class:`~genrepo.collection.models.CollectionObject` identified by
    pid.
    '''
    repo = Repository(request=request)
    obj = repo.get_object(pid, type=CollectionObject)
    # if the object does not exist or the current user doesn't have
    # permission to see that it exists, 404
    if not obj.exists:
        raise Http404
    return render_to_response('collection/view.html', {'obj': obj},
                              request=request)
示例#6
0
def _create_or_edit_collection(request, pid=None):
    """View to create a new
    :class:`~genrepo.collection.models.CollectionObject` or update an
    existing one.

    On GET, display the form.  When valid form data is POSTed, creates
    a new collection (if pid is None) or updates an existing
    collection.
    """
    status_code = None
    repo = Repository(request=request)
    # get the object (if pid is not None), or create a new instance
    obj = repo.get_object(pid, type=CollectionObject)

    # on GET, instantiate the form with existing object data (if any)
    if request.method == 'GET':
        form = CollectionDCEditForm(instance=obj.dc.content)

    # on POST, create a new collection object, update DC from form
    # data (if valid), and save
    elif request.method == 'POST':
        form = CollectionDCEditForm(request.POST, instance=obj.dc.content)
        if form.is_valid():
            form.update_instance()
            # also use dc:title as object label
            obj.label = obj.dc.content.title
            try:
                if obj.exists:
                    action = 'updated'
                    save_msg = 'updated via genrepo'
                else:
                    action = 'created new'
                    save_msg = 'ingested via genrepo'

                # save message must be specified in order for Fedora
                # to generate & store an ingest audit trail event
                result = obj.save(save_msg)
                messages.success(request,
                'Successfully %s collection <a href="%s"><b>%s</b></a>' % \
                         (action, reverse('collection:edit', args=[obj.pid]), obj.pid))

                # maybe redirect to collection view page when we have one
                # - and maybe return a 201 Created status code
                return HttpResponseSeeOtherRedirect(reverse('site-index'))
            except (DigitalObjectSaveFailure, RequestFailed) as rf:
                # do we need a different error message for DigitalObjectSaveFailure?
                if isinstance(rf, PermissionDenied):
                    msg = 'You don\'t have permission to create a collection in the repository.'
                else:
                    msg = 'There was an error communicating with the repository.'
                messages.error(request,
                               msg + ' Please contact a site administrator.')

                # pass the fedora error code back in the http response
                status_code = getattr(rf, 'code', None)

    # if form is not valid, fall through and re-render the form with errors
    response = render_to_response('collection/edit.html', {
        'form': form,
        'obj': obj
    },
                                  request=request)
    # if a non-standard status code is set, set it in the response before returning
    if status_code is not None:
        response.status_code = status_code
    return response