comparison membermap/views.py @ 1024:6164cc091649

Perform image_check on membermap messages.
author Brian Neal <bgneal@gmail.com>
date Thu, 10 Dec 2015 21:11:10 -0600
parents 21c592cac71c
children e1c03da72818
comparison
equal deleted inserted replaced
1023:a5ebc74dc3f3 1024:6164cc091649
11 from django.http import HttpResponseBadRequest 11 from django.http import HttpResponseBadRequest
12 from django.http import HttpResponseForbidden 12 from django.http import HttpResponseForbidden
13 from django.views.decorators.http import require_POST 13 from django.views.decorators.http import require_POST
14 from django.core.cache import cache 14 from django.core.cache import cache
15 15
16 from core.html import ImageCheckError
17 from core.html import image_check
18 from core.markup import site_markup
16 from membermap.models import MapEntry 19 from membermap.models import MapEntry
17 from membermap.forms import MapEntryForm 20 from membermap.forms import MapEntryForm
18 21
19 CACHE_KEY = 'membermap_json' 22 CACHE_KEY = 'membermap_json'
20 CACHE_TIMEOUT = 5 * 60 23 CACHE_TIMEOUT = 5 * 60
102 return HttpResponseForbidden('You must be logged in.') 105 return HttpResponseForbidden('You must be logged in.')
103 106
104 loc = request.POST.get('loc', None) 107 loc = request.POST.get('loc', None)
105 lat = request.POST.get('lat', None) 108 lat = request.POST.get('lat', None)
106 lon = request.POST.get('lon', None) 109 lon = request.POST.get('lon', None)
107 msg = request.POST.get('msg', '') 110 msg = request.POST.get('msg', '').strip()
108 111
109 if loc is None or lat is None or lon is None: 112 if loc is None or lat is None or lon is None:
110 return HttpResponseBadRequest('Missing parameters') 113 return HttpResponseBadRequest('Missing parameters')
111 114
112 try: 115 try:
113 lat = float(lat) 116 lat = float(lat)
114 lon = float(lon) 117 lon = float(lon)
115 except ValueError: 118 except ValueError:
116 return HttpResponseBadRequest('Invalid lat/lon') 119 return HttpResponseBadRequest('Invalid lat/lon')
117 120
121 html = ''
122 if msg:
123 html = site_markup(msg)
124 try:
125 image_check(html)
126 except ImageCheckError as ex:
127 return HttpResponseBadRequest(str(ex))
128
118 try: 129 try:
119 entry = MapEntry.objects.get(user=request.user) 130 entry = MapEntry.objects.get(user=request.user)
120 except MapEntry.DoesNotExist: 131 except MapEntry.DoesNotExist:
121 entry = MapEntry(user=request.user) 132 entry = MapEntry(user=request.user)
122 133
123 entry.location = loc 134 entry.location = loc
124 entry.lat = lat 135 entry.lat = lat
125 entry.lon = lon 136 entry.lon = lon
126 entry.message = msg 137 entry.message = msg
127 entry.save() 138 entry.save(html=html)
128 139
129 cache.delete(CACHE_KEY) 140 cache.delete(CACHE_KEY)
130 141
131 avatar_url = None 142 avatar_url = None
132 profile = entry.user.profile 143 profile = entry.user.profile