コード例 #1
0
ファイル: publisher.py プロジェクト: kelly-mcdougall/doaj
def _file_upload(f, schema, previous):
    
    # prep a record to go into the index, to record this upload
    record = models.FileUpload()
    record.upload(current_user.id, f.filename)
    record.set_id()
    
    # the file path that we are going to write to
    xml = os.path.join(app.config.get("UPLOAD_DIR", "."), record.local_filename)
    
    # it's critical here that no errors cause files to get left behind unrecorded
    try:
        # write the incoming file out to the XML file
        f.save(xml)
        
        # save the index entry
        record.save()
    except:
        # if we can't record either of these things, we need to back right off
        try:
            os.remove(xml)
        except:
            pass
        try:
            record.delete()
        except:
            pass
        
        flash("Failed to upload file - please contact an administrator", "error")
        return render_template('publisher/uploadfile.html', previous=previous)
        
    # now we have the record in the index and on disk, we can attempt to
    # validate it
    try:
        actual_schema = None
        with open(xml) as handle:
            actual_schema = article.check_schema(handle, schema)
    except:
        # file is a dud, so remove it
        try:
            os.remove(xml)
        except:
            pass
        
        # if we're unable to validate the file, we should record this as
        # a file error.
        record.failed("Unable to parse file")
        record.save()
        previous = [record] + previous
        flash("Failed to parse file - it is invalid XML; please fix it before attempting to upload again.", "error")
        return render_template('publisher/uploadfile.html', previous=previous)
    
    if actual_schema:
        record.validated(actual_schema)
        record.save()
        previous = [record] + previous # add the new record to the previous records
        flash("File successfully uploaded - it will be processed shortly", "success")
        return render_template('publisher/uploadfile.html', previous=previous)
    else:
        record.failed("File could not be validated against a known schema")
        record.save()
        os.remove(xml)
        previous = [record] + previous
        flash("File could not be validated against a known schema; please fix this before attempting to upload again", "error")
        return render_template('publisher/uploadfile.html', previous=previous)
コード例 #2
0
ファイル: ingestarticles.py プロジェクト: jbarnsby/doaj
    elif parsed_url.scheme == 'http':
        if not http_upload():
            continue
    else:
        print 'We only support HTTP and FTP uploads by URL. This is a: ' + parsed_url.scheme
        continue

    print "...downloaded as", remote.local_filename
    
    # now we have the record in the index and on disk, we can attempt to
    # validate it
    print "validating", remote.local_filename
    try:
        actual_schema = None
        with open(path) as handle:
            actual_schema = article.check_schema(handle, remote.schema)
    except:
        # file is a dud, so remove it
        try:
            os.remove(path)
        except:
            pass
        remote.failed("Unable to parse file")
        remote.save()
        print "...failed"
        continue
        
    # if we get to here then we have a successfully downloaded and validated
    # document, so we can write it to the index
    remote.validated(actual_schema)
    remote.save()
コード例 #3
0
    elif parsed_url.scheme == 'http':
        if not http_upload():
            continue
    else:
        print 'We only support HTTP and FTP uploads by URL. This is a: ' + parsed_url.scheme
        continue

    print "...downloaded as", remote.local_filename

    # now we have the record in the index and on disk, we can attempt to
    # validate it
    print "validating", remote.local_filename
    try:
        actual_schema = None
        with open(path) as handle:
            actual_schema = article.check_schema(handle, remote.schema)
    except:
        # file is a dud, so remove it
        try:
            os.remove(path)
        except:
            pass
        remote.failed("Unable to parse file")
        remote.save()
        print "...failed"
        continue

    # if we get to here then we have a successfully downloaded and validated
    # document, so we can write it to the index
    remote.validated(actual_schema)
    remote.save()