Permalink
Browse files

Abstract out i18n name setting

  • Loading branch information...
1 parent 99d0f78 commit d86317d81f9878529fee39c5237b5e6daf1bac95 @cnorthwood cnorthwood committed May 27, 2011
@@ -9,7 +9,7 @@
from molly.apps.places.providers import BaseMapsProvider
from molly.apps.places.models import Source, Entity, EntityType, EntityTypeCategory
from molly.conf.settings import batch
-from molly.utils.i18n import override
+from molly.utils.i18n import override, set_name_in_language
class BBCTPEGResolver(etree.Resolver):
"""
@@ -74,7 +74,6 @@ def import_data(self, metadata, output):
entities[id] = entity
entity.source = source
- entity.title = message.find('summary').text
entity.primary_type = entity_type
locs = map(self._wgs84_to_point, road_traffic_message.findall('location_container/location_coordinates/WGS84'))
@@ -98,6 +97,8 @@ def import_data(self, metadata, output):
}
entity.save(identifiers={'bbc-tpeg': id})
+ set_name_in_language(entity, 'en',
+ title = message.find('summary').text)
entity.all_types = [entity_type]
entity.update_all_types_completion()
seen.add(entity.pk)
@@ -136,17 +137,8 @@ def _get_entity_type(self):
entity_type.save()
for lang_code, lang_name in settings.LANGUAGES:
with override(lang_code):
- name = entity_type.names.filter(language_code=lang_code)
- if name.count() == 0:
- entity_type.names.create(
- language_code=lang_code,
- verbose_name=_('travel alert'),
- verbose_name_singular=_('a travel alert'),
- verbose_name_plural=_('travel alerts'))
- else:
- name = name[0]
- name.verbose_name = _('travel alert')
- name.verbose_name_singular = _('a travel alert')
- name.verbose_name_plural = _('travel alerts')
- name.save()
+ set_name_in_language(entity_type, lang_code,
+ verbose_name=_('travel alert'),
+ verbose_name_singular=_('a travel alert'),
+ verbose_name_plural=_('travel alerts'))
return entity_type
@@ -117,15 +117,7 @@ def endElement(self, name):
sa.save()
for lang_code, name in self.names.items():
if lang_code is None: lang_code = 'en'
- names = sa.names.filter(language_code=lang_code)
- if names.count() == 0:
- sa.names.create(
- language_code=lang_code,
- title=name
- )
- else:
- names[0].name = name
- names[0].save()
+ set_name_in_language(entity, lang_code, name=name)
elif name == 'CommonName':
if self.lang not in self.names:
@@ -271,16 +263,7 @@ def add_stop(self, meta, entity_type, source):
for lang_code, name in names.items():
# This is the NaPTAN, so default to English
if lang_code is None: lang_code = 'en'
- titles = entity.names.filter(language_code=lang_code)
- if titles.count() == 0:
- entity.names.create(
- language_code=lang_code,
- title=name
- )
- else:
- title = titles[0]
- title.title = name
- title.save()
+ set_name_in_language(entity, lang_code, title=name)
entity.all_types = (entity_type,)
entity.update_all_types_completion()
@@ -790,19 +773,10 @@ def _get_entity_types(self):
entity_type.save()
for lang_code, lang_name in settings.LANGUAGES:
with override(lang_code):
- name = entity_type.names.filter(language_code=lang_code)
- if name.count() == 0:
- entity_type.names.create(
- language_code=lang_code,
- verbose_name=ugettext(et['verbose-name']),
- verbose_name_singular=ugettext(et['verbose-name-singular']),
- verbose_name_plural=ugettext(et['verbose-name-plural']))
- else:
- name = name[0]
- name.verbose_name=ugettext(et['verbose-name'])
- name.verbose_name_singular=ugettext(et['verbose-name-singular'])
- name.verbose_name_plural=ugettext(et['verbose-name-plural'])
- name.save()
+ set_name_in_language(entity_type, lang_code,
+ verbose_name=ugettext(et['verbose-name']),
+ verbose_name_singular=ugettext(et['verbose-name-singular']),
+ verbose_name_plural=ugettext(et['verbose-name-plural']))
entity_types[stop_type] = entity_type
@@ -19,7 +19,7 @@
EntityTypeCategory, EntityName)
from molly.apps.places.providers import BaseMapsProvider
from molly.utils.misc import AnyMethodRequest
-from molly.utils.i18n import override
+from molly.utils.i18n import override, set_name_in_language
from molly.geolocation import reverse_geocode
from molly.conf.settings import batch
@@ -174,16 +174,7 @@ def endElement(self, name):
entity.save(identifiers={'osm': self.id})
for lang_code, name in names.items():
- titles = entity.names.filter(language_code=lang_code)
- if titles.count() == 0:
- entity.names.create(
- language_code=lang_code,
- title=name
- )
- else:
- title = titles[0]
- title.title = name
- title.save()
+ set_name_in_language(entity, lang_code, title=name)
entity.all_types = [self.entity_types[et] for et in types]
entity.update_all_types_completion()
@@ -342,19 +333,10 @@ def _get_entity_types(self):
entity_type.save()
for lang_code, lang_name in settings.LANGUAGES:
with override(lang_code):
- name = entity_type.names.filter(language_code=lang_code)
- if name.count() == 0:
- entity_type.names.create(
- language_code=lang_code,
- verbose_name=_(et['verbose_name']),
- verbose_name_singular=_(et['verbose_name_singular']),
- verbose_name_plural=_(et['verbose_name_plural']))
- else:
- name = name[0]
- name.verbose_name=_(et['verbose_name'])
- name.verbose_name_singular=_(et['verbose_name_singular'])
- name.verbose_name_plural=_(et['verbose_name_plural'])
- name.save()
+ set_name_in_language(entity, lang_code,
+ verbose_name=_(et['verbose_name']),
+ verbose_name_singular=_(et['verbose_name_singular']),
+ verbose_name_plural=_(et['verbose_name_plural']))
new_entity_types.add(slug)
entity_types[slug] = entity_type
@@ -15,7 +15,7 @@
from molly.apps.places.providers import BaseMapsProvider
from molly.apps.places.models import Entity, EntityType, Source, EntityTypeCategory
-from molly.utils.i18n import override
+from molly.utils.i18n import override, set_name_in_language
from molly.conf.settings import batch
@@ -84,16 +84,8 @@ def _load_from_csv(self, reader, entity_type, source):
'postcode-canonical': postcode,
}
entity.save(identifiers=identifiers)
- titles = entity.names.all()
- if titles.count() == 0:
- entity.names.create(
- language_code=settings.LANGUAGE_CODE,
- title=postcode
- )
- else:
- for title in titles:
- title.title = postcode
- title.save()
+ set_name_in_language(entity, 'en',
+ title = postcode)
entity.all_types.add(entity_type)
entity.update_all_types_completion()
@@ -108,19 +100,10 @@ def _get_entity_type(self):
entity_type.save()
for lang_code, lang_name in settings.LANGUAGES:
with override(lang_code):
- name = entity_type.names.filter(language_code=lang_code)
- if name.count() == 0:
- entity_type.names.create(
- language_code=lang_code,
- verbose_name=_('postcode'),
- verbose_name_singular=_('a postcode'),
- verbose_name_plural=_('postcodes'))
- else:
- name = name[0]
- name.verbose_name = _('postcode')
- name.verbose_name_singular = _('a postcode')
- name.verbose_name_plural = _('postcodes')
- name.save()
+ set_name_in_language(entity_type, lang_code,
+ verbose_name=_('postcode'),
+ verbose_name_singular=_('a postcode'),
+ verbose_name_plural=_('postcodes'))
return entity_type
def _get_source(self):
@@ -7,9 +7,9 @@
from django.utils.translation import ugettext_noop as _
from molly.conf.settings import batch
+from molly.utils.i18n import set_name_in_language
from molly.apps.podcasts.providers import BasePodcastsProvider
from molly.apps.podcasts.models import Podcast, PodcastItem, PodcastCategory, PodcastEnclosure
-
from molly.apps.podcasts.providers.rss import RSSPodcastsProvider
logger = logging.getLogger(__name__)
@@ -41,15 +41,7 @@ def decode_category(self, attrib):
slug = slugify(cat)
podcast_category, created = PodcastCategory.objects.get_or_create(slug=slug)
- name = category.names.filter(language_code=settings.LANGUAGE_CODE)
- if name.count() == 0:
- category.names.create(
- language_code=settings.LANGUAGE_CODE,
- name=cat)
- else:
- name = name[0]
- name.name = cat
- name.save()
+ set_name_in_language(category, lang_code, name=cat)
try:
podcast_category.order = self.CATEGORY_ORDERS[slug]
@@ -10,6 +10,7 @@
from molly.conf.settings import batch
from molly.apps.podcasts.providers import BasePodcastsProvider
from molly.apps.podcasts.models import Podcast, PodcastItem, PodcastCategory, PodcastEnclosure
+from molly.utils.i18n import set_name_in_language
from rss import RSSPodcastsProvider
@@ -31,15 +32,7 @@ def import_data(self, metadata, output):
slug = link.attrib['href'].split('/')[-1]
category, created = PodcastCategory.objects.get_or_create(slug=slug)
- name = category.names.filter(language_code=settings.LANGUAGE_CODE)
- if name.count() == 0:
- category.names.create(
- language_code=settings.LANGUAGE_CODE,
- name=category_elem.find(atom('title')).text)
- else:
- name = name[0]
- name.name = category_elem.find(atom('title')).text
- name.save()
+ set_name_in_language(category, lang_code, name=category_elem.find(atom('title')).text)
category.order = i
category.save()
View
@@ -9,6 +9,11 @@
from molly.utils.views import BaseView
def name_in_language(obj, field):
+ """
+ Assuming the object follows the Molly pattern for i18n data (related manager
+ called names, and the related object has a language_code field), then get
+ the i18n'd version of 'field' in the user's current language.
+ """
try:
return getattr(obj.names.get(language_code=get_language()), field)
except ObjectDoesNotExist:
@@ -20,6 +25,25 @@ def name_in_language(obj, field):
else:
raise
+def set_name_in_language(obj, lang, **fields):
+ """
+ Assuming the object follows the Molly pattern for i18n data (related manager
+ called names, and the related object has a language_code field), then set
+ name/language pair to 'field'.
+ """
+
+ names = obj.names.filter(language_code=lang)
+ if names.count() == 0:
+
+ obj.names.create(
+ language_code=lang,
+ **fields)
+ else:
+ name = names[0]
+ for k, v in fields.items():
+ setattr(name, k, v)
+ name.save()
+
class SetLanguageView(BaseView):
def handle_GET(self, request, context):

0 comments on commit d86317d

Please sign in to comment.