def __init__(self, record=None): if record: self.id = record.id artists = record.comicartist(db.artist.id == db.comicartist.artist).select() record.artists = ';'.join(map(lambda row: row.artist.name, artists)) writers = record.comicwriter(db.writer.id == db.comicwriter.writer).select() record.writers = ';'.join(map(lambda row: row.writer.name, writers)) record.publisher = record.publisher.name else: self.id = None fields = [field for field in db.comic if field.name != 'publisher'] # Only show a 'box' field if we're creating the comic if not self.id: user_boxes = [(row.id, row.name) for row in db(db.box.owner == auth.user.id).select(orderby=db.box.id)] fields.insert(0, Field('box', 'reference box', requires=IS_IN_SET(user_boxes, zero=None))) fields.insert(3, Field('publisher', 'string', requires=IS_NOT_EMPTY())) fields.insert(4, Field('artists', 'string', requires=IS_NOT_EMPTY())) fields.insert(5, Field('writers', 'string', requires=IS_NOT_EMPTY())) self.form = SQLFORM.factory( *fields, record=record, table_name='comic', showid=False ) add_element_required_attr(db.comic, self.form)
def create(): """ POST /box/create Creates a new box for this user. """ form = SQLFORM(db.box, fields=['name', 'private']) add_element_required_attr(db.box, form) form.vars.owner = auth.user if form.process(onvalidation=_validate_box_form).accepted: flash('success', 'Created box.', URL('box', 'view', args=[form.vars.id])) elif form.errors: flash('danger', 'Form has errors.') return { 'form': form }
def view(): """ GET /box/view/:id Views a box, ensures that the logged in user has permission to view it. """ user_id = auth.user.id if auth.is_logged_in() else 0 box = get_or_404(db.box, ((db.box.id == request.args(0)) & ((db.box.owner == user_id) | (db.box.private == False)))) # find all comics in this box comics = db(db.comicbox.comic == db.comic.id)(db.comicbox.box == box.id).select(db.comic.ALL) comic_ids = map(lambda c: c.id, comics) # find all the comics owned by this user that aren't already in the box other_comics = db(db.comic.id == db.comicbox.comic)(db.box.id == db.comicbox.box)( db.box.owner == user_id).select(db.comic.ALL, groupby=db.comic.id) other_comics = filter(lambda c: c.id not in comic_ids, other_comics) user_owned = user_id == box.owner.id can_edit = user_owned and not box.is_unfiled if can_edit: rename_form = SQLFORM(db.box, box, fields=['name'], showid=False) add_element_required_attr(db.box, rename_form) if rename_form.process(onvalidation=_validate_box_form).accepted: flash('info', 'Box renamed successfully.', request.env['PATH_INFO']) elif rename_form.errors: flash('danger', 'Form has errors.') else: rename_form = None return { 'box': box, 'comics': comics, 'other_comics': other_comics, 'can_edit': can_edit, 'user_owned': user_owned, 'rename_form': rename_form }