def link_tome_to_file(): tome_id = request.args[0] tome = pdb.get_tome(tome_id) form = SQLFORM.factory(Field('hash'), Field('file_extension', default="epub"), Field('fidelity', requires=FidelityValidator(), default=70), submit_button='Save') title_text = title.coalesce_title(tome['title'], tome['subtitle']) response.title = u'Edit Files of {} - Montag'.format(title_text) if form.process(keepvalues=True).accepted: file_hash = read_form_field(form, 'hash') file_extension = read_form_field(form, 'file_extension') fidelity = read_form_field(form, 'fidelity') local_file_size = pyrosetup.fileserver().get_local_file_size(file_hash) if not local_file_size: response.flash = 'This file is not known to the database, please check the hash' else: pdb.link_tome_to_file(tome_id, file_hash, local_file_size, file_extension, FileType.Content, fidelity) session.flash = 'Added new file link' redirect(URL('edit_tome_file_link', args=(tome_id, file_hash))) elif form.errors: response.flash = 'form has errors' return dict(form=form, tome=tome)
def add_tome_from_file(): file_hash = request.args[0] file_extension = request.args[1] file_size = request.args[2] response.title = "Add Tome - Montag" author_ids = [] if 'metadata' not in session: return create_error_page('It seems that the session broke somehow - do you have a cookie blocker?') form = _add_tome_from_file_form(session.metadata) if form.process(keepvalues=True, dbio=False).accepted: fidelity = read_form_field(form, 'fidelity') authors = read_form_field(form, 'authors') author_ids = pdb.find_or_create_authors(authors, fidelity) tome_id = pdb.find_or_create_tome(read_form_field(form, 'title'), read_form_field(form, 'principal_language'), author_ids, read_form_field(form, 'subtitle'), read_form_field(form, 'tome_type'), fidelity, edition=read_form_field(form, 'edition'), publication_year=read_form_field(form, 'publication_year')) tome = pdb.get_tome(tome_id) pdb.link_tome_to_file(tome_id, file_hash, file_size, file_extension, FileType.Content, fidelity) response.flash = 'Tome successfully added.' redirect(URL(f='view_tome', c='default', args=(tome['guid']))) elif form.errors: response.flash = 'form has errors' return dict(form=form, author_ids=author_ids)
def select_author_merge_partner(): first_author_guid = request.args[0] first_author = pdb.get_author_by_guid(first_author_guid) if first_author is None: session.flash = "Author not found" redirect(URL('default', 'tomesearch')) return search_form = _select_author_merge_partner_form(first_author) retval = { 'first_author_guid': first_author_guid, 'first_author': first_author } response.title = "Select Merge Target - Montag" if search_form.validate(formname='search', session=None, request_vars=request.vars, message_onsuccess='', keepvalues=True): author_to_search_for = read_form_field(search_form, 'query') retval['query'] = read_form_field(search_form, 'query') else: author_to_search_for = first_author['name'] retval['query'] = author_to_search_for if len(author_to_search_for) > 0: # noinspection PyAugmentAssignment if author_to_search_for[0] != '%': author_to_search_for = '%' + author_to_search_for if author_to_search_for[-1] != '%': author_to_search_for += '%' page_number = 0 if 'page' in request.vars: page_number = int(request.vars.page) search_forms.pass_paged_author_query_results_to_view( pdb, author_to_search_for, retval, page_number) retval['form'] = search_form retval['request'] = request return retval
def edit_tome_file_link(): tome_id = request.args[0] file_hash = request.args[1] tome = pdb.get_tome(tome_id) files = pdb.get_tome_files(tome_id, file_type=pydb.FileType.Content, include_local_file_info=True) tome_files = filter(lambda x: x['hash'] == file_hash, files) tome_file = tome_files[0] form = SQLFORM.factory(Field('file_extension', default=db_str_to_form(tome_file['file_extension'])), Field('fidelity', requires=FidelityValidator(), default=tome_file['fidelity']+0.1), submit_button='Save') title_text = title.coalesce_title(tome['title'], tome['subtitle']) response.title = u"Edit Files {} - Montag".format(title_text) field_names = ['file_extension', 'fidelity'] if form.process(keepvalues=True).accepted: doc = pdb.get_tome_document_by_guid(tome['guid'], hide_private_tags=False) other_files = filter(lambda x: x['hash'] != file_hash, doc['files']) tome_file_doc = filter(lambda x: x['hash'] == file_hash, doc['files'])[0] for f in field_names: tome_file_doc[f] = read_form_field(form, f) other_files.append(tome_file_doc) doc['files'] = other_files pdb.load_own_tome_document(doc) response.flash = 'Stored new values' redirect(URL('edit_tome', args=(tome['guid']), anchor='files')) elif form.errors: response.flash = 'form has errors' return dict(form=form, tome=tome, file=tome_file)
def set_main_cover(): tome_id = request.args[0] file_hash = request.args[1] file_extension = request.args[2] tome = pdb.get_tome(tome_id) form = _set_main_cover_form() title_text = title.coalesce_title(tome['title'], tome['subtitle']) response.title = u'Set Cover for {} - Montag'.format(title_text) file_size = pyrosetup.fileserver().get_local_file_size(file_hash) if form.process(keepvalues=True).accepted: fidelity = read_form_field(form, 'fidelity') pdb.link_tome_to_file(tome_id, file_hash, file_size, file_extension, FileType.Cover, fidelity) doc = pdb.get_tome_document_by_guid(tome['guid'], hide_private_tags=False) other_files = filter(lambda x: x['hash'] != file_hash, doc['files']) tome_file_doc = filter(lambda x: x['hash'] == file_hash, doc['files'])[0] tome_file_doc['fidelity'] = fidelity other_files.append(tome_file_doc) doc['files'] = other_files pdb.load_own_tome_document(doc) redirect(URL('default', 'view_tome', args=(tome['guid']))) return {'tome': tome, 'file_hash': file_hash, 'file_extension': file_extension, 'form': form}
def edit_friend(): friend_id = request.args[0] friend = _load_friend(friend_id) comm_data = _load_comm_data(friend_id) form = _friend_edit_form(friend, comm_data) response.title = u'Edit friend {} - Montag'.format(friend['name']) if form.process(keepvalues=True).accepted: comm_data = _load_comm_data(friend_id) comm_data_fields = ['hostname', 'type', 'port', 'secret'] for f in comm_data_fields: value = read_form_field(form, f) if f != 'secret' or (f == 'secret' and value.count('*') != len(value)): comm_data[f] = value cds = pyrosetup.comm_data_store() cds.set_comm_data(friend_id, comm_data) new_name = read_form_field(form, 'name') if new_name != friend['name']: friend['name'] = new_name pdb.set_friend_name(friend['id'], friend['name']) new_can_connect_to = '1' if read_form_field(form, 'can_connect_to') else '0' if new_can_connect_to != friend['can_connect_to']: friend['can_connect_to'] = new_can_connect_to pdb.set_friend_can_connect_to(friend['id'], new_can_connect_to) comm_data = _load_comm_data(friend_id) response.flash = 'Stored new values' redirect('../list_friends') elif form.errors: response.flash = 'comm data has errors' return dict(form=form, comm_data=comm_data, friend_id=friend_id)
def add_friend(): form = _friend_add_form() response.title = 'Add friend' if form.process(keepvalues=True).accepted: friend_id = pdb.add_friend(read_form_field(form, 'name')) response.flash = 'Added new friend' redirect(URL('edit_friend', args=[friend_id])) elif form.errors: response.flash = 'form has errors' return dict(form=form)
def tomesearch(): retval = {'tome_info': None} form = search_forms.build_search_form(pdb) if form.validate(formname='search', session=None, request_vars=request.vars, message_onsuccess='', keepvalues=True): query = read_form_field(form, 'query').strip() if _is_tome_or_author_guid(query): tome = pdb.get_tome_by_guid(query) if tome is not None: redirect(URL('view_tome', args=[query])) return retval author = pdb.get_author_by_guid(query) if author is not None: redirect(URL('view_author', args=[query])) return retval if _is_file_hash(query): tome_files = pdb.get_tome_files_by_hash(query) for tome_file in tome_files: tome_id = tome_file['tome_id'] tome = pdb.get_tome(tome_id) if tome is not None: redirect(URL('view_tome', args=[tome['guid']])) return retval response.title = "Search Results - Montag" search_query = search_forms.build_search_query(form) page_number = 0 if 'page' in request.vars: page_number = int(request.vars.page) search_forms.pass_paged_query_results_to_view(pdb, search_query, retval, page_number) retval['form'] = form retval['query'] = read_form_field(form, 'query') retval['request'] = request return retval
def link_tome_to_author(): tome_id = request.args[0] tome = pdb.get_tome(tome_id) tome_authors = pdb.get_tome_authors(tome_id) if tome_authors: last_tome_author = max(tome_authors, key=lambda x: float(x['author_order'])) max_author_order = last_tome_author['author_order'] else: max_author_order = 0 form = SQLFORM.factory(Field('author_name'), Field('order', default=max_author_order+1, label='Order Value'), Field('fidelity', requires=FidelityValidator(), default=70), submit_button='Save') title_text = title.coalesce_title(tome['title'], tome['subtitle']) response.title = u"Add Author to {} - Montag".format(title_text) if form.process(keepvalues=True).accepted: author_name = read_form_field(form, 'author_name') fidelity = read_form_field(form, 'fidelity') author_order = float(read_form_field(form, 'order')) author_ids = pdb.find_or_create_authors([author_name], fidelity) author_id = author_ids[0] pdb.link_tome_to_author(tome_id, author_id, author_order, fidelity) session.flash = 'Added author to tome' redirect(URL('edit_tome', args=(tome['guid']), anchor='authors')) elif form.errors: response.flash = 'form has errors' return dict(form=form, tome=tome, authors=tome_authors)
def unlock_comm_data(): form = _unlock_comm_data_form() response.title = 'Unlock Comm Data' if form.process(keepvalues=True).accepted: password = read_form_field(form, 'unlock_password') cds = pyrosetup.comm_data_store() try: cds.unlock(str(password)) redirect('list_friends') except KeyError: response.flash = 'Invalid password' elif form.errors: response.flash = 'form has errors' return dict(form=form)
def edit_tome_author_link(): tome_id = request.args[0] author_id = request.args[1] tome = pdb.get_tome(tome_id) tome_authors = pdb.get_tome_authors(tome_id) for ta in tome_authors: if int(ta['id']) == int(author_id): tome_author = ta break else: return dict(error="Tome and author not linked?", form=None, tome=tome, author=None, authors=tome_authors) author_guid = tome_author['guid'] form = SQLFORM.factory(Field('order', default=tome_author['author_order']), Field('fidelity', requires=FidelityValidator(), default=tome_author['link_fidelity']+0.1), submit_button='Save') title_text = title.coalesce_title(tome['title'], tome['subtitle']) response.title = u"Edit Author Link {} <=> {} - Montag".format(tome_author['name'], title_text) if form.process(keepvalues=True).accepted: doc = pdb.get_tome_document_by_guid(tome['guid'], hide_private_tags=False) other_authors = filter(lambda x: x['guid'] != author_guid, doc['authors']) tome_author_doc = filter(lambda x: x['guid'] == author_guid, doc['authors'])[0] for f in ('order', 'fidelity'): tome_author_doc[f] = read_form_field(form, f) other_authors.append(tome_author_doc) doc['authors'] = other_authors pdb.load_own_tome_document(doc) tome_authors = pdb.get_tome_authors(tome_id) response.flash = 'Stored new values' redirect(URL('edit_tome', args=(tome['guid']), anchor='authors')) elif form.errors: response.flash = 'form has errors' return dict(form=form, tome=tome, author=tome_author, authors=tome_authors)
def add_tome_from_file(): file_hash = request.args[0] file_extension = request.args[1] file_size = request.args[2] response.title = "Add Tome - Montag" author_ids = [] if 'metadata' not in session: return create_error_page( 'It seems that the session broke somehow - do you have a cookie blocker?' ) form = _add_tome_from_file_form(session.metadata) if form.process(keepvalues=True, dbio=False).accepted: fidelity = read_form_field(form, 'fidelity') authors = read_form_field(form, 'authors') author_ids = pdb.find_or_create_authors(authors, fidelity) tome_id = pdb.find_or_create_tome( read_form_field(form, 'title'), read_form_field(form, 'principal_language'), author_ids, read_form_field(form, 'subtitle'), read_form_field(form, 'tome_type'), fidelity, edition=read_form_field(form, 'edition'), publication_year=read_form_field(form, 'publication_year')) tome = pdb.get_tome(tome_id) pdb.link_tome_to_file(tome_id, file_hash, file_size, file_extension, FileType.Content, fidelity) response.flash = 'Tome successfully added.' redirect(URL(f='view_tome', c='default', args=(tome['guid']))) elif form.errors: response.flash = 'form has errors' return dict(form=form, author_ids=author_ids)
def select_tome_merge_partner(): first_tome_guid = request.args[0] first_tome = pdb.get_tome_document_by_guid(first_tome_guid, keep_id=True, include_local_file_info=False, include_author_detail=True, hide_private_tags=False) if first_tome is None: session.flash = "Tome not found" redirect(URL('default', 'tomesearch')) return search_form = _select_tome_merge_partner_form(first_tome) retval = {'first_tome_guid': first_tome_guid, 'first_tome': first_tome} response.title = "Select Merge Target - Montag" page_number = 0 if search_form.validate(formname='search', session=None, request_vars=request.vars, message_onsuccess='', keepvalues=True): search_query = search_forms.build_search_query(search_form) if 'page' in request.vars: page_number = int(request.vars.page) retval['query'] = read_form_field(search_form, 'query') else: search_query = first_tome['title'] retval['query'] = search_query search_forms.pass_paged_query_results_to_view(pdb, search_query, retval, page_number) retval['form'] = search_form retval['request'] = request return retval
def set_cover_from_content(): tome_id = request.args[0] content_hash = request.args[1] content_extension = request.args[2] only_display_cover_afterwards = request.args[3] if only_display_cover_afterwards.lower() == 'false': only_display_cover_afterwards = False tome = pdb.get_tome(tome_id) form = _set_cover_from_content_form() title_text = title.coalesce_title(tome['title'], tome['subtitle']) response.title = u'Set Cover for {} - Montag'.format(title_text) if form.process(keepvalues=True).accepted: fidelity = read_form_field(form, 'fidelity') cover_contents = _extract_image_from_content(content_hash, content_extension) if cover_contents is None: session.flash('Cover could not be loaded - sorry!') redirect(URL('default', 'view_tome', args=(tome['guid']))) return file_extension = 'jpg' fd_cover, path_cover = tempfile.mkstemp('.' + file_extension) with os.fdopen(fd_cover, 'wb') as cover_file: cover_file.write(cover_contents.getvalue()) file_server = pyrosetup.fileserver() (local_file_id, file_hash, file_size) = file_server.add_file_from_local_disk(path_cover, file_extension, move_file=True) pdb.link_tome_to_file(tome_id, file_hash, file_size, file_extension, FileType.Cover, fidelity) if only_display_cover_afterwards: redirect(URL('covers', 'get_cover_image', args=(file_hash, file_extension))) else: redirect(URL('default', 'view_tome', args=(tome['guid']))) return {'tome': tome, 'content_hash': content_hash, 'content_extension': content_extension, 'form': form}
def edit_author(): author_guid = request.args[0] author_doc = pdb.get_author_document_by_guid(author_guid, keep_id=True) if 'name' not in author_doc: session.flash = "No such author" redirect(URL('tomesearch')) required_fidelity = pdb.calculate_required_author_fidelity(author_doc['id']) form = _author_edit_form(author_doc, required_fidelity) response.title = u'Edit {} - Montag'.format(author_doc['name']) if form.process(keepvalues=True, session=None).accepted: field_names = ['name', 'date_of_birth', 'date_of_death', 'fidelity'] for f in field_names: author_doc[f] = read_form_field(form, f) pdb.load_own_author_document(author_doc) author_doc = pdb.get_author_document_by_guid(author_guid, keep_id=True) response.flash = 'Stored new values' redirect(URL('view_author', args=[author_guid])) elif form.errors: response.flash = 'form has errors' return dict(form=form, author=author_doc, required_fidelity=required_fidelity)
def _edit_tome(tome_doc): if not tome_doc: return "Tome no longer existing!" title_text = title.coalesce_title(tome_doc['title'], tome_doc['subtitle']) response.title = u"Edit {} - Montag".format(title_text) field_names = ['title', 'subtitle', 'edition', 'principal_language', 'publication_year', 'tags', 'type', 'fidelity'] syn_field_names = ['content', 'fidelity'] tome_id = tome_doc['id'] required_tome_fidelity = pdb.calculate_required_tome_fidelity(tome_id) form = _tome_edit_form(tome_doc, required_tome_fidelity) synforms = list() relevant_synopses = list(network_params.relevant_items(tome_doc['synopses'])) tome_doc['synopses'] = relevant_synopses for synopsis in relevant_synopses: synforms.append(_tome_synopses_form(synopsis)) if 'guid' in request.vars: synopsis_guid = request.vars.guid formname = u'edit_synopsis_{}'.format(synopsis_guid) for syn_idx, synform in enumerate(synforms): if synform.process(session=None, formname=formname, keepvalues=True).accepted: if not synopsis_guid: raise ValueError("No synopsis guid found") for synopsis in tome_doc['synopses']: if synopsis['guid'] == synopsis_guid: synopsis_to_edit = synopsis break else: synopsis_to_edit = {'guid': synopsis_guid} tome_doc['synopses'].append(synopsis_to_edit) for sf in syn_field_names: synopsis_to_edit[sf] = read_form_field(synform, sf) pdb.load_own_tome_document(tome_doc) redirect(URL('edit_tome', args=(tome_doc['guid']), anchor='synopses')) elif synform.errors: response.flash = 'form has errors' if form.process(session=None, formname='edit_tome', keepvalues=True).accepted: for f in field_names: tome_doc[f] = read_form_field(form, f) if 'authors' not in tome_doc: tome_doc['authors'] = [] if 'files' not in tome_doc: tome_doc['files'] = [] if 'publication_year' not in tome_doc: tome_doc['publication_year'] = None elif tome_doc['publication_year'] == 'None': tome_doc['publication_year'] = None pdb.load_own_tome_document(tome_doc) redirect(URL('view_tome', args=(tome_doc['guid']))) elif form.errors: response.flash = 'form has errors' tome_doc['id'] = tome_id return dict(form=form, tome=tome_doc, tome_id=tome_id, synforms=synforms, required_fidelity=required_tome_fidelity)