def index(): if session.forced_language == "en": locale = "en_US" elif session.forced_language == "de": locale = "de_DE" else: locale = "" ompdal = OMPDAL(db, myconf) press = ompdal.getPress(myconf.take("omp.press_id")) 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 = [] for submission_row in ompdal.getSubmissionsByPress(press.press_id, ignored_submission_id): authors = [ OMPItem(author, OMPSettings(ompdal.getAuthorSettings(author.author_id))) for author in ompdal.getAuthorsBySubmission(submission_row.submission_id) ] editors = [ OMPItem(editor, OMPSettings(ompdal.getAuthorSettings(editor.author_id))) for editor in ompdal.getEditorsBySubmission(submission_row.submission_id) ] publication_dates = [ dateFromRow(pd) for pf in ompdal.getAllPublicationFormatsBySubmission( submission_row.submission_id, available=True, approved=True ) for pd in ompdal.getPublicationDatesByPublicationFormat(pf.publication_format_id) ] submission = OMPItem( submission_row, OMPSettings(ompdal.getSubmissionSettings(submission_row.submission_id)), {"authors": authors, "editors": editors}, ) 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) submissions = sorted( submissions, key=lambda s: min(s.associated_items.get("publication_dates", [datetime(1, 1, 1)])), reverse=True ) return locals()
def series(): if session.forced_language == "en": locale = "en_US" elif session.forced_language == "de": locale = "de_DE" else: locale = "" ignored_submission_id = myconf.take("omp.ignore_submissions") if myconf.take("omp.ignore_submissions") else -1 if request.args == []: redirect(URL("home", "index")) series_path = request.args[0] ompdal = OMPDAL(db, myconf) press = ompdal.getPress(myconf.take("omp.press_id")) if not press: redirect(URL("home", "index")) series_row = ompdal.getSeriesByPathAndPress(series_path, press.press_id) # If series path is unknown if not series_row: redirect(URL("home", "index")) series = OMPItem(series_row, OMPSettings(ompdal.getSeriesSettings(series_row.series_id))) submission_rows = ompdal.getSubmissionsBySeries( series_row.series_id, ignored_submission_id=ignored_submission_id, status=3 ) submissions = [] for submission_row in submission_rows: authors = [ OMPItem(author, OMPSettings(ompdal.getAuthorSettings(author.author_id))) for author in ompdal.getAuthorsBySubmission(submission_row.submission_id) ] editors = [ OMPItem(editor, OMPSettings(ompdal.getAuthorSettings(editor.author_id))) for editor in ompdal.getEditorsBySubmission(submission_row.submission_id) ] submission = OMPItem( submission_row, OMPSettings(ompdal.getSubmissionSettings(submission_row.submission_id)), {"authors": authors, "editors": editors}, ) submissions.append(submission) submissions = sorted(submissions, cmp=seriesPositionCompare, reverse=True) series.associated_items["submissions"] = submissions return locals()
def index(): ompdal = OMPDAL(db, myconf) json_list = dict(xml_url='') if len(request.args) < 2: raise HTTP(404) submission_id = request.args[0] file_id = request.args[1] if str(file_id).endswith('.xml'): path = os.path.join(request.folder, 'static/files/presses', myconf.take('omp.press_id'), 'monographs', submission_id, 'submission/proof', file_id) if os.path.exists(path) is False: raise HTTP(404) submission_settings = ompdal.getSubmissionSettings(submission_id) series = ompdal.getSeriesBySubmissionId(submission_id) series_settings = ompdal.getSeriesSettings( series.get('series_id')) if series else [] cssStyles = get_setting_value(series_settings, 'cssStyles') if not cssStyles: cssStyles = get_setting_value(submission_settings, 'cssStyles') cssStylesStr = cssStyles.replace("'", "\"") if len(cssStyles) > 0 else '{}' cssStylesDict = json.loads(cssStylesStr) font_family = cssStylesDict.get('font-face').get( 'family') if cssStylesDict.get('font-face') else 'Source Sans Pro' authors = [ OMPItem(author, OMPSettings(ompdal.getAuthorSettings(author.author_id))) for author in ompdal.getAuthorsBySubmission(submission_id) ] authors_string = ', '.join( (ompformat.formatName(a.settings) for a in authors)) return dict(json_list=XML(gluon.serializers.json(json_list)), authors=authors_string, font_family=font_family) else: path = os.path.join(request.folder, 'static/files/presses', myconf.take('omp.press_id'), 'monographs', submission_id, 'submission/', file_id) return response.stream(path, chunk_size=1048576)
def series(): if session.forced_language == 'en': locale = 'en_US' elif session.forced_language == 'de': locale = 'de_DE' else: locale = '' ignored_submission_id = myconf.take('omp.ignore_submissions') if myconf.take('omp.ignore_submissions') else -1 if request.args == []: redirect( URL('home', 'index')) series_path = request.args[0] ompdal = OMPDAL(db, myconf) press = ompdal.getPress(myconf.take('omp.press_id')) if not press: redirect(URL('home', 'index')) series_row = ompdal.getSeriesByPathAndPress(series_path, press.press_id) # If series path is unknown if not series_row: redirect(URL('home', 'index')) series = OMPItem(series_row, OMPSettings(ompdal.getSeriesSettings(series_row.series_id))) submission_rows = ompdal.getSubmissionsBySeries(series_row.series_id, ignored_submission_id=ignored_submission_id, status=3) submissions = [] for submission_row in submission_rows: authors = [OMPItem(author, OMPSettings(ompdal.getAuthorSettings(author.author_id))) for author in ompdal.getAuthorsBySubmission(submission_row.submission_id)] editors = [OMPItem(editor, OMPSettings(ompdal.getAuthorSettings(editor.author_id))) for editor in ompdal.getEditorsBySubmission(submission_row.submission_id)] submission = OMPItem(submission_row, OMPSettings(ompdal.getSubmissionSettings(submission_row.submission_id)), {'authors': authors, 'editors': editors} ) submissions.append(submission) submissions = sorted(submissions, cmp=seriesPositionCompare, reverse=True) series.associated_items['submissions'] = submissions return locals()
def index(): if session.forced_language == 'en': locale = 'en_US' elif session.forced_language == 'de': locale = 'de_DE' else: locale = '' ompdal = OMPDAL(db, myconf) press = ompdal.getPress(myconf.take('omp.press_id')) 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 = [] for submission_row in ompdal.getSubmissionsByPress(press.press_id, ignored_submission_id): authors = [OMPItem(author, OMPSettings(ompdal.getAuthorSettings(author.author_id))) for author in ompdal.getAuthorsBySubmission(submission_row.submission_id)] editors = [OMPItem(editor, OMPSettings(ompdal.getAuthorSettings(editor.author_id))) for editor in ompdal.getEditorsBySubmission(submission_row.submission_id)] publication_dates = [dateFromRow(pd) for pf in ompdal.getAllPublicationFormatsBySubmission(submission_row.submission_id, available=True, approved=True) for pd in ompdal.getPublicationDatesByPublicationFormat(pf.publication_format_id)] submission = OMPItem(submission_row, OMPSettings(ompdal.getSubmissionSettings(submission_row.submission_id)), {'authors': authors, 'editors': editors} ) 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) submissions = sorted(submissions, key=lambda s: min(s.associated_items.get('publication_dates', [datetime(1, 1, 1)])), reverse = True) return locals()
def book(): if session.forced_language == 'en': locale = 'en_US' elif session.forced_language == 'de': locale = 'de_DE' else: locale = '' submission_id = request.args[0] if request.args else redirect( URL('home', 'index')) dal = OMPDAL(db, myconf) press = dal.getPress(myconf.take('omp.press_id')) if not press: redirect(URL('home', 'index')) press_settings = OMPSettings(dal.getPressSettings(press.press_id)) # Get basic submission info (check, if submission is associated with the actual press and if the submission has been published) submission = dal.getPublishedSubmission(submission_id, press_id=myconf.take('omp.press_id')) if not submission: redirect(URL('home', 'index')) submission_settings = OMPSettings(dal.getSubmissionSettings(submission_id)) # Get contributors and contributor settings editor_rows = dal.getEditorsBySubmission(submission_id) editors = [OMPItem(e, OMPSettings(dal.getAuthorSettings(e.author_id))) for e in editor_rows] # Do not load authors if the submission has editors authors = [] if editors else [OMPItem(a, OMPSettings(dal.getAuthorSettings(a.author_id))) for a in dal.getActualAuthorsBySubmission(submission_id, filter_browse=True)] # Get chapters and chapter authors chapters = [] for chapter in dal.getChaptersBySubmission(submission_id): chapters.append(OMPItem(chapter, OMPSettings(dal.getChapterSettings(chapter.chapter_id)), {'authors': [OMPItem(a, OMPSettings(dal.getAuthorSettings(a.author_id))) for a in dal.getAuthorsByChapter(chapter.chapter_id)]}) ) # Get digital publication formats, settings, files, and identification codes digital_publication_formats = [] for pf in dal.getDigitalPublicationFormats(submission_id, available=True, approved=True): publication_format = OMPItem(pf, OMPSettings(dal.getPublicationFormatSettings(pf.publication_format_id)), {'identification_codes': dal.getIdentificationCodesByPublicationFormat(pf.publication_format_id), 'publication_dates': dal.getPublicationDatesByPublicationFormat(pf.publication_format_id)} ) full_file = dal.getLatestRevisionOfFullBookFileByPublicationFormat(submission_id, pf.publication_format_id) if full_file: publication_format.associated_items['full_file'] = OMPItem(full_file, OMPSettings(dal.getSubmissionFileSettings(full_file.file_id))) digital_publication_formats.append(publication_format) for chapter in chapters: chapter_file = dal.getLatestRevisionOfChapterFileByPublicationFormat(chapter.attributes.chapter_id, pf.publication_format_id) if chapter_file: chapter.associated_items.setdefault('files', {})[pf.publication_format_id] = OMPItem(chapter_file, OMPSettings(dal.getSubmissionFileSettings(chapter_file.file_id))) # Get physical publication formats, settings, and identification codes physical_publication_formats = [] for pf in dal.getPhysicalPublicationFormats(submission_id, available=True, approved=True): physical_publication_formats.append(OMPItem(pf, OMPSettings(dal.getPublicationFormatSettings(pf.publication_format_id)), {'identification_codes': dal.getIdentificationCodesByPublicationFormat(pf.publication_format_id), 'publication_dates': dal.getPublicationDatesByPublicationFormat(pf.publication_format_id)}) ) pdf = dal.getPublicationFormatByName(submission_id, myconf.take('omp.doi_format_name')).first() # Get DOI from the format marked as DOI carrier if pdf: doi = OMPSettings(dal.getPublicationFormatSettings(pdf.publication_format_id)).getLocalizedValue("pub-id::doi", "") # DOI always has empty locale else: doi = "" def get_first(l): if l: return l[0] else: return None date_published = None # Get the OMP publication date (column publication_date contains latest catalog entry edit date.) Try: # 1. Custom publication date entered for a publication format calles "PDF" if pdf: date_published = get_first([dateFromRow(pd) for pd in dal.getPublicationDatesByPublicationFormat(pdf.publication_format_id) if pd.role=="01"]) # 2. Date on which the catalog entry was first published if not date_published: date_published = get_first([pd.date_logged for pd in dal.getMetaDataPublishedDates(submission_id)]) # 3. Date on which the submission status was last modified (always set) if not date_published: date_published = submission.date_status_modified series = dal.getSeriesBySubmissionId(submission_id) if series: series = OMPItem(series, OMPSettings(dal.getSeriesSettings(series.series_id))) # Get purchase info representatives = dal.getRepresentativesBySubmission(submission_id, myconf.take('omp.representative_id_type')) #stats = OMPStats(myconf, db, locale) return locals()
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 book(): ompdal = OMPDAL(db, myconf) submission_id = request.args[ 0] if request.args and request.args[0].isdigit() else raise400() press_id = 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 press = ompdal.getPress(press_id) submission = ompdal.getPublishedSubmission(submission_id, press_id=press_id) if not submission or not press: raise HTTP(400) if len(request.args) > 1 and request.args[1][0] == 'c' and request.args[1][ 1:].isdigit(): chapter_id = int(request.args[1][1:]) elif len(request.args) > 1: redirect(URL(r=request, args=request.args[0:1])) else: chapter_id = 0 submission_settings = OMPSettings( ompdal.getSubmissionSettings(submission_id)) press_settings = OMPSettings(ompdal.getPressSettings(press.press_id)) # Get chapters and chapter authors chapters = [] chapter = {} for i in ompdal.getChaptersBySubmission(submission_id): item = OMPItem( i, OMPSettings(ompdal.getChapterSettings(i.chapter_id)), { 'authors': [ OMPItem(a, OMPSettings(ompdal.getAuthorSettings(a.author_id))) for a in ompdal.getAuthorsByChapter(i.chapter_id) ] }) chapters.append(item) contributors_by_group = defaultdict(list) contributors_by_id = {} for contrib in ompdal.getAuthorsBySubmission(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) contributors_by_id[contrib.author_id] = 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] # if no editors or authors are saved for this submission, treat chapter authors as authors if not editors and not authors: authors = chapter_authors translators = [] if translator_group_id: translators = contributors_by_group[translator_group_id] # Get digital publication formats, settings, files, and identification codes c = None chapter_doi = None digital_publication_formats = [] for pf in ompdal.getDigitalPublicationFormats(submission_id, available=True, approved=True): publication_format = OMPItem( pf, OMPSettings( ompdal.getPublicationFormatSettings(pf.publication_format_id)), { 'identification_codes': ompdal.getIdentificationCodesByPublicationFormat( pf.publication_format_id), 'publication_dates': ompdal.getPublicationDatesByPublicationFormat( pf.publication_format_id) }) full_file = ompdal.getLatestRevisionOfFullBookFileByPublicationFormat( submission_id, pf.publication_format_id) full_epub_file = ompdal.getLatestRevisionOfEBook( submission_id, pf.publication_format_id) if full_epub_file: publication_format.associated_items['full_file'] = OMPItem( full_epub_file, OMPSettings( ompdal.getSubmissionFileSettings(full_epub_file.file_id))) if full_file: publication_format.associated_items['full_file'] = OMPItem( full_file, OMPSettings(ompdal.getSubmissionFileSettings( full_file.file_id))) digital_publication_formats.append(publication_format) for i in chapters: chapter_file = ompdal.getLatestRevisionOfChapterFileByPublicationFormat( i.attributes.chapter_id, pf.publication_format_id) if chapter_file: i.associated_items.setdefault( 'files', {})[pf.publication_format_id] = OMPItem( chapter_file, OMPSettings( ompdal.getSubmissionFileSettings( chapter_file.file_id))) if chapter_id > 0 and chapter_id == i.attributes.chapter_id: c = i if c: c_title = c.settings.getLocalizedValue('title', locale) c_subtitle = c.settings.getLocalizedValue('subtitle', locale) c_abstract = c.settings.getLocalizedValue('abstract', locale) c_authors = c.associated_items.get('authors', []) c_files = c.associated_items.get('files', {}) chapter_doi = c.settings.getLocalizedValue(DOI_SETTING_NAME, '') # Get physical publication formats, settings, and identification codes physical_publication_formats = [] for pf in ompdal.getPhysicalPublicationFormats(submission_id, available=True, approved=True): physical_publication_formats.append( OMPItem( pf, OMPSettings( ompdal.getPublicationFormatSettings( pf.publication_format_id)), { 'identification_codes': ompdal.getIdentificationCodesByPublicationFormat( pf.publication_format_id), 'publication_dates': ompdal.getPublicationDatesByPublicationFormat( pf.publication_format_id) })) pdf = ompdal.getPublicationFormatByName( submission_id, myconf.take('omp.doi_format_name')).first() doi = "" submission_doi = submission_settings.getLocalizedValue( DOI_SETTING_NAME, '') if submission_doi: doi = submission_doi elif pdf: # DOI always has empty locale doi = OMPSettings( ompdal.getPublicationFormatSettings( pdf.publication_format_id)).getLocalizedValue( DOI_SETTING_NAME, "") date_published = None date_first_published = None # Get the OMP publication date (column publication_date contains latest catalog entry edit date.) Try: # 1. Custom publication date entered for a publication format calles "PDF" if pdf: date_published = ompformat.dateFromRow( ompdal.getPublicationDatesByPublicationFormat( pdf.publication_format_id, "01").first()) date_first_published = ompformat.dateFromRow( ompdal.getPublicationDatesByPublicationFormat( pdf.publication_format_id, "11").first()) # 2. Date on which the catalog entry was first published if not date_published: metadatapublished_date = ompdal.getMetaDataPublishedDates( submission_id).first() date_published = metadatapublished_date.date_logged if metadatapublished_date else None # 3. Date on which the submission status was last modified (always set) if not date_published: date_published = submission.date_status_modified series = ompdal.getSeriesBySubmissionId(submission_id) if series: series = OMPItem( series, OMPSettings(ompdal.getSeriesSettings(series.series_id))) # Get purchase info representatives = ompdal.getRepresentativesBySubmission( submission_id, myconf.take('omp.representative_id_type')) # stats = OMPStats(myconf, db, locale) onix_types = ONIX_PRODUCT_IDENTIFIER_TYPE_CODES # submissions = sorted(submissions, key=lambda s: s.attributes['series_id'], reverse=True) pfs = digital_publication_formats + physical_publication_formats idntfrs = {} for p in pfs: for i in p.associated_items['identification_codes'].as_list(): idntfrs['{}.{}'.format( i['code'], p.settings.getLocalizedValue( 'name', locale))] = (i['value'], i['code'], p.settings.getLocalizedValue('name', locale)) idntfrs = sorted(idntfrs.items(), key=lambda i: IDENTIFIER_ORDER.index( (i[0]) if i in IDENTIFIER_ORDER else '15.PDF')) category_row = ompdal.getCategoryBySubmissionId(submission_id) category = OMPItem( category_row, OMPSettings(ompdal.getCategorySettings( category_row.category_id))) if category_row else None cleanTitle = " ".join([ submission_settings.getLocalizedValue('prefix', locale), submission_settings.getLocalizedValue('title', locale) ]) subtitle = submission_settings.getLocalizedValue('subtitle', locale) abstract = submission_settings.getLocalizedValue('abstract', locale) series_name = "" if series: series_title = " ".join([ series.settings.getLocalizedValue('prefix', locale), series.settings.getLocalizedValue('title', locale) ]) series_subtitle = series.settings.getLocalizedValue('subtitle', locale) series_name = " – ".join( [t for t in [series_title.strip(), series_subtitle] if t]) citation = ompformat.formatCitation( cleanTitle, subtitle, authors, editors, translators, date_published, press_settings.getLocalizedValue('location', ''), press_settings.getLocalizedValue('publisher', ''), locale=locale, series_name=series_name, series_pos=submission.series_position, max_contrib=3, date_first_published=date_first_published) if authors: attribution = ompformat.formatContributors(authors, max_contributors=4, with_and=True) additional_attribution = ompformat.formatAttribution( editors, [], translators, []) title_attribution = ompformat.formatName(authors[0].settings) elif editors: suffix = T("(Eds.)") if len(editors) > 1 else T("(Ed.)") title_attribution = "{} {}".format( ompformat.formatContributors(editors, max_contributors=4, with_and=True), suffix) attribution = "{} {}".format( ompformat.formatContributors(editors, max_contributors=100, with_and=True), suffix) additional_attribution = "" else: title_attribution = ompformat.formatName(chapter_authors[0].settings) attribution = ompformat.formatAttribution([], [], [], chapter_authors) additional_attribution = "" response.title = "{}: {} - {}".format( title_attribution, cleanTitle, settings.short_title if settings.short_title else settings.title) if c: # Select different template for chapters citation = ompformat.formatChapterCitation(citation, c, locale) response.view = 'catalog/book/chapter/index.html' 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()
class PDFOrder(): ADDRESS_FIELDS = ['adresszeile1', 'adresszeile2', 'adresszeile3','strasse_und_nr'] IMG_PATH = 'applications/knv/static/images' PRESS_CONFIGURATON = { 1: ('Logo_heiBOOKS.png', 150, 255, 50, 40), 2: ('Logo_Arthistoricum.png', 120, 262, 80, 40), 3: ('Logo_Propylaeum.png', 140, 262, 60, 30), 4: ('Logo_Crossasia.png', 150, 262, 50, 30), 6: ('Logo_heiUP.png', 61, 244, 135, 55) } FOOTER = ['Bitte nicht buchen, Rechnung folgt.', 'Bei Rückfragen wenden Sie sich bitte an <a ' 'href="mailto:[email protected]"><u><font ' 'color="blue">[email protected]</font></u></a>, ' 'Tel.: +49 6221 542383.' ] FONTS = [('ArialMT', 'Arial.ttf', 10), ('DejaVu', 'DejaVuSans.ttf', 9.5)] TABLE_STYLE = TableStyle( [('VALIGN', (0, 0), (-1, -1), "TOP"), ('ALIGN', (0, 0), (-1, -1), "LEFT"), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.transparent), ('BOX', (0, 0), (-1, -1), 0.25, colors.transparent) ]) TABLE_HEADERS = ['Pos.', 'Menge', 'Kurztitel', 'Einband', 'ISBN', 'Preis'] WIDTH, HEIGHT = A4 def __init__(self, pdf_file, request, record, db, conf): self.IM_PATH = join(request.env.web2py_path, PDFOrder.IMG_PATH) self.record = record.first().as_dict() self.submission_id = int(self.record.get('submission_id')) self.ompdal = OMPDAL(db, conf) self.press_id = self.ompdal.getSubmission(self.submission_id)['context_id'] self.styles = getSampleStyleSheet() self.canvas = canvas.Canvas(pdf_file, pagesize=A4) self.setFont(self.canvas) def coordinates(self, x, y, unit=1): x, y = x * unit, PDFOrder.HEIGHT - y * unit return x, y def drawUTFText(self, canvas, x, y, text): canvas.drawString(x, y, u'{}'.format(text.encode('utf-8'))) def drawParagraph(self, text, size, style, top_margin, left_margin=25): p = Paragraph('<font size="{}">{}</font>'.format(size, text), self.styles[style]) p.wrapOn(self.canvas, PDFOrder.WIDTH, PDFOrder.HEIGHT) p.drawOn(self.canvas, *self.coordinates(left_margin, top_margin, mm)) def createPDF(self): self.drawLogo() self.drawSenderAddress() self.drawReceiverAddress() self.drawDelliveryNote() self.drawOrderSignareOfCustomer() self.canvas.line(75, 447, 565, 447 ) self.createTable() self.createShortTitleFlowable() self.createFooter() self.canvas.save() def createFooter(self): customer_notice = textwrap.wrap(str(self.record.get('customer_notice')), 120) for i,line in enumerate(customer_notice): self.drawParagraph(line, 9, "Normal", 230-len(customer_notice)*5 + 5*i) for i, part in enumerate(PDFOrder.FOOTER): self.drawParagraph(part, 9, "Normal", 250 + 10 * i) def createShortTitleFlowable(self): for i, line in enumerate(self.getShortTitle()): self.drawUTFText(self.canvas, 150, 434 - 10 * i, line) def createTable(self): data = [] data.append([self.createTableTH(val) for val in PDFOrder.TABLE_HEADERS]) data.append(["1", self.record.get('copies'), "", self.record.get('format').decode('utf-8'), self.getISBN(), self.getPrice()]) t = Table(data, colWidths=( 1.2 * cm, 1.6 * cm, 7.0 * cm, 2 * cm, 4 * cm, 2 * cm), rowHeights=(0.6 * cm, 0.6 * cm)) t.setStyle(PDFOrder.TABLE_STYLE) t.wrapOn(self.canvas, PDFOrder.WIDTH, PDFOrder.HEIGHT) t.drawOn(self.canvas, *self.coordinates(25, 145, mm)) def getShortTitle(self): result = [] lastnames = [a['last_name'] for a in self.ompdal.getAuthorsBySubmission(self.submission_id)] if len(lastnames) > 4 : lastnames = lastnames[0:4] lastnames[3] += ' et al' authors = '/'.join(lastnames) submission_settings = self.ompdal.getSubmissionSettings( self.submission_id).find(lambda row: row.locale == LOCALE).find( lambda row: row.setting_name == 'title') if len(submission_settings) ==0 : raise HTTP(403,'Title not found') result += textwrap.wrap(authors, 40) result += textwrap.wrap(str(submission_settings.first()['setting_value']),40) return result def createTableTH(self, content): return Paragraph('<b>%s</b>' % content, styleSheet["BodyText"]) def drawOrderSignareOfCustomer(self): if self.record.get('item_number'): self.drawParagraph('{} {}'.format('Bestellzeichen Kunde:',self.record.get('item_number')),10, "Normal", 129.5, left_margin=140) def drawDelliveryNote(self): self.drawParagraph( '{} {}'.format('Lieferschein vom', self.record.get('sent_date')), 10, "Normal", 125) self.drawParagraph( '{} {}-{}'.format('Lieferschein Nr. ub-pub-', self.record.get('curyear'), str(self.record.get('order_number')).rjust(5, "0")), 10, "Normal", 129.5) def drawSenderAddress(self): self.drawParagraph(self.record.get('abs_zeile1'), 6, "Normal", 60) self.drawParagraph(self.record.get('abs_zeile2'), 6, "Normal", 62.5) def drawReceiverAddress(self): address = [] for line in PDFOrder.ADDRESS_FIELDS: if len(str(self.record.get(line))) > 0: address.append(str(self.record.get(line))) address.append(' '.join([self.record.get(l) for l in ['laendercode', 'plz', 'ort'] if len(str(self.record.get(l))) > 0])) for i, line in enumerate(address): self.drawParagraph(line, 10, "Normal", 70 + i * 4) def setFont(self, canvas): for f in PDFOrder.FONTS: pdfmetrics.registerFont(TTFont(f[0], f[1])) canvas.setFont(f[0], f[2]) def drawLogo(self): for k, v in PDFOrder.PRESS_CONFIGURATON.items(): press_id = self.ompdal.getSubmission(int(self.submission_id))['context_id'] if k == int(press_id): self.canvas.drawImage(join(self.IM_PATH, v[0]), v[1] * mm, v[2] * mm, width=v[3] * mm, height=v[4] * mm, preserveAspectRatio=True, mask='auto') def getISBN(self): pf_id = self.getPublicationFormatID(self.submission_id) ics = self.ompdal.getIdentificationCodesByPublicationFormat(pf_id) if pf_id else None isbn = ics.first().get('value') if ics else '' return isbn def getPublicationFormatID(self,submission_id): pfs = self.ompdal.getPublicationFormatByName(submission_id, self.record.get('format')) pf_id = pfs.first().get('publication_format_id') if pfs else None return pf_id def getPrice(self): pf_id = self.getPublicationFormatID(self.submission_id) markets = self.ompdal.getMarketsByPublicationFormat(pf_id) if markets: price = float(markets.first().get('price',0).replace(',','.')) else: raise HTTP(403, 'Bitte geben Sie den Preis für {} ein'.format(self.record.get('format'))) copies = self.record.get('copies',1) copies = int(copies) if str(copies).isdigit() else None currency_rate = int(self.record.get('currency_rate', 1.0)) total = '{:5.2f}'.format(price * copies * currency_rate) if copies and price else '' if self.record.get('price_formattng')=='Komma': total = total.replace('.','.') return '{} {}'.format(total, self.record.get("currency","€"))