Exemplo n.º 1
0
def init_root():
    click.echo("Initializing root directory")
    # Ensure that we have a root directory
    # XXX there's definitely a better way to do this, I don't have access to the
    # docs right now since I'm on a plane, but I'd wager the SQLAlchemy has a way to
    # get the number of rows in a table without retrieving them (especially since
    # Postgres definitely support this)
    if len([d for d in Directory.query.all()]) == 0:
        root_dir = Directory(
            None, "Gallery!",
            "A Multimedia Gallery Written in Python with Flask!", "root",
            DEFAULT_THUMBNAIL_NAME, "{\"g\":[]}")
        db.session.add(root_dir)
        db.session.flush()
        db.session.commit()

        # Upload the default thumbnail photo to S3 if it's not already up there
        # XXX it's probably a good idea to move this outside of the root directory
        # creation check. That way if a deployment is given incorrect S3 credentials
        # when the root directory is created we can still recover from the case
        # where there is not default thumbnail
        default_thumbnail_path = "thumbnails/" + DEFAULT_THUMBNAIL_NAME + ".jpg"

        with open(default_thumbnail_path, "rb") as f_hnd:
            storage_interface.put("files/{}".format(DEFAULT_THUMBNAIL_NAME),
                                  f_hnd,
                                  "thumb_" + DEFAULT_THUMBNAIL_NAME + ".jpg",
                                  "image/jpeg")
Exemplo n.º 2
0
def check_for_dir_db_entry(dictionary, path, parent_dir):
    uuid_thumbnail = "reedphoto.jpg"

    # check db for this path with parents shiggg
    dir_name = path.split('/')[-1]
    if dir_name == "":
        dir_name = "root"
    dir_model = None
    if parent_dir:
        dir_model = Directory.query.filter(Directory.name == dir_name) \
                                   .filter(Directory.parent == parent_dir.id).first()
    else:
        dir_model = Directory.query.filter(Directory.parent == None).first()

    if dir_model is None:
        # f**k go back this directory doesn't exist as a model
        # we gotta add this shit
        if parent_dir:
            dir_model = Directory(parent_dir.id, dir_name, "", "root",
                                  uuid_thumbnail, "{\"g\":[]}")
        else:
            dir_model = Directory(None, dir_name, "", "root",
                                  uuid_thumbnail, "{\"g\":[]}")
        db.session.add(dir_model)
        db.session.flush()
        db.session.commit()
        db.session.refresh(dir_model)

    # get directory class as dir_model
    for dir_p in dictionary:
        # Don't traverse local files
        if dir_p == '.':
            continue
        check_for_dir_db_entry(
            dictionary[dir_p],
            os.path.join(path, dir_p),
            dir_model)

    for file_p in dictionary['.']:
        # check db for this file path
        file_model = File.query.filter(File.parent == dir_model.id) \
                               .filter(File.name == file_p).first()
        if file_model is None:
            add_file(file_p, path, dir_model.id, "", "root")
            click.echo("adding file: " + file_p)
Exemplo n.º 3
0
def add_directory(parent_id, name, description, owner):
    dir_siblings = Directory.query.filter(Directory.parent == parent_id).all()
    for sibling in dir_siblings:
        if sibling.get_name() == name:
            return None

    uuid_thumbnail = DEFAULT_THUMBNAIL_NAME
    dir_model = Directory(parent_id, name, description, owner,
                          uuid_thumbnail, "{\"g\":[]}")
    db.session.add(dir_model)
    db.session.flush()
    db.session.commit()
    db.session.refresh(dir_model)

    return dir_model.id
Exemplo n.º 4
0
from gallery.ldap import ldap_get_members

for func in inspect.getmembers(gallery_ldap):
    if func[0].startswith("ldap_"):
        unwrapped = inspect.unwrap(func[1])
        if inspect.isfunction(unwrapped):
            app.add_template_global(inspect.unwrap(unwrapped), name=func[0])

# Ensure that we have a root directory
# XXX there's definitely a better way to do this, I don't have access to the
# docs right now since I'm on a plane, but I'd wager the SQLAlchemy has a way to
# get the number of rows in a table without retrieving them (especially since
# Postgres definitely support this)
if len([d for d in Directory.query.all()]) == 0:
    root_dir = Directory(None, "Gallery!",
                         "A Multimedia Gallery Written in Python with Flask!",
                         "root", DEFAULT_THUMBNAIL_NAME, "{\"g\":[]}")
    db.session.add(root_dir)
    db.session.flush()
    db.session.commit()

    # Upload the default thumbnail photo to S3 if it's not already up there
    # XXX it's probably a good idea to move this outside of the root directory
    # creation check. That way if a deployment is given incorrect S3 credentials
    # when the root directory is created we can still recover from the case
    # where there is not default thumbnail
    default_thumbnail_path = "thumbnails/" + DEFAULT_THUMBNAIL_NAME + ".jpg"
    file_stat = os.stat(default_thumbnail_path)

    with open(default_thumbnail_path, "rb") as f_hnd:
        s3.put_object(app.config['S3_BUCKET_ID'],