Ejemplo n.º 1
0
    def test_admin_change_form_language_tabs_urls(self):
        with LanguageOverride('en'):
            with self.login_user_context(username='******', password='******'):
                get_url = reverse('admin:app_normal_change',
                                  args=(self.normal_id[1], ))
                test_urls = [
                    '%s?%s' % (get_url, '_changelist_filters=q%3Dsearchparam'),
                    '%s?%s' %
                    (get_url,
                     '_changelist_filters=q%3Dsearchparam&language=fr'),
                ]

                for test_url in test_urls:
                    response = self.client.get(test_url)
                    self.assertEqual(response.status_code, 200)
                    tabs = response.context['language_tabs']

                    expected_querydict = QueryDict(urlparse(test_url).query,
                                                   mutable=True)

                    for actual_tab_url, name, key, status in tabs:
                        expected_querydict['language'] = key
                        actual_querydict = QueryDict(
                            urlparse(actual_tab_url).query)
                        self.assertEqual(expected_querydict, actual_querydict)
Ejemplo n.º 2
0
    def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
        "Construct a POST request."

        if content_type is MULTIPART_CONTENT:
            post_data = encode_multipart(BOUNDARY, data)
        else:
            # Encode the content so that the byte representation is correct.
            match = CONTENT_TYPE_RE.match(content_type)
            if match:
                charset = match.group(1)
            else:
                charset = settings.DEFAULT_CHARSET
            post_data = smart_str(data, encoding=charset)

        parsed = urlparse(path)
        r = {
            'CONTENT_LENGTH': len(post_data),
            'CONTENT_TYPE': content_type,
            'PATH_INFO': self._get_path(parsed),
            'QUERY_STRING': parsed[4],
            'REQUEST_METHOD': 'POST',
            'wsgi.input': FakePayload(post_data),
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 3
0
    def put(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
        "Construct a PUT request."

        if content_type is MULTIPART_CONTENT:
            post_data = encode_multipart(BOUNDARY, data)
        else:
            post_data = data

        # Make `data` into a querystring only if it's not already a string. If
        # it is a string, we'll assume that the caller has already encoded it.
        query_string = None
        if not isinstance(data, string_types):
            query_string = urlencode(data, doseq=True)

        parsed = urlparse(path)
        r = {
            'CONTENT_LENGTH': len(post_data),
            'CONTENT_TYPE': content_type,
            'PATH_INFO': self._get_path(parsed),
            'QUERY_STRING': query_string or parsed[4],
            'REQUEST_METHOD': 'PUT',
            'wsgi.input': FakePayload(post_data),
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 4
0
    def post(self, path, data={}, content_type=MULTIPART_CONTENT,
             **extra):
        "Construct a POST request."

        if content_type is MULTIPART_CONTENT:
            post_data = encode_multipart(BOUNDARY, data)
        else:
            # Encode the content so that the byte representation is correct.
            match = CONTENT_TYPE_RE.match(content_type)
            if match:
                charset = match.group(1)
            else:
                charset = settings.DEFAULT_CHARSET
            post_data = smart_str(data, encoding=charset)

        parsed = urlparse(path)
        r = {
            'CONTENT_LENGTH': len(post_data),
            'CONTENT_TYPE':   content_type,
            'PATH_INFO':      self._get_path(parsed),
            'QUERY_STRING':   parsed[4],
            'REQUEST_METHOD': 'POST',
            'wsgi.input':     FakePayload(post_data),
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 5
0
    def put(self, path, data={}, content_type=MULTIPART_CONTENT,
            **extra):
        "Construct a PUT request."

        if content_type is MULTIPART_CONTENT:
            post_data = encode_multipart(BOUNDARY, data)
        else:
            post_data = data

        # Make `data` into a querystring only if it's not already a string. If
        # it is a string, we'll assume that the caller has already encoded it.
        query_string = None
        if not isinstance(data, string_types):
            query_string = urlencode(data, doseq=True)

        parsed = urlparse(path)
        r = {
            'CONTENT_LENGTH': len(post_data),
            'CONTENT_TYPE':   content_type,
            'PATH_INFO':      self._get_path(parsed),
            'QUERY_STRING':   query_string or parsed[4],
            'REQUEST_METHOD': 'PUT',
            'wsgi.input':     FakePayload(post_data),
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 6
0
    def render_change_form(self,
                           request,
                           context,
                           add=False,
                           change=False,
                           form_url='',
                           obj=None):
        lang_code = self._language(request)
        lang = get_language_name(lang_code)
        available_languages = self.get_available_languages(obj)
        context['title'] = '%s (%s)' % (context['title'], lang)
        context['current_is_translated'] = lang_code in available_languages
        context['allow_deletion'] = len(available_languages) > 1
        context['language_tabs'] = self.get_language_tabs(
            request, available_languages)
        context['base_template'] = self.get_change_form_base_template()

        # Ensure form action url carries over tab language
        qs_language = request.GET.get('language')
        if qs_language:
            form_url = urlparse(form_url or request.get_full_path())
            query = QueryDict(form_url.query, mutable=True)
            if 'language' not in query:
                query['language'] = qs_language
            form_url = form_url._replace(query=query.urlencode()).geturl()

        return super(TranslatableAdmin,
                     self).render_change_form(request, context, add, change,
                                              form_url, obj)
Ejemplo n.º 7
0
    def test_admin_change_form_language_tabs_urls(self):
        with LanguageOverride('en'):
            with self.login_user_context(username='******', password='******'):
                get_url = reverse('admin:app_normal_change', args=(self.normal_id[1],))
                test_urls = [
                    '%s?%s' % (get_url, '_changelist_filters=q%3Dsearchparam'),
                    '%s?%s' % (get_url, '_changelist_filters=q%3Dsearchparam&language=fr'),
                ]

                for test_url in test_urls:
                    response = self.client.get(test_url)
                    self.assertEqual(response.status_code, 200)
                    tabs = response.context['language_tabs']

                    expected_querydict = QueryDict(urlparse(test_url).query, mutable=True)

                    for actual_tab_url, name, key, status in tabs:
                        expected_querydict['language'] = key
                        actual_querydict = QueryDict(urlparse(actual_tab_url).query)
                        self.assertEqual(expected_querydict, actual_querydict)
Ejemplo n.º 8
0
    def delete(self, path, data={}, **extra):
        "Construct a DELETE request."

        parsed = urlparse(path)
        r = {
            'PATH_INFO': self._get_path(parsed),
            'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
            'REQUEST_METHOD': 'DELETE',
            'wsgi.input': FakePayload('')
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 9
0
    def options(self, path, data={}, **extra):
        "Constrict an OPTIONS request"

        parsed = urlparse(path)
        r = {
            'PATH_INFO': self._get_path(parsed),
            'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
            'REQUEST_METHOD': 'OPTIONS',
            'wsgi.input': FakePayload('')
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 10
0
    def delete(self, path, data={}, **extra):
        "Construct a DELETE request."

        parsed = urlparse(path)
        r = {
            'PATH_INFO':       self._get_path(parsed),
            'QUERY_STRING':    urlencode(data, doseq=True) or parsed[4],
            'REQUEST_METHOD': 'DELETE',
            'wsgi.input':      FakePayload('')
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 11
0
    def options(self, path, data={}, **extra):
        "Constrict an OPTIONS request"

        parsed = urlparse(path)
        r = {
            'PATH_INFO':       self._get_path(parsed),
            'QUERY_STRING':    urlencode(data, doseq=True) or parsed[4],
            'REQUEST_METHOD': 'OPTIONS',
            'wsgi.input':      FakePayload('')
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 12
0
    def get(self, path, data={}, **extra):
        "Construct a GET request"

        parsed = urlparse(path)
        r = {
            'CONTENT_TYPE': 'text/html; charset=utf-8',
            'PATH_INFO': self._get_path(parsed),
            'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
            'REQUEST_METHOD': 'GET',
            'wsgi.input': FakePayload('')
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 13
0
    def get(self, path, data={}, **extra):
        "Construct a GET request"

        parsed = urlparse(path)
        r = {
            'CONTENT_TYPE':    'text/html; charset=utf-8',
            'PATH_INFO':       self._get_path(parsed),
            'QUERY_STRING':    urlencode(data, doseq=True) or parsed[4],
            'REQUEST_METHOD': 'GET',
            'wsgi.input':      FakePayload('')
        }
        r.update(extra)
        return self.request(**r)
Ejemplo n.º 14
0
 def test_admin_change_form_action_url(self):
     with LanguageOverride('en'):
         with self.login_user_context(username='******', password='******'):
             url = reverse('admin:app_normal_change', args=(self.normal_id[1],))
             tests = (
                 '',
                 'language=fr',
                 '_changelist_filters=q%3Dparam&language=fr',
             )
             for query_string in tests:
                 expected_dict = QueryDict(query_string)
                 full_url = '%s?%s' % (url, query_string) if query_string else url
                 response = self.client.get(full_url)
                 form_url = urlparse(response.context['form_url'])
                 self.assertEqual(expected_dict, QueryDict(form_url.query),
                                  'query_string=%r' % query_string)
Ejemplo n.º 15
0
 def test_admin_change_form_action_url(self):
     with LanguageOverride('en'):
         with self.login_user_context(username='******', password='******'):
             url = reverse('admin:app_normal_change',
                           args=(self.normal_id[1], ))
             tests = (
                 '',
                 'language=fr',
                 '_changelist_filters=q%3Dparam&language=fr',
             )
             for query_string in tests:
                 expected_dict = QueryDict(query_string)
                 full_url = '%s?%s' % (
                     url, query_string) if query_string else url
                 response = self.client.get(full_url)
                 form_url = urlparse(response.context['form_url'])
                 self.assertEqual(expected_dict, QueryDict(form_url.query),
                                  'query_string=%r' % query_string)