diff --git a/core/models.py b/core/models.py index facc5ff..7c23ad9 100644 --- a/core/models.py +++ b/core/models.py @@ -1,4 +1,4 @@ -import urllib, urlparse, string, os, datetime, logging +import urllib, urlparse, string, os, datetime, logging, re from django.forms import ModelForm from django.db import models from django.contrib import admin @@ -13,6 +13,21 @@ getcontext().prec=2 #use 2 significant figures for decimal calculations from models_survex import * +def get_related_by_wikilinks(wiki_text): + found=re.findall(settings.QM_PATTERN,wiki_text) + res=[] + for wikilink in found: + qmdict={'urlroot':settings.URL_ROOT,'cave':wikilink[2],'year':wikilink[1],'number':wikilink[3]} + try: + qm=QM.objects.get(found_by__cave__kataster_number = qmdict['cave'], + found_by__date__year = qmdict['year'], + number = qmdict['number']) + res.append(qm) + except QM.DoesNotExist: + print 'fail on '+str(wikilink) + + return res + logging.basicConfig(level=logging.DEBUG, filename=settings.LOGFILE, filemode='w') @@ -524,6 +539,14 @@ class CaveDescription(TroggleModel): def get_absolute_url(self): return urlparse.urljoin(settings.URL_ROOT, reverse('cavedescription', args=(self.short_name,))) + + def save(self): + super(CaveDescription, self).save() + qm_list=get_related_by_wikilinks(self.description) + print qm_list + for qm in qm_list: + self.linked_qms.add(qm) + super(CaveDescription, self).save() class NewSubCave(TroggleModel): name = models.CharField(max_length=200, unique = True) diff --git a/core/templatetags/wiki_markup.py b/core/templatetags/wiki_markup.py index b4d97ac..ec8a997 100644 --- a/core/templatetags/wiki_markup.py +++ b/core/templatetags/wiki_markup.py @@ -40,7 +40,7 @@ def wiki_list(line, listdepth): def wiki_to_html(value, autoescape=None): """ This is the tag which turns wiki syntax into html. It is intended for long pieces of wiki. - Hence it splits the wiki into paragraphs double line feeds. + Hence it splits the wiki into HTML paragraphs based on double line feeds. """ #find paragraphs outValue = "" @@ -55,7 +55,7 @@ def wiki_to_html(value, autoescape=None): def wiki_to_html_short(value, autoescape=None): """ This is the tag which turns wiki syntax into html. It is intended for short pieces of wiki. - Hence it is not split the wiki into paragraphs using where it find double line feeds. + Hence it is not split the wiki into paragraphs using where it finds double line feeds. """ if autoescape: value = conditional_escape(value) @@ -71,7 +71,7 @@ def wiki_to_html_short(value, autoescape=None): value = re.sub("\[\[\s*person:(.+)\]\]",r'\1' % url_root, value, re.DOTALL) #make qm links. this takes a little doing - qmMatchPattern="\[\[\s*[Qq][Mm]:([ABC]?)(\d{4})-(\d*)-(\d*)\]\]" + qmMatchPattern=settings.QM_PATTERN def qmrepl(matchobj): """ A function for replacing wikicode qm links with html qm links. diff --git a/settings.py b/settings.py index 0b2b172..d1a8686 100644 --- a/settings.py +++ b/settings.py @@ -88,6 +88,6 @@ INSTALLED_APPS = ( 'troggle.imagekit', ) -FEINCMS_ADMIN_MEDIA=MEDIA_URL + 'feincms/' +QM_PATTERN="\[\[\s*[Qq][Mm]:([ABC]?)(\d{4})-(\d*)-(\d*)\]\]" from localsettings import * #localsettings needs to take precedence. Call it to override any existing vars. \ No newline at end of file diff --git a/templates/cave_description.html b/templates/cave_description.html index f41d9fd..12b9e1b 100644 --- a/templates/cave_description.html +++ b/templates/cave_description.html @@ -1,6 +1,5 @@ {% extends "cavebase.html" %} {% load wiki_markup %} -{% load mptt_tags %} {% block title %} {{cave_description.short_name}} {% endblock title %} {% block editLink %}Edit description {{cave_description}}{% endblock %} diff --git a/utils.py b/utils.py index 2c181c7..5245589 100644 --- a/utils.py +++ b/utils.py @@ -1,8 +1,5 @@ from django.conf import settings -try: - from django.db import models -except:#We want to get rid of this try statement if possible - from troggle.core.models import LogbookEntry + import random, re, logging def weighted_choice(lst): @@ -14,16 +11,17 @@ def weighted_choice(lst): return item def randomLogbookSentence(): + from troggle.core.models import LogbookEntry randSent={} # needs to handle empty logbooks without crashing #Choose a random logbook entry - randSent['entry']=models.LogbookEntry.objects.order_by('?')[0] + randSent['entry']=LogbookEntry.objects.order_by('?')[0] #Choose again if there are no sentances (this happens if it is a placeholder entry) while len(re.findall('[A-Z].*?\.',randSent['entry'].text))==0: - randSent['entry']=models.LogbookEntry.objects.order_by('?')[0] + randSent['entry']=LogbookEntry.objects.order_by('?')[0] #Choose a random sentence from that entry. Store the sentence as randSent['sentence'], and the number of that sentence in the entry as randSent['number'] sentenceList=re.findall('[A-Z].*?\.',randSent['entry'].text) @@ -157,4 +155,6 @@ def html_to_wiki(text, codec = "utf-8"): #substitutions for regex, repl in re_subs: out = regex.sub(repl, out) - return out \ No newline at end of file + return out + +