Ejemplo n.º 1
0
class BaseLanguageTestCase(BaseUnittest):
    """
    Contains some language helper stuff.    
    """
    def tearDown(self):
        super(BaseLanguageTestCase, self).tearDown()
        if self._system_preferences is not None:
            # revert changes from self.enable_i18n_debug()
            self._system_preferences["message_level_anonymous"] = self.old_message_level
            self._system_preferences.save()
        settings.DEBUG = False
        settings.PYLUCID.I18N_DEBUG = False

    def enable_i18n_debug(self):
        """
        enable DEBUG, PYLUCID.I18N_DEBUG and set message_level_anonymous to DEBUG.
        """
        cache.clear()
        from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
        self._system_preferences = SystemPreferencesForm()
        self.old_message_level = self._system_preferences["message_level_anonymous"]
        self._system_preferences["message_level_anonymous"] = message_constants.DEBUG
        self._system_preferences.save()
        settings.DEBUG = True
        settings.PYLUCID.I18N_DEBUG = True

    def _pre_setup(self, *args, **kwargs):
        """ create some language related attributes """
        super(BaseLanguageTestCase, self)._pre_setup(*args, **kwargs)

        self._system_preferences = None  # used in enable_i18n_debug() and tearDown()

        # default language is defined with settings.LANGUAGE_CODE
        self.default_language = Language.objects._get_default_language()
        self.failUnlessEqual(self.default_language.code, settings.LANGUAGE_CODE)

        self.other_lang_code = "de"
        assert self.other_lang_code != self.default_language.code
        self.other_language = Language.objects.get(code=self.other_lang_code)

    def assertContentLanguage(self, response, lang):
        """ Check if response is in right language """
        assert isinstance(lang, Language)
        is_lang = response["content-language"]
        if is_lang != lang.code:
            self.raise_browser_traceback(response,
                msg="Header 'Content-Language' is not %r it's: %r" % (lang.code, is_lang)
            )
        self.assertResponse(response,
            must_contain=(
                '<body lang="%s">' % lang.code,
                '<html lang="%(code)s">' % {
                    "code": lang.code
                },
            )
        )
Ejemplo n.º 2
0
class BaseLanguageTestCase(BaseUnittest):
    """
    Contains some language helper stuff.    
    """
    def tearDown(self):
        super(BaseLanguageTestCase, self).tearDown()
        if self._system_preferences is not None:
            # revert changes from self.enable_i18n_debug()
            self._system_preferences["message_level_anonymous"] = self.old_message_level
            self._system_preferences.save()
        settings.DEBUG = False
        settings.PYLUCID.I18N_DEBUG = False

    def enable_i18n_debug(self):
        """
        enable DEBUG, PYLUCID.I18N_DEBUG and set message_level_anonymous to DEBUG.
        """
        cache.clear()
        from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
        self._system_preferences = SystemPreferencesForm()
        self.old_message_level = self._system_preferences["message_level_anonymous"]
        self._system_preferences["message_level_anonymous"] = message_constants.DEBUG
        self._system_preferences.save()
        settings.DEBUG = True
        settings.PYLUCID.I18N_DEBUG = True

    def _pre_setup(self, *args, **kwargs):
        """ create some language related attributes """
        super(BaseLanguageTestCase, self)._pre_setup(*args, **kwargs)

        self._system_preferences = None # used in enable_i18n_debug() and tearDown()

        # default language is defined with settings.LANGUAGE_CODE
        self.default_language = Language.objects._get_default_language()
        self.failUnlessEqual(self.default_language.code, settings.LANGUAGE_CODE)

        self.other_lang_code = "de"
        assert self.other_lang_code != self.default_language.code
        self.other_language = Language.objects.get(code=self.other_lang_code)

    def assertContentLanguage(self, response, lang):
        """ Check if response is in right language """
        assert isinstance(lang, Language)
        is_lang = response["content-language"]
        if is_lang != lang.code:
            self.raise_browser_traceback(response,
                msg="Header 'Content-Language' is not %r it's: %r" % (lang.code, is_lang)
            )
        self.assertResponse(response,
            must_contain=(
                '<body lang="%s">' % lang.code,
                '<html lang="%(code)s">' % {
                    "code": lang.code
                },
            )
        )
Ejemplo n.º 3
0
 def enable_i18n_debug(self):
     """
     enable DEBUG, PYLUCID.I18N_DEBUG and set message_level_anonymous to DEBUG.
     """
     cache.clear()
     from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
     self._system_preferences = SystemPreferencesForm()
     self.old_message_level = self._system_preferences["message_level_anonymous"]
     self._system_preferences["message_level_anonymous"] = message_constants.DEBUG
     self._system_preferences.save()
     settings.DEBUG = True
     settings.PYLUCID.I18N_DEBUG = True
Ejemplo n.º 4
0
    def setUp(self):
        cache.clear()

        from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
        self.system_pref_form = SystemPreferencesForm()
        self.system_preferences = self.system_pref_form.get_preferences()
        self.system_preferences["message_level_anonymous"] = message_constants.DEBUG
        self.system_pref_form.save()

        DEBUG_LANG_FILTER = True
        settings.PYLUCID.I18N_DEBUG = True

        self.pref_form = BlogPrefForm()
Ejemplo n.º 5
0
    def __init__(self, request):
        self.request = request

        self.path_info = PathInfo(request)

        self.preferences_form = SystemPreferencesForm()
        self.preferences = self.preferences_form.get_preferences()

        # FIXME: import here, against import loop:
        from pylucid_project.apps.pylucid.models import Language

        self.languages = Language.objects.get_languages(request)
        self.default_language = Language.objects.get_or_create_default(request)
        try:
            self.current_language = self.languages[0]
        except IndexError, err:
            messages.info(request,
                (
                    "There exist no language on this site!"
                    " Used default one."
                    " Go into 'django admin/PyLucid/Languages' and"
                    " add at least one language to this site!"
                    " (Original error was: %s)"
                ) % err
            )
            self.languages = [self.default_language]
            self.current_language = self.default_language
Ejemplo n.º 6
0
class BlogPluginAnonymousTestCase(BlogPluginTestCase):
    def setUp(self):
        cache.clear()

        from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
        self.system_pref_form = SystemPreferencesForm()
        self.system_preferences = self.system_pref_form.get_preferences()
        self.system_preferences["message_level_anonymous"] = message_constants.DEBUG
        self.system_pref_form.save()

        DEBUG_LANG_FILTER = True
        settings.PYLUCID.I18N_DEBUG = True

        self.pref_form = BlogPrefForm()

    def _set_language_filter(self, language_filter):
        self.pref_form["language_filter"] = language_filter
        self.pref_form.save()
Ejemplo n.º 7
0
class BlogPluginAnonymousTestCase(BlogPluginTestCase):
    def setUp(self):
        cache.clear()

        from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
        self.system_pref_form = SystemPreferencesForm()
        self.system_preferences = self.system_pref_form.get_preferences()
        self.system_preferences["message_level_anonymous"] = message_constants.DEBUG
        self.system_pref_form.save()

        DEBUG_LANG_FILTER = True
        settings.PYLUCID.I18N_DEBUG = True

        self.pref_form = BlogPrefForm()

    def _set_language_filter(self, language_filter):
        self.pref_form["language_filter"] = language_filter
        self.pref_form.save()
Ejemplo n.º 8
0
    def log_action(self,
                   app_label,
                   action,
                   request=None,
                   message=None,
                   long_message=None,
                   data=None):
        if request is None:
            request = ThreadLocal.get_current_request()

        kwargs = {
            "uri": request.build_absolute_uri(),
            "app_label": app_label,
            "action": action,
            "message": message,
            "long_message": long_message,
            "data": data,
        }

        if hasattr(request, "PYLUCID"):
            kwargs["used_language"] = request.PYLUCID.current_language
            preferences = request.PYLUCID.preferences  # Get SystemPreferences
        else:
            from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm  # import loops
            preferences_form = SystemPreferencesForm()
            preferences = preferences_form.get_preferences()

        for key in META_KEYS:
            value = request.META.get(key)
            if value and len(value) > 255:
                value = "%s..." % value[:252]
            kwargs[key.lower()] = value

        new_entry = self.model(**kwargs)
        new_entry.save()

        # Auto cleanup Log Table to protect against overloading.
        max_count = preferences.get("max_log_entries", 1000)
        queryset = LogEntry.objects.order_by('-createtime')
        ids = tuple(queryset[max_count:].values_list('id', flat=True))
        if ids:
            queryset.filter(id__in=ids).delete()

        return new_entry
Ejemplo n.º 9
0
 def enable_i18n_debug(self):
     """
     enable DEBUG, PYLUCID.I18N_DEBUG and set message_level_anonymous to DEBUG.
     """
     cache.clear()
     from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
     self._system_preferences = SystemPreferencesForm()
     self.old_message_level = self._system_preferences["message_level_anonymous"]
     self._system_preferences["message_level_anonymous"] = message_constants.DEBUG
     self._system_preferences.save()
     settings.DEBUG = True
     settings.PYLUCID.I18N_DEBUG = True
Ejemplo n.º 10
0
    def test_non_caching_pages_with_messages(self):
        system_preferences = SystemPreferencesForm()
        system_preferences["message_level_anonymous"] = message_constants.DEBUG
        system_preferences.save()

        url = self.url + "test_messages/"
        client = Client()

        # Put page into cache?
        client.cookies["test_messages"] = "one"
        response = client.get(url)
        self.assertResponse(response,
            must_contain=(
                "Response from unittest_plugin.test_messages() [one]",
                "A &#39;debug&#39; message",
                "A &#39;info&#39; message",
                "A &#39;success&#39; message",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",
            ),
            must_not_contain=("Traceback",)
        )
        self.assertFalse(response._from_cache)

        # Request from cache?
        client.cookies["test_messages"] = "two"
        response = client.get(url)
        self.assertResponse(response,
            must_contain=(
                "Response from unittest_plugin.test_messages() [two]",
                "A &#39;debug&#39; message",
                "A &#39;info&#39; message",
                "A &#39;success&#39; message",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",

            ),
            must_not_contain=("Traceback",)
        )
        self.assertFalse(response._from_cache)
Ejemplo n.º 11
0
    def test_non_caching_pages_with_messages(self):
        system_preferences = SystemPreferencesForm()
        system_preferences["message_level_anonymous"] = message_constants.DEBUG
        system_preferences.save()

        url = self.url + "test_messages/"
        client = Client()

        # Put page into cache?
        client.cookies["test_messages"] = "one"
        response = client.get(url)
        self.assertResponse(response,
            must_contain=(
                "Response from unittest_plugin.test_messages() [one]",
                "A &#39;debug&#39; message",
                "A &#39;info&#39; message",
                "A &#39;success&#39; message",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",
            ),
            must_not_contain=("Traceback",)
        )
        self.assertFalse(response._from_cache)

        # Request from cache?
        client.cookies["test_messages"] = "two"
        response = client.get(url)
        self.assertResponse(response,
            must_contain=(
                "Response from unittest_plugin.test_messages() [two]",
                "A &#39;debug&#39; message",
                "A &#39;info&#39; message",
                "A &#39;success&#39; message",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",

            ),
            must_not_contain=("Traceback",)
        )
        self.assertFalse(response._from_cache)
Ejemplo n.º 12
0
    def setUp(self):
        cache.clear()

        from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm
        self.system_pref_form = SystemPreferencesForm()
        self.system_preferences = self.system_pref_form.get_preferences()
        self.system_preferences["message_level_anonymous"] = message_constants.DEBUG
        self.system_pref_form.save()

        DEBUG_LANG_FILTER = True
        settings.PYLUCID.I18N_DEBUG = True

        self.pref_form = BlogPrefForm()
Ejemplo n.º 13
0
class PyLucidRequestObjects(object):
    """ PyLucid request objects """
    _check_setattr = False
    def __init__(self, request):
        self.request = request

        self.path_info = PathInfo(request)

        self.preferences_form = SystemPreferencesForm()
        self.preferences = self.preferences_form.get_preferences()

        # FIXME: import here, against import loop:
        from pylucid_project.apps.pylucid.models import Language

        self.languages = Language.objects.get_languages(request)
        self.default_language = Language.objects.get_or_create_default(request)
        try:
            self.current_language = self.languages[0]
        except IndexError, err:
            messages.info(request,
                (
                    "There exist no language on this site!"
                    " Used default one."
                    " Go into 'django admin/PyLucid/Languages' and"
                    " add at least one language to this site!"
                    " (Original error was: %s)"
                ) % err
            )
            self.languages = [self.default_language]
            self.current_language = self.default_language


        # Storing extra html head code from plugins, used in:
        # pylucid.defaulttags.extraheadBlock - redirect {% extrahead %} block tag content
        # pylucid_plugin.extrahead.context_middleware - insert the data into the global page
        self.extrahead = extrahead.ExtraHead()

        # objects witch will be set later:
        # self.object2comment - Object to comment
        # self.pagetree - The current PageTree model instance
        # self.pagemeta - The current PageMeta model instance
        #
        # if current page == PageTree.PAGE_TYPE: # a normal content page
        #     self.pagecontent - PageContent instance, attached at pylucid.views._render_page()
        # elif  current page == PageTree.PLUGIN_TYPE: # a plugin page
        #     self.pluginpage - PluginPage instance, attached at pylucid.system.pylucid_plugin.call_plugin()
        #
        # self.page_template - The global page template as a string
        # self.context - The global context

        self._check_setattr = settings.DEBUG
Ejemplo n.º 14
0
    def log_action(self, app_label, action, request=None, message=None, long_message=None, data=None):
        if request is None:
            request = ThreadLocal.get_current_request()

        kwargs = {
            "uri": request.build_absolute_uri(),
            "app_label": app_label,
            "action": action,
            "message": message,
            "long_message": long_message,
            "data": data,
        }

        if hasattr(request, "PYLUCID"):
            kwargs["used_language"] = request.PYLUCID.current_language
            preferences = request.PYLUCID.preferences # Get SystemPreferences
        else:
            from pylucid_project.apps.pylucid.preference_forms import SystemPreferencesForm # import loops
            preferences_form = SystemPreferencesForm()
            preferences = preferences_form.get_preferences()

        for key in META_KEYS:
            value = request.META.get(key)
            if value and len(value) > 255:
                value = "%s..." % value[:252]
            kwargs[key.lower()] = value

        new_entry = self.model(**kwargs)
        new_entry.save()

        # Auto cleanup Log Table to protect against overloading.                 
        max_count = preferences.get("max_log_entries", 1000)
        queryset = LogEntry.objects.order_by('-createtime')
        ids = tuple(queryset[max_count:].values_list('id', flat=True))
        if ids:
            queryset.filter(id__in=ids).delete()

        return new_entry
Ejemplo n.º 15
0
    def __init__(self, request):
        self.request = request

        self.path_info = PathInfo(request)

        self.preferences_form = SystemPreferencesForm()
        self.preferences = self.preferences_form.get_preferences()

        # FIXME: import here, against import loop:
        from pylucid_project.apps.pylucid.models import Language

        self.languages = Language.objects.get_languages(request)
        self.default_language = Language.objects.get_or_create_default(request)
        try:
            self.current_language = self.languages[0]
        except IndexError, err:
            messages.info(request,
                          ("There exist no language on this site!"
                           " Used default one."
                           " Go into 'django admin/PyLucid/Languages' and"
                           " add at least one language to this site!"
                           " (Original error was: %s)") % err)
            self.languages = [self.default_language]
            self.current_language = self.default_language
Ejemplo n.º 16
0
class PyLucidRequestObjects(object):
    """ PyLucid request objects """
    _check_setattr = False

    def __init__(self, request):
        self.request = request

        self.path_info = PathInfo(request)

        self.preferences_form = SystemPreferencesForm()
        self.preferences = self.preferences_form.get_preferences()

        # FIXME: import here, against import loop:
        from pylucid_project.apps.pylucid.models import Language

        self.languages = Language.objects.get_languages(request)
        self.default_language = Language.objects.get_or_create_default(request)
        try:
            self.current_language = self.languages[0]
        except IndexError, err:
            messages.info(request,
                          ("There exist no language on this site!"
                           " Used default one."
                           " Go into 'django admin/PyLucid/Languages' and"
                           " add at least one language to this site!"
                           " (Original error was: %s)") % err)
            self.languages = [self.default_language]
            self.current_language = self.default_language

        # Storing extra html head code from plugins, used in:
        # pylucid.defaulttags.extraheadBlock - redirect {% extrahead %} block tag content
        # pylucid_plugin.extrahead.context_middleware - insert the data into the global page
        self.extrahead = extrahead.ExtraHead()

        # objects witch will be set later:
        # self.object2comment - Object to comment
        # self.pagetree - The current PageTree model instance
        # self.pagemeta - The current PageMeta model instance
        #
        # if current page == PageTree.PAGE_TYPE: # a normal content page
        #     self.pagecontent - PageContent instance, attached at pylucid.views._render_page()
        # elif  current page == PageTree.PLUGIN_TYPE: # a plugin page
        #     self.pluginpage - PluginPage instance, attached at pylucid.system.pylucid_plugin.call_plugin()
        #
        # self.page_template - The global page template as a string
        # self.context - The global context

        self._check_setattr = settings.DEBUG
Ejemplo n.º 17
0
    def test_MessageLevelMiddleware(self):
        url = self.url + "test_messages/"
        client = Client()
        system_preferences = SystemPreferencesForm()

        def get_response(level):
            system_preferences["message_level_anonymous"] = level
            system_preferences.save()
            return client.get(url)

        self.assertResponse(get_response(message_constants.DEBUG),
            must_contain=(
                "Response from unittest_plugin.test_messages()",
                "A &#39;debug&#39; message",
                "A &#39;info&#39; message",
                "A &#39;success&#39; message",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",
            ),
            must_not_contain=("Traceback",)
        )

        self.assertResponse(get_response(message_constants.INFO),
            must_contain=(
                "Response from unittest_plugin.test_messages()",
                "A &#39;info&#39; message",
                "A &#39;success&#39; message",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",
            ),
            must_not_contain=("Traceback",
                "A &#39;debug&#39; message",
            )
        )

        self.assertResponse(get_response(message_constants.SUCCESS),
            must_contain=(
                "Response from unittest_plugin.test_messages()",
                "A &#39;success&#39; message",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",
            ),
            must_not_contain=("Traceback",
                "A &#39;info&#39; message",
                "A &#39;debug&#39; message",
            )
        )

        self.assertResponse(get_response(message_constants.WARNING),
            must_contain=(
                "Response from unittest_plugin.test_messages()",
                "A &#39;warning&#39; message",
                "A &#39;error&#39; message",
            ),
            must_not_contain=("Traceback",
                "A &#39;success&#39; message",
                "A &#39;info&#39; message",
                "A &#39;debug&#39; message",
            )
        )

        self.assertResponse(get_response(message_constants.ERROR),
            must_contain=(
                "Response from unittest_plugin.test_messages()",
                "A &#39;error&#39; message",
            ),
            must_not_contain=("Traceback",
                "A &#39;warning&#39; message",
                "A &#39;success&#39; message",
                "A &#39;info&#39; message",
                "A &#39;debug&#39; message",
            )
        )