def do_work(ignore_time=False):
    from purchasing.jobs.job_base import JobStatus
    turn_off_sqlalchemy_events()
    jobs = JobStatus.query.filter(JobStatus.status == 'new').all()
    for job in jobs:
        task = getattr(nightly_jobs, job.name)(time_override=ignore_time)
        task.run_job(job)
    turn_on_sqlalchemy_events()
    refresh_search_view()
def import_state(filepath):
    '''
    Takes a csv of state contracts and imports them into the DB
    '''
    from purchasing.data.importer.state import main
    print 'Importing data from {filepath}\n'.format(filepath=filepath)
    main(filepath)
    print 'Import finished!'
    print 'Refreshing search_view...'
    refresh_search_view()
    print 'Done!'
    return
def upload_costars_contract(_file):
    '''Upload a COSTARS pdf document to S3

    Arguments:
        _file: A werkzeug `FileStorage`_ object

    Returns:
        A two-tuple of (the name of the uploaded file, the path/url to the file)
    '''
    filename = secure_filename(_file.filename)

    if current_app.config['UPLOAD_S3']:
        try:
            turn_off_sqlalchemy_events()
            conn, bucket = connect_to_s3(
                current_app.config['AWS_ACCESS_KEY_ID'],
                current_app.config['AWS_SECRET_ACCESS_KEY'], 'costars')

            file_href = upload_file(filename,
                                    bucket,
                                    input_file=_file,
                                    prefix='/',
                                    from_file=True)
            return filename, file_href
        except Exception:
            raise
        finally:
            turn_on_sqlalchemy_events()
            refresh_search_view()

    else:
        try:
            os.mkdir(current_app.config['UPLOAD_DESTINATION'])
        except:
            pass

        filepath = os.path.join(current_app.config['UPLOAD_DESTINATION'],
                                filename)
        _file.save(filepath)
        return filename, filepath
def upload_costars_contract(_file):
    '''Upload a COSTARS pdf document to S3

    Arguments:
        _file: A werkzeug `FileStorage`_ object

    Returns:
        A two-tuple of (the name of the uploaded file, the path/url to the file)
    '''
    filename = secure_filename(_file.filename)

    if current_app.config['UPLOAD_S3']:
        try:
            turn_off_sqlalchemy_events()
            conn, bucket = connect_to_s3(
                current_app.config['AWS_ACCESS_KEY_ID'],
                current_app.config['AWS_SECRET_ACCESS_KEY'],
                'costars'
            )

            file_href = upload_file(filename, bucket, input_file=_file, prefix='/', from_file=True)
            return filename, file_href
        except Exception:
            raise
        finally:
            turn_on_sqlalchemy_events()
            refresh_search_view()

    else:
        try:
            os.mkdir(current_app.config['UPLOAD_DESTINATION'])
        except:
            pass

        filepath = os.path.join(current_app.config['UPLOAD_DESTINATION'], filename)
        _file.save(filepath)
        return filename, filepath