From 6a12b68cb0de3616aa4f9d9eaad7928eb5b309a1 Mon Sep 17 00:00:00 2001
From: Steven H Johnson <shjohnson.pi@gmail.com>
Date: Fri, 17 Jun 2016 17:56:28 -0400
Subject: [PATCH] Add hotels widget. Change nodelinks filter to docreplace tag

---
 django_usta/settings.py                       |   4 +--
 .../templates/htmleditor/news_article.html    |   2 +-
 .../templates/htmleditor/news_summary.html    |   2 +-
 htmleditor/templates/htmleditor/view.html     |   4 +--
 htmleditor/templatetags/htmleditor_tags.py    |  12 +++++--
 .../ckeditor/plugins/hotels/icons/hotels.png  | Bin 0 -> 2142 bytes
 .../ckeditor/plugins/hotels/plugin.js         |  22 ++++++++++++
 .../ckeditor/plugins/hotels/styles/hotels.css |  11 ++++++
 usta/templates/usta/college_coaches.html      |   2 +-
 usta/templates/usta/hotels_tag.html           |  31 ++++++++++++++++
 usta/templates/usta/index.html                |   2 +-
 usta/templates/usta/map.html                  |  34 ++----------------
 usta/templatetags/usta_tags.py                |  22 ++++++++++++
 usta/views.py                                 |   7 +---
 .../volunteer/view_volunteer_topics.html      |   2 +-
 15 files changed, 108 insertions(+), 49 deletions(-)
 create mode 100644 static/ckeditor/ckeditor/plugins/hotels/icons/hotels.png
 create mode 100644 static/ckeditor/ckeditor/plugins/hotels/plugin.js
 create mode 100644 static/ckeditor/ckeditor/plugins/hotels/styles/hotels.css
 create mode 100644 usta/templates/usta/hotels_tag.html

diff --git a/django_usta/settings.py b/django_usta/settings.py
index 98a19ac..32fe1fe 100644
--- a/django_usta/settings.py
+++ b/django_usta/settings.py
@@ -284,7 +284,7 @@ CKEDITOR_CONFIGS = {
         'toolbar_Custom': [
             ['Save', 'Undo', 'Redo', 'Maximize', 'ShowBlocks'],
             ['Image', 'Table', 'HorizontalRule', 'SpecialChar', 'Tickets',
-             'Youtube'],
+             'Hotels', 'Youtube'],
             ['Cut', 'Copy', 'Paste', 'Find', 'Replace'],
             ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat'],
             ['NumberedList', 'BulletedList', 'Outdent', 'Indent',
@@ -293,7 +293,7 @@ CKEDITOR_CONFIGS = {
             ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
             ['Link', 'Unlink', 'NodeLink'],
         ],
-        'extraPlugins': 'youtube,nodelink,tickets',
+        'extraPlugins': 'youtube,nodelink,tickets,hotels',
         'removePlugins': 'iframe',
         'stylesSet': [
             {'name': 'Boys 16', 'element': 'span', 'attributes': {
diff --git a/htmleditor/templates/htmleditor/news_article.html b/htmleditor/templates/htmleditor/news_article.html
index 84ff43e..ff5e06d 100644
--- a/htmleditor/templates/htmleditor/news_article.html
+++ b/htmleditor/templates/htmleditor/news_article.html
@@ -3,7 +3,7 @@
     {% if news.image.url %}
     <img class="news-image" alt="{{ news.image.alt }}" src="{{ news.image.url }}">
     {% endif %}
-    {{ news.content|nodelinks }}
+    {% docreplace news.content %}
     <p class="news-source">Source{{ news.sources.count|pluralize }}:
     {% for source in news.sources.all %}
         {{ source }}{% if not forloop.last %} &amp;{% endif %}
diff --git a/htmleditor/templates/htmleditor/news_summary.html b/htmleditor/templates/htmleditor/news_summary.html
index 035f908..72d93f6 100644
--- a/htmleditor/templates/htmleditor/news_summary.html
+++ b/htmleditor/templates/htmleditor/news_summary.html
@@ -11,7 +11,7 @@
         </a>
         <div class="news-date">{{ news.date_created|date }}</div>
     </div>
-    <div class="news-content">{% if perms.htmleditor.change_newsarticle %}<a href="{% url 'admin:htmleditor_newsarticle_change' news.pk %}?next={{ request.get_full_path|urlencode }}">&#9997;</a> {% endif %}{{ news.content|nodelinks|truncatewords_html:20 }}</div>
+    <div class="news-content">{% if perms.htmleditor.change_newsarticle %}<a href="{% url 'admin:htmleditor_newsarticle_change' news.pk %}?next={{ request.get_full_path|urlencode }}">&#9997;</a> {% endif %}{% docreplace news.content as content %}{{ content|truncatewords_html:20 }}</div>
     <p class="news-source">Source{{ news.sources.count|pluralize }}:
     {% for source in news.sources.all %}
         {{ source }}{% if not forloop.last %} &amp;{% endif %}
diff --git a/htmleditor/templates/htmleditor/view.html b/htmleditor/templates/htmleditor/view.html
index 1cbf2e9..8c8c5c2 100644
--- a/htmleditor/templates/htmleditor/view.html
+++ b/htmleditor/templates/htmleditor/view.html
@@ -6,7 +6,7 @@
 {% block main %}
 {% if document %}
     <h1>{{ document.name }} {% if perms.htmleditor.change_document %}<a href="{% url 'admin:htmleditor_document_change' document.pk %}?next={{ request.get_full_path|urlencode }}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}</h1>
-    {{ document.content|nodelinks }}
+    {% docreplace document.content %}
 {% else %}
     <h1>{{ node.name }}</h1>
 {% endif %}
@@ -23,7 +23,7 @@
             <div id="tab-{{ tab.pk }}" class="content">
                 <h1>{{ tab.document.name }} {% if perms.htmleditor.change_document %}<a href="{% url 'admin:htmleditor_document_change' tab.document.pk %}?next={{ request.get_full_path|urlencode }}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}</h1>
                 <br>
-                {{ tab.document.content|nodelinks }}
+                {% docreplace tab.document.content %}
             </div>
         {% endfor %}
     </div>
diff --git a/htmleditor/templatetags/htmleditor_tags.py b/htmleditor/templatetags/htmleditor_tags.py
index 5e454db..2afb4c6 100644
--- a/htmleditor/templatetags/htmleditor_tags.py
+++ b/htmleditor/templatetags/htmleditor_tags.py
@@ -9,13 +9,14 @@ from django.core.urlresolvers import reverse
 from django.conf import settings
 
 from htmleditor.models import NewsArticle, NavigationNode, Image
+from usta.templatetags import usta_tags
 
 register = template.Library()
 
 NODE_PATTERN = re.compile(r'\{\{\s+NODE:(?P<pk>\d+)\s+\}\}')
 
 TICKET_PATTERN = re.compile(r'<div\s+class="ticket-seating">\s+<p>Ticket Seating Widget</p>\s+</div>')
-
+HOTEL_PATTERN = re.compile(r'<div\s+class="hotels-table">\s+<p>Hotels Widget</p>\s+</div>')
 
 @register.filter
 def has_content(node, str_list):
@@ -30,8 +31,9 @@ def has_content(node, str_list):
     return node.has_content() or node.children_have_content()
 
 
-@register.filter
-def nodelinks(content):
+@register.simple_tag(takes_context=True)
+def docreplace(context, content):
+    "Add widgets and links to htmleditor docs"
     def repl(match):
         if match.group(1) == '0':
             return '/'
@@ -46,6 +48,10 @@ def nodelinks(content):
         template = loader.get_template('htmleditor/ticket_seating.html')
         return template.render()
     content = TICKET_PATTERN.sub(repl, content)
+    def repl(match):
+        t = template.Template("{% load usta_tags %}{% hotels_table false %}")
+        return t.render(context)
+    content = HOTEL_PATTERN.sub(repl, content)
 
     return mark_safe(content)
 
diff --git a/static/ckeditor/ckeditor/plugins/hotels/icons/hotels.png b/static/ckeditor/ckeditor/plugins/hotels/icons/hotels.png
new file mode 100644
index 0000000000000000000000000000000000000000..5a544aa3cc8541041fced09bd9a7a7144cb2bde3
GIT binary patch
literal 2142
zcmbtW3rtgI7`?re_7<eQEjk<^wiSwF4$5>u<yCLWq@lnnx(&n)kWHBe3RQu~^a7Ws
zqE(#oD9C*AUKmQkVtA}N>BQ1xPJ@O=0To3B!3Q8X_TMs%i6KIkem6}{&wtMOzBb?A
zoG*gbTbNm!K@emi^_OgbAj;3=gU|q?tuWVsH(FG{dI>Z`zRH@)^8vz8_=hSX$aw|%
zP?FC2?g1cumo(6iZlJL(Kc;!nTrB|dP%2rsG3oZ>?tp|lPN-NO(AQKisQuvZqtyNK
zs-s%MYukk2l{LlvsP(g(d-t2*$VCT8Oh71)ED1su8wLfiIpFtGhyMzDSlbZmU`XCP
z(t&zemW|9j_Bf=wic679`u&y}W;WVo&E=&HCLKyQ?|)iSE)p`<`QtK41ShQyD$H%Q
zr+x%SJHhO<Ckli;-J!HybTGq6>bwxehx5@h+3A&t3`5;Y3Ub8+TK3JPK5KjF0D6+@
zrHkail(8AAfLHP3t>RoEBNdvQp|aD8tS+MRj6sjfj;_JV0z!;WF9S9WE^#4j0T1Qw
zcR_g-$#Qdc^{8!Ddoosf&v?=^U5ZeubQ=jNLe;Ltk-q*0JE5gMzHz5je+)ApMO2@G
zI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yM{zTDbR%eR{3vPWG{xHFCQIWgjb*$
z)9qD5tZU9g-}1NQNy)`H_f~voSuTJ&EByYw`erjPfI6GYxY%*)o=+&Jm$m&GnxPKc
z(G3>BJo|aih46n`U%-?${7}2Q8b>Hx3<8ynlEtTj0@xhzf~kw^pZC<lkYJuy;akIf
s^WD5NjkkX`^SWT)R+w}2_<0|QK2-QmbKc>0@J|4e`UOdT!6MTB1^Q=(#{d8T

literal 0
HcmV?d00001

diff --git a/static/ckeditor/ckeditor/plugins/hotels/plugin.js b/static/ckeditor/ckeditor/plugins/hotels/plugin.js
new file mode 100644
index 0000000..6002272
--- /dev/null
+++ b/static/ckeditor/ckeditor/plugins/hotels/plugin.js
@@ -0,0 +1,22 @@
+CKEDITOR.plugins.add('hotels', {
+    requires: 'widget',
+    icons: 'hotels',
+
+    init: function(editor) {
+        editor.addContentsCss(this.path + 'styles/hotels.css');
+        editor.widgets.add('hotels', {
+            button: 'Add Hotels Widget',
+            allowedContent: 'div(!hotels-table)',
+            requiredContent: 'div(hotels-table)',
+
+            upcast: function(element) {
+                return element.name == 'div' && element.hasClass('hotels-table');
+            },
+
+            template:
+                '<div class="hotels-table">' +
+                    '<p>Hotels Widget</p>' +
+                '</div>'
+        });
+    }
+});
diff --git a/static/ckeditor/ckeditor/plugins/hotels/styles/hotels.css b/static/ckeditor/ckeditor/plugins/hotels/styles/hotels.css
new file mode 100644
index 0000000..492b10e
--- /dev/null
+++ b/static/ckeditor/ckeditor/plugins/hotels/styles/hotels.css
@@ -0,0 +1,11 @@
+.hotels-table {
+    padding: 8px;
+    margin: auto;
+    background: #eee;
+    border-radius: 20px;
+    border: 1px solid #ddd;
+    width: 554px;
+    height: 525px;
+    text-align: center;
+    font-size: large;
+}
diff --git a/usta/templates/usta/college_coaches.html b/usta/templates/usta/college_coaches.html
index d808752..9a8bf55 100644
--- a/usta/templates/usta/college_coaches.html
+++ b/usta/templates/usta/college_coaches.html
@@ -41,7 +41,7 @@
         {% if document %}
             <div id="coach-registration" class="content">
                 {% if perms.htmleditor.change_document %}<a href="{% url 'admin:htmleditor_document_change' document.pk %}?next={{ request.get_full_path|urlencode }}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}
-                {{ document.content|nodelinks }}
+                {% docreplace document.content %}
             </div>
         {% endif %}
 </div>
diff --git a/usta/templates/usta/hotels_tag.html b/usta/templates/usta/hotels_tag.html
new file mode 100644
index 0000000..82ec622
--- /dev/null
+++ b/usta/templates/usta/hotels_tag.html
@@ -0,0 +1,31 @@
+{% load static htmleditor_tags %}
+{% if show_title %}
+    <h2 class="map-header">Hotels {% if perms.htmleditor.change_document %}<a href="{% url 'admin:htmleditor_document_change' hotels_information.pk %}?next={{ request.get_full_path|urlencode }}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}</h2>
+{% else %}
+    {% if perms.htmleditor.change_document %}<h2><a href="{% url 'admin:htmleditor_document_change' hotels_information.pk %}?next={{ request.get_full_path|urlencode }}">Edit This Section <img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a></h2>{% endif %}
+{% endif %}
+{% docreplace hotels_information.content %}<br>
+<table id="map-table-hotels" class="map-table">
+    <tr>
+        <th>Name</th>
+        <th class="map-address">Address</th>
+        <th>Rate</th>
+        <th class="map-description">Description</th>
+    </tr>
+    {% if radisson %}{# Host hotel comes first #}
+    <tr>
+        <td class="">{% if radisson.link %}<a href="{{ radisson.link }}">{% endif %}{{ radisson.name }}{% if radisson.link %}</a>{% endif %}</td>
+        <td class=" map-address"><a target="_blank" title="Directions" href='http://maps.google.com/?q=%22{{ radisson.address|urlencode:"" }}%22'>{{ radisson.address }}</a><br>{{ radisson.phone }}</td>
+        <td class="">{{ radisson.rate }}</td>
+        <td class="map-description">{{ radisson.description }}</td>
+    </tr>
+    {% endif %}
+    {% for location in locations %}
+    <tr>
+        <td class="">{% if location.link %}<a href="{{ location.link }}">{% endif %}{{ location.name }}{% if location.link %}</a>{% endif %}</td>
+        <td class=" map-address"><a target="_blank" title="Directions" href='http://maps.google.com/?q=%22{{ location.address|urlencode:"" }}%22'>{{ location.address }}</a><br>{{ location.phone }}</td>
+        <td class="">{{ location.rate }}</td>
+        <td class="map-description">{{ location.description }}</td>
+    </tr>
+    {% endfor %}
+</table>
diff --git a/usta/templates/usta/index.html b/usta/templates/usta/index.html
index 7a6e0b7..e8c8faf 100644
--- a/usta/templates/usta/index.html
+++ b/usta/templates/usta/index.html
@@ -54,7 +54,7 @@
             </div>
             <div id="front-page-center">
                 {% if document %}
-                    {{ document.content|nodelinks }}
+                    {% docreplace document.content %}
                 {% endif %}
             </div>
         </div>
diff --git a/usta/templates/usta/map.html b/usta/templates/usta/map.html
index 7c75928..6c66ced 100644
--- a/usta/templates/usta/map.html
+++ b/usta/templates/usta/map.html
@@ -1,5 +1,5 @@
 {% extends "usta/base.html" %}
-{% load i18n staticfiles htmleditor_tags %}
+{% load i18n staticfiles htmleditor_tags usta_tags %}
 {% block title %}{{ block.super }} | Map{% endblock %}
 
 {% block comment %}
@@ -72,7 +72,7 @@
         {% if category.1 == "Parking" %}
             <div class="map-divider">
                 <h2 class="map-header">Parking {% if perms.htmleditor.change_document %}<a href="{% url 'admin:htmleditor_document_change' parking_information.pk %}?next={{ request.get_full_path|urlencode }}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}</h2>
-                {{ parking_information.content|nodelinks }}<br>
+                {% docreplace parking_information.content %}<br>
                 <table id="map-table-parking" class="map-table">
                     <tr>
                         <th>Name</th>
@@ -93,35 +93,7 @@
         {% endif %}
         {% if category.1 == "Hotel" %}
             <div class="map-divider">
-                <h2 class="map-header">Hotels {% if perms.htmleditor.change_document %}<a href="{% url 'admin:htmleditor_document_change' hotels_information.pk %}?next={{ request.get_full_path|urlencode }}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}</h2>
-                {{ hotels_information.content|nodelinks }}<br>
-                <table id="map-table-hotels" class="map-table">
-                    <tr>
-                        <th>Name</th>
-                        <th class="map-address">Address</th>
-                        <th>Rate</th>
-                        <th class="map-phone">Phone</th>
-                        <th class="map-description">Description</th>
-                    </tr>
-                    {% if radisson %}{# Host hotel comes first #}
-                    <tr>
-                        <td class="">{% if radisson.link %}<a href="{{ radisson.link }}">{% endif %}{{ radisson.name }}{% if radisson.link %}</a>{% endif %}</td>
-                        <td class=" map-address"><a target="_blank" title="Directions" href='http://maps.google.com/?q=%22{{ radisson.address|urlencode:"" }}%22'>{{ radisson.address }}</a></td>
-                        <td class="">{{ radisson.rate }}</td>
-                        <td>{{ radisson.phone }}</td>
-                        <td class="map-description">{{ radisson.description }}</td>
-                    </tr>
-                    {% endif %}
-                    {% for location in places %}
-                    <tr>
-                        <td class="">{% if location.link %}<a href="{{ location.link }}">{% endif %}{{ location.name }}{% if location.link %}</a>{% endif %}</td>
-                        <td class=" map-address"><a target="_blank" title="Directions" href='http://maps.google.com/?q=%22{{ location.address|urlencode:"" }}%22'>{{ location.address }}</a></td>
-                        <td class="">{{ location.rate }}</td>
-                        <td>{{ location.phone }}</td>
-                        <td class="map-description">{{ location.description }}</td>
-                    </tr>
-                    {% endfor %}
-                </table>
+                {% hotels_table %}
             </div>
         {% endif %}
         {% if category.1 == "Hospital" %}
diff --git a/usta/templatetags/usta_tags.py b/usta/templatetags/usta_tags.py
index 58cd4ff..04dbb30 100644
--- a/usta/templatetags/usta_tags.py
+++ b/usta/templatetags/usta_tags.py
@@ -18,6 +18,7 @@ from usta.models import (
     YOUTUBE_PATTERN,
     TournamentDate,
     PlayerOfTheDay,
+    MapLocation,
 )
 from htmleditor.models import get_random_generic_banner, Banner
 from sponsors.models import get_random_sponsor
@@ -405,3 +406,24 @@ def potd_drilldown():
     return {
         'years': years,
     }
+
+@register.inclusion_tag('usta/hotels_tag.html', takes_context=True)
+def hotels_table(context, show_title=True):
+    "Show the hotels doc and table"
+    locations = MapLocation.objects.filter(
+        category=MapLocation.HOTEL).order_by('name').exclude(
+            name__contains="Radisson")
+
+    site_settings = get_site_settings()
+    hotels_information = site_settings.get_setting('hotels_document', None)
+
+    radisson = MapLocation.objects.filter(name__contains="Radisson").first()
+
+    context.update({
+        'radisson': radisson,
+        'locations': locations,
+        'hotels_information': hotels_information,
+        'show_title': show_title,
+    })
+
+    return context
diff --git a/usta/views.py b/usta/views.py
index a692f71..12b0db8 100644
--- a/usta/views.py
+++ b/usta/views.py
@@ -244,24 +244,19 @@ def map_view(request):
         letter = MapLocation.static_get_category_letter(category)
         icon = 'images/map_pins/{0}.png'.format(letter.lower())
         key = (name, name.replace(' ', '_'), icon)
-        locations[key] = MapLocation.objects.filter(
-            category=category).order_by('name').exclude(
-                name__contains="Radisson")
+        locations[key] = MapLocation.objects.filter(category=category)
     map_center = "{lat: 42.291, lng: -85.598}"
 
     site_settings = get_site_settings()
     hotels_information = site_settings.get_setting('hotels_document', None)
     parking_information = site_settings.get_setting('parking_document', None)
 
-    radisson = MapLocation.objects.filter(name__contains="Radisson").first()
-
     return render(request, 'usta/map.html', {
         'locations': locations,
         'map_center': map_center,
         'GOOGLE_API_KEY': getattr(settings, 'GOOGLE_API_KEY', ''),
         'hotels_information': hotels_information,
         'parking_information': parking_information,
-        'radisson': radisson,
         'title': 'Map Locations',
         'show_generic': True,
     })
diff --git a/volunteer/templates/volunteer/view_volunteer_topics.html b/volunteer/templates/volunteer/view_volunteer_topics.html
index e9438d3..12e0876 100644
--- a/volunteer/templates/volunteer/view_volunteer_topics.html
+++ b/volunteer/templates/volunteer/view_volunteer_topics.html
@@ -45,7 +45,7 @@
         <section id="{{ topic.name|slugify }}">
             <h1>{{ topic.name }} {% if perms.volunteer.change_volunteertopic %}<a href="{% url 'volunteer:edit_volunteer_topic' topic.pk %}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}</h1>
 
-            {{ topic.content|nodelinks }}
+            {% docreplace topic.content %}
         </section>
     {% endfor %}
 </div>
-- 
GitLab