annotate roundup/cgi/TranslationService.py @ 5710:0b79bfcb3312

Add support for making an idempotent POST. This allows retrying a POST that was interrupted. It involves creating a post once only (poe) url /rest/data/<class>/@poe/<random_token>. This url acts the same as a post to /rest/data/<class>. However once the @poe url is used, it can't be used for a second POST. To make these changes: 1) Take the body of post_collection into a new post_collection_inner function. Have post_collection call post_collection_inner. 2) Add a handler for POST to rest/data/class/@poe. This will return a unique POE url. By default the url expires after 30 minutes. The POE random token is only good for a specific user and is stored in the session db. 3) Add a handler for POST to rest/data/<class>/@poe/<random token>. The random token generated in 2 is validated for proper class (if token is not generic) and proper user and must not have expired. If everything is valid, call post_collection_inner to process the input and generate the new entry. To make recognition of 2 stable (so it's not confused with rest/data/<:class_name>/<:item_id>), removed @ from Routing::url_to_regex. The current Routing.execute method stops on the first regular expression to match the URL. Since item_id doesn't accept a POST, I was getting 405 bad method sometimes. My guess is the order of the regular expressions is not stable, so sometime I would get the right regexp for /data/<class>/@poe and sometime I would get the one for /data/<class>/<item_id>. By removing the @ from the url_to_regexp, there was no way for the item_id case to match @poe. There are alternate fixes we may need to look at. If a regexp matches but the method does not, return to the regexp matching loop in execute() looking for another match. Only once every possible match has failed should the code return a 405 method failure. Another fix is to implement a more sophisticated mechanism so that @Routing.route("/data/<:class_name>/<:item_id>/<:attr_name>", 'PATCH') has different regexps for matching <:class_name> <:item_id> and <:attr_name>. Currently the regexp specified by url_to_regex is used for every component. Other fixes: Made failure to find any props in props_from_args return an empty dict rather than throwing an unhandled error. Make __init__ for SimulateFieldStorageFromJson handle an empty json doc. Useful for POSTing to rest/data/class/@poe with an empty document. Testing: added testPostPOE to test/rest_common.py that I think covers all the code that was added. Documentation: Add doc to rest.txt in the "Client API" section titled: Safely Re-sending POST". Move existing section "Adding new rest endpoints" in "Client API" to a new second level section called "Programming the REST API". Also a minor change to the simple rest client moving the header setting to continuation lines rather than showing one long line.
author John Rouillard <rouilj@ieee.org>
date Sun, 14 Apr 2019 21:07:11 -0400
parents b0c2307be3d1
children 718f205dbe50
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
1 # TranslationService for Roundup templates
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
2 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
3 # This module is free software, you may redistribute it
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
4 # and/or modify under the same terms as Python.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
5 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
6 # This module provides National Language Support
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
7 # for Roundup templating - much like roundup.i18n
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
8 # module for Roundup command line interface.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
9 # The only difference is that translator objects
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
10 # returned by get_translation() have one additional
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
11 # method which is used by TAL engines:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
12 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
13 # translate(domain, msgid, mapping, context, target_language, default)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
14 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
15
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
16 from roundup import i18n
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
17 from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
18 from roundup.cgi.TAL import TALInterpreter
5416
56c9bcdea47f Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents: 4570
diff changeset
19 from roundup.anypy.strings import us2u, u2s
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
20
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
21 ### Translation classes
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
22
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
23 class TranslationServiceMixin:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
24
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
25 def translate(self, domain, msgid, mapping=None,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
26 context=None, target_language=None, default=None
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
27 ):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
28 _msg = self.gettext(msgid)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
29 #print ("TRANSLATE", msgid, _msg, mapping, context)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
30 _msg = TALInterpreter.interpolate(_msg, mapping)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
31 return _msg
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
32
5447
41532b2ab141 better detection if we need a gettext workaround
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5446
diff changeset
33 if hasattr(i18n.RoundupTranslations, 'ugettext'):
5446
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
34 def gettext(self, msgid):
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
35 msgid = us2u(msgid)
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
36 msgtrans=self.ugettext(msgid)
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
37 return u2s(msgtrans)
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
38
5446
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
39 def ngettext(self, singular, plural, number):
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
40 singular = us2u(singular)
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
41 plural = us2u(plural)
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
42 msgtrans=self.ungettext(singular, plural, number)
214f34e18678 fix infinite recursion in Python3
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5416
diff changeset
43 return u2s(msgtrans)
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
44
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
45 class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
46 pass
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
47
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
48 class NullTranslationService(TranslationServiceMixin,
3775
beaf7ea86e5e handle 8-bit untranslateable messages in tracker templates
Richard Jones <richard@users.sourceforge.net>
parents: 2807
diff changeset
49 i18n.RoundupNullTranslations):
5477
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
50 if hasattr(i18n.RoundupNullTranslations, 'ugettext'):
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
51 def ugettext(self, message):
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
52 if self._fallback:
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
53 return self._fallback.ugettext(message)
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
54 # Sometimes the untranslatable message is a UTF-8 encoded string
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
55 # (thanks to PageTemplate's internals).
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
56 message = us2u(message)
b0c2307be3d1 applied remaining part of original patch 045 (slightly modified)
Christof Meerwald <cmeerw@cmeerw.org>
parents: 5447
diff changeset
57 return message
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
58
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
59 ### TAL patching
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
60 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
61 # Template Attribute Language (TAL) uses only global translation service,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
62 # which is not thread-safe. We will use context variable 'i18n'
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
63 # to access request-dependent transalation service (with domain
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
64 # and target language set during initializations of the roundup
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
65 # client interface.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
66 #
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
67
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
68 class Context(TALES.Context):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
69
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
70 def __init__(self, compiler, contexts):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
71 TALES.Context.__init__(self, compiler, contexts)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
72 if not self.contexts.get('i18n', None):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
73 # if the context contains no TranslationService,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
74 # create default one
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
75 self.contexts['i18n'] = get_translation()
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
76 self.i18n = self.contexts['i18n']
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
77
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
78 def translate(self, domain, msgid, mapping=None,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
79 context=None, target_language=None, default=None):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
80 if context is None:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
81 context = self.contexts.get('here')
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
82 return self.i18n.translate(domain, msgid,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
83 mapping=mapping, context=context, default=default,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
84 target_language=target_language)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
85
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
86 class Engine(TALES.Engine):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
87
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
88 def getContext(self, contexts=None, **kwcontexts):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
89 if contexts is not None:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
90 if kwcontexts:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
91 kwcontexts.update(contexts)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
92 else:
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
93 kwcontexts = contexts
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
94 return Context(self, kwcontexts)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
95
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
96 # patching TAL like this is a dirty hack,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
97 # but i see no other way to specify different Context class
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
98 Expressions._engine = Engine(PathIterator.Iterator)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
99 Expressions.installHandlers(Expressions._engine)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
100
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
101 ### main API function
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
102
2807
aa0316a1b2aa get_translation: removed 'domain' argument, added 'tracker_home' argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2556
diff changeset
103 def get_translation(language=None, tracker_home=None,
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
104 translation_class=TranslationService,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
105 null_translation_class=NullTranslationService
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
106 ):
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
107 """Return Translation object for given language and domain
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
108
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
109 Arguments 'translation_class' and 'null_translation_class'
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
110 specify the classes that are instantiated for existing
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
111 and non-existing translations, respectively.
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
112 """
2807
aa0316a1b2aa get_translation: removed 'domain' argument, added 'tracker_home' argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2556
diff changeset
113 return i18n.get_translation(language=language,
aa0316a1b2aa get_translation: removed 'domain' argument, added 'tracker_home' argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents: 2556
diff changeset
114 tracker_home=tracker_home,
2556
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
115 translation_class=translation_class,
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
116 null_translation_class=null_translation_class)
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
117
297dedb7226e TranslationService for Roundup templates
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
diff changeset
118 # vim: set et sts=4 sw=4 :

Roundup Issue Tracker: http://roundup-tracker.org/