mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2024-11-29 21:31:54 +00:00
[svn] override save for CaveDescriptions to scan qm wikilinks and add into the manytomany field linked_qms
This commit is contained in:
parent
3a61639a08
commit
6f0be2c492
@ -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')
|
||||
@ -525,6 +540,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)
|
||||
def __unicode__(self):
|
||||
|
@ -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'<a href="%s/person/\1/">\1</a>' % 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.
|
||||
|
@ -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.
|
@ -1,6 +1,5 @@
|
||||
{% extends "cavebase.html" %}
|
||||
{% load wiki_markup %}
|
||||
{% load mptt_tags %}
|
||||
{% block title %} {{cave_description.short_name}} {% endblock title %}
|
||||
{% block editLink %}<a href={{cave_description.get_admin_url}}>Edit description {{cave_description}}</a>{% endblock %}
|
||||
|
||||
|
12
utils.py
12
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)
|
||||
@ -158,3 +156,5 @@ def html_to_wiki(text, codec = "utf-8"):
|
||||
for regex, repl in re_subs:
|
||||
out = regex.sub(repl, out)
|
||||
return out
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user