Example #1
0
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)
Example #2
0
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()
Example #3
0
def save_author_to_work(session, workid, authorname: str) -> None:

    author = session.query(Person)\
                    .filter(Person.name == authorname)\
                    .first()
    if not author:
        return

    authorid = author.id

    already_exists = session.query(Contributor, Contributor.role_id == 0)\
                            .join(Part)\
                            .filter(Contributor.person_id == authorid)\
                            .filter(Part.work_id == workid)\
                            .count()
    if already_exists == 0:
        parts = session.query(Part)\
                       .filter(Part.work_id == workid)\
                       .all()
        for part in parts:
            author = Contributor(person_id=authorid,
                                 part_id=part.id,
                                 role_id=0)
            session.add(author)

        session.commit()
Example #4
0
def add_authored(authorid):
    session = new_session()
    author = session.query(Person)\
                    .filter(Person.id == authorid)\
                    .first()
    work = Work()

    search_list = {}

    form = WorkForm(request.form)
    if request.method == 'GET':
        form.author.data = author.name

    if form.validate_on_submit():
        person = session.query(Person)\
                        .filter(Person.name == authorid)\
                        .first()
        parts = session.query(Part)\
                       .filter(work_id == workid)\
                       .all()
        for part in parts:
            app.logger.debug("part = {}, author= {}".format(part.id, authorid))
            author = Contributor(part_id=part.id,
                                 person_id=authorid,
                                 role_id=0)
            session.add(author)
        session.commit()
        return redirect(url_for('work', workid=work.id))
    else:
        app.logger.debug("Errors: {}".format(form.errors))
    return render_template('person.html',
                           id=authorid,
                           form=form,
                           search_lists=search_list,
                           source='')
Example #5
0
def save_authors_to_story() -> Response:
    session = new_session()

    (storyid, people_ids) = get_select_ids(request.form)
    existing_people = session.query(Contributor, Contributor.role_id == 0)\
                             .join(Part)\
                             .filter(Part.id == Contributor.part_id)\
                             .filter(Part.shortstory_id == storyid)\
                             .all()

    (to_add,
     to_remove) = get_join_changes([x.person_id for x in existing_people],
                                   [int(x['id']) for x in people_ids])

    for id in to_remove:
        auth = session.query(Contributor, Contributor.role_id == 0)\
                      .filter(Contributor.person_id == id)\
                      .join(Part)\
                      .filter(Part.id == Contributor.part_id)\
                      .filter(Part.shortstory_id == storyid)\
                      .first()
        session.delete(auth)
    for id in to_add:
        parts = session.query(Part)\
                       .filter(Part.shortstory_id == storyid)\
                       .all()
        for part in parts:
            auth = Contributor(person_id=id, part_id=part.id, role_id=0)
            session.add(auth)

    session.commit()
    msg = 'Tallennus onnistui'
    category = 'success'
    resp = {'feedback': msg, 'category': category}
    return make_response(jsonify(resp), 200)
Example #6
0
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)
Example #7
0
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)
Example #8
0
def save_author_to_story(session, storyid, authorname: str) -> None:

    parts = session.query(Part)\
                   .filter(Part.shortstory_id == storyid)\
                   .all()

    author = session.query(Person)\
                    .filter(Person.name == authorname)\
                    .first()

    for part in parts:
        auth = Contributor(part_id=part.id, person_id=author.id, role_id=0)
        session.add(auth)
    session.commit()
Example #9
0
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))
Example #10
0
def save_translator_to_edition() -> Any:
    session = new_session()

    (editionid, people_ids) = get_select_ids(request.form)

    existing_people = session.query(Contributor, Contributor.role_id == 1)\
        .join(Part)\
        .filter(Part.id == Contributor.part_id)\
        .filter(Part.edition_id == editionid)\
        .all()

    parts = session.query(Part)\
                   .filter(Part.edition_id == editionid)\
                   .all()

    (to_add,
     to_remove) = get_join_changes([x.person_id for x in existing_people],
                                   [int(x['id']) for x in people_ids])

    for id in to_remove:
        tr = session.query(Contributor, Contributor.role_id == 1)\
                    .filter(Contributor.person_id == id)\
                    .join(Part)\
                    .filter(Part.id == Contributor.part_id)\
                    .filter(Part.edition_id == editionid)\
                    .first()
        session.delete(tr)
    for id in to_add:
        for part in parts:
            tr = Contributor(part_id=part.id, person_id=id, role_id=1)
            session.add(tr)

    session.commit()

    msg = 'Tallennus onnistui'
    category = 'success'
    resp = {'feedback': msg, 'category': category}
    return make_response(jsonify(resp), 200)
Example #11
0
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}.')
Example #12
0
def edit_edition(editionid: Any) -> Any:
    session = new_session()
    publisher_series: Any
    publisher: Any
    pubseries: Any
    translators: Any
    editors: Any
    if str(editionid) != '0':
        edition = session.query(Edition)\
            .filter(Edition.id == editionid)\
            .first()
        publisher = session.query(Publisher)\
                           .filter(Publisher.id == edition.publisher_id)\
                           .first()
        translators = session.query(Person)\
                             .join(Contributor.person)\
                             .filer(Contributor.role_id == 1)\
                             .join(Part)\
                             .filter(Contributor.part_id == Part.id,
                                     Part.edition_id == Edition.id,
                                     Edition.id == editionid)\
                             .first()
        editors = session.query(Person)\
                         .join(Contributor.person)\
                         .filter(Contributor.role_id == 2)\
                         .join(Part, Part.id == Contributor.part_id)\
                         .filter(Part.edition_id == editionid)\
                         .first()
        pubseries = \
            session.query(Pubseries)\
            .filter(Pubseries.id == edition.pubseries_id)\
            .first()
        if publisher:
            publisher_series = pubseries_list(session, publisher.id)
        else:
            publisher_series = {}
    else:
        edition = Edition()
        publisher = Publisher()
        translators = Contributor(role_id=1)
        editors = Contributor(role_id=2)
        pubseries = Pubseries()

    form = EditionForm()
    selected_pubseries = '0'
    source = edition.imported_string
    form.binding.choices = binding_list(session)
    form.format.choices = format_list(session)
    form.size.choices = size_list(session)

    if request.method == 'GET':
        form.id.data = edition.id
        form.workid.data = ','.join([str(x.id) for x in edition.work])
        form.title.data = edition.title
        form.subtitle.data = edition.subtitle
        form.pubyear.data = edition.pubyear
        form.edition.data = edition.editionnum
        if edition.version:
            form.version.data = edition.version
        if publisher:
            form.publisher.data = publisher.name
        if translators:
            form.translators.data = translators.name
        if editors:
            form.editors.data = editors.name
        if pubseries:
            form.pubseries.data = pubseries.name
        form.pubseriesnum.data = edition.pubseriesnum
        form.pages.data = edition.pages
        form.binding.data = edition.binding_id
        form.format.data = edition.format_id
        form.size.data = edition.size_id
        # form.artist.data  = artist.name
        form.isbn.data = edition.isbn
        form.misc.data = edition.misc
        form.image_src.data = edition.image_src
        # if pubseries:
        #     i = 0
        #     for idx, s in enumerate(publisher_series):
        #         if s[1] == pubseries.name:
        #             i = s[0]
        #             break
        #     form.pubseries.choices = [('0', 'Ei sarjaa')] + publisher_series
        #     form.pubseries.default = str(i)
        #     selected_pubseries = str(i)
        # form.translators.data = ', '.join([t.name for t in translators])
        # form.editors.data = ', '.join([e.name for e in editors])
    if form.validate_on_submit():
        save_edition(session, form, edition)
        return redirect(url_for('edition', editionid=edition.id))
    else:
        app.logger.debug("Errors: {}".format(form.errors))
        # app.logger.debug("pubid={}".format(form.pubseries.data))
    return render_template('edit_edition.html', form=form, source=source)
Example #13
0
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()