From 180ca4e7f4e230bd0db4166d0749cc6dbd89e7e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 26 Nov 2019 13:57:26 +0100
Subject: [PATCH 1/6] remove support for custom links (#37967)

---
 auquotidien/auquotidien.py      |   4 -
 auquotidien/modules/admin.py    |   7 +-
 auquotidien/modules/links.py    |  21 ----
 auquotidien/modules/links_ui.py | 194 --------------------------------
 auquotidien/modules/root.py     |  45 +-------
 5 files changed, 2 insertions(+), 269 deletions(-)
 delete mode 100644 auquotidien/modules/links.py
 delete mode 100644 auquotidien/modules/links_ui.py

diff --git a/auquotidien/auquotidien.py b/auquotidien/auquotidien.py
index e9a3fe3..551188e 100644
--- a/auquotidien/auquotidien.py
+++ b/auquotidien/auquotidien.py
@@ -6,7 +6,6 @@ from wcs.qommon.misc import get_cfg
 
 from modules import admin
 from modules import backoffice
-from modules import links_ui
 from modules import announces_ui
 from modules import categories_admin
 from modules import events_ui
@@ -31,9 +30,6 @@ rdb.items = []
 rdb.register_directory('announces', announces_ui.AnnouncesDirectory())
 rdb.register_menu_item('announces/', _('Announces'))
 
-rdb.register_directory('links', links_ui.LinksDirectory())
-rdb.register_menu_item('links/', _('Links'))
-
 rdb.register_directory('events', events_ui.EventsDirectory())
 rdb.register_menu_item('events/', _('Events'))
 
diff --git a/auquotidien/modules/admin.py b/auquotidien/modules/admin.py
index d027249..90202b7 100644
--- a/auquotidien/modules/admin.py
+++ b/auquotidien/modules/admin.py
@@ -81,10 +81,6 @@ class PanelDirectory(Directory):
             form.add(SingleSelectWidget, 'events', title = _('Admin role for events'),
                     value = permissions_cfg.get('events', None),
                     options = [(None, _('Nobody'), None)] + get_user_roles())
-        if get_publisher().has_site_option('auquotidien-links'):
-            form.add(SingleSelectWidget, 'links', title = _('Admin role for links'),
-                    value = permissions_cfg.get('links', None),
-                    options = [(None, _('Nobody'), None)] + get_user_roles())
         if get_publisher().has_site_option('auquotidien-announces'):
             form.add(SingleSelectWidget, 'announces', title = _('Admin role for announces'),
                     value = permissions_cfg.get('announces', None),
@@ -113,7 +109,7 @@ class PanelDirectory(Directory):
         else:
             from wcs.admin.settings import cfg_submit
             cfg_submit(form, 'aq-permissions',
-                        ('forms', 'events', 'links', 'announces', 'payments', 'strongbox'))
+                        ('forms', 'events', 'announces', 'payments', 'strongbox'))
             return redirect('..')
 
     def event_keywords(self):
@@ -254,7 +250,6 @@ class PanelDirectory(Directory):
 class SettingsDirectory(wcs.admin.settings.SettingsDirectory):
     def _q_index(self):
         if not (get_publisher().has_site_option('auquotidien-announces') or
-                get_publisher().has_site_option('auquotidien-links') or
                 get_publisher().has_site_option('auquotidien-events') or
                 get_publisher().has_site_option('auquotidien-payments') or
                 get_publisher().has_site_option('auquotidien-strongvox')):
diff --git a/auquotidien/modules/links.py b/auquotidien/modules/links.py
deleted file mode 100644
index df2f67a..0000000
--- a/auquotidien/modules/links.py
+++ /dev/null
@@ -1,21 +0,0 @@
-from wcs.qommon.storage import StorableObject
-
-class Link(StorableObject):
-    _names = 'links'
-
-    title = None
-    url = None
-    position = None
-
-    def sort_by_position(cls, links):
-        def cmp_position(x, y):
-            if x.position == y.position:
-                return 0
-            if x.position is None:
-                return 1
-            if y.position is None:
-                return -1
-            return cmp(x.position, y.position)
-        links.sort(cmp_position)
-    sort_by_position = classmethod(sort_by_position)
-
diff --git a/auquotidien/modules/links_ui.py b/auquotidien/modules/links_ui.py
deleted file mode 100644
index 2be4f9a..0000000
--- a/auquotidien/modules/links_ui.py
+++ /dev/null
@@ -1,194 +0,0 @@
-from quixote import get_request, get_response, get_session, redirect
-from quixote.directory import Directory, AccessControlled
-from quixote.html import TemplateIO, htmltext
-
-import wcs
-import wcs.admin.root
-
-from wcs.qommon import _
-from wcs.qommon import errors
-from wcs.qommon.form import *
-from wcs.qommon.backoffice.menu import html_top
-from wcs.qommon.admin.menu import command_icon
-from wcs.qommon import get_cfg
-
-from .links import Link
-
-
-class LinkDirectory(Directory):
-    _q_exports = ['', 'edit', 'delete']
-
-    def __init__(self, link):
-        self.link = link
-
-    def _q_index(self):
-        form = Form(enctype='multipart/form-data')
-        form.add_submit('edit', _('Edit'))
-        form.add_submit('delete', _('Delete'))
-        form.add_submit('back', _('Back'))
-
-        if form.get_submit() == 'edit':
-            return redirect('edit')
-        if form.get_submit() == 'delete':
-            return redirect('delete')
-        if form.get_submit() == 'back':
-            return redirect('..')
-
-        html_top('links', title = _('Link: %s') % self.link.title)
-        r = TemplateIO(html=True)
-        r += htmltext('<h2>%s</h2>') % _('Link: %s') % self.link.title
-        r += htmltext('<p>')
-        r += self.link.url
-        r += htmltext('</p>')
-
-        r += form.render()
-        return r.getvalue()
-
-    def edit(self):
-        form = self.form()
-        if form.get_submit() == 'cancel':
-            return redirect('.')
-
-        if form.is_submitted() and not form.has_errors():
-            self.submit(form)
-            return redirect('..')
-
-        html_top('links', title = _('Edit Link: %s') % self.link.title)
-        r = TemplateIO(html=True)
-        r += htmltext('<h2>%s</h2>') % _('Edit Link: %s') % self.link.title
-        r += form.render()
-        return r.getvalue()
-
-
-    def form(self):
-        form = Form(enctype='multipart/form-data')
-        form.add(StringWidget, 'title', title = _('Title'), required = True,
-                value = self.link.title)
-        form.add(StringWidget, 'url', title = _('URL'), required=False,
-                value = self.link.url,
-                hint=_('Leave empty to create a title'))
-        form.add_submit('submit', _('Submit'))
-        form.add_submit('cancel', _('Cancel'))
-        return form
-
-    def submit(self, form):
-        for k in ('title', 'url'):
-            widget = form.get_widget(k)
-            if widget:
-                setattr(self.link, k, widget.parse())
-        self.link.store()
-
-    def delete(self):
-        form = Form(enctype='multipart/form-data')
-        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
-                        'You are about to irrevocably delete this link.')))
-        form.add_submit('submit', _('Submit'))
-        form.add_submit('cancel', _('Cancel'))
-        if form.get_submit() == 'cancel':
-            return redirect('..')
-        if not form.is_submitted() or form.has_errors():
-            get_response().breadcrumb.append(('delete', _('Delete')))
-            html_top('links', title = _('Delete Link'))
-            r = TemplateIO(html=True)
-            r += htmltext('<h2>%s</h2>') % _('Deleting Link: %s') % self.link.title
-            r += form.render()
-            return r.getvalue()
-        else:
-            self.link.remove_self()
-            return redirect('..')
-
-
-class LinksDirectory(AccessControlled, Directory):
-    _q_exports = ['', 'new', 'listing', 'update_order']
-    label = N_('Links')
-
-    def is_accessible(self, user):
-        from .backoffice import check_visibility
-        return check_visibility('links', user)
-
-    def _q_access(self):
-        user = get_request().user
-        if not user:
-            raise errors.AccessUnauthorizedError()
-
-        if not self.is_accessible(user):
-            raise errors.AccessForbiddenError(
-                    public_msg = _('You are not allowed to access Links Management'),
-                    location_hint = 'backoffice')
-
-        get_response().breadcrumb.append(('links/', _('Links')))
-
-
-    def _q_index(self):
-        html_top('links', _('Links'))
-        r = TemplateIO(html=True)
-        get_response().add_javascript(['jquery.js', 'jquery-ui.js', 'biglist.js'])
-        get_response().filter['sidebar'] = self.get_sidebar()
-
-        links = Link.select()
-        Link.sort_by_position(links)
-
-        r += htmltext('<ul class="biglist sortable" id="links-list">')
-        for l in links:
-            link_id = l.id
-            r += htmltext('<li class="biglistitem" id="itemId_%s">') % link_id
-            r += htmltext('<strong class="label"><a href="%s/">%s</a></strong>') % (link_id, l.title)
-            r += htmltext('<p class="details">')
-            r += l.url
-            r += htmltext('</p>')
-            r += htmltext('<p class="commands">')
-            r += command_icon('%s/edit' % link_id, 'edit')
-            r += command_icon('%s/delete' % link_id, 'remove')
-            r += htmltext('</p></li>')
-        r += htmltext('</ul>')
-        return r.getvalue()
-
-    def get_sidebar(self):
-        r = TemplateIO(html=True)
-        r += htmltext('<ul id="sidebar-actions">')
-        r += htmltext('  <li><a class="new-item" href="new">%s</a></li>') % _('New Link')
-        r += htmltext('</ul>')
-        return r.getvalue()
-
-    def update_order(self):
-        request = get_request()
-        new_order = request.form['order'].strip(';').split(';')
-        links = Link.select()
-        dict = {}
-        for l in links:
-            dict[str(l.id)] = l
-        for i, o in enumerate(new_order):
-            dict[o].position = i + 1
-            dict[o].store()
-        return 'ok'
-
-
-    def new(self):
-        link_ui = LinkDirectory(Link())
-
-        form = link_ui.form()
-        if form.get_submit() == 'cancel':
-            return redirect('.')
-
-        if form.is_submitted() and not form.has_errors():
-            link_ui.submit(form)
-            return redirect('%s/' % link_ui.link.id)
-
-        get_response().breadcrumb.append(('new', _('New Link')))
-        html_top('links', title = _('New Link'))
-        r = TemplateIO(html=True)
-        r += htmltext('<h2>%s</h2>') % _('New Link')
-        r += form.render()
-        return r.getvalue()
-
-    def _q_lookup(self, component):
-        try:
-            link = Link.get(component)
-        except KeyError:
-            raise errors.TraversalError()
-        get_response().breadcrumb.append((str(link.id), link.title))
-        return LinkDirectory(link)
-
-    def listing(self):
-        return redirect('.')
-
diff --git a/auquotidien/modules/root.py b/auquotidien/modules/root.py
index 043063c..d6664d7 100644
--- a/auquotidien/modules/root.py
+++ b/auquotidien/modules/root.py
@@ -34,7 +34,6 @@ from wcs.qommon.tokens import Token
 from wcs.qommon.admin.emails import EmailsDirectory
 from wcs.qommon.admin.texts import TextsDirectory
 
-from .links import Link
 from .announces import Announce, AnnounceSubscription
 from .myspace import MyspaceDirectory
 from .agenda import AgendaDirectory
@@ -781,11 +780,7 @@ class AlternateRootDirectory(OldRootDirectory):
     preview = AlternatePreviewDirectory()
 
     def get_substitution_variables(self):
-        d = {}
-        def print_links(fd):
-            fd.write(str(self.links()))
-        d['links'] = print_links
-        return d
+        return {'links': ''}
 
     def _q_traverse(self, path):
         self.feed_substitution_parts()
@@ -1088,9 +1083,6 @@ class AlternateRootDirectory(OldRootDirectory):
             r += htmltext('<input type="submit" value="%s"/>') % _('Load')
             r += htmltext('</form>')
 
-        if get_cfg('aq-permissions', {}).get('links'):
-            r += self.links()
-
         cats = Category.select(order_by = 'name')
         cats = [x for x in cats if x.url_name != 'consultations' and x.get_homepage_position() == 'side']
         Category.sort_by_position(cats)
@@ -1134,41 +1126,6 @@ class AlternateRootDirectory(OldRootDirectory):
     def has_anonymous_access_codes(self):
         return any((x for x in FormDef.select() if x.enable_tracking_codes))
 
-    def links(self):
-        links = Link.select()
-        if not links:
-            return ''
-
-        Link.sort_by_position(links)
-
-        r = TemplateIO(html=True)
-
-        r += htmltext('<div id="links">')
-        if links[0].url:
-            # first link has an URL, so it's not a title, so we display a
-            # generic title
-            r += htmltext('<h3>%s</h3>') % _('Useful links')
-        has_ul = False
-        vars = get_publisher().substitutions.get_context_variables()
-        for link in links:
-            if not link.url:
-                # acting title
-                if has_ul:
-                    r += htmltext('</ul>')
-                r += htmltext('<h3>%s</h3>') % link.title
-                r += htmltext('<ul>')
-                has_ul = True
-            else:
-                if not has_ul:
-                    r += htmltext('<ul>')
-                    has_ul = True
-                r += htmltext('<li class="link-%s"><a href="%s">%s</a></li>') % (
-                        simplify(link.title), get_variadic_url(link.url, vars), link.title)
-        if has_ul:
-            r += htmltext('</ul>')
-        r += htmltext('</div>')
-        return r.getvalue()
-
     def announces(self):
         announces = Announce.get_published_announces()
         if not announces:
-- 
2.24.0

