diff --git a/app/impl.py b/app/impl.py index f13fe74..64bd8f5 100644 --- a/app/impl.py +++ b/app/impl.py @@ -466,32 +466,38 @@ def set_language( Returns: Union[ResponseType, None]: The response type or None. """ - if ((data['language'] and data['language']['id'] != item.language) - or (data['language'] is None and item.language is not None)): + if isinstance(data['language'], str) or 'id' not in data['language']: + # User added a new language. Front returns this as a string + # in the language field so we need to add this language to + # the database first. + lang_id = add_language(data['language']) + # if ((data['language'] and data['language']['id'] != item.language) + # or (data['language'] is None and item.language is not None)): + else: lang_id = None if old_values is not None: if item.language_name: old_values['Kieli'] = item.language_name.name else: old_values['Kieli'] = None - if data['language'] and 'id' not in data['language']: - # User added a new language. Front returns this as a string - # in the language field so we need to add this language to - # the database first. - lang_id = add_language(data['language']) - else: - lang_id = check_int(data['language']['id']) if data['language'] \ - else None - if lang_id is not None: - lang = session.query(Language)\ - .filter(Language.id == lang_id)\ - .first() - if not lang: - app.logger.error('SetLanguage: Language not found. Id = %s' - ', {lang_id') - return ResponseType('Kieltä ei löydy', - status=HttpResponseCode.BAD_REQUEST) - item.language = lang_id + # if data['language'] and 'id' not in data['language']: + # # User added a new language. Front returns this as a string + # # in the language field so we need to add this language to + # # the database first. + # lang_id = add_language(data['language']) + # else: + lang_id = check_int(data['language']['id']) if data['language'] \ + else None + if lang_id is not None: + lang = session.query(Language)\ + .filter(Language.id == lang_id)\ + .first() + if not lang: + app.logger.error('SetLanguage: Language not found. Id = %s' + ', {lang_id') + return ResponseType('Kieltä ei löydy', + status=HttpResponseCode.BAD_REQUEST) + item.language = lang_id return None diff --git a/app/impl_country.py b/app/impl_country.py index cd2aaef..335f04f 100644 --- a/app/impl_country.py +++ b/app/impl_country.py @@ -4,12 +4,24 @@ from typing import Any, Union from app import app -def AddCountry(name: str) -> Union[int, None]: - session = new_session() +def AddCountry(session, name: str) -> Union[int, None]: + """ + Adds a country to the database if it doesn't already exist, and returns + the country's ID. + + Args: + session (Any): The database session. + name (str): The name of the country to add. + + Returns: + Union[int, None]: The ID of the country if it already exists, or None + if an exception occurs. + """ + try: country = Country(name=name) session.add(country) - session.commit() + session.flush() return country.id except SQLAlchemyError as exp: app.logger.error('Exception in AddCountry: ' + str(exp)) diff --git a/app/impl_people.py b/app/impl_people.py index 95b7939..a8a9430 100644 --- a/app/impl_people.py +++ b/app/impl_people.py @@ -115,9 +115,10 @@ def _set_nationality( old_values['Kansallisuus'] = person.nationality.name else: old_values['Kansallisuus'] = '' - if 'id' not in data['nationality'] and data['nationality'] != '': + if (isinstance(data['nationality'], str) or + ('id' not in data['nationality'] and data['nationality'] != '')): # Add new nationality to db - nat_id = AddCountry(data['nationality']) + nat_id = AddCountry(session, data['nationality']) elif data['nationality'] == '': nat_id = None else: diff --git a/app/model.py b/app/model.py index c74247a..870f25f 100644 --- a/app/model.py +++ b/app/model.py @@ -226,6 +226,7 @@ class Meta: contributions = ma.List(fields.Nested(WorkContributorSchema)) images = ma.List(fields.Nested(EditionImageBriefSchema)) publisher = fields.Nested(lambda: PublisherSchema(only=('id', 'name'))) + Language = fields.Nested(lambda: LanguageSchema(only=('id', 'name'))) class WorkTypeBriefSchema(ma.SQLAlchemyAutoSchema): # type: ignore @@ -252,6 +253,7 @@ class Meta: genres = ma.List(fields.Nested(GenreBriefSchema)) bookseries = fields.Nested(BookseriesBriefSchema) tags = ma.List(fields.Nested(TagBriefSchema)) + language = fields.Nested(LanguageSchema) class EditionBriefestSchema(ma.SQLAlchemyAutoSchema): # type: ignore @@ -486,7 +488,7 @@ class Meta: genres = ma.List(fields.Nested(GenreBriefSchema)) bookseries = fields.Nested(BookseriesBriefSchema) tags = ma.List(fields.Nested(TagBriefSchema)) - language_name = fields.Nested(CountryBriefSchema) + language_name = fields.Nested(LanguageSchema) authors = ma.List(fields.Nested(PersonBriefSchema)) links = ma.List(fields.Nested(WorkLinkBriefSchema)) stories = ma.List(fields.Nested(ShortBriefSchema)) diff --git a/app/model_person.py b/app/model_person.py index 6c8046e..56d89c3 100644 --- a/app/model_person.py +++ b/app/model_person.py @@ -4,7 +4,8 @@ from app import ma from app.orm_decl import (Person, Edition, Contributor, Article, Awarded, ShortStory) -from .model import (PersonBriefSchema, PersonLinkBriefSchema, WorkBriefSchema, +from .model import (LanguageSchema, PersonBriefSchema, PersonLinkBriefSchema, + WorkBriefSchema, ShortBriefSchema, PublisherBriefSchema, EditionImageBriefSchema, ContributorRoleSchema, IssueBriefSchema, @@ -76,6 +77,7 @@ class PersonPageEditionWorkSchema(ma.SQLAlchemySchema): # type: ignore tags = ma.List(fields.Nested(TagBriefSchema)) contributions = ma.List(fields.Nested(lambda: WorkContributorSchema( only=['description', 'person', 'role']))) + language_name = fields.Nested(LanguageSchema) class PersonPageEditionSchema(ma.SQLAlchemyAutoSchema): # type: ignore @@ -90,7 +92,7 @@ class Meta: pubseries = fields.Nested(PersonPagePubseriesSchema) work = ma.List(fields.Nested(lambda: PersonPageEditionWorkSchema( only=['id', 'title', 'orig_title', 'pubyear', 'editions', 'genres', - 'bookseries', 'tags', 'contributions']))) + 'bookseries', 'tags', 'contributions', 'language_name']))) # images = ma.List(fields.Nested(EditionImageBriefSchema)) # binding = fields.Nested(BindingBriefSchema) # format = fields.Nested(FormatBriefSchema) @@ -129,7 +131,7 @@ class Meta: genres = ma.List(fields.Nested(GenreBriefSchema)) contributors = ma.List(fields.Nested(PersonPageContributorSchema)) tags = ma.List(fields.Nested(TagBriefSchema)) - language = fields.Nested(CountryBriefSchema) + language_name = fields.Nested(CountryBriefSchema) class PersonPageWorkBriefSchema(ma.SQLAlchemySchema): # type: ignore @@ -146,6 +148,7 @@ class PersonPageWorkBriefSchema(ma.SQLAlchemySchema): # type: ignore lambda: BookseriesBriefSchema(exclude=['works'])) bookseriesnum = fields.String() tags = ma.List(fields.Nested(TagBriefSchema)) + language_name = fields.Nested(LanguageSchema) class PersonSchema(ma.SQLAlchemyAutoSchema): # type: ignore