Esempio n. 1
0
def challenge_user(_id):
    if _id == current_user._id:
        flash('You can not even challenge yourself. Nice try.', 'error')
        return redirect(url_for('user_list'))

    args = request.args

    # <-- receive parameters -->
    sort_key = args.get('sort')
    sort_direction = args.get('direction')
    page = args.get('page')
    filter_option = args.get('foption')
    filter_term = args.get('fterm')

    # <-- validate parameters and set fallback values -->
    sort_key_possible = ('name', 'owner', 'rating')
    sort_key = sort_key if sort_key in sort_key_possible else 'rating'

    sort_direction_possible = ('desc', 'asc')
    sort_direction = (sort_direction
                      if sort_direction in sort_direction_possible else 'desc')
    sort_direction_bool = sort_direction == 'desc'

    filter_option_possible = ('name', 'owner')
    filter_option = (filter_option
                     if filter_option in filter_option_possible else 'name')

    # filter_term: filter only applied if non-empty string given (None case)
    # TODO: filter_term validation analogous to tags/user input sanitization

    page = page or 1  # equals 1 if None; else: stays the same
    try:
        page = int(page)
    except ValueError:
        page = 1

    # <-- filter form -->
    form = ChallengeFilterForm()
    if form.validate_on_submit():
        filter_option = form.option.data
        filter_term = form.term.data

        kwargs = {key: value for key, value in args.items()}
        kwargs.update(sort=sort_key,
                      direction=sort_direction,
                      foption=filter_option,
                      fterm=filter_term,
                      page=1)

        return redirect(url_for('challenge_user', _id=_id, **kwargs))

    form.term.data = filter_term
    form.option.data = filter_option

    cardboxes = CardBox.fetch_all(db)

    # <-- filter process -->
    # checks for filter_option = 'name', 'owner' if term is part of string
    if filter_term:
        if filter_option == 'name' or filter_option == 'owner':
            cardboxes = [
                box for box in cardboxes
                if filter_term.lower() in getattr(box, filter_option).lower()
            ]
        else:
            cardboxes = [
                box for box in cardboxes
                if filter_term in getattr(box, filter_option)
            ]

    # <-- sort process -->
    def _sort_key_of(box):
        if sort_key == 'name':
            return box.name.lower()
        if sort_key == 'owner':
            return box.owner.lower()

        return getattr(box, sort_key)

    if cardboxes:
        cardboxes.sort(key=_sort_key_of, reverse=sort_direction_bool)

    # <-- pagination -->
    per_page = 50
    cardbox_count = len(cardboxes)
    page_range = utils.page_range(total_count=cardbox_count, per_page=per_page)
    page = (page if page in page_range else 1)

    pagination = utils.Pagination(parent=cardboxes,
                                  page=page,
                                  per_page=per_page,
                                  total_count=cardbox_count)

    # <-- standard values-->
    kwargs = {key: value for key, value in args.items()}
    kwargs.update(sort=sort_key,
                  direction=sort_direction,
                  foption=filter_option,
                  fterm=filter_term,
                  page=page,
                  _id=_id)

    # <-- creation of dynamic content -->
    pag_kwargs = dict(pagination=pagination,
                      endpoint='challenge_user',
                      prev='<',
                      next='>',
                      ellipses='...',
                      size='lg',
                      args=kwargs)

    def partner_id_producer(item):
        return _id

    wrapper = utils.TableItemWrapper(dict(partner_id=partner_id_producer))

    table = ChooseBoxTable(wrapper(pagination.items),
                           _id,
                           sort_reverse=sort_direction_bool,
                           sort_by=sort_key)

    return render_template('challenge.html',
                           _id=_id,
                           table=table,
                           filter_form=form,
                           pagination_kwargs=pag_kwargs,
                           active='versus')
Esempio n. 2
0
def huge_list():

    args = request.args

    sort_key = args.get('sort') or 'rating'
    sort_direction = args.get('direction')
    page = int(args.get('page')) if args.get('page') else 1

    if not sort_direction:
        sort_direction = True
    else:
        sort_direction = sort_direction == 'desc'

    filter_option = args.get('foption')
    filter_term = args.get('fterm')

    form = FilterForm()
    if form.validate_on_submit():
        filter_option = form.option.data
        filter_term = form.term.data

        kwargs = {key: value for key, value in args.items()}
        kwargs.update(foption=filter_option, fterm=filter_term, page=1)

        return redirect(url_for('huge_list', **kwargs))

    form.term.data = filter_term
    filter_option = filter_option or 'tags'
    form.option.data = filter_option

    def sort_key_of(box):
        if sort_key == 'name':
            return box.name.lower()
        if sort_key == 'owner':
            return box.owner.lower()

        return getattr(box, sort_key)

    cardboxes = CardBox.fetch_all(db)

    if filter_term:
        cardboxes = [
            c for c in cardboxes if filter_term in getattr(c, filter_option)
        ]

    if sort_key and cardboxes and hasattr(cardboxes[0], sort_key):
        cardboxes.sort(key=sort_key_of, reverse=sort_direction)
    else:
        sort_key = None

    pagination = utils.Pagination(parent=cardboxes,
                                  page=page,
                                  per_page=50,
                                  total_count=len(cardboxes))

    kwargs = {key: value for key, value in args.items()}
    kwargs.update(page=page, foption=filter_option, direction=sort_direction)

    pag_kwargs = dict(pagination=pagination,
                      endpoint='huge_list',
                      prev='<',
                      next='>',
                      ellipses='...',
                      size='lg',
                      args=kwargs)

    table = CardBoxTable(pagination.items,
                         sort_reverse=sort_direction,
                         sort_by=sort_key)

    return render_template('huge_list.html',
                           table=table,
                           filter_form=form,
                           pagination_kwargs=pag_kwargs)