def submit(self): if self.action == "close": self.go_back() form_data = self.form_data subset = form_data["subset"] languages = set(form_data["published_languages"]) targets = form_data["publication_targets"] check = (self.action == "check") results = self.results for publishable in subset: for target in targets: for language in sorted(languages & set(target.languages)): if check: try: with language_context(language): existing_post = target.find_post(publishable) results.append( (publishable, target, language, existing_post)) except Exception, ex: results.append((publishable, target, language, ex)) else: try: with language_context(language): target.publish(publishable) except Exception, ex: results.append((publishable, target, language, ex)) else: results.append( (publishable, target, language, None))
def test_translation_enabled(self): from cocktail.translations import language_context from woost.models import (Publishable, ReadPermission, ReadTranslationPermission) self.everybody_role.permissions.append( ReadPermission(matching_items={ "type": "woost.models.publishable.Publishable" })) self.everybody_role.permissions.append(ReadTranslationPermission()) self.config.languages = ["en"] self.config.published_languages = [] with language_context("en"): a = Publishable() a.per_language_publication = True a.translation_enabled = True a.insert() b = Publishable() b.per_language_publication = True b.translation_enabled = False b.insert() c = Publishable() c.per_language_publication = True c.translation_enabled = True c.insert() d = Publishable() d.per_language_publication = True d.set("translation_enabled", True, "de") d.insert() e = Publishable() e.per_language_publication = False e.enabled = True e.insert() accessible = self.accessible_items() print a in accessible print b in accessible print c in accessible print d in accessible print e in accessible assert self.accessible_items() == set([a, c, e]) self.config.published_languages = ["en"] assert self.accessible_items() == set([a, c, e]) with language_context("de"): self.config.published_languages = ["de"] assert self.accessible_items() == set([d, e])
def submit(self): if self.action == "close": self.go_back() form_data = self.form_data subset = form_data["subset"] languages = set(form_data["published_languages"]) targets = form_data["publication_targets"] check = (self.action == "check") results = self.results if check: og = OpenGraphExtension.instance for target in targets: if check: posts = target.feed_posts() for publishable in subset: for language in sorted(languages & set(target.languages)): if check: try: with language_context(language): uri = og.get_properties(publishable)["og:url"] results.append(( publishable, target, language, first( post for post in posts if post.get("link") == uri ) )) except Exception, ex: results.append((publishable, target, language, ex)) else: try: with language_context(language): target.publish(publishable) except Exception, ex: results.append((publishable, target, language, ex)) else: results.append((publishable, target, language, None))
def _create_ecommerceorder_completed_trigger(self): from woost.extensions.ecommerce.ecommerceordercompletedtrigger import \ ECommerceOrderCompletedTrigger trigger = ECommerceOrderCompletedTrigger( ) trigger.qname = \ "woost.extensions.ecommerce.ecommerceorder_completed_trigger" self.__translate_field(trigger, "title") Configuration.instance.triggers.append(trigger) trigger.condition = "target.customer and not target.customer.anonymous and target.customer.email" trigger.matching_items = {'type': u'woost.extensions.ecommerce.ecommerceorder.ECommerceOrder'} # EmailTemplate template = EmailTemplate() template.qname = \ "woost.extensions.ecommerce.ecommerceorder_completed_emailtemplate" self.__translate_field(template, "title") template.sender = '"%s"' % User.require_instance( qname = "woost.administrator" ).email template.receivers = '[items[0].customer.email]' template.embeded_images = """ from woost.models import Configuration logo = Configuration.instance.get_setting("logo") if logo: images["logo"] = logo """ template.template_engine = "cocktail" for language in translations: with language_context(language): template.subject = template.title template.body = """ <% from cocktail.controllers import Location from cocktail.html import templates order = items[0] order_summary = templates.new("woost.extensions.ecommerce.OrderConfirmationMessage") order_summary.order = order %> <html> <head> <base href="@{unicode(Location.get_current_host())}"/> </head> <body> ${order_summary.render()} </body> </html> """ # Response response = SendEmailTriggerResponse() response.qname = \ "woost.extensions.ecommerce.ecommerceorder_completed_response" response.email_template = template trigger.responses = [response] return trigger
def create_instance(language=None): if hidden: with language_context(language): return self.create_hidden_input(self.data, member) else: instance = self.create_field_instance(member, language) entry.field_instances.append(instance) return instance
def eval(self, context, accessor = None): languages = self.languages if languages is None: languages = [get_language()] subject_tokens = set() stemming = self.stemming if stemming is None: stemming = getattr(self.subject, "stemming", False) if stemming: iter_tokens = words.iter_stems else: def iter_tokens(text, language = None): text = words.normalize(text, locale = language) return words.split(text, locale = language) for language in languages: with language_context(language): lang_text = self.subject.eval(context, accessor) if not lang_text: continue get_searchable_text = getattr( lang_text, "get_searchable_text", None ) if get_searchable_text is not None: lang_text = get_searchable_text(languages = (language,)) subject_tokens.update(iter_tokens(lang_text, language)) for language in languages: query_tokens = words.get_unique_stems(self.query, language) if self.match_mode == "whole_word": if self.logic == "and": if subject_tokens.issuperset(query_tokens): return True elif not query_tokens.isdisjoint(subject_tokens): return True else: if self.match_mode == "prefix": query_tokens = [token + "*" for token in query_tokens] operand = all if self.logic == "and" else any if operand( fnmatch.filter(subject_tokens, token) for token in query_tokens ): return True return False
def test_translation_permitted(self): from cocktail.translations import language_context from woost.models import (Publishable, ReadPermission, ReadTranslationPermission, set_current_user) set_current_user(self.user) self.everybody_role.permissions.append( ReadPermission(matching_items={ "type": "woost.models.publishable.Publishable" })) self.everybody_role.permissions.append( ReadTranslationPermission(matching_languages=["ca", "es"])) self.everybody_role.permissions.append( ReadTranslationPermission(matching_languages=["en"], authorized=False)) self.config.languages = ["ca", "es", "en"] self.config.published_languages = [] a = Publishable() a.per_language_publication = True a.insert() b = Publishable() b.per_language_publication = False b.insert() with language_context("ca"): a.translation_enabled = True assert a.is_accessible() assert b.is_accessible() assert self.accessible_items() == set([a, b]) for language in "es", "en", "de": with language_context(language): assert not a.is_accessible() assert b.is_accessible() assert self.accessible_items() == set([b])
def _create_incoming_order_trigger(self): from woost.extensions.ecommerce.incomingordertrigger import \ IncomingOrderTrigger trigger = IncomingOrderTrigger( ) trigger.qname = "woost.extensions.ecommerce.incoming_order.trigger" self.__translate_field(trigger, "title") Configuration.instance.triggers.append(trigger) trigger.matching_items = {'type': u'woost.extensions.ecommerce.ecommerceorder.ECommerceOrder'} # EmailTemplate template = EmailTemplate() template.qname = \ "woost.extensions.ecommerce.incoming_order.email_template" self.__translate_field(template, "title") admin = User.require_instance(qname = "woost.administrator") template.sender = repr(admin.email) template.receivers = repr([admin.email]) template.template_engine = "cocktail" for language in translations: with language_context(language): template.subject = template.title template.body = """ <% from cocktail.translations import translations from woost.models import Publishable order = items[0] bo = Publishable.require_instance(qname = "woost.backoffice") edit_url = bo.get_uri(host = ".", path = ["content", str(order.id)]) %> <html> <body> <a href="${edit_url}">${translations('woost.extensions.ecommerce.incoming_order.edit_link')} </a> </body> </html> """ # Response response = SendEmailTriggerResponse() response.email_template = template trigger.responses = [response] return trigger
def create_row(self, index, item): self.__split_row_iterators.clear() self.__split_row_values.clear() row = Element("tr") row.add_class(index % 2 == 0 and "odd" or "even") if (self.selection_mode != NO_SELECTION and self.use_separate_selection_column): row.append(self.create_selection_cell(item)) if self.schema.primary_member: row["id"] = item.id for group, columns in self.columns_by_group: for column in columns: if self.translations and column.translated: for language in self.translations: with language_context(language): cell = self.create_cell(item, column, language) row.append(cell) else: key = column.name sequence_factory = self.__split_rows.get(key) if sequence_factory is not None: iterator = iter(sequence_factory(item)) self.__split_row_iterators[key] = iterator try: value = next(iterator) except StopIteration: value = None self.__split_row_values[key] = value cell = self.create_cell(item, column) row.append(cell) if (self.selection_mode != NO_SELECTION and not self.use_separate_selection_column): row.children[0].insert(0, self.create_selection_control(item)) return row
def post_redirection(destination, data): """Redirect the client to the given URL using a POST request.""" with language_context(get_language() or "en"): trans_prefix = "cocktail.controllers.redirection.post_redirection." title = translations(trans_prefix + "title") submit_label = translations(trans_prefix + "submit_button") explanation = translations(trans_prefix + "explanation") form_content = "\n".join("""<input type="hidden" name="%s" value="%s">""" % (key, value) for key, value in data.items()) cherrypy.response.status = 200 cherrypy.response.body = (""" <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>%(title)s</title> <script type="text/javascript"> <!-- onload = function () { document.getElementById("redirectionForm").submit(); } //--> </script> </head> <body> <form id="redirectionForm" method="POST" action="%(action)s"> %(form_content)s <noscript> <p>%(explanation)s</p> <input type="submit" value="%(submit_label)s"> </noscript> </form> </body> </html> """ % { "title": title, "action": destination, "form_content": form_content, "explanation": explanation, "submit_label": submit_label }) raise StopRequest()
def create_translated_values(self, member): cell = Element("td") table = Element("table") table.add_class("translated_values") cell.append(table) for language in (self.translations or (get_language(), )): language_row = Element("tr") language_row.add_class(language) table.append(language_row) language_label = Element("th") language_label.append(translations(language)) language_row.append(language_label) with language_context(language): language_value_cell = self.create_value(member) language_row.append(language_value_cell) return cell
def create_field_instance(self, member, language=None): field_instance = Element("td" if self.table_layout else "div") field_instance.add_class("field_instance") # Label if not self.get_member_hidden(member): if member.translated and not self.redundant_translation_labels: label = self.create_language_label(member, language) else: label = self.create_label(member, language) field_instance.label = label field_instance.append(label) # Control with language_context(language): field_instance.control = self.create_control(self.data, member) if field_instance.control.class_css: for class_name in field_instance.control.class_css.split(" "): field_instance.add_class("field_instance-" + class_name) insert = getattr(field_instance.control, "insert_into_form", None) if insert: insert(self, field_instance) else: field_instance.append(field_instance.control) if field_instance.control.tag \ in ("input", "button", "select", "textarea"): field_instance.label["for"] = field_instance.control.require_id() return field_instance
def __translate_field(self, obj, key): for language in translations: with language_context(language): value = translations("%s.%s" % (obj.qname, key)) if value: obj.set(key, value)
def test_translate(self): values = ( ( ["ca", "es"], ( (Decimal("0"), "0"), (Decimal("-0"), "-0"), # Heh... (Decimal("+0"), "0"), # Heh... (Decimal("1"), "1"), (Decimal("+1"), "1"), (Decimal("105"), "105"), (Decimal("50.0"), "50,0"), (Decimal("50.00"), "50,00"), (Decimal("150.25"), "150,25"), (Decimal("150.2500"), "150,2500"), (Decimal("0.3333"), "0,3333"), (Decimal("0.04"), "0,04"), (Decimal("0.00007"), "0,00007"), (Decimal("5000"), "5.000"), (Decimal("100000"), "100.000"), (Decimal("5.000"), "5,000"), (Decimal("100.000"), "100,000"), (Decimal("15000.03"), "15.000,03"), (Decimal("250000.3"), "250.000,3"), (Decimal("-1"), "-1"), (Decimal("-105"), "-105"), (Decimal("-50.0"), "-50,0"), (Decimal("-150.25"), "-150,25"), (Decimal("-0.3333"), "-0,3333"), (Decimal("-0.04"), "-0,04"), (Decimal("-0.00007"), "-0,00007"), (Decimal("-5000"), "-5.000"), (Decimal("-100000"), "-100.000"), (Decimal("-5.000"), "-5,000"), (Decimal("-100.000"), "-100,000"), (Decimal("-15000.03"), "-15.000,03"), (Decimal("-250000.3"), "-250.000,3"))), ( ["en"], ( (Decimal("0"), "0"), (Decimal("-0"), "-0"), # Again... (Decimal("+0"), "0"), (Decimal("1"), "1"), (Decimal("+1"), "1"), (Decimal("105"), "105"), (Decimal("50.0"), "50.0"), (Decimal("50.00"), "50.00"), (Decimal("150.25"), "150.25"), (Decimal("150.2500"), "150.2500"), (Decimal("0.3333"), "0.3333"), (Decimal("0.04"), "0.04"), (Decimal("0.00007"), "0.00007"), (Decimal("5000"), "5,000"), (Decimal("100000"), "100,000"), (Decimal("5.000"), "5.000"), (Decimal("100.000"), "100.000"), (Decimal("15000.03"), "15,000.03"), (Decimal("250000.3"), "250,000.3"), (Decimal("-1"), "-1"), (Decimal("-105"), "-105"), (Decimal("-50.0"), "-50.0"), (Decimal("-150.25"), "-150.25"), (Decimal("-0.3333"), "-0.3333"), (Decimal("-0.04"), "-0.04"), (Decimal("-0.00007"), "-0.00007"), (Decimal("-5000"), "-5,000"), (Decimal("-100000"), "-100,000"), (Decimal("-5.000"), "-5.000"), (Decimal("-100.000"), "-100.000"), (Decimal("-15000.03"), "-15,000.03"), (Decimal("-250000.3"), "-250,000.3")))) for languages, tests in values: for language in languages: with language_context(language): for raw, expected in tests: assert translations(raw) == expected
def test_decimal(self): from decimal import Decimal from cocktail.schema import Decimal as DecimalMember from cocktail.controllers.parameters import FormSchemaReader from cocktail.translations import language_context reader = FormSchemaReader() member = DecimalMember() ca_es_values = ( ("foo", "foo"), ("0", Decimal("0")), ("-0", Decimal("-0")), ("+0", Decimal("0")), ("1", Decimal("1")), ("+1", Decimal("1")), ("105", Decimal("105")), ("50,0", Decimal("50.0")), ("150,25", Decimal("150.25")), ("0,3333", Decimal("0.3333")), ("5000", Decimal("5000")), ("5.000", Decimal("5000")), ("100.000", Decimal("100000")), ("5,000", Decimal("5.000")), ("100,000", Decimal("100.000")), ("15.000,03", Decimal("15000.03")), ("250.000,3", Decimal("250000.3")), ("250,000.3", "250,000.3"), ("10.00.000", "10.00.000") ) values = ( ("ca", ca_es_values), ("en", ( ("foo", "foo"), ("0", Decimal("0")), ("-0", Decimal("-0")), ("+0", Decimal("0")), ("1", Decimal("1")), ("+1", Decimal("1")), ("105", Decimal("105")), ("50.0", Decimal("50.0")), ("150.25", Decimal("150.25")), ("0.3333", Decimal("0.3333")), ("5,000", Decimal("5000")), ("100,000", Decimal("100000")), ("5000", Decimal("5000")), ("5.000", Decimal("5.000")), ("100.000", Decimal("100.000")), ("15,000.03", Decimal("15000.03")), ("250,000.3", Decimal("250000.3")), ("250.000,3", "250.000,3"), ("10,00,000", "10,00,000") )) ) for language, language_values in values: with language_context(language): for raw, expected in language_values: self.assertEqual( member.parse_request_value(reader, raw), expected )