Пример #1
0
def edit_album(album, data):
    """Update an album's settings (description, format, etc.)"""
    album = sanitize_name(album)
    index = get_index()
    if not album in index["albums"]:
        logger.error("Failed to edit album: not found!")
        return

    index["settings"][album].update(data)
    write_index(index)
Пример #2
0
def edit_album(album, data):
    """Update an album's settings (description, format, etc.)"""
    album = sanitize_name(album)
    index = get_index()
    if not album in index["albums"]:
        logger.error("Failed to edit album: not found!")
        return

    index["settings"][album].update(data)
    write_index(index)
Пример #3
0
def set_album_cover(album, key):
    """Change the album's cover photo."""
    album = sanitize_name(album)
    index = get_index()
    logger.info("Changing album cover for {} to {}".format(album, key))
    if album in index["albums"] and key in index["albums"][album]:
        index["covers"][album] = key
        write_index(index)
        return
    logger.error("Failed to change album index! Album or photo not found.")
Пример #4
0
def set_album_cover(album, key):
    """Change the album's cover photo."""
    album = sanitize_name(album)
    index = get_index()
    logger.info("Changing album cover for {} to {}".format(album, key))
    if album in index["albums"] and key in index["albums"][album]:
        index["covers"][album] = key
        write_index(index)
        return
    logger.error("Failed to change album index! Album or photo not found.")
Пример #5
0
def preview():
    # Get the form fields.
    form = get_comment_form(request.form)
    thread = sanitize_name(form["thread"])

    # Trap fields.
    trap1 = request.form.get("website", "x") != "http://"
    trap2 = request.form.get("email", "x") != ""
    if trap1 or trap2:
        flash("Wanna try that again?")
        return redirect(url_for("index"))

    # Validate things.
    if len(form["message"]) == 0:
        flash("You must provide a message with your comment.")
        return redirect(form["url"])

    # Gravatar?
    gravatar = Comment.gravatar(form["contact"])

    # Are they submitting?
    if form["action"] == "submit":
        Comment.add_comment(
            thread=thread,
            uid=g.info["session"]["uid"],
            ip=remote_addr(),
            time=int(time.time()),
            image=gravatar,
            name=form["name"],
            subject=form["subject"],
            message=form["message"],
            url=form["url"],
        )

        # Are we subscribing to the thread?
        if form["subscribe"] == "true":
            email = form["contact"]
            if "@" in email:
                Comment.add_subscriber(thread, email)
                flash(
                    "You have been subscribed to future comments on this page."
                )

        flash("Your comment has been added!")
        return redirect(form["url"])

    # Gravatar.
    g.info["gravatar"] = gravatar
    g.info["preview"] = Comment.format_message(form["message"])
    g.info["pretty_time"] = pretty_time(Config.comment.time_format,
                                        time.time())

    g.info.update(form)
    return template("comment/preview.html")
Пример #6
0
def rename_album(old_name, new_name):
    """Rename an existing photo album.

    Returns True on success, False if the new name conflicts with another
    album's name."""
    old_name = sanitize_name(old_name)
    new_name = sanitize_name(new_name)
    index = get_index()

    # New name is unique?
    if new_name in index["albums"]:
        logger.error("Can't rename album: new name already exists!")
        return False

    def transfer_key(obj, old_key, new_key):
        # Reusable function to do a simple move on a dict key.
        obj[new_key] = obj[old_key]
        del obj[old_key]

    # Simple moves.
    transfer_key(index["albums"], old_name, new_name)
    transfer_key(index["covers"], old_name, new_name)
    transfer_key(index["photo-order"], old_name, new_name)
    transfer_key(index["settings"], old_name, new_name)

    # Update the photo -> album maps.
    for photo in index["map"]:
        if index["map"][photo] == old_name:
            index["map"][photo] = new_name

    # Fix the album ordering.
    new_order = list()
    for name in index["album-order"]:
        if name == old_name:
            name = new_name
        new_order.append(name)
    index["album-order"] = new_order

    # And save.
    write_index(index)
    return True
Пример #7
0
def rename_album(old_name, new_name):
    """Rename an existing photo album.

    Returns True on success, False if the new name conflicts with another
    album's name."""
    old_name = sanitize_name(old_name)
    new_name = sanitize_name(new_name)
    index = get_index()

    # New name is unique?
    if new_name in index["albums"]:
        logger.error("Can't rename album: new name already exists!")
        return False

    def transfer_key(obj, old_key, new_key):
        # Reusable function to do a simple move on a dict key.
        obj[new_key] = obj[old_key]
        del obj[old_key]

    # Simple moves.
    transfer_key(index["albums"], old_name, new_name)
    transfer_key(index["covers"], old_name, new_name)
    transfer_key(index["photo-order"], old_name, new_name)
    transfer_key(index["settings"], old_name, new_name)

    # Update the photo -> album maps.
    for photo in index["map"]:
        if index["map"][photo] == old_name:
            index["map"][photo] = new_name

    # Fix the album ordering.
    new_order = list()
    for name in index["album-order"]:
        if name == old_name:
            name = new_name
        new_order.append(name)
    index["album-order"] = new_order

    # And save.
    write_index(index)
    return True
Пример #8
0
def preview():
    # Get the form fields.
    form   = get_comment_form(request.form)
    thread = sanitize_name(form["thread"])

    # Trap fields.
    trap1 = request.form.get("website", "x") != "http://"
    trap2 = request.form.get("email", "x") != ""
    if trap1 or trap2:
        flash("Wanna try that again?")
        return redirect(url_for("index"))

    # Validate things.
    if len(form["message"]) == 0:
        flash("You must provide a message with your comment.")
        return redirect(form["url"])

    # Gravatar?
    gravatar = Comment.gravatar(form["contact"])

    # Are they submitting?
    if form["action"] == "submit":
        Comment.add_comment(
            thread=thread,
            uid=g.info["session"]["uid"],
            ip=remote_addr(),
            time=int(time.time()),
            image=gravatar,
            name=form["name"],
            subject=form["subject"],
            message=form["message"],
            url=form["url"],
        )

        # Are we subscribing to the thread?
        if form["subscribe"] == "true":
            email = form["contact"]
            if "@" in email:
                Comment.add_subscriber(thread, email)
                flash("You have been subscribed to future comments on this page.")

        flash("Your comment has been added!")
        return redirect(form["url"])

    # Gravatar.
    g.info["gravatar"]    = gravatar
    g.info["preview"]     = Comment.format_message(form["message"])
    g.info["pretty_time"] = pretty_time(Config.comment.time_format, time.time())

    g.info.update(form)
    return template("comment/preview.html")
Пример #9
0
def list_photos(album):
    """List the photos in an album."""
    album = sanitize_name(album)
    index = get_index()

    if not album in index["albums"]:
        return None

    result = []
    for key in index["photo-order"][album]:
        data = index["albums"][album][key]
        result.append(dict(
            key=key,
            data=data,
        ))

    return result
Пример #10
0
def list_photos(album):
    """List the photos in an album."""
    album = sanitize_name(album)
    index = get_index()

    if not album in index["albums"]:
        return None

    result = []
    for key in index["photo-order"][album]:
        data = index["albums"][album][key]
        result.append(dict(
            key=key,
            data=data,
        ))

    return result
Пример #11
0
def process_photo(form, filename):
    """Formats an incoming photo."""

    # Resize the photo to each of the various sizes and collect their names.
    sizes = dict()
    for size in PHOTO_SCALES.keys():
        sizes[size] = resize_photo(filename, size)

    # Remove the temp file.
    os.unlink(filename)

    # What album are the photos going to?
    album = form.get("album", "")
    new_album = form.get("new-album", None)
    new_desc = form.get("new-description", None)
    if album == "" and new_album:
        album = new_album

    # Sanitize the name.
    album = sanitize_name(album)
    if album == "":
        logger.warning(
            "Album name didn't pass sanitization! Fall back to default album name."
        )
        album = Config.photo.default_album

    # Make up a unique public key for this set of photos.
    key = random_hash()
    while photo_exists(key):
        key = random_hash()
    logger.debug("Photo set public key: {}".format(key))

    # Get the album index to manipulate ordering.
    index = get_index()

    # Update the photo data.
    if not album in index["albums"]:
        index["albums"][album] = {}
    if not "settings" in index:
        index["settings"] = dict()
    if not album in index["settings"]:
        index["settings"][album] = {
            "format": "classic",
            "description": new_desc,
        }

    index["albums"][album][key] = dict(ip=remote_addr(),
                                       author=g.info["session"]["uid"],
                                       uploaded=int(time.time()),
                                       caption=form.get("caption", ""),
                                       description=form.get("description", ""),
                                       **sizes)

    # Maintain a photo map to album.
    index["map"][key] = album

    # Add this pic to the front of the album.
    if not album in index["photo-order"]:
        index["photo-order"][album] = []
    index["photo-order"][album].insert(0, key)

    # If this is a new album, add it to the front of the album ordering.
    if not album in index["album-order"]:
        index["album-order"].insert(0, album)

    # Set the album cover for a new album.
    if not album in index["covers"] or len(index["covers"][album]) == 0:
        index["covers"][album] = key

    # Save changes to the index.
    write_index(index)

    return dict(success=True, photo=key)
Пример #12
0
def process_photo(form, filename):
    """Formats an incoming photo."""

    # Resize the photo to each of the various sizes and collect their names.
    sizes = dict()
    for size in PHOTO_SCALES.keys():
        sizes[size] = resize_photo(filename, size)

    # Remove the temp file.
    os.unlink(filename)

    # What album are the photos going to?
    album     = form.get("album", "")
    new_album = form.get("new-album", None)
    new_desc  = form.get("new-description", None)
    if album == "" and new_album:
        album = new_album

    # Sanitize the name.
    album = sanitize_name(album)
    if album == "":
        logger.warning("Album name didn't pass sanitization! Fall back to default album name.")
        album = Config.photo.default_album

    # Make up a unique public key for this set of photos.
    key = random_hash()
    while photo_exists(key):
        key = random_hash()
    logger.debug("Photo set public key: {}".format(key))

    # Get the album index to manipulate ordering.
    index = get_index()

    # Update the photo data.
    if not album in index["albums"]:
        index["albums"][album] = {}
    if not "settings" in index:
        index["settings"] = dict()
    if not album in index["settings"]:
        index["settings"][album] = {
            "format": "classic",
            "description": new_desc,
        }

    index["albums"][album][key] = dict(
        ip=remote_addr(),
        author=g.info["session"]["uid"],
        uploaded=int(time.time()),
        caption=form.get("caption", ""),
        description=form.get("description", ""),
        **sizes
    )

    # Maintain a photo map to album.
    index["map"][key] = album

    # Add this pic to the front of the album.
    if not album in index["photo-order"]:
        index["photo-order"][album] = []
    index["photo-order"][album].insert(0, key)

    # If this is a new album, add it to the front of the album ordering.
    if not album in index["album-order"]:
        index["album-order"].insert(0, album)

    # Set the album cover for a new album.
    if not album in index["covers"] or len(index["covers"][album]) == 0:
        index["covers"][album] = key

    # Save changes to the index.
    write_index(index)

    return dict(success=True, photo=key)