Skip to content

Commit

Permalink
Adding new countries and languages didn't work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Makistos committed Dec 17, 2024
1 parent 4e5105c commit 12880f4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
46 changes: 26 additions & 20 deletions app/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
18 changes: 15 additions & 3 deletions app/impl_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions app/impl_people.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 6 additions & 3 deletions app/model_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 12880f4

Please sign in to comment.