示例#1
0
def register_experiment_ws_xmldata(request):
    ''' Web-service mechanism for registering an experiment, and triggering a corresponding file transfer.
        Although intended to be called as a web service, it actually works fine as a normal form, at 
        /experiment/register '''

    # --- start function body ---
    global experiment, idless_statuses, current_action, debug_POST
    experiment=None
    idless_statuses=[]
    logger.debug("Starting ingest process")
    # Check that we have received a form, abort otherwise
    try:
        if request.method != 'POST':
            # Happens when just viewing the form
            form = RegisterExperimentForm()  # An unbound form
            return send_retry_response(request, form, '')
        
        logger.info("Starting experiment ingest processing")
        
        from datetime import datetime
    
        temp_title = "Ingest Received: " + \
                     datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
    
        # A form bound to the POST data
        form = RegisterExperimentForm(request.POST, request.FILES)
        
        # Check that the form is filled out, abort otherwise.
        if not form.is_valid():  
            fail_message = "Form validation failure: <br/>" \
                "Form Errors: " + str(form.errors) + "<br/>" \
    
            try:
                add_status(RegistrationStatus.ERROR, fail_message)
                return send_retry_response(request, form, '')
            except Exception as ex:
                logger.error("Really an exception %s" % ex)
       
        logger.debug("Form validation: ok")
        xmldata = request.FILES['xmldata']
        xmldata_meta = xmldata.name
        username = form.cleaned_data['username']
        originid = form.cleaned_data['originid']
        from_url = form.cleaned_data['from_url']
        owners = request.POST.getlist('experiment_owner')

        debug_POST = "username: "******"<br/>" \
            "xmldata: " + xmldata_meta + "<br/>" \
            "originid: " + originid + "<br/>" \
            "from_url: " + from_url + "<br/>" \
    
    
        user = auth_service.authenticate(request=request,
                       authMethod=localdb_auth_key)
        
        # Check user is authenticated, and user information is present, abort otherwise
        if not authentication_ok(user):
            return return_response_error(request)
        logger.debug("User authentication: ok")
        # Basic checks have passed, so create the experiment.
        global experiment
        experiment = Experiment(title=temp_title, approved=True, created_by=user,)
        experiment.save()
        
        # Now update old registration statuses with the new experiment number.
        
        for oldstatus in idless_statuses:
            rs = RegistrationStatus.objects.get(pk=oldstatus)
            rs.experiment=experiment
            rs.save()
    
        # If no owner provided, record a warning.
        check_owner(owners)        

        # Write the submitted XML file to disk
        filename = path.join(experiment.get_or_create_directory(),
                             'mets_upload.xml')
    
        f = open(filename, 'wb+')
        for chunk in xmldata.chunks():
            f.write(chunk)
        f.close()
    
        add_status(status=RegistrationStatus.PASS,
                   message="Ingest Successfully Received")
    
        # Now process METS/XML file
        current_action = "Ingest Processing"
        try:
            _registerExperimentDocument(filename=filename,
                                        created_by=user,
                                        expid=experiment.id,
                                        owners=owners,
                                        username=username)
        except:
            add_status(status=RegistrationStatus.ERROR,
                       message="METS metadata ingest failed",
                       exception=True)
            return return_response_error(request)
    
        add_status(status=RegistrationStatus.PASS,
                   message="Ingest Successfully Processed")
    
        if from_url:
        # form is ok, METS file ingested ok, and they also specified a file to transer
            current_action = 'File Transfer Request'
            logger.debug("transferring file")
            file_transfer_url = from_url + '/file_transfer/'
            do_file_transfer(file_transfer_url, originid, request) 
    
        # Success: respond with just the ID of the newly created and processed experiment.
        response = HttpResponse(str(experiment.id), status=200)
        response['Location'] = request.build_absolute_uri(
            '/experiment/view/' + str(experiment.id))
        return response
    except Exception as ex:
        add_status(RegistrationStatus.ERROR, "Unhandled exception in METS ingest.", exception=True)
示例#2
0
def register_experiment_ws_xmldata(request):

    status = ''
    if request.method == 'POST':  # If the form has been submitted...

        # A form bound to the POST data
        form = RegisterExperimentForm(request.POST, request.FILES)
        if form.is_valid():  # All validation rules pass

            xmldata = request.FILES['xmldata']
            username = form.cleaned_data['username']
            origin_id = form.cleaned_data['originid']
            from_url = form.cleaned_data['from_url']

            user = auth_service.authenticate(request=request,
                                             authMethod=localdb_auth_key)
            if user:
                if not user.is_active:
                    return return_response_error(request)
            else:
                return return_response_error(request)

            e = Experiment(
                title='Placeholder Title',
                approved=True,
                created_by=user,
                )
            e.save()
            eid = e.id

            filename = path.join(e.get_or_create_directory(),
                                 'mets_upload.xml')
            f = open(filename, 'wb+')
            for chunk in xmldata.chunks():
                f.write(chunk)
            f.close()

            logger.info('=== processing experiment: START')
            owners = request.POST.getlist('experiment_owner')
            try:
                _registerExperimentDocument(filename=filename,
                                            created_by=user,
                                            expid=eid,
                                            owners=owners,
                                            username=username)
                logger.info('=== processing experiment %s: DONE' % eid)
            except:
                logger.exception('=== processing experiment %s: FAILED!' % eid)
                return return_response_error(request)

            if from_url:
                logger.debug('=== sending file request')
                logger.info('Sending received_remote signal')
                from tardis.tardis_portal.signals import received_remote
                received_remote.send(sender=Experiment,
                        instance=e,
                        uid=origin_id,
                        from_url=from_url)

            response = HttpResponse(str(eid), status=200)
            response['Location'] = request.build_absolute_uri(
                '/experiment/view/' + str(eid))
            return response
    else:
        form = RegisterExperimentForm()  # An unbound form

    c = Context({
        'form': form,
        'status': status,
        'subtitle': 'Register Experiment',
        'searchDatafileSelectionForm': getNewSearchDatafileSelectionForm()})
    return HttpResponse(render_response_index(request,
                        'tardis_portal/register_experiment.html', c))