def filing_form(db, user_id, comic_id, submit_button='Submit'): """ Create a FORM object where the user can select which boxes to file a comic in. """ # Get a list of the user's boxes. boxes = db_access.get_user_boxes(user_id, include_private=True) # Find which boxes the comic is already filed in. filed_boxes = [box.id for box in db_access.get_boxes_for_comic(comic_id, include_private=True)] # Create a list of objects, to put into the form. form_items = [] # List of 4 lists, one for each column of checkboxes. columns = [ [], [], [], [] ] # Add a checkbox for each of the user's boxes to the next available column. for index, box in enumerate(boxes): column = index % 4 columns[column].append( DIV( LABEL( INPUT(_type='checkbox', _name='boxes', _value=box.id, value=(box.id in filed_boxes)), box.name ), _class='checkbox' ) ) # Create a DIV for each column, which will be centered within its parent. for column in columns: form_items.append( DIV( DIV( *column, _class='column' ), _class='column-parent col-md-3' ) ) # Add the submit button. form_items.append( DIV( # Container to set width DIV( # Actual submit button INPUT(_type='submit', _class='btn btn-default', _value=submit_button), _class='col-md-12 text-center' ), _class='form-group' ) ) # Create the actual form by expanding the list of checkboxes, then adding # the submit button. form = FORM(*form_items, _id='filing-form', _class='form-horizontal') return form
def index(): """ URLs: - /comic?id=123 Function: - Display the details of the comic with the given ID. """ # Get the ID of the comic to display. 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 and its owner. comic = db_access.get_comic(requested_comic) # If the requested comic doesn't exist, throw a 404 error. if comic is None: raise HTTP(404) # Get the details of the comic's owner. user = db_access.get_user(comic.owner_id) # Get a list of the boxes the comic is stored in. If the comic's owner is # the currently logged in user, this can include private boxes. include_private = (comic.owner_id == user.id) boxes = db_access.get_boxes_for_comic(requested_comic, include_private=include_private) # If the comic is only filed in private boxes and the current user doesn't # own the comic, it shouldn't be visible. In this case, since the query # above would have excluded private boxes, then no boxes will have been # returned. has_permission = len(boxes) > 0 # If the user doesn't have permission, redirect to the private page. if not has_permission: redirect(URL('private', 'index', args='comic')) # Set the title of the page to show the user where they are. response.title = "{title} #{issue}".format(title=comic.title, issue=comic.issue_number) # Create a link to the comic's owner. user_link = A(user.username, _href=URL('user', 'index', vars={'id': user.id})) # Display the owner of the comic below the title. response.subtitle = 'Owned by {user}'.format(user=user_link) # The action buttons depend on whether the comic belongs to the logged-in # user. They shouldn't be able to delete or edit someone else's comic, or # copy their own comic to their collection. if comic.owner_id == auth.user_id: # Add buttons to the top of the page to edit, delete, and file the comic. page_actions = [ ('File in Boxes', URL('comic', 'file', vars={'id': comic.id}), 'credit-card'), ('Edit', URL('comic', 'edit', vars={'id': comic.id}), 'pencil'), ('Delete', URL('comic', 'confirm_delete', vars={'id': comic.id}), 'trash') ] elif auth.user_id is not None: # Add a button to copy the comic to the current user's collection, if a # user is actually logged in. page_actions = [ ('Copy to My Collection', URL('comic', 'copy', vars={'id': comic.id}), 'duplicate') ] else: page_actions = [] return dict(comic=comic, user=user, boxes=boxes, has_permission=has_permission, page_actions=page_actions)