From b5bb31898c3cbe772403edbafd858d7d927b923d Mon Sep 17 00:00:00 2001
From: Steven H Johnson <shjohnson.pi@gmail.com>
Date: Fri, 8 Jul 2016 17:56:23 -0400
Subject: [PATCH] Add Practice Courts Info to maps. Fix crash when no doc set
 for a map info

---
 htmleditor/models.py                          |  1 +
 usta/admin.py                                 |  4 +++-
 ...2_sitesettings_practice_courts_document.py | 22 +++++++++++++++++++
 usta/models.py                                |  5 +++++
 usta/templates/usta/hotels_tag.html           |  4 ++--
 usta/templates/usta/map.html                  |  5 +++--
 usta/views.py                                 |  3 +++
 7 files changed, 39 insertions(+), 5 deletions(-)
 create mode 100644 usta/migrations/0022_sitesettings_practice_courts_document.py

diff --git a/htmleditor/models.py b/htmleditor/models.py
index 21a5508..a3c4d07 100644
--- a/htmleditor/models.py
+++ b/htmleditor/models.py
@@ -163,6 +163,7 @@ class Document(models.Model):
             names = [
                 'hotels_document',
                 'parking_document',
+                'practice_courts_document',
                 'coach_registration_document',
                 'home_page_document',
                 'individual_patrons_document',
diff --git a/usta/admin.py b/usta/admin.py
index 0c52462..79dfa19 100644
--- a/usta/admin.py
+++ b/usta/admin.py
@@ -335,7 +335,9 @@ class SiteSettingsAdmin(SingleInstanceAdminMixin, admin.ModelAdmin):
             'fields': ('reunion_year', 'show_reunion_form')
         }),
         ('Map Locations', {
-            'fields': ('hotels_document', 'parking_document')
+            'fields': (
+                'hotels_document', 'parking_document',
+                'practice_courts_document')
         }),
         ('College Coach', {
             'fields': ('coach_registration_document',)
diff --git a/usta/migrations/0022_sitesettings_practice_courts_document.py b/usta/migrations/0022_sitesettings_practice_courts_document.py
new file mode 100644
index 0000000..de5eb7b
--- /dev/null
+++ b/usta/migrations/0022_sitesettings_practice_courts_document.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.1 on 2016-07-08 21:43
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('htmleditor', '0029_document_content'),
+        ('usta', '0021_sitesettings_individual_patrons_document'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='sitesettings',
+            name='practice_courts_document',
+            field=models.ForeignKey(blank=True, help_text='The document that contains the Practice Courts Information', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='htmleditor.Document'),
+        ),
+    ]
diff --git a/usta/models.py b/usta/models.py
index 376f3f6..bbc0c45 100644
--- a/usta/models.py
+++ b/usta/models.py
@@ -533,6 +533,11 @@ class SiteSettings(GetSettingMixin, SingleInstanceMixin, models.Model):
         blank=True, null=True,
         help_text="The document that contains the Parking Information",
         related_name='+')
+    practice_courts_document = models.ForeignKey('htmleditor.Document',
+        models.SET_NULL,
+        blank=True, null=True,
+        help_text="The document that contains the Practice Courts Information",
+        related_name='+')
     # -- College Coach Settings -----------------------------------------------
     coach_registration_document = models.ForeignKey('htmleditor.Document',
         models.SET_NULL,
diff --git a/usta/templates/usta/hotels_tag.html b/usta/templates/usta/hotels_tag.html
index 82ec622..1c0f16b 100644
--- a/usta/templates/usta/hotels_tag.html
+++ b/usta/templates/usta/hotels_tag.html
@@ -1,8 +1,8 @@
 {% 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>
+    <h2 class="map-header">Hotels {% if hotels_information and 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 %}
+    {% if hotels_information and 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">
diff --git a/usta/templates/usta/map.html b/usta/templates/usta/map.html
index 6c66ced..66dc047 100644
--- a/usta/templates/usta/map.html
+++ b/usta/templates/usta/map.html
@@ -50,7 +50,8 @@
         {% endif %}
         {% if category.1 == "Practice_Court" %}
             <div class="map-divider">
-                <h2 class="map-header">Practice Courts</h2>
+                <h2 class="map-header">Practice Courts{% if practice_courts_information and perms.htmleditor.change_document %} <a href="{% url 'admin:htmleditor_document_change' practice_courts_information.pk %}?next={{ request.get_full_path|urlencode }}"><img alt="Pencil" title="Edit" src="{% static 'usta/edit.png' %}"></a>{% endif %}</h2>
+                {% docreplace practice_courts_information.content %}<br>
                 <table id="map-table-practice" class="map-table">
                     <tr>
                         <th>Name</th>
@@ -71,7 +72,7 @@
         {% endif %}
         {% 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>
+                <h2 class="map-header">Parking {% if parking_information and 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>
                 {% docreplace parking_information.content %}<br>
                 <table id="map-table-parking" class="map-table">
                     <tr>
diff --git a/usta/views.py b/usta/views.py
index 12b0db8..acd2418 100644
--- a/usta/views.py
+++ b/usta/views.py
@@ -250,6 +250,8 @@ def map_view(request):
     site_settings = get_site_settings()
     hotels_information = site_settings.get_setting('hotels_document', None)
     parking_information = site_settings.get_setting('parking_document', None)
+    practice_courts_information = site_settings.get_setting(
+        'practice_courts_document', None)
 
     return render(request, 'usta/map.html', {
         'locations': locations,
@@ -257,6 +259,7 @@ def map_view(request):
         'GOOGLE_API_KEY': getattr(settings, 'GOOGLE_API_KEY', ''),
         'hotels_information': hotels_information,
         'parking_information': parking_information,
+        'practice_courts_information': practice_courts_information,
         'title': 'Map Locations',
         'show_generic': True,
     })
-- 
GitLab