Python GAE Optimization: Django Filter for Language Support

I have a filter that I use to support the language in my web application. But when I publish it to GAE, he constantly tells me that the processor utilization is too great.

I think I found a problem with my filters, which I use for support. I use this in my templates:

<h1>{{ "collection.header"|translate:lang }}</h1>

The filter code is as follows:

import re
from google.appengine.ext import webapp
from util import dictionary

register = webapp.template.create_template_register()

def translate(key, lang):
    d = dictionary.GetDictionaryKey(lang, key)
    if d == False:
        return "no key for " + key
    else: 
        return d.value

register.filter(translate)

I'm new to Python to figure out what's wrong with him. Or is this the whole wrong approach?

..fredrik


A little more about what I'm trying to do: I'm trying to move away from language support. The user should be able to update text elements through the admin page. At the moment, all text elements are stored in db.model. And use the filter to get the correct language-based key.

. . 30-50 . 1500 (900 API) . , ?

memcache , . . memcache ?

:

import re
from google.appengine.ext import webapp
from google.appengine.api import memcache

from util import dictionary

register = webapp.template.create_template_register()

def translate(key, lang):
    re = "no key for " + key

    data = memcache.get("dictionary" + lang)

    if data is None:
        data = dictionary.GetDictionaryKey(lang)
        memcache.add("dictionary" + lang, data, 60)

    if key in data:
        return data[key]
    else:   
        return "no key for " + key


register.filter(translate)

util.dictionary :

from google.appengine.ext import db

class DictionaryEntries(db.Model):
    lang = db.StringProperty()
    dkey = db.StringProperty()
    value = db.TextProperty()
    params = db.StringProperty()

    @property
    def itemid(self):
        return self.key().id()

def GetDictionaryKey(lang):
    entries = DictionaryEntries.all().filter("lang = ", lang)
    if entries.count() > 0:
        langObj = {}
        for entry in entries:
            langObj[entry.dkey] = entry.value

        return langObj 
    else:
        return False
+3
2

gettext? Gettext - Python ( Django).

:

:

{% load i18n %}
<h1>{% trans "Header of my Collection" %}</h1>

manage.py:

manage.py makemessages -l fr

(fr) , .

Gettext , , memcache. , "" , , , , , ( ).

+2

, , , GAE , BigTable (), entries.count() for entry in entrie , .

, :

utils.py

def GetDictionaryKey(lang, key):
    chache_key = 'dictionary_%s_%s' % (lang, key)
    data = memcache.get(cache_key)
    if not data:
         entry = DictionaryEntries.all().filter("lang = ", lang).filter("value =", key).get()
         if entry:
             data = memcache.add(cache_key, entry.value, 60)
         else:
             data = 'no result for %s' % key
    return data

:

 def translate(key, lang):
     return dictionary.GetDictionaryKey(lang, key)

, :

  • count
  • MVC, ( ), GetDictionaryKey .

, django, cache_key:

from django.template.defaultfilters import slugify
def GetDictionaryKey(lang, key):
    chache_key = 'dictionary_%s_%s' % (slugify(lang), slugify(key))
    data = memcache.get(cache_key)
    if not data:
         entry = DictionaryEntries.all().filter("lang = ", lang).filter("value =", key).get()
         if entry:
             data = memcache.add(cache_key, entry.value, 60)
         else:
             data = 'no result for %s' % key
    return data
+3

Source: https://habr.com/ru/post/1725157/


All Articles