def category():
    ignored_submission_id = myconf.take(
        'omp.ignore_submissions') if myconf.take(
            'omp.ignore_submissions') else -1

    if request.args == []:
        redirect(URL('home', 'index'))
    category_path = request.args[0]

    ompdal = OMPDAL(db, myconf)

    press = ompdal.getPress(myconf.take('omp.press_id'))
    if not press:
        redirect(URL('home', 'index'))

    category_row = ompdal.getCategoryByPathAndPress(category_path,
                                                    press.press_id)

    if not category_row:
        redirect(URL('home', 'index'))

    category = OMPItem(
        category_row,
        OMPSettings(ompdal.getCategorySettings(category_row.category_id)))
    submission_rows = ompdal.getSubmissionsByCategory(
        category_row.category_id,
        ignored_submission_id=ignored_submission_id,
        status=3)
    submissions = []
    for submission_row in submission_rows:
        contributors_by_group = defaultdict(list)
        for contrib in ompdal.getAuthorsBySubmission(
                submission_row.submission_id, filter_browse=True):
            contrib_item = OMPItem(
                contrib,
                OMPSettings(ompdal.getAuthorSettings(contrib.author_id)))
            contributors_by_group[contrib.user_group_id].append(contrib_item)

        editors = contributors_by_group[myconf.take('omp.editor_id', cast=int)]
        authors = contributors_by_group[myconf.take('omp.author_id', cast=int)]
        chapter_authors = contributors_by_group[myconf.take(
            'omp.chapter_author_id', cast=int)]
        translators = []
        if myconf.get('omp.translator_id'):
            translators = contributors_by_group[int(
                myconf.take('omp.translator_id'))]
        submission = OMPItem(
            submission_row,
            OMPSettings(
                ompdal.getSubmissionSettings(submission_row.submission_id)), {
                    'authors': authors,
                    'editors': editors,
                    'chapter_authors': chapter_authors
                })
        if authors:
            attribution = ompformat.formatContributors(authors,
                                                       max_contributors=4,
                                                       with_and=True)
            additional_attribution = ompformat.formatAttribution(
                editors, [], translators, [])
        else:
            attribution = ompformat.formatAttribution(editors, [], [],
                                                      chapter_authors)
            additional_attribution = ""
        submission.attribution = attribution
        submission.additional_attribution = additional_attribution

        series_row = ompdal.getSeries(submission_row.series_id)
        if series_row:
            submission.associated_items['series'] = OMPItem(
                series_row,
                OMPSettings(ompdal.getSeriesSettings(series_row.series_id)))

        category_row = ompdal.getCategoryBySubmissionId(
            submission_row.submission_id)
        if category_row:
            submission.associated_items['category'] = OMPItem(
                category_row,
                OMPSettings(
                    ompdal.getCategorySettings(category_row.category_id)))

        publication_dates = [
            ompformat.dateFromRow(pd)
            for pf in ompdal.getAllPublicationFormatsBySubmission(
                submission_row.submission_id)
            for pd in ompdal.getPublicationDatesByPublicationFormat(
                pf.publication_format_id)
        ]
        if publication_dates:
            submission.associated_items[
                'publication_dates'] = publication_dates
        submissions.append(submission)

    sortby = ompdal.getCategorySettings(category_row.category_id).find(
        lambda row: row.setting_name == 'sortOption').first()
    if sortby:
        b = Browser(submissions, 0, locale, 100, sortby.get('setting_value'),
                    [])
        submissions = b.process_submissions(submissions)

    return locals()
def index():
    ompdal = OMPDAL(db, myconf)
    press = ompdal.getPress(myconf.take('omp.press_id'))
    editor_group_id = myconf.take('omp.editor_id', cast=int)
    author_group_id = myconf.take('omp.author_id', cast=int)
    chapter_author_group_id = myconf.take('omp.chapter_author_id', cast=int)
    translator_group_id = int(myconf.get('omp.translator_id')) if myconf.get(
        'omp.translator_id') else None

    if not press:
        redirect(URL('home', 'index'))
    press_settings = OMPSettings(ompdal.getPressSettings(press.press_id))

    ignored_submission_id = myconf.take(
        'omp.ignore_submissions') if myconf.take(
            'omp.ignore_submissions') else -1

    submissions = []
    submission_rows = ompdal.getSubmissionsByPress(press.press_id,
                                                   ignored_submission_id)

    for submission_row in submission_rows:
        # Get contributors and contributor settings
        contributors_by_group = defaultdict(list)
        for contrib in ompdal.getAuthorsBySubmission(
                submission_row.submission_id, filter_browse=True):
            contrib_item = OMPItem(
                contrib,
                OMPSettings(ompdal.getAuthorSettings(contrib.author_id)))
            contributors_by_group[contrib.user_group_id].append(contrib_item)

        editors = contributors_by_group[editor_group_id]
        authors = contributors_by_group[author_group_id]
        chapter_authors = contributors_by_group[chapter_author_group_id]
        translators = []

        if translator_group_id:
            translators = contributors_by_group[translator_group_id]
        publication_dates = [
            ompformat.dateFromRow(pd)
            for pf in ompdal.getAllPublicationFormatsBySubmission(
                submission_row.submission_id, available=True, approved=True)
            for pd in ompdal.getPublicationDatesByPublicationFormat(
                pf.publication_format_id)
        ]
        for s in ompdal.getDigitalPublicationFormats(
                submission_row.submission_id, available=True, approved=True):
            if s['remote_url']:
                frontpage_url = s['remote_url']
                break
        else:
            frontpage_url = URL('book', args=[submission_row.submission_id])
        submission = OMPItem(
            submission_row,
            OMPSettings(
                ompdal.getSubmissionSettings(submission_row.submission_id)), {
                    'authors': authors,
                    'editors': editors,
                    'translators': translators,
                    'chapter_authors': chapter_authors,
                    'frontpage_url': frontpage_url
                })
        if authors:
            attribution = ompformat.formatContributors(authors,
                                                       max_contributors=4,
                                                       with_and=True)
            additional_attribution = ompformat.formatAttribution(
                editors, [], translators, [])
        else:
            attribution = ompformat.formatAttribution(editors, [], [],
                                                      chapter_authors)
            additional_attribution = ""
        submission.attribution = attribution
        submission.additional_attribution = additional_attribution
        category_row = ompdal.getCategoryBySubmissionId(
            submission_row.submission_id)
        if category_row:
            submission.associated_items['category'] = OMPItem(
                category_row,
                OMPSettings(
                    ompdal.getCategorySettings(category_row.category_id)))

        series_row = ompdal.getSeries(submission_row.series_id)
        if series_row:
            submission.associated_items['series'] = OMPItem(
                series_row,
                OMPSettings(ompdal.getSeriesSettings(series_row.series_id)))
        if publication_dates:
            submission.associated_items[
                'publication_dates'] = publication_dates

        submissions.append(submission)

    session.filters = request.vars.get('filter_by').strip('[').strip(
        ']') if request.vars.get('filter_by') else session.get('filters', '')
    session.per_page = int(
        request.vars.get('per_page')) if request.vars.get('per_page') else int(
            session.get('per_page', 20))
    if request.vars.get('sort_by'):
        session.sort_by = request.vars.get('sort_by')
    elif session.get('sort_by'):
        session.sort_by = session.get('sort_by')
    else:
        session.sort_by = 'datePublished-2'

    current = int(request.vars.get('page_nr', 1)) - 1

    b = Browser(submissions, current, locale, session.get('per_page'),
                session.get('sort_by'), session.get('filters'))
    submissions = b.process_submissions(submissions)

    return locals()