Skip to content

Commit

Permalink
[classification] implementa cache para traduções das coleções
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusandrade committed Mar 10, 2021
1 parent 60e2f17 commit cd280c8
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions bireme/classification/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! coding: utf-8
from django.utils.translation import ugettext_lazy as _, get_language
from django.core.cache import cache
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User
Expand Down Expand Up @@ -88,11 +89,23 @@ def save(self, *args, **kwargs):
super(Collection, self).save(*args, **kwargs)

def __unicode__(self):
col_name = self.name
if self.country:
col_name = u"{} ({})".format(self.name, self.country)

return col_name
lang_code = get_language()
cache_id = "classification_collection-{}-{}".format(lang_code, self.id)
collection_local = cache.get(cache_id)
if not collection_local:
translation = CollectionLocal.objects.filter(collection=self.id, language=lang_code)
if translation:
collection_local = translation[0].name
else:
collection_local = self.name

has_children = Collection.objects.filter(parent=self.id).exists()
if self.country and has_children:
collection_local = u"{} ({})".format(collection_local, self.country)

cache.set(cache_id, collection_local, None)

return collection_local

class CollectionLocal(models.Model):

Expand Down

0 comments on commit cd280c8

Please sign in to comment.