summaryrefslogtreecommitdiff
path: root/app/utils
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2018-07-07 20:41:09 -0400
committerluxagraf <sng@luxagraf.net>2018-07-07 20:41:09 -0400
commit6a2393e6819ea09aeb559354a69746750aa8cbdf (patch)
tree0ac9890740f9afcd9720ea9b550a3895d95ecb50 /app/utils
parentfe13c3830c6b36fb8f78009a788650a992cb3070 (diff)
added campsite model, refactored some code to avoid circular imports,
reorganized some auxilary functions and fixed broken build JS.
Diffstat (limited to 'app/utils')
-rw-r--r--app/utils/util.py93
-rw-r--r--app/utils/widgets.py79
2 files changed, 96 insertions, 76 deletions
diff --git a/app/utils/util.py b/app/utils/util.py
index 7ef244c..4cc7d31 100644
--- a/app/utils/util.py
+++ b/app/utils/util.py
@@ -1,5 +1,25 @@
-from locations.models import CheckIn
+import re
from django.contrib.gis.geos import GEOSGeometry
+from django.apps import apps
+from django.template.loader import render_to_string
+from django.conf import settings
+from bs4 import BeautifulSoup
+import markdown
+
+
+def markdown_to_html(txt):
+ md = markdown.Markdown(
+ extensions=[
+ 'markdown.extensions.fenced_code',
+ 'markdown.extensions.codehilite(css_class=highlight,linenums=False)',
+ 'markdown.extensions.attr_list',
+ 'footnotes',
+ 'extra'
+ ],
+ output_format='html5',
+ safe_mode=False
+ )
+ return md.convert(txt)
def convertll(lat, lon):
@@ -9,7 +29,76 @@ def convertll(lat, lon):
def get_latlon():
- loc = CheckIn.objects.latest()
+ loc = apps.get_model('locations', 'CheckIn').objects.latest()
lat_converted, lon_converted = convertll(loc.lat, loc.lon)
return lat_converted, lon_converted
+
+def extract_main_image(markdown):
+ soup = BeautifulSoup(markdown, 'html.parser')
+ try:
+ image = soup.find_all('img')[0]['id']
+ img_pk = image.split('image-')[1]
+ return apps.get_model('photos', 'LuxImage').objects.get(pk=img_pk)
+ except IndexError:
+ return None
+
+
+def parse_image(s):
+ soup = BeautifulSoup(s.group(), "lxml")
+ for img in soup.find_all('img'):
+ cl = img['class']
+ if cl[0] == 'postpic' or cl[0] == 'postpicright':
+ s = str(img).replace('[[base_url]]', settings.IMAGES_URL)
+ return s
+ else:
+ image_id = img['id'].split("image-")[1]
+ i = apps.get_model('photos', 'LuxImage').objects.get(pk=image_id)
+ caption = False
+ exif = False
+ cluster_class = None
+ extra = None
+ if cl[0] == 'cluster':
+ css_class = cl[0]
+ cluster_class = cl[1]
+ try:
+ if cl[2] == 'caption':
+ caption = True
+ elif cl[2] == 'exif':
+ exif = True
+ else:
+ extra = cl[2]
+
+ if len(cl) > 3:
+ if cl[3] == 'exif':
+ exif = True
+ except:
+ pass
+ elif cl[0] != 'cluster' and len(cl) > 1:
+ css_class = cl[0]
+ if cl[1] == 'caption':
+ caption = True
+ if cl[1] == 'exif':
+ exif = True
+ elif cl[0] != 'cluster' and len(cl) > 2:
+ css_class = cl[0]
+ if cl[1] == 'caption':
+ caption = True
+ if cl[2] == 'exif':
+ exif = True
+ print('caption'+str(caption))
+ else:
+ css_class = cl[0]
+ return render_to_string("lib/img_%s.html" % css_class, {'image': i, 'caption': caption, 'exif': exif, 'cluster_class': cluster_class, 'extra':extra})
+
+
+def render_images(s):
+ s = re.sub('<img(.*)/>', parse_image, s)
+ return s
+
+
+def parse_video(s):
+ soup = BeautifulSoup(s, "lxml")
+ if soup.find('video'):
+ return True
+ return False
diff --git a/app/utils/widgets.py b/app/utils/widgets.py
index 2b76f6b..c038252 100644
--- a/app/utils/widgets.py
+++ b/app/utils/widgets.py
@@ -5,24 +5,15 @@ from django.contrib.admin.widgets import AdminFileWidget
from django.contrib.gis.admin import OSMGeoAdmin
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
+from django.template.loader import render_to_string
+from django.template import Context
from django.forms.widgets import SelectMultiple
from django.conf import settings
-import markdown
+import markdown
-def markdown_to_html(txt):
- md = markdown.Markdown(
- extensions=[
- 'markdown.extensions.fenced_code',
- 'markdown.extensions.codehilite(css_class=highlight,linenums=False)',
- 'markdown.extensions.attr_list',
- 'footnotes',
- 'extra'
- ],
- output_format='html5',
- safe_mode=False
- )
- return md.convert(txt)
+from bs4 import BeautifulSoup
+from django.utils.module_loading import import_string
class CustomSelectMultiple(SelectMultiple):
@@ -152,63 +143,3 @@ class OLAdminBase(OSMGeoAdmin):
openlayers_url = '/static/admin/js/OpenLayers.js'
-from bs4 import BeautifulSoup
-from photos.models import LuxImage
-from django.template.loader import render_to_string
-from django.template import Context
-
-
-def parse_image(s):
- soup = BeautifulSoup(s.group(), "lxml")
- for img in soup.find_all('img'):
- cl = img['class']
- if cl[0] == 'postpic' or cl[0] == 'postpicright':
- s = str(img).replace('[[base_url]]', settings.IMAGES_URL)
- return s
- else:
- image_id = img['id'].split("image-")[1]
- i = LuxImage.objects.get(pk=image_id)
- caption = False
- exif = False
- cluster_class = None
- extra = None
- if cl[0] == 'cluster':
- css_class = cl[0]
- cluster_class = cl[1]
- try:
- if cl[2] == 'caption':
- caption = True
- elif cl[2] == 'exif':
- exif = True
- else:
- extra = cl[2]
-
- if len(cl) > 3:
- if cl[3] == 'exif':
- exif = True
- except:
- pass
- elif cl[0] != 'cluster' and len(cl) > 1:
- css_class = cl[0]
- if cl[1] == 'caption':
- caption = True
- if cl[1] == 'exif':
- exif = True
- elif cl[0] != 'cluster' and len(cl) > 2:
- css_class = cl[0]
- if cl[1] == 'caption':
- caption = True
- if cl[2] == 'exif':
- exif = True
- print('caption'+str(caption))
- else:
- css_class = cl[0]
- return render_to_string("lib/img_%s.html" % css_class, {'image': i, 'caption': caption, 'exif': exif, 'cluster_class': cluster_class, 'extra':extra})
-
-
-def parse_video(s):
- soup = BeautifulSoup(s, "lxml")
- if soup.find('video'):
- return True
- return False
-