예제 #1
0
    def test_manager_and_admin_mail_prefix(self):
        """
        String prefix + lazy translated subject = bad output
        Regression for #13494
        """
        mail_managers(ugettext_lazy('Subject'), 'Content')
        message = self.get_the_message()
        self.assertEqual(message.get('subject'), '[Django] Subject')

        self.flush_mailbox()
        mail_admins(ugettext_lazy('Subject'), 'Content')
        message = self.get_the_message()
        self.assertEqual(message.get('subject'), '[Django] Subject')
예제 #2
0
    def test_add_lazy_translation(self):
        storage = self.get_storage()
        response = self.get_response()

        storage.add(constants.INFO, ugettext_lazy('lazy message'))
        storage.update(response)

        storing = self.stored_messages_count(storage, response)
        self.assertEqual(storing, 1)
예제 #3
0
 def test_create_relation_with_ugettext_lazy(self):
     reporter = Reporter.objects.create(first_name='John',
                                        last_name='Smith',
                                        email='*****@*****.**')
     lazy = ugettext_lazy('test')
     reporter.article_set.create(headline=lazy,
                                 pub_date=datetime(2011, 6, 10))
     notlazy = six.text_type(lazy)
     article = reporter.article_set.get()
     self.assertEqual(article.headline, notlazy)
예제 #4
0
 def resolve(self, context):
     """Resolve this variable against a given context."""
     if self.lookups is not None:
         # We're dealing with a variable that needs to be resolved
         value = self._resolve_lookup(context)
     else:
         # We're dealing with a literal, so it's already been "resolved"
         value = self.literal
     if self.translate:
         if self.message_context:
             return pgettext_lazy(self.message_context, value)
         else:
             return ugettext_lazy(value)
     return value
예제 #5
0
    def test_validation_error(self):
        ###################
        # ValidationError #
        ###################

        # Can take a string.
        self.assertHTMLEqual(str(ErrorList(ValidationError("There was an error.").messages)),
                         '<ul class="errorlist"><li>There was an error.</li></ul>')

        # Can take a unicode string.
        self.assertHTMLEqual(six.text_type(ErrorList(ValidationError("Not \u03C0.").messages)),
                         '<ul class="errorlist"><li>Not π.</li></ul>')

        # Can take a lazy string.
        self.assertHTMLEqual(str(ErrorList(ValidationError(ugettext_lazy("Error.")).messages)),
                         '<ul class="errorlist"><li>Error.</li></ul>')

        # Can take a list.
        self.assertHTMLEqual(str(ErrorList(ValidationError(["Error one.", "Error two."]).messages)),
                         '<ul class="errorlist"><li>Error one.</li><li>Error two.</li></ul>')

        # Can take a mixture in a list.
        self.assertHTMLEqual(str(ErrorList(ValidationError(["First error.", "Not \u03C0.", ugettext_lazy("Error.")]).messages)),
                         '<ul class="errorlist"><li>First error.</li><li>Not π.</li><li>Error.</li></ul>')

        @python_2_unicode_compatible
        class VeryBadError:
            def __str__(self): return "A very bad error."

        # Can take a non-string.
        self.assertHTMLEqual(str(ErrorList(ValidationError(VeryBadError()).messages)),
                         '<ul class="errorlist"><li>A very bad error.</li></ul>')

        # Escapes non-safe input but not input marked safe.
        example = 'Example of link: <a href="http://www.example.com/">example</a>'
        self.assertHTMLEqual(str(ErrorList([example])),
                         '<ul class="errorlist"><li>Example of link: &lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;</li></ul>')
        self.assertHTMLEqual(str(ErrorList([mark_safe(example)])),
                         '<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>')
        self.assertHTMLEqual(str(ErrorDict({'name': example})),
                         '<ul class="errorlist"><li>nameExample of link: &lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;</li></ul>')
        self.assertHTMLEqual(str(ErrorDict({'name': mark_safe(example)})),
                         '<ul class="errorlist"><li>nameExample of link: <a href="http://www.example.com/">example</a></li></ul>')
예제 #6
0
def get_text_list(list_, last_word=ugettext_lazy('or')):
    """
    >>> get_text_list(['a', 'b', 'c', 'd'])
    'a, b, c or d'
    >>> get_text_list(['a', 'b', 'c'], 'and')
    'a, b and c'
    >>> get_text_list(['a', 'b'], 'and')
    'a and b'
    >>> get_text_list(['a'])
    'a'
    >>> get_text_list([])
    ''
    """
    if len(list_) == 0: return ''
    if len(list_) == 1: return force_text(list_[0])
    return '%s %s %s' % (
        # Translators: This string is used as a separator between list elements
        _(', ').join([force_text(i) for i in list_][:-1]),
        force_text(last_word), force_text(list_[-1]))
예제 #7
0
 def test_create_relation_with_ugettext_lazy(self):
     """
     Test that ugettext_lazy objects work when saving model instances
     through various methods. Refs #10498.
     """
     notlazy = 'test'
     lazy = ugettext_lazy(notlazy)
     reporter = Article.objects.create(headline=lazy, pub_date=datetime.now())
     article = Article.objects.get()
     self.assertEqual(article.headline, notlazy)
     # test that assign + save works with Promise objecs
     article.headline = lazy
     article.save()
     self.assertEqual(article.headline, notlazy)
     # test .update()
     Article.objects.update(headline=lazy)
     article = Article.objects.get()
     self.assertEqual(article.headline, notlazy)
     # still test bulk_create()
     Article.objects.all().delete()
     Article.objects.bulk_create([Article(headline=lazy, pub_date=datetime.now())])
     article = Article.objects.get()
     self.assertEqual(article.headline, notlazy)
예제 #8
0
 def test_header_injection(self):
     email = EmailMessage('Subject\nInjection Test', 'Content', '*****@*****.**', ['*****@*****.**'])
     self.assertRaises(BadHeaderError, email.message)
     email = EmailMessage(ugettext_lazy('Subject\nInjection Test'), 'Content', '*****@*****.**', ['*****@*****.**'])
     self.assertRaises(BadHeaderError, email.message)
예제 #9
0
from djangocg.contrib.admin.util import quote, get_fields_from_path, lookup_needs_distinct, prepare_lookup_value

# Changelist settings
ALL_VAR = "all"
ORDER_VAR = "o"
ORDER_TYPE_VAR = "ot"
PAGE_VAR = "p"
SEARCH_VAR = "q"
TO_FIELD_VAR = "t"
IS_POPUP_VAR = "pop"
ERROR_FLAG = "e"

IGNORED_PARAMS = (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR)

# Text to display within change-list table cells if the value is blank.
EMPTY_CHANGELIST_VALUE = ugettext_lazy("(None)")


class ChangeList(object):
    def __init__(
        self,
        request,
        model,
        list_display,
        list_display_links,
        list_filter,
        date_hierarchy,
        search_fields,
        list_select_related,
        list_per_page,
        list_max_show_all,
예제 #10
0
from djangocg.utils.translation import ugettext_lazy

JP_PREFECTURES = (
    ("hokkaido", ugettext_lazy("Hokkaido")),
    ("aomori", ugettext_lazy("Aomori")),
    ("iwate", ugettext_lazy("Iwate")),
    ("miyagi", ugettext_lazy("Miyagi")),
    ("akita", ugettext_lazy("Akita")),
    ("yamagata", ugettext_lazy("Yamagata")),
    ("fukushima", ugettext_lazy("Fukushima")),
    ("ibaraki", ugettext_lazy("Ibaraki")),
    ("tochigi", ugettext_lazy("Tochigi")),
    ("gunma", ugettext_lazy("Gunma")),
    ("saitama", ugettext_lazy("Saitama")),
    ("chiba", ugettext_lazy("Chiba")),
    ("tokyo", ugettext_lazy("Tokyo")),
    ("kanagawa", ugettext_lazy("Kanagawa")),
    ("yamanashi", ugettext_lazy("Yamanashi")),
    ("nagano", ugettext_lazy("Nagano")),
    ("niigata", ugettext_lazy("Niigata")),
    ("toyama", ugettext_lazy("Toyama")),
    ("ishikawa", ugettext_lazy("Ishikawa")),
    ("fukui", ugettext_lazy("Fukui")),
    ("gifu", ugettext_lazy("Gifu")),
    ("shizuoka", ugettext_lazy("Shizuoka")),
    ("aichi", ugettext_lazy("Aichi")),
    ("mie", ugettext_lazy("Mie")),
    ("shiga", ugettext_lazy("Shiga")),
    ("kyoto", ugettext_lazy("Kyoto")),
    ("osaka", ugettext_lazy("Osaka")),
    ("hyogo", ugettext_lazy("Hyogo")),
예제 #11
0
 def __init__(self, attrs=None):
     choices = (('1', ugettext_lazy('Unknown')),
                ('2', ugettext_lazy('Yes')),
                ('3', ugettext_lazy('No')))
     super(NullBooleanSelect, self).__init__(attrs, choices)
예제 #12
0
from __future__ import unicode_literals

from django import forms

from djangocg.contrib.auth import authenticate
from djangocg.contrib.auth.forms import AuthenticationForm
from djangocg.contrib.auth.models import User
from djangocg.utils.translation import ugettext_lazy, ugettext as _

ERROR_MESSAGE = ugettext_lazy("Please enter the correct username and password "
        "for a staff account. Note that both fields are case-sensitive.")

class AdminAuthenticationForm(AuthenticationForm):
    """
    A custom authentication form used in the admin app.

    """
    this_is_the_login_form = forms.BooleanField(widget=forms.HiddenInput, initial=1,
        error_messages={'required': ugettext_lazy("Please log in again, because your session has expired.")})

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        message = ERROR_MESSAGE

        if username and password:
            self.user_cache = authenticate(username=username, password=password)
            if self.user_cache is None:
                if '@' in username:
                    # Mistakenly entered e-mail address instead of username? Look it up.
                    try:
예제 #13
0
    if len(queryset) == 1:
        objects_name = force_text(opts.verbose_name)
    else:
        objects_name = force_text(opts.verbose_name_plural)

    if perms_needed or protected:
        title = _("Cannot delete %(name)s") % {"name": objects_name}
    else:
        title = _("Are you sure?")

    context = {
        "title": title,
        "objects_name": objects_name,
        "deletable_objects": [deletable_objects],
        'queryset': queryset,
        "perms_lacking": perms_needed,
        "protected": protected,
        "opts": opts,
        "app_label": app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
    }

    # Display the confirmation page
    return TemplateResponse(request, modeladmin.delete_selected_confirmation_template or [
        "admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.object_name.lower()),
        "admin/%s/delete_selected_confirmation.html" % app_label,
        "admin/delete_selected_confirmation.html"
    ], context, current_app=modeladmin.admin_site.name)

delete_selected.short_description = ugettext_lazy("Delete selected %(verbose_name_plural)s")