Esempio n. 1
0
 def test_init_by_cmodel(self):
     # create file and image objects to test initialization
     fileobj = self.repo_admin.get_object(type=FileObject)
     fileobj.save()
     imgobj = self.repo_admin.get_object(type=ImageObject)
     imgobj.save()
     self.pids.extend([fileobj.pid, imgobj.pid])
     # init a new object from file pid - should be a file object
     initobj = init_by_cmodel(fileobj.pid)
     self.assert_(isinstance(initobj, FileObject))
     # since ImageObject extends FileObject, confirm that we didn't get the wrong thing
     self.assert_(not isinstance(initobj, ImageObject))
     # image pid should be returned as an ImageObject
     initobj = init_by_cmodel(imgobj.pid)
     self.assert_(isinstance(initobj, ImageObject))
Esempio n. 2
0
def image_region(request, pid):
    # expose djatoka getRegion method for use in seadragon deep zoom functionality
    img = init_by_cmodel(pid, request)
    # convert svc.param format used by djatoka to param format used by fedora disseminator
    params = dict(
        (k.replace('svc.', ''), v) for k, v in request.GET.iteritems())
    return HttpResponse(img.get_region(params), mimetype='image/jpeg')
Esempio n. 3
0
def view_metadata(request, pid):
    # init the appropriate type (image, file) according to the cmodel
    obj = init_by_cmodel(pid, request)
    # 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 

    template = getattr(obj, 'view_template', 'file/view.html')
    env = EXTRA_ENV.copy()
    env.update(obj=obj)
    return render(request, template, env)
Esempio n. 4
0
def view_metadata(request, pid):
    # init the appropriate type (image, file) according to the cmodel
    obj = init_by_cmodel(pid, request)
    # 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

    template = getattr(obj, 'view_template', 'file/view.html')
    env = EXTRA_ENV.copy()
    env.update(obj=obj)
    return render(request, template, env)
Esempio n. 5
0
def download_file(request, pid):
    '''Download the master file datastream associated with a
    :class:`~genrepo.file.models.FileObject`'''
    repo = Repository(request=request)
    obj = init_by_cmodel(pid, request)
    # use original or edited filename as download filename
    extra_headers = {
        'Content-Disposition': "attachment; filename=%s" % obj.master.label
    } 
    # use generic raw datastream view from eulcore
    # - use the datastream id and digital object type returned by cmodel init
    return raw_datastream(request, pid, obj.master.id, type=obj.__class__,
                          repo=repo, headers=extra_headers)
Esempio n. 6
0
def download_file(request, pid):
    '''Download the master file datastream associated with a
    :class:`~genrepo.file.models.FileObject`'''
    repo = Repository(request=request)
    obj = init_by_cmodel(pid, request)
    # use original or edited filename as download filename
    extra_headers = {
        'Content-Disposition': "attachment; filename=%s" % obj.master.label
    }
    # use generic raw datastream view from eulcore
    # - use the datastream id and digital object type returned by cmodel init
    return raw_datastream(request,
                          pid,
                          obj.master.id,
                          type=obj.__class__,
                          repo=repo,
                          headers=extra_headers)
Esempio n. 7
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.
    """
    # response status should be 200 unless something goes wrong
    status_code = 200
    # init the object as the appropriate type
    obj = init_by_cmodel(pid, request)

    # on GET, instantiate the form with existing object data (if any)
    if request.method == 'GET':
        # enable_oai should pre-selected if object already has an oai id
        initial_data = {'enable_oai': bool(obj.oai_id),
                        'file_name': obj.master.label}
        form = DublinCoreEditForm(instance=obj.dc.content, initial=initial_data)

    # 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
            # update master datastream label (required, should always be set)
            obj.master.label = form.cleaned_data['file_name']
            # set or remove oai itemID based on form selection
            if 'enable_oai' in form.cleaned_data:
                enable_oai = form.cleaned_data['enable_oai']
                # FIXME: with ARKs we use ARK for the OAI id; what should we do without?
                if enable_oai:
                    obj.oai_id = 'oai:%s' % obj.uri
                else:
                    obj.oai_id = None 
            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 (if any) back in the http response
                if hasattr(rf, 'code'):
                    status_code = getattr(rf, 'code')

    # if form is not valid, fall through and re-render the form with errors
    return render(request, 'file/edit.html', {'form': form, 'obj': obj},
                  status=status_code)
Esempio n. 8
0
def image_region(request, pid):
    # expose djatoka getRegion method for use in seadragon deep zoom functionality
    img = init_by_cmodel(pid, request)
    # convert svc.param format used by djatoka to param format used by fedora disseminator
    params = dict((k.replace('svc.', ''),v) for k,v in request.GET.iteritems())
    return HttpResponse(img.get_region(params), mimetype='image/jpeg')
Esempio n. 9
0
def image_dzi(request, pid):
    # DZI xml image information  required by SeaDragon for deepzom
    # should be one of the image cmodels
    img = init_by_cmodel(pid, request=request)
    return HttpResponse(img.deepzoom_info().serialize(pretty=True), mimetype='text/xml')
Esempio n. 10
0
def preview(request, pid):
    # image preview of an object
    # currently only supported for image objects
    obj = init_by_cmodel(pid, request)
    return HttpResponse(obj.get_preview_image(), mimetype='image/jpeg')
Esempio n. 11
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.
    """
    # response status should be 200 unless something goes wrong
    status_code = 200
    # init the object as the appropriate type
    obj = init_by_cmodel(pid, request)

    # on GET, instantiate the form with existing object data (if any)
    if request.method == 'GET':
        # enable_oai should pre-selected if object already has an oai id
        initial_data = {
            'enable_oai': bool(obj.oai_id),
            'file_name': obj.master.label
        }
        form = DublinCoreEditForm(instance=obj.dc.content,
                                  initial=initial_data)

    # 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
            # update master datastream label (required, should always be set)
            obj.master.label = form.cleaned_data['file_name']
            # set or remove oai itemID based on form selection
            if 'enable_oai' in form.cleaned_data:
                enable_oai = form.cleaned_data['enable_oai']
                # FIXME: with ARKs we use ARK for the OAI id; what should we do without?
                if enable_oai:
                    obj.oai_id = 'oai:%s' % obj.uri
                else:
                    obj.oai_id = None
            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 (if any) back in the http response
                if hasattr(rf, 'code'):
                    status_code = getattr(rf, 'code')

    # if form is not valid, fall through and re-render the form with errors
    return render(request,
                  'file/edit.html', {
                      'form': form,
                      'obj': obj
                  },
                  status=status_code)
Esempio n. 12
0
def image_dzi(request, pid):
    # DZI xml image information  required by SeaDragon for deepzom
    # should be one of the image cmodels
    img = init_by_cmodel(pid, request=request)
    return HttpResponse(img.deepzoom_info().serialize(pretty=True),
                        mimetype='text/xml')
Esempio n. 13
0
def preview(request, pid):
    # image preview of an object
    # currently only supported for image objects
    obj = init_by_cmodel(pid, request)
    return HttpResponse(obj.get_preview_image(), mimetype='image/jpeg')