Esempio n. 1
0
		def getKeyTxn( kindName, orderKey ):
			"""Generates and returns a new, unique Key"""
			seqObj = db.GetOrInsert(kindName, "viur_bill_sequences", count=1000)
			idx = seqObj["count"]
			seqObj["count"] += 1
			db.Put( seqObj )
			return str(idx)
Esempio n. 2
0
    def ensureOwnModuleRootNode(self):
        """
		Ensures, that general root-node for the current module exists.
		If no root-node exists yet, it will be created.

		:returns: The entity of the root-node.
		:rtype: :class:`server.db.Entity`
		"""
        key = "rep_module_repo"
        return db.GetOrInsert(key,
                              self.viewLeafSkel().kindName + "_rootNode",
                              creationdate=datetime.now(),
                              rootNode=1)
Esempio n. 3
0
    def ensureOwnUserRootNode(self):
        """
		Ensures, that an root-node for the current user exists.
		If no root-node exists yet, it will be created.

		:returns: The entity of the root-node or None, if this was request was made by a guest.
		:rtype: :class:`server.db.Entity`
		"""
        thisuser = conf["viur.mainApp"].user.getCurrentUser()
        if thisuser:
            key = "rep_user_%s" % str(thisuser["key"])
            return db.GetOrInsert(key,
                                  self.viewLeafSkel().kindName + "_rootNode",
                                  creationdate=datetime.now(),
                                  rootNode=1,
                                  user=str(thisuser["key"]))
Esempio n. 4
0
def translate(key, **kwargs):
    """
	Translate *key* into language text pendant.

	This function is part of ViURs language support facilities for supporting internationalization (i18n).

	Translations are provided in the applications *translations* module in form of a dict, where the keys
	should be the language strings in the project's major language (usually english), and the values the
	strings provided in the particular language implemented. The translation key strings must be given
	in a lower-case order, altought they may be capitalized or upper-case written. If no key is found
	within a specific translation, it is directly used as the output string.

	The strings may contain placeholders in form ``{{placeholer}}``, which can be assigned via the
	*kwargs* argument.

	``translate()`` is also provided as ``_()`` as global function.

	In this simple example, a file ``translations/de.py`` is implemented with the content:

	.. code-block:: python

		de = {
				"welcome to viur": u"Willkommen in ViUR",
				"hello {{user}}!": u"Hallo, {{user}}!"
		}

	To support internationalization, it is simply done this way:

	.. code-block:: python

		txt = _( "Hello {{user}}!", user="******" ) + " - "  + _( "Welcome to ViUR" )

	Language support is also provided in Jinja2-templates like this:

	.. code-block:: jinja

		{{ _( "Hello {{user}}!", user="******" ) }} - {{ _( "Welcome to ViUR" ) }}

	This will both output "Hello John Doe! - Welcome to ViUR" in an english-configured language environment,
	and "Hallo John Doe! - Willkommen in ViUR" in a german-configured language environment.

	The current session language (or default language) can be overridden with ``_lang``, e.g.

	.. code-block:: python

		txt = _( "Hello {{user}}!", user="******" ) + " - "  + _( "Welcome to ViUR", lang="en" )

	will result in "Hallo John Doe! - Welcome to ViUR" in a german-configured language environment.

	:param key: The key value that should be translated; If no key is found in the configured language,
		key is directly used.
	:type key: str
	:param kwargs: May contain place-holders replaced as ``{{placeholer}}`` within the key or translation.
		The special-value ``_lang`` overrides the current language setting.

	:return: Translated text or key, with replaced placeholders, if given.
	:rtype: str
	"""

    try:
        lang = request.current.get().language
    except:
        return (key)

    if key is None:
        return (None)
    elif not isinstance(key, basestring):
        raise ValueError("Can only translate strings, got %s instead" %
                         str(type(key)))

    res = None
    lang = lang or conf["viur.defaultLanguage"]

    if "_lang" in kwargs.keys():
        lang = kwargs["_lang"]

    if lang in conf["viur.languageAliasMap"].keys():
        lang = conf["viur.languageAliasMap"][lang]

    if lang and lang in dir(translations):
        langDict = getattr(translations, lang)

        if key.lower() in langDict.keys():
            res = langDict[key.lower()]

    if res is None and lang and lang in dir(servertrans):
        langDict = getattr(servertrans, lang)

        if key.lower() in langDict.keys():
            res = langDict[key.lower()]

    if res is None and conf["viur.logMissingTranslations"]:
        from server import db
        db.GetOrInsert(key="%s-%s" % (key, str(lang)),
                       kindName="viur-missing-translations",
                       langkey=key,
                       lang=lang)

    if res is None:
        res = key

    for k, v in kwargs.items():
        res = res.replace("{{%s}}" % k, unicode(v))

    return (res)