def process_request(self, request): remaps = DocumentZoneURLRemapsJob().get(request.locale) for original_path, new_path in remaps: if request.path_info.startswith(original_path): # Is this a request for the "original" wiki path? Redirect to # new URL root, if so. new_path = request.path_info.replace(original_path, new_path, 1) new_path = '/%s%s' % (request.locale, new_path) query = request.GET.copy() if 'lang' in query: query.pop('lang') new_path = urlparams(new_path, query_dict=query) return HttpResponseRedirect(new_path) elif request.path_info.startswith(new_path): # Is this a request for the relocated wiki path? If so, rewrite # the path as a request for the proper wiki view. request.path_info = request.path_info.replace(new_path, original_path, 1) break
def test_intermediate(self): """ Test that the intermediate DocumentAttachment gets created correctly when adding an Attachment with a document_id. """ doc = document(locale='en', slug='attachment-test-intermediate') doc.save() rev = revision(document=doc, is_approved=True) rev.save() file_for_upload = make_test_file( content='A file for testing intermediate attachment model.') post_data = { 'title': 'Intermediate test file', 'description': 'Intermediate test file', 'comment': 'Initial upload', 'file': file_for_upload, } add_url = urlparams(reverse('attachments.new_attachment'), document_id=doc.id) resp = self.client.post(add_url, data=post_data) eq_(302, resp.status_code) eq_(1, doc.files.count()) intermediates = DocumentAttachment.objects.filter(document__pk=doc.id) eq_(1, intermediates.count()) intermediate = intermediates[0] eq_('admin', intermediate.attached_by.username) eq_(file_for_upload.name.split('/')[-1], intermediate.name)
def test_persona_signup_copy(self): """ After a new user signs up with Persona, their username, an indication that Persona was used to log in, and a logout link appear in the auth tools section of the page. """ persona_signup_email = '*****@*****.**' persona_signup_username = '******' with mock.patch('requests.post') as requests_mock: requests_mock.return_value.json.return_value = { 'status': 'okay', 'email': persona_signup_email, } r = self.client.post(reverse('persona_login'), follow=True) data = {'website': '', 'username': persona_signup_username, 'email': persona_signup_email, 'terms': True} r = self.client.post( reverse('socialaccount_signup', locale=settings.WIKI_DEFAULT_LANGUAGE), data=data, follow=True) profile_url = reverse( 'users.profile', kwargs={'username': persona_signup_username}, locale=settings.WIKI_DEFAULT_LANGUAGE) signout_url = urlparams( reverse('account_logout', locale=settings.WIKI_DEFAULT_LANGUAGE), next=reverse('home', locale=settings.WIKI_DEFAULT_LANGUAGE)) parsed = pq(r.content) login_info = parsed.find('.header-login .user-state') ok_(len(login_info.children())) signed_in_message = login_info.children()[0] ok_('title' in signed_in_message.attrib) eq_('Signed in with Persona', signed_in_message.attrib['title']) auth_links = login_info.children()[1].getchildren() ok_(len(auth_links)) profile_link = auth_links[0].getchildren()[0] ok_('href' in profile_link.attrib) eq_(profile_url, profile_link.attrib['href']) signout_link = auth_links[1].getchildren()[0] ok_('href' in signout_link.attrib) eq_(signout_url.replace('%2F', '/'), # urlparams() encodes slashes signout_link.attrib['href'])
def test_files_dict(self): doc = document(locale='en', slug='attachment-test-files-dict') doc.save() rev = revision(document=doc, is_approved=True) rev.save() test_file_1 = make_test_file( content='A file for testing the files dict') post_data = { 'title': 'Files dict test file', 'description': 'Files dict test file', 'comment': 'Initial upload', 'file': test_file_1, } add_url = urlparams(reverse('attachments.new_attachment'), document_id=doc.id) self.client.post(add_url, data=post_data) test_file_2 = make_test_file( content='Another file for testing the files dict') post_data = { 'title': 'Files dict test file 2', 'description': 'Files dict test file 2', 'comment': 'Initial upload', 'file': test_file_2, } self.client.post(add_url, data=post_data) doc = Document.objects.get(pk=doc.id) files_dict = doc.files_dict() file1 = files_dict[test_file_1.name.split('/')[-1]] eq_('admin', file1['attached_by']) eq_('Files dict test file', file1['description']) eq_('text/plain', file1['mime_type']) ok_(test_file_1.name.split('/')[-1] in file1['url']) file2 = files_dict[test_file_2.name.split('/')[-1]] eq_('admin', file2['attached_by']) eq_('Files dict test file 2', file2['description']) eq_('text/plain', file2['mime_type']) ok_(test_file_2.name.split('/')[-1] in file2['url'])
def process(request, document_path=None, *args, **kwargs): if kwargs.get('bypass_process_document_path', False): # Support an option to bypass this decorator altogether, so one # view can directly call another view. del kwargs['bypass_process_document_path'] return func(request, document_path, *args, **kwargs) document_slug, document_locale = None, None if document_path: # Parse the document path into locale and slug. document_locale, document_slug, needs_redirect = ( locale_and_slug_from_path(document_path, request)) # Add check for "local" URL, remove trailing slash slug_length = len(document_slug) if slug_length and document_slug[slug_length - 1] == '/': needs_redirect = True document_slug = document_slug.rstrip('/') if not document_slug: # If there's no slug, then this is just a 404. raise Http404 if request.GET.get('raw', False) is not False: # HACK: There are and will be a lot of kumascript templates # based on legacy DekiScript which will attempt to request # old-style URLs. Skip 301 redirects for raw content. needs_redirect = False if needs_redirect: # This catches old MindTouch locales, missing locale, and a few # other cases to fire off a 301 Moved permanent redirect. url = reverse('wiki.document', locale=document_locale, args=[document_slug]) url = urlparams(url, query_dict=request.GET) return HttpResponsePermanentRedirect(url) # Set the kwargs that decorated methods will expect. kwargs['document_slug'] = document_slug kwargs['document_locale'] = document_locale return func(request, *args, **kwargs)
def process_request(self, request): remaps = DocumentZone.objects.get_url_remaps(request.locale) for remap in remaps: if request.path_info.startswith(remap['original_path']): # Is this a request for the "original" wiki path? Redirect to # new URL root, if so. new_path = request.path_info.replace(remap['original_path'], remap['new_path'], 1) new_path = '/%s%s' % (request.locale, new_path) query = request.GET.copy() if 'lang' in query: query.pop('lang') new_path = urlparams(new_path, query_dict=query) return HttpResponseRedirect(new_path) elif request.path_info.startswith(remap['new_path']): # Is this a request for the relocated wiki path? If so, rewrite # the path as a request for the proper wiki view. request.path_info = request.path_info.replace( remap['new_path'], remap['original_path'], 1) break