示例#1
0
def box_selection_form(db, user_id, submit_button='Submit'):
    """
    Create a FORM object which is used to select a box from a list of
    the user's existing boxes.
    """
    # Get a list of the user's boxes.
    boxes = db_access.get_user_boxes(user_id, include_private=True)

    # Create a dropdown item for each box the user owns, where the displayed
    # text is the name of the box, but the 'value' of the item is the box ID.
    dropdown_items = [OPTION(box.name, _value=box.id) for box in boxes]

    # Create the actual dropdown, passing in the list of items and expanding
    # the list into function arguments.
    dropdown = SELECT(*dropdown_items, _name='box_id', _class='form-control')

    # Embed the dropdown in DIVs to give the correct bootstrap styles and to
    # provide a label.
    dropdown_wrapped = DIV(
                          LABEL('File in Box', _for='box_id', _class='col-md-3 control-label'),
                          DIV(
                              dropdown,
                              _class='col-md-9'
                          ),
                          P('Comics can be moved to other boxes later when viewing your collection.', _class='help-block col-md-9 col-md-offset-3'),
                          _class='form-group'
                       )

    # Create the actual form, containing the dropdown and a submit button.
    form = FORM(
        # Dropdown for box
        DIV(
            # "File in Box" label
            LABEL('File in Box', _for='box_id', _class='col-md-2 control-label'),
            # Textbox container to set width - form[0][1]
            DIV(
                # Actual dropdown
                dropdown,
                _class='col-md-10'
            ),
            P('Comics can be moved to other boxes later when viewing your collection.', _class='help-block col-md-10 col-md-offset-2'),
            _class='form-group'
        ),

        # Group for submit button
        DIV(
            # Container to set width
            DIV(
                # Actual submit button
                INPUT(_type='submit', _class='btn btn-default', _value=submit_button),
                _class='col-md-10 col-md-offset-2'
            ),
            _class='form-group'
        ),

        # Form attributes
        _id='box-selection-form', _class='form-horizontal'
    )

    return form
示例#2
0
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
示例#3
0
def comic_edit_form(db, user_id, submit_button='Submit', edit_comic_id=None):
    """
    Create a FORM object for editing a comic. The user_id parameter should be set
    to the ID of the user creating or editing the comic, so that a list of their
    boxes can be retrieved. The submit_button parameter changes the text displayed
    in the submit button.
    If an existing comic is being edited, pass its ID as edit_comic_id.
    """
    # The owner_id field will be set automatically based on the currently
    # logged in user, so the form should contain every field except that one.
    fields = [field.name for field in db.Comic if field.name != 'owner_id']

    # Create a help message for the cover_image field.
    help_text = {
        'cover_image': 'Cover image must be no larger than 300 pixels wide by 400 pixels tall.'
    }

    # If a comic is being edited, append some additional text to the cover_image help text.
    if edit_comic_id is not None:
        help_text['cover_image'] += ' Leave this blank to keep the existing cover image, or upload a new image to replace the existing one.'

    # Create an SQLFORM, which will create all the fields automatically,
    # including handling the image upload field.
    # The buttons parameter is set to an empty list to remove the
    # generated submit button.
    # Deletable is set to false to hide the checkbox which allows the user to
    # delete the comic from this form, since that is done on a separate page.
    # Pass edit_comic_id in to change the form to an update form. Otherwise,
    # passing record=None will create an insert form.
    # Set showid to False because the user does not need to care what the ID of
    # the comic is.
    form = SQLFORM(db.Comic, record=edit_comic_id, fields=fields, buttons=[], col3=help_text, deletable=False, showid=False)

    # Create a custom submit button with the specified text, to avoid the
    # 'btn-primary' class added by web2py which makes the button blue.
    # It must be wrapped in DIVs to provide the correct bootstrap styles.
    button = DIV(
                DIV(
                    INPUT(_type='submit', _class='btn btn-default', _value=submit_button),
                    _class='col-md-9 col-md-offset-3'
                ),
                _class='form-group'
            )

    # Insert the button at position 99, because that will ensure it always
    # gets added to the end of the form, even if new fields are added later.
    form[0].insert(99, button)

    # Create a dropdown for the user to select a box to file the new comic
    # into. This will only be done if a new comic is being added, because
    # putting the dropdown on the edit form would lead to confusion and
    # possibly duplicate filings.
    if edit_comic_id is None:
        # Retrieve a list of the user's boxes from the database.
        user_boxes = db_access.get_user_boxes(user_id, include_private=True)

        # Create a dropdown item for each box the user owns, where the displayed
        # text is the name of the box, but the 'value' of the item is the box ID.
        dropdown_items = [OPTION(box.name, _value=box.id) for box in user_boxes]

        # Create the actual dropdown, passing in the list of items and expanding
        # the list into function arguments.
        dropdown = SELECT(*dropdown_items, _name='box_id', _class='form-control')

        # Embed the dropdown in DIVs to give the correct bootstrap styles and to
        # provide a label.
        dropdown_wrapped = DIV(
                              LABEL('File in Box', _for='box_id', _class='col-md-3 control-label'),
                              DIV(
                                  dropdown,
                                  _class='col-md-9'
                              ),
                              P('Comics can be moved to other boxes later when viewing your collection.', _class='help-block col-md-9 col-md-offset-3'),
                              _class='form-group'
                           )

        # Insert the dropdown into the form, at the end.
        form[0].insert(-1, dropdown_wrapped)

    return form