From 087a02838877b34cc9deb368412567b92bd742b8 Mon Sep 17 00:00:00 2001
From: expo <expo@expobox.potato.hut>
Date: Sun, 21 Jun 2015 15:08:09 +0100
Subject: [PATCH] Moved notable caves to settings.py, link to a script to fix
 permissions

---
 core/models.py      | 23 +++++++++++++++++++----
 core/views_caves.py |  2 +-
 settings.py         |  2 ++
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/core/models.py b/core/models.py
index c3d82b0..fa41f95 100644
--- a/core/models.py
+++ b/core/models.py
@@ -1,4 +1,5 @@
 import urllib, urlparse, string, os, datetime, logging, re
+import subprocess
 from django.forms import ModelForm
 from django.db import models
 from django.contrib import admin
@@ -33,7 +34,13 @@ def get_related_by_wikilinks(wiki_text):
     
     return res
 
-logging.basicConfig(level=logging.DEBUG,
+try:
+      logging.basicConfig(level=logging.DEBUG,
+                           filename=settings.LOGFILE,
+                           filemode='w')
+except:
+      subprocess.call(settings.FIX_PERMISSIONS) 
+      logging.basicConfig(level=logging.DEBUG,
                            filename=settings.LOGFILE,
                            filemode='w')
 
@@ -447,7 +454,7 @@ class Cave(TroggleModel):
         return unicode(self.slug())
 
     def get_QMs(self):
-        return QM.objects.filter(found_by__cave=self)	
+        return QM.objects.filter(found_by__cave_slug=self.caveslug_set.all())	
 
     def new_QM_number(self, year=datetime.date.today().year):
             """Given a cave and the current year, returns the next QM number."""
@@ -497,7 +504,11 @@ class Cave(TroggleModel):
         return res
     
     def writeDataFile(self):
-        f = open(os.path.join(settings.CAVEDESCRIPTIONS, self.filename), "w")
+        try:
+            f = open(os.path.join(settings.CAVEDESCRIPTIONS, self.filename), "w")
+        except:
+            subprocess.call(settings.FIX_PERMISSIONS) 
+            f = open(os.path.join(settings.CAVEDESCRIPTIONS, self.filename), "w")
         t = loader.get_template('dataformat/cave.xml')
         c = Context({'cave': self})
         u = t.render(c)
@@ -673,7 +684,11 @@ class Entrance(TroggleModel):
         return self.cached_primary_slug
 
     def writeDataFile(self):
-        f = open(os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename), "w")
+        try:
+            f = open(os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename), "w")
+        except:
+            subprocess.call(settings.FIX_PERMISSIONS) 
+            f = open(os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename), "w")
         t = loader.get_template('dataformat/entrance.xml')
         c = Context({'entrance': self})
         u = t.render(c)
diff --git a/core/views_caves.py b/core/views_caves.py
index 8fd03b2..0fb509e 100644
--- a/core/views_caves.py
+++ b/core/views_caves.py
@@ -50,7 +50,7 @@ def caveCmp(x, y):
 
 def caveindex(request):
     caves = Cave.objects.all()
-    notablecavehrefs = [ "161", "204", "258", "76", "107" ]  # could detect notability by trips and notability of people who have been down them
+    notablecavehrefs = settings.NOTABLECAVESHREFS
     notablecaves = [Cave.objects.get(kataster_number=kataster_number)  for kataster_number in notablecavehrefs ]
     caves1623 = list(Cave.objects.filter(area__short_name = "1623"))
     caves1626 = list(Cave.objects.filter(area__short_name = "1626"))
diff --git a/settings.py b/settings.py
index 0032c2a..ca3011e 100644
--- a/settings.py
+++ b/settings.py
@@ -30,6 +30,8 @@ SITE_ID = 1
 # to load the internationalization machinery.
 USE_I18N = True
 
+FIX_PERMISSIONS = []
+NOTABLECAVESHREFS = [ "161", "204", "258", "76", "107", "264" ]
 
 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
 # trailing slash.