Exemplo n.º 1
0
 def test_oai_set_validation(self):
     dcform = CollectionDCEditForm()
     dcform.cleaned_data = {'oai_set': 'foo'}
     self.assertRaises(forms.ValidationError, dcform.clean)
     dcform.cleaned_data = {'oai_set_name': 'foo stuff'}
     self.assertRaises(forms.ValidationError, dcform.clean)
     dcform.cleaned_data = {'oai_set': 'foo', 'oai_set_name': 'foo stuff'}
     # should not raise an exception
     self.assertEqual(dcform.cleaned_data, dcform.clean())
Exemplo n.º 2
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
Exemplo n.º 3
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 will be 200 unless something goes wrong
    status_code = 200
    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":
        # pre-populate oai setSpec and setName if set on the object
        initial_data = {"oai_set": obj.oai_set, "oai_set_name": obj.oai_setlabel}
        form = CollectionDCEditForm(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 = 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

            # if the form is valid, OAI set & set name are either both set or both empty
            oai_set = form.cleaned_data["oai_set"]
            oai_setname = form.cleaned_data["oai_set_name"]
            if oai_set and oai_setname:
                obj.oai_set = oai_set
                obj.oai_setlabel = oai_setname
            else:
                # if no value, clear out any previous OAI set information
                obj.oai_set = None
                obj.oai_setlabel = None

            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?
                # permission error could be generic creation error
                if pid is None:
                    action = "create a"
                # OR could be failure to update this particular object
                else:
                    action = "update this"
                if isinstance(rf, PermissionDenied):
                    msg = "You don't have permission to %s collection in the repository." % action
                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, "collection/edit.html", {"form": form, "obj": obj}, status=status_code)
Exemplo n.º 4
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 will be 200 unless something goes wrong
    status_code = 200
    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':
        # pre-populate oai setSpec and setName if set on the object
        initial_data = {
            'oai_set': obj.oai_set,
            'oai_set_name': obj.oai_setlabel
        }
        form = CollectionDCEditForm(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 = 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

            # if the form is valid, OAI set & set name are either both set or both empty
            oai_set = form.cleaned_data['oai_set']
            oai_setname = form.cleaned_data['oai_set_name']
            if oai_set and oai_setname:
                obj.oai_set = oai_set
                obj.oai_setlabel = oai_setname
            else:
                # if no value, clear out any previous OAI set information
                obj.oai_set = None
                obj.oai_setlabel = None

            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?
                # permission error could be generic creation error
                if pid is None:
                    action = 'create a'
                # OR could be failure to update this particular object
                else:
                    action = 'update this'
                if isinstance(rf, PermissionDenied):
                    msg = 'You don\'t have permission to %s collection in the repository.' % action
                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,
                  'collection/edit.html', {
                      'form': form,
                      'obj': obj
                  },
                  status=status_code)