示例#1
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)
示例#2
0
def edit():
    """
    URLs:
    - /comic/edit?id=123

    Function:
    - Display a form so the user can edit an existing comic.

    Notes:
    - If the comic does not belong to the logged in user, they will be redirected
      to /private/comic.
    """
    # Find out which comic is being edited.
    requested_comic = request.vars.get('id')

    # For some reason, when the form is submitted, request.vars.id is a list
    # containing the same value twice.
    if type(requested_comic) == type([]):
        requested_comic = requested_comic[0]

    # 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 = 'Edit Comic'

    # Create a comic editing form, passing in the ID of the comic to set the
    # values of the form, and to put the SQLFORM into update mode.
    form = comic_edit_form(db, user_id=auth.user_id, submit_button='Save Changes', edit_comic_id=comic_id)

    # Validate and process the form.
    if form.process().accepted:
        # Return to the comic's page.
        redirect(URL('comic', 'index', vars={'id': comic.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)