Ejemplo n.º 1
0
def edit():
    """
    POST /comic/edit/:id

    Updates comic details.
    """

    comic = get_or_404(db.comic, request.args(0))

    # Ensure the user owns this comic
    if not comic_helpers.user_can_edit(db, comic.id, auth.user.id):
        flash_and_redirect_back('danger', 'You cannot edit a comic you did not create.')

    form = ComicForm(comic)

    if form.process().accepted:
        flash('info', 'Comic updated successfully.', comic.url)
    elif form.form.errors:
        flash('danger', 'Form has errors.')

    return {
        'form': form.form,
        'comic': comic,
        'owner': auth.user,
    }
Ejemplo n.º 2
0
def delete():
    """
    POST /comic/delete/:id

    Deletes a comic.
    """
    comic = get_or_404(db.comic, request.args(0))

    if not comic_helpers.user_can_edit(db, comic.id, auth.user.id):
        flash_and_redirect_back('danger', 'You cannot delete a comic you did not create.')

    comic.delete_record()
    flash_and_redirect_back('info', 'Deleted %s.' % comic.full_name,
                            default=URL('collection', 'view', args=[auth.user.id]),
                            avoid='/comic/view')
Ejemplo n.º 3
0
def set_privacy():
    """
    GET /box/set_privacy/:id?privacy=(private|public)

    Updates the privacy of a box from private <-> public.
    """
    box = get_or_404(db.box, request.args(0), owner=auth.user.id)

    new_privacy_str = request.get_vars['privacy']
    privacy_str_to_private_bool = {
        'private': True,
        'public': False
    }

    new_private_value = privacy_str_to_private_bool.get(new_privacy_str)
    if new_private_value is None:
        flash_and_redirect_back('danger', 'Invalid privacy option for box.')

    box.update_record(private=new_private_value)
    flash_and_redirect_back('info', 'Box is now %s.' % new_privacy_str)
Ejemplo n.º 4
0
def remove_comic():
    """
    POST /box/remove_comic

    Removes a comic from a box and ensures it is in at least the 'Unfiled' box.
    """

    box = get_or_404(db.box, request.post_vars['box'], owner=auth.user.id)
    comic = get_or_404(db.comic, request.post_vars['comic'])

    if box.is_unfiled:
        flash_and_redirect_back('danger', 'A comic cannot be removed from the Unfiled box.')

    db(db.comicbox.box == box.id)(db.comicbox.comic == comic.id).delete()

    # if the comic no longer belongs to any boxes, add it to the 'Unfiled' box
    if db(db.comicbox.comic == comic.id).isempty():
        db.comicbox.insert(comic=comic.id, box=_unfiled_box().id)

    flash_and_redirect_back('info', 'Removed %s from %s.' % (comic.full_name, box.name))
Ejemplo n.º 5
0
def add_comic():
    """
    POST /box/add_comic

    Adds a comic to a box, also may create or update an existing box if 'new box' was specified.
    The 'comic' POST var can occur multiple times to add many comics to a single box in one operation.
    """

    # Create a new box
    if request.post_vars['box'] == 'new':
        target_box = db.box(db.box.name.like(request.post_vars['name']) & (db.box.owner == auth.user.id))
        private = bool(request.post_vars['private'])

        if target_box:
            target_box.update(private=private)
        else:
            target_box_id = db.box.insert(name=request.post_vars['name'], owner=auth.user.id, private=private)
            target_box = db.box[target_box_id]
    else:
        target_box = get_or_404(db.box, request.post_vars['box'], owner=auth.user.id)

    raw_comic = request.post_vars['comic']

    if raw_comic is None:
        flash_and_redirect_back('warning', 'No comics selected.')

    comics = raw_comic if isinstance(raw_comic, list) else [raw_comic]

    for source_comic_id in comics:
        source_comic = get_or_404(db.comic, source_comic_id)

        # Is the comic already in the box we want to add it to?
        if db.comicbox((db.comicbox.box == target_box.id) & (db.comicbox.comic == source_comic.id)):
            flash_and_redirect_back('warning', 'This comic already exists in %s.' % target_box.name)

        # If this user doesn't own the comic, duplicate it
        if db(db.box.owner == auth.user.id)(db.comicbox.box == db.box.id)(db.comicbox.comic == source_comic.id).isempty():
            target_comic_id = db.comic.insert(
                publisher=source_comic.publisher,
                title=source_comic.title,
                issue=source_comic.issue,
                description=source_comic.description,
                cover_image=source_comic.cover_image
            )

            for comicwriter in source_comic.comicwriter.select():
                db.comicwriter.insert(writer=comicwriter.writer, comic=target_comic_id)

            for comicartist in source_comic.comicartist.select():
                db.comicartist.insert(artist=comicartist.artist, comic=target_comic_id)

        elif target_box.is_unfiled:
            return flash_and_redirect_back('danger',
                                           'This comic cannot be added to "Unfiled" as it is already belongs to a box.')

        else:
            target_comic_id = source_comic.id

        # Add comic to box
        db.comicbox.insert(comic=target_comic_id, box=target_box.id)

        # Find the Unfiled box for this user
        if not target_box.is_unfiled:
            db((db.comicbox.comic == target_comic_id) & (db.comicbox.box == _unfiled_box().id)).delete()

    flash_text = 'Added comic%s to %s.' % ('s' if len(comics) > 1 else '', target_box.name)

    # if we just duplicated this comic, redirect to the new comic
    if source_comic.owner.id != auth.user.id:
        flash('success', flash_text, URL('comic', 'view', args=[target_comic_id]))

    # otherwise, redirect back
    else:
        flash_and_redirect_back('success', flash_text)