Exemplo n.º 1
0
def copy():
    """
    URLs:
    - /comic/copy?id=123

    Function:
    - Ask the user which box to file a copy of a comic into, and then perform
      the copy operation.
    """
    # Find out which comic is being copied.
    requested_comic = request.vars.get('id')

    # If a malformed ID (or no ID) is passed in, throw a 404 error.
    try:
        comic_id = int(requested_comic)
    except ValueError as error:
        raise HTTP(404)
    
    # Get the details of the comic.
    comic = db_access.get_comic(requested_comic)

    # If the requested comic doesn't exist, throw a 404.
    if comic is None:
        raise HTTP(404)

    # Get the id of the current user.
    current_user = auth.user_id

    # Get the details of the comic's owner.
    owner = db_access.get_user(comic.owner_id)

    # Set the title of the page.
    response.title = 'Copy Comic'

    # Remind the user what comic they are copying.
    response.subtitle = 'Copying {title} #{issue} from {name}'.format(title=comic.title,
                                                                      issue=comic.issue_number,
                                                                      name=owner.username)

    # Create a box selection form populated with a list of the user's boxes.
    form = box_selection_form(db, current_user, submit_button='Copy')

    # The form has no validation so it will always be accepted.
    if form.process().accepted:
        # Perform the copy operation, assigning the comic to the new user.
        new_comic_id = db_access.copy_comic(comic_id, current_user)

        # Create a filing between the new comic and the selected box.
        db_access.create_filing(new_comic_id, form.vars.box_id)

        # Take the user to their new comic.
        redirect(URL('comic', 'index', vars={'id': new_comic_id}))

    return dict(form=form)
Exemplo n.º 2
0
def add():
    """
    URLs:
    - /comic/add

    Function:
    - Provides a form for the user to enter the details of a new comic to add
      to their collecion.
    """
    # Create a form for the user to enter the details into.
    form = comic_edit_form(db, user_id=auth.user_id, submit_button='Add Comic')

    # Automatically fill in the owner_id field of the form, which is hidden so
    # the user cannot edit it.
    form.vars.owner_id = auth.user_id

    # Set the title of the page.
    response.title = 'Add Comic'

    # Validate and process the form.
    if form.process().accepted:
        # Create a filing between the new comic and the box that the user chose,
        # then take the user to the new comic.
        db_access.create_filing(form.vars.id, form.vars.box_id)
        redirect(URL('comic', 'index', vars={'id': form.vars.id}))

    # If there are errors in the form, highlight the appropriate sections in red.
    if form.errors is not None:
        if len(form.errors) > 0:
            # Highlight the title field if it wasn't entered.
            if form.errors.title is not None:
                form[0][0]['_class'] += ' has-error'

            # Highlight the issue number field if that wasn't entered.
            if form.errors.issue_number is not None:
                form[0][1]['_class'] += ' has-error'

            # Highlight the image upload field if the image was too large.
            if form.errors.cover_image is not None:
                form[0][2]['_class'] += ' has-error'
                
    return dict(form=form)
Exemplo n.º 3
0
def file():
    """
    URLs:
    - /comic/file?id=123

    Function:
    - Allow the user to select which boxes to file a comic into.
    """
    # Find out which comic is being filed.
    requested_comic = request.vars.get('id')

    # If a malformed ID (or no ID) is passed in, throw a 404 error.
    try:
        comic_id = int(requested_comic)
    except ValueError as error:
        raise HTTP(404)
    
    # Get the details of the comic.
    comic = db_access.get_comic(requested_comic)

    # If the requested comic doesn't exist, throw a 404.
    if comic is None:
        raise HTTP(404)

    # Get the id of the current user.
    current_user = auth.user_id

    # If the comic doesn't belong to the current user, redirect to the private page.
    if comic.owner_id != current_user:
        redirect(URL('private', 'index', args='comic', vars={'edit': 'true'}))

    # Set the title of the page.
    response.title = 'File Comic'
    response.subtitle = 'Choose boxes to file {title} #{issue} into'.format(title=comic.title,
                                                                            issue=comic.issue_number)

    # Create a form for the user to choose boxes from.
    form = filing_form(db, current_user, comic_id, 'File Comic')

    # Process the form. A custom validation function is used to ensure that at
    # least one box is selected.
    if form.process(onvalidation=validate_filing).accepted:
        # The validation was successful. Unfile the comic from all boxes, then
        # file it into the chosen boxes.
        db_access.delete_filings_for_comic(comic_id)

        # If only one box is selected, form.vars.boxes will be a string instead
        # of a list, meaning it cannot be iterated over correctly. Put it in a
        # list so it can.
        if type(form.vars.boxes) == type(''):
            boxes = [form.vars.boxes]
        else:
            boxes = form.vars.boxes

        for box_id in boxes:
            db_access.create_filing(comic_id, box_id)

        # Take the user back to the comic page.
        redirect(URL('comic', 'index', vars={'id': comic_id}))

    # If the validation fails, add an error message above the submit button.
    elif form.errors.error is not None:
        error_message = DIV(
                            DIV(
                                DIV('Please choose at least one box to file the comic into.', _class='error'),
                                _class='col-md-12 text-center'
                            ),
                            _class='form-group'
                        )
        form.insert(-1, error_message)

    return dict(form=form)