Beispiel #1
0
def load_status_updates(filename):
    '''
    Opens a CSV file with user data and
    adds it to an existing instance of
    UserCollection

    Requirements:
    - If a user_id already exists, it
    will ignore it and continue to the
    next.
    - Returns False if there are any errors
    (such as empty fields in the source CSV file)
    - Otherwise, it returns True.
    '''
    if not filename.endswith('.csv'):
        logger.error('Status files must be in csv format')
        return False
    # Collect user_ids to ensure forign key compliance
    user_ids = set()
    with sql.connection(sql.USER) as table:
        data = table.all()
        for row in data:
            user_ids.add(row['user_id'])
    with open(filename, newline='') as statusfile:
        logger.info('Loading status updates from %s', filename)
        data = csv.reader(statusfile)
        try:
            next(data)  # Eat header data
        except StopIteration:
            logger.error('%s does not contain data', filename)
            return False
        for row in data:
            logger.debug('Inspecting %s', row)
            # Inspect csv data
            if len(row) != 3:
                logger.error('Statuses need three parameters')
                return False
            # Empty strings, 0, and None are all considered empty fields
            for item in row:
                if not item:
                    logger.error('Statuses can not have empty fields')
                    return False
            if row[1] in user_ids:
                status = user_status.make_status(*row)
                with sql.connection(sql.STATUS) as table:
                    table.insert(**status)
                    logger.info('Status %s added to database', row[0])
                    continue  # Moves onto the next row if there are no exceptions
                # IntegrityError is the only excpetion that we catch
                logger.error('Status %s already exists', row[0])
            else:
                logger.error('User %s does not exist', row[1])
            # If status does not meet requirements ignore and continue
    return True
Beispiel #2
0
def test_api_differences(pic, client):
    '''
    Test each aspect of the deifferences page in the API
    '''
    expected = {}
    result = {}
    responce = client.get('/differences')
    assert responce.data == jsonify(expected).data  # Confirm no images
    # Add three pictures
    picture.add_picture(*pic)
    picture.add_picture(*pic)
    picture.add_picture(*pic)
    result['files-db'] = []
    result['db-files'] = []
    expected[pic[0]] = result
    responce = client.get('/differences')
    assert responce.data == jsonify(expected).data  # Confirm no differences
    tag_path = pic[0] / picture.tags_to_path(pic[1])
    # Remove the second picture from the image directory
    (tag_path / picture.id_to_filename(2)).unlink()
    # Remove the third picture from the database
    with sql.connection(sql.PICTURE) as table:
        table.delete(picture_id=3)
    result['files-db'] = [(pic[0], str(tag_path), picture.id_to_filename(3))]
    result['db-files'] = [(pic[0], str(tag_path), picture.id_to_filename(2))]
    expected[pic[0]] = result
    responce = client.get('/differences')
    assert responce.data == jsonify(expected).data  # Confirm normal operation
Beispiel #3
0
def add_status(status_id, user_id, status_text):
    '''
    Adds a new status to the dataset
    '''
    new_status = make_status(status_id, user_id, status_text)
    # Test for foreign key constraint
    if not users.check_for_user(user_id):
        logger.error('Foreign Key %s does not exist', user_id)
        return False
    # Foreign Key exists
    with sql.connection(sql.STATUS) as table:
        table.insert(**new_status)
        logger.info('Status %s added to database', status_id)
        return True
    logger.error('Status %s already exists', status_id)
    return False
Beispiel #4
0
def reconcile_images(user_id):
    '''
    List images in the database but not the directory and vice versa
    '''
    files = set(list_user_images(user_id))
    database = set()
    result = dict()
    logger.debug('Contents of image directory: %s', files)
    with sql.connection(sql.PICTURE) as table:
        rows = table.find(user_id=user_id)
        for row in rows:
            tag_path = user_id / tags_to_path(row['tags'])
            database.add((row['user_id'], str(tag_path),
                          id_to_filename(row['picture_id'])))
    logger.debug('Contents of database: %s', database)
    result['files-db'] = files.difference(database)
    result['db-files'] = database.difference(files)
    return result