def save_work(session: Any, form: Any, work: Any) -> Any: author = session.query(Person).filter(Person.name == form.author.data).first() work.id = form.id.data work.title = form.title.data work.pubyear = form.pubyear.data work.language = form.language.data if form.bookseries.data != '': bookseries = session.query(Bookseries).filter(Bookseries.name == form.bookseries.data).first() work.bookseries_id = bookseries.id else: work.bookseries_id = None work.bookseriesnum = form.bookseriesnum.data work.misc = form.misc.data work.collection = form.collection.data session.add(work) session.commit() if work.id == 0: # New work so needs a row to Edition and Part tables. edition = Edition() edition.title = work.title edition.subtitle = work.subtitle edition.pubyear = work.pubyear session.add(edition) session.commit() part = Part() part.work_id = work.id part.edition_id = edition.id part.title = work.title session.add(part) session.commit()
def new_work_for_person(personid: Any) -> Any: session = new_session() form = WorkForm(request.form) if request.method == 'GET': form.hidden_author_id.data = personid elif form.validate_on_submit(): work = Work() work.title = form.title.data work.subtitle = form.subtitle.data work.orig_title = form.orig_title.data work.pubyear = form.pubyear.data session.add(work) session.commit() edition_id = create_first_edition(session, work) part = Part(work_id=work.id, edition_id=edition_id) session.add(part) session.commit() author = Contributor( person_id=form.hidden_author_id.data, part_id=part.id, role_id=0) session.add(author) session.commit() types: List[str] = [''] * 4 types[1] = 'checked' return render_template('work.html', work=work, form=form, types=types, next_book=None, prev_book=None) return render_template('new_work.html', form=form)
def save_story_to_edition(session, editionid, title: str) -> None: story = session.query(ShortStory)\ .filter(ShortStory.title == title)\ .first() if not story: return works = session.query(Work)\ .join(Part)\ .filter(Part.work_id == Work.id)\ .filter(Part.edition_id == editionid)\ .all() authors = session.query(Contributor, Contributor.role_id == 0)\ .join(Part)\ .filter(Contributor.part_id == Part.id)\ .filter(Part.edition_id == editionid)\ .all() for work in works: part = Part(work_id=work.id, edition_id=editionid, shortstory_id=story.id) session.add(part) work.collection = True session.add(work) session.commit() for author in authors: auth = Contributor(part_id=part.id, person_id=author.id, role_id=0) session.add(auth) session.commit()
def add_story_to_work() -> Any: session = new_session() (workid, story_ids) = get_select_ids(request.form) existing_stories = session.query(ShortStory)\ .join(Part)\ .filter(Part.work_id == workid)\ .filter(Part.shortstory_id == ShortStory.id)\ .all() existing_story_ids = [x.id for x in existing_stories] story_ids = create_new_shortstory_to_work(session, story_ids, workid) (to_add, to_remove) = get_join_changes( existing_story_ids, [int(x['id']) for x in story_ids]) editions = session.query(Edition)\ .join(Part)\ .filter(Part.edition_id == Edition.id)\ .filter(Part.work_id == workid)\ .all() authors = session.query(Person)\ .join(Contributor, Contributor.role_id == 0)\ .filter(Person.id == Contributor.person_id)\ .join(Part)\ .filter(Part.id == Contributor.part_id)\ .filter(Part.work_id == workid)\ .all() for id in to_remove: part = session.query(Part)\ .join(ShortStory)\ .filter(Part.work_id == workid)\ .filter(Part.shortstory_id == id)\ .first() session.delete(part) for id in to_add: for edition in editions: part = Part(work_id=workid, edition_id=edition.id, shortstory_id=id) session.add(part) session.commit() if authors: for person in authors: author = Contributor( person_id=person.id, part_id=part.id, role_id=0) session.add(author) session.commit() update_creators_to_story(session, id, authors) session.commit() msg = 'Tallennus onnistui' category = 'success' resp = {'feedback': msg, 'category': category} return make_response(jsonify(resp), 200)
def add_edition(workid: Any) -> Any: session = new_session() works = session.query(Work).filter(Work.id == workid).all() edition = Edition(title=works[0].title) edition.imported_string = current_user.name session.add(edition) session.commit() editionid = edition.id edition = session.query(Edition).filter(Edition.id == editionid).first() # Copy authors to edition from work authors = session.query(Contributor, Contributor.role_id == 0)\ .join(Part)\ .filter(Contributor.part_id == Part.id)\ .filter(Part.work_id == workid)\ .distinct(Contributor.part_id)\ .all() for author in authors: part = Part(work_id=workid, edition_id=edition.id) session.add(part) session.commit() auth = Contributor( part_id=part.id, person_id=author.person_id, role_id=0) session.add(auth) session.commit() session.commit() form = EditionForm(request.form) editors = session.query(Person)\ .join(Contributor, Contributor.role_id == 2)\ .join(Part, Part.id == Contributor.part_id)\ .filter(Person.id == Contributor.person_id)\ .filter(Part.edition_id == editionid)\ .all() translators = session.query(Person)\ .join(Contributor, Contributor.role_id == 1)\ .filter(Person.id == Contributor.person_id)\ .join(Part)\ .filter(Part.id == Contributor.part_id, Part.edition_id == edition.id)\ .all() stories = session.query(ShortStory)\ .join(Part)\ .filter(Part.shortstory_id == ShortStory.id)\ .filter(Part.edition_id == edition.id)\ .group_by(Part.shortstory_id)\ .all() binding_count = session.query(func.count(BindingType.id)).first() bindings: List[str] = [''] * (binding_count[0] + 1) if edition.binding_id: bindings[edition.binding_id] = 'checked' return render_template('edition.html', form=form, edition=edition, authors=None, other_editions=None, works=works, translators=translators, editors=editors, stories=stories, bindings=bindings)
def add_edition_to_route(workid: Any) -> Any: session = new_session() work = session.query(Work).filter(Work.id == workid).all() edition = Edition(title=work[0].title) session.add(edition) session.commit() part = Part(work_id=workid, edition_id=edition.id) session.add(part) session.commit() authors = session.query(Person)\ .join(Contributor, Contributor.role_id == 0)\ .filter(Person.id == Contributor.person_id)\ .join(Part)\ .filter(Part.id == Contributor.part_id)\ .filter(Part.work_id == workid)\ .distinct()\ .all() for author in authors: auth = Contributor(part_id=part.id, person_id=author.id, role_id=0) session.add(auth) session.commit() stories = session.query(Part)\ .filter(Part.work_id == workid, Part.shortstory_id != None)\ .all() for story in stories: part = Part(work_id=workid, edition_id=edition.id, shortstory_id=story.id) session.add(part) session.commit() log_change(session, 'Edition', edition.id, action='NEW') return redirect(url_for('edition', editionid=edition.id))
def import_stories(s, name: str, issue_num: Optional[int], issue_extra: str, issue_year: Optional[int], issue_count: Optional[int], id: int): if not name in stories: return try: for story in stories[name]: in_issue: bool = False if story['Count'] != '': if (story['Count'] == str(issue_count) and story['Number_extra'] == issue_extra): in_issue = True else: if (story['Number'] == str(issue_num) and story['Number_extra'] == issue_extra and story['Year'] == str(issue_year)): in_issue = True if in_issue: magazine = story['Lehti'] # Not really needed year = story['Year'] authors = story['Tekijä'] title = story['Novelli'] orig_title = story['Alkup-novelli'] orig_year = story['Alkup-vuosi'] translators = story['Suomentaja'] runo = story['runo'] raapale = story['raapale'] filk = story['filk'] if orig_title == '': orig_title = title if year == '': year = None if orig_year == '': orig_year = year author_ids = [] for author in authors.split('&'): person_id = get_person(s, author.strip(), True) if person_id: author_ids.append(person_id) translator_ids = [] if len(translators) > 0: for person in translators.split('&'): person_id = get_person(s, person.strip(), True) if person_id: translator_ids.append(person_id) story_item = s.query(ShortStory)\ .filter(ShortStory.title == orig_title)\ .first() if not story_item: # if len(author_ids) > 1: # creator_str = make_creators(s, author_ids) # else: # creator_str = authors.strip() story_item = ShortStory(title=title, orig_title=orig_title, pubyear=orig_year) # creator_str=creator_str) s.add(story_item) s.commit() part_item = Part(shortstory_id=story_item.id, title=title) s.add(part_item) s.commit() for auth_id in author_ids: auth = Contributor(part_id=part_item.id, person_id=auth_id, role_id=0) s.add(auth) for trans_id in translator_ids: translator = Contributor(part_id=part_item.id, person_id=trans_id, role_id=1) s.add(translator) if runo != '': add_tag(s, story_item.id, 'runo') if raapale != '': add_tag(s, story_item.id, 'raapale') if filk != '': add_tag(s, story_item.id, 'filk') ic = IssueContent(issue_id=id, shortstory_id=story_item.id) s.add(ic) s.commit() except Exception as e: print(f'Exception in import_stories: {e}.')
def save_edition(session: Any, form: Any, edition: Any) -> None: edition.title = form.title.data edition.subtitle = form.subtitle.data edition.pubyear = form.pubyear.data edition.editionnum = form.edition.data edition.version = form.version.data translator = session.query(Person).filter( Person.name == form.translators.data).first() editor = session.query(Person).filter( Person.name == form.editors.data).first() publisher = session.query(Publisher).filter( Publisher.name == form.publisher.data).first() if form.pubseries.data == 0: edition.pubseries_id = None else: edition.pubseries_id = form.pubseries.data edition.publisher_id = publisher.id edition.pubseriesnum = form.pubseriesnum.data edition.pages = form.pages.data edition.cover_id = form.cover.data edition.binding_id = form.binding.data edition.format_id = form.format.data edition.size_id = form.size.data # edition.artist_id = artist.id edition.isbn = form.isbn.data edition.misc = form.misc.data edition.image_src = form.image_src.data session.add(edition) session.commit() parts: List[Part] = [] if form.id == 0: # Add parts for work in form.workid.split(','): part = Part(edition_id=edition.id, work_id=work) session.add(part) parts.append(part) session.commit() else: parts = session.query(Part)\ .filter(Part.edition_id == edition.id)\ .all() # Save translator and editor for part in parts: if translator: transl = session.query(Contributor, Contributor.role_id == 1)\ .filter(Contributor.part_id == part.id, Contributor.person_id == translator.id)\ .first() if not transl: transl = Contributor(part_id=part.id, person_id=translator.id, role_id=1) session.add(transl) if editor: ed = session.query(Contributor, Contributor.role_id == 2)\ .join(Part, Part.id == Contributor.part_id)\ .filter(Part.edition_id == edition.id, Contributor.person_id == editor.id)\ .first() if not ed: ed = Contributor(part_id=part.id, person_id=editor.id, role_id=2) session.add(ed) session.commit()