def test_import(self, browser): file_data = open( "%s/assets/redirector_config.xlsx" % os.path.split(__file__)[0], 'r') file_ = StringIO(file_data.read()) file_.filename = 'redirector_config.xlsx' file_.content_type = 'application/vnd.ms-excel' self.grant('Manager') browser.login().open(IRedirectConfig(self.portal), view='import') browser.fill({'Excel redirect config': file_}).submit() self.assertEquals([{ 'destination': u'/Plone', 'source_path': u'/theploneasdf' }, { 'destination': u'http://www.google.ch/', 'source_path': u'/google' }, { 'destination': u'/ziel', 'source_path': u'/test' }, { 'destination': u'/blub', 'source_path': u'/bla' }, { 'destination': u'/gnarg', 'source_path': u'/gna' }, { 'destination': u'/same', 'source_path': u'/same' }], IRedirectConfig(self.portal).rules)
def test_redirecting_with_umlauts(self, browser): self.grant("Manager") config = IRedirectConfig(self.portal) config.rules = make_rules((u"/hall\xf6chen", u"/target")) create(Builder("page").titled("target")) browser.replace_request_header("X-zope-handle-errors", "True") browser.open("http://nohost/plone/hall\xc3\xb6chen") self.assertEqual("http://nohost/plone/target", browser.url)
def test_redirected_when_having_matching_rule(self, browser): self.grant("Manager") config = IRedirectConfig(self.portal) config.rules = make_rules(("/foo", "/target")) create(Builder("page").titled("target")) browser.replace_request_header("X-zope-handle-errors", "True") browser.open("http://nohost/plone/foo") self.assertEqual("http://nohost/plone/target", browser.url)
def test_redirecting_with_umlauts(self, browser): self.grant('Manager') config = IRedirectConfig(self.portal) config.rules = make_rules((u'/hall\xf6chen', u'/target')) create(Builder('page').titled(u'target')) # Set accept header (needed since Plone 5). browser.append_request_header('Accept', 'text/html') browser.open('http://nohost/plone/hall\xc3\xb6chen') self.assertEqual('http://nohost/plone/target', browser.url)
def test_redirected_when_having_matching_rule(self, browser): self.grant('Manager') config = IRedirectConfig(self.portal) config.rules = make_rules(('/foo', '/target')) create(Builder('page').titled(u'target')) # Set accept header (needed since Plone 5). browser.append_request_header('Accept', 'text/html') browser.open('http://nohost/plone/foo') self.assertEqual('http://nohost/plone/target', browser.url)
def handleApply(self, action): data, errors = self.extractData() if errors: # since we have only one field we can copy the error message # to make it more visible self.status = errors[0].message return excel_file = StringIO(data['rules_file'].data) rules = load_rules_from_excel(excel_file) rconfig = IRedirectConfig(self.context) rconfig.rules = rules messages = IStatusMessage(self.request) messages.add(_("The redirect config has been replaced."), type=u'info') self.request.RESPONSE.redirect(self.context.absolute_url())
def create_rules_excel(): portal = api.portal.get() request = portal.REQUEST rules = IRedirectConfig(portal).rules if not rules: rules = [] book = Workbook() sheet = book.active title = translate(_(u'Redirect Configuration'), context=request) source_title = translate(_(u'label_source_path', default=u'Source Path'), context=request) destination_title = translate(_(u'label_destination', default=u'Destination'), context=request) # HEADER sheet.title = title bold = Font(bold=True) cell = sheet.cell(column=1, row=1) cell.font = bold cell.value = source_title cell = sheet.cell(column=2, row=1) cell.font = bold cell.value = destination_title # DATA for rule_nr, rule in enumerate(rules): cell = sheet.cell(column=1, row=RULES_START_ROW + rule_nr) cell.value = rule['source_path'] cell = sheet.cell(column=2, row=RULES_START_ROW + rule_nr) cell.value = rule['destination'] # match the width to the longest entry for column in sheet.columns: maxwidth = 0 for cell in column: if not cell.value: continue cwidth = len(cell.value) maxwidth = cwidth if cwidth > maxwidth else maxwidth # a bit more space for readability sheet.column_dimensions[cell.column].width = maxwidth + 5 return save_virtual_workbook(book)
def try_redirect(self): path = self.get_path() if not path: return None config = IRedirectConfig(self.context, None) if not config: return None strategy = getMultiAdapter((config, self.request), IRedirectStrategy) target = strategy.find_redirect(path.decode('utf-8')) if not target: return None if '://' not in target: target = api.portal.get().absolute_url() + target return self.request.response.redirect(target, status=301, lock=1)
def test_export(self, browser): self.grant('Manager') browser.login().open(IRedirectConfig(self.portal)) browser.find(u'Edit').click() browser.fill({ u'Redirect rules': [{ u'Source Path': u'/foo', u'Destination': u'/bar' }, { u'Source Path': u'/something/one', u'Destination': u'/something/two' }] }).save() excel_file = StringIO(create_rules_excel()) excel = load_workbook(excel_file) sheet = excel.active self.assertEquals('/foo', sheet['A2'].value) self.assertEquals('/something/one', sheet['A3'].value) self.assertEquals('/bar', sheet['B2'].value) self.assertEquals('/something/two', sheet['B3'].value)
def test_excel_views_are_available(self, browser): self.grant('Manager') browser.login() browser.open(IRedirectConfig(self.portal), view='import') browser.open(IRedirectConfig(self.portal), view='export')
def test_excel_actions_are_available(self, browser): self.grant('Manager') browser.login().open(IRedirectConfig(self.portal)) self.assertIn("Excel export", browser.contents) self.assertIn("Excel import", browser.contents)
def lookup_config(context): return IRedirectConfig(aq_parent(aq_inner(context)))
def test_get_config_by_adapting_any_context_in_site_root(self): self.grant('Manager') obj = create(Builder('folder').within(create(Builder('folder')))) self.assertEquals(IRedirectConfig(self.portal), IRedirectConfig(obj)) self.assertTrue(IRedirectConfig.providedBy(IRedirectConfig(obj)))
def test_get_config_by_adapting_portal(self): config = IRedirectConfig(self.portal) self.assertTrue(IRedirectConfig.providedBy(config))