Products.CMFPlone/plip-1486-redirection: Add form to add redirect in config form

Stephan Klinger <jenkins-z4DKO/[email protected]>
Newsgroups gmane.comp.web.zope.plone.cvs
Message-ID <[email protected]>
Repository: Products.CMFPlone
Branch: refs/heads/plip-1486-redirection
Date: 2017-07-10T09:31:40+03:00
Author: Stephan Klinger (staeff) <[email protected]>
Commit: https://github.com/plone/Products.CMFPlone/commit/350a7b0d52c5c6a20f4c776c80963285ff155dae

Add form to add redirect in config form

Files changed:
M Products/CMFPlone/controlpanel/browser/redirects-controlpanel.pt
M Products/CMFPlone/controlpanel/browser/redirects.py

diff --git a/Products/CMFPlone/controlpanel/browser/redirects-controlpanel.pt b/Products/CMFPlone/controlpanel/browser/redirects-controlpanel.pt
index 6e85880f6..def3b8f31 100644
--- a/Products/CMFPlone/controlpanel/browser/redirects-controlpanel.pt
+++ b/Products/CMFPlone/controlpanel/browser/redirects-controlpanel.pt
@@ -146,7 +146,37 @@
       </form>
 
       <form
-          tal:attributes="action view/view_url"
+          action="${view/view_url}"
+          method="post">
+        <fieldset>
+          <legend i18n:translate="add_alias">
+            Add a new alias
+          </legend>
+
+          <label for="redirection">Source Path</label>
+          <input
+              type="text"
+              name="redirection">
+
+          <label for="target_path">Target Path</label>
+          <input
+              type="text"
+              name="target_path">
+
+          <div class="formControls">
+            <input class="context"
+                   type="submit"
+                   value="Add"
+                   name="form.button.Add"
+                   i18n:attributes="value" />
+          </div>
+
+        </fieldset>
+      </form>
+
+
+      <form
+          action="${view/view_url}"
           method="post"
           enctype="multipart/form-data">
         <fieldset>
diff --git a/Products/CMFPlone/controlpanel/browser/redirects.py b/Products/CMFPlone/controlpanel/browser/redirects.py
index 80e6cc795..bce124dbc 100644
--- a/Products/CMFPlone/controlpanel/browser/redirects.py
+++ b/Products/CMFPlone/controlpanel/browser/redirects.py
@@ -59,7 +59,9 @@ def absolutize_path(path, context=None, is_alias=True):
             else:
                 context_path = "/".join(context.getPhysicalPath()[:-1])
                 path = "%s/%s" % (context_path, path)
-        if not err and is_alias:  # noqa XXX should we require Modify Alias permission on the target as well?
+        # Check whether obj exists and
+        # noqa XXX should we require Modify Alias permission on the target as well?
+        if not err and is_alias:
             source = path.split('/')
             while len(source):
                 obj = portal.unrestrictedTraverse(source, None)
@@ -218,6 +220,8 @@ def __call__(self):
         elif 'form.button.Save' in form:
             dst = IAliasesSchema(self.context)
             dst.managed_types = self.request.form['form.widgets.managed_types']
+        elif 'form.button.Add' in form:
+            self.add(form['redirection'], form['target_path'], portal, storage, status)
         elif 'form.button.Upload' in form:
             self.upload(form['file'], portal, storage, status)
 
@@ -225,6 +229,27 @@ def __call__(self):
         self.form.update()
         return self.template()
 
+    def add(self, redirection, target, portal, storage, status):
+        """Add the redirections from the form. If anything goes wrong, do nothing."""
+
+        abs_redirection, err = absolutize_path(redirection, is_alias=True)
+        abs_target, target_err = absolutize_path(target, is_alias=False)
+        
+        if err and target_err:
+            err = "{0} {1}".format(err, target_err)
+        elif target_err:
+            err = target_err
+        else:
+            if abs_redirection == abs_target:
+                err = _(u"Aliases that point to themselves will cause"
+                        u"an endless cycle of redirects.")
+                # TODO: detect indirect recursion
+
+        if not err:
+            storage.add(abs_redirection, abs_target)
+            status.addStatusMessage(_(u"Alias {0} &rarr; {1} added.").format(abs_redirection, abs_target),
+                                    type='info')
+
     def upload(self, file, portal, storage, status):
         """Add the redirections from the CSV file `file`. If anything goes wrong, do nothing."""
         # Turn all kinds of newlines into LF ones. The csv module doesn't do



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.