Changeset 485:a4eac18cbd43
- Timestamp:
- 01/14/10 22:38:16 (2 months ago)
- Author:
- rgrp <http://rufuspollock.org>
- Branch:
- default
- Message:
-
[contrllers/factlet][s]: fix up factlet edit form to work with new coords (should have caught this before but inadequate tests -- 3h or work!).
- Location:
- microfacts
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r481
|
r485
|
|
| 1 | 1 | import logging |
| 2 | | import simplejson as sj |
| | 2 | |
| | 3 | try: |
| | 4 | import json as sj |
| | 5 | except ImportError: |
| | 6 | import simplejson as sj |
| 3 | 7 | import genshi |
| | 8 | import geojson |
| 4 | 9 | |
| 5 | 10 | logger = logging.getLogger(__name__) |
| … |
… |
|
| 178 | 183 | iscommit = request.params.has_key('commit') |
| 179 | 184 | |
| 180 | | c.edit_core = self.edit_core(id) |
| 181 | | |
| 182 | 185 | if not (iscommit or ispreview): |
| | 186 | c.edit_core = self.edit_core(id) |
| 183 | 187 | return render('factlet/edit.html') |
| 184 | 188 | |
| 185 | | |
| 186 | | # works in place |
| 187 | | self.unflatten_form_data(request.params) |
| 188 | | entity_data = dict(request.params) |
| 189 | | |
| 190 | | if ispreview: |
| 191 | | self._preview(entity_data) |
| 192 | | return render('factlet/edit.html') |
| 193 | | |
| 194 | | elif iscommit: |
| | 189 | entity_data = self.unflatten_form_data(request.params) |
| | 190 | # add id in (rgrp 2010-01-14: is no longer in form and hence |
| | 191 | # request.params as of -- should it be?) |
| | 192 | entity_data['id'] = id |
| | 193 | if iscommit: |
| 195 | 194 | mode = EntityPut('/factlet/%s' % id, |
| 196 | 195 | request_data=entity_data, |
| … |
… |
|
| 201 | 200 | else: # errors |
| 202 | 201 | c.error = '%s' % mode.response_code |
| 203 | | return self._preview(entity_data) |
| 204 | | |
| 205 | | def _preview(self, entity_data): |
| 206 | | # to be compatible needs to dump it back to json (even though below we |
| 207 | | # will immediately do simplejson.loads!) |
| 208 | | entity_data = sj.dumps(entity_data) |
| | 202 | return render('factlet/edit.html') |
| | 203 | |
| | 204 | # must be either preview or commit error |
| | 205 | |
| | 206 | json_data = sj.dumps(entity_data) |
| | 207 | c.factlet = self._parse_json_data(json_data) |
| | 208 | assert c.factlet.location |
| | 209 | c.factlet_html = render('factlet/read_core.html') |
| | 210 | # TODO: this is not quite true ... |
| 209 | 211 | # this works because data from the form is same as data from json load |
| 210 | | json_data = entity_data |
| 211 | | c.factlet = self._parse_json_data(json_data) |
| 212 | | c.factlet_html = render('factlet/read_core.html') |
| | 212 | c.edit_core = self.edit_core(id, factlet=c.factlet) |
| | 213 | return render('factlet/edit.html') |
| 213 | 214 | |
| 214 | 215 | def unflatten_form_data(self, formdict): |
| | 216 | # have multiple values in formdict - it's a multidict |
| | 217 | out = dict(formdict) |
| 215 | 218 | # convert back to json string format (not actual domain object) |
| 216 | 219 | coords = formdict.getall('location[geometry][coordinates][]') |
| 217 | | |
| 218 | | try: |
| 219 | | coords[0] = float(coords[0]) if coords[0] else None |
| 220 | | coords[1] = float(coords[1]) if coords[1] else None |
| 221 | | f = geojson.Feature() |
| 222 | | f.geometry = geojson.Point(coordinates=coords) |
| 223 | | print f |
| 224 | | formdict['location'] = f |
| 225 | | except: |
| 226 | | pass |
| 227 | | |
| | 220 | for idx in [0,1]: |
| | 221 | coords[idx] = None if coords[idx] in ['',None] else float(coords[idx]) |
| | 222 | f = geojson.Feature() |
| | 223 | f.geometry = geojson.Point(coordinates=coords) |
| | 224 | # need to be in simple dict format |
| | 225 | out['location'] = dict(f) |
| | 226 | return out |
| 228 | 227 | |
| 229 | 228 | def template(self, id): |
| … |
… |
|
| 266 | 265 | factlet.id = fctid |
| 267 | 266 | return factlet |
| | 267 | |
-
|
r463
|
r485
|
|
| 143 | 143 | newtitle = 'Battle of Austerlitz Modified but Not Saved' |
| 144 | 144 | newstart = '2008-08-22' |
| | 145 | newcoords = [0.0,1.0] |
| 145 | 146 | form['title'] = newtitle |
| 146 | 147 | form['start'] = newstart |
| | 148 | form.set('location[geometry][coordinates][]', newcoords[0], index=0) |
| | 149 | form.set('location[geometry][coordinates][]', newcoords[1], index=1) |
| 147 | 150 | response = form.submit('preview') |
| 148 | 151 | assert newtitle in response, response |
| 149 | | assert newstart in response |
| | 152 | assert newstart in response, response |
| | 153 | assert newcoords[1] in response, response |
| 150 | 154 | form = response.forms[0] |
| 151 | 155 | response = form.submit('commit', status=[200,404,302]) |
| … |
… |
|
| 154 | 158 | fct = model.Factlet.query.filter_by(id=factletId).first() |
| 155 | 159 | assert fct.title == newtitle |
| | 160 | assert fct.start == newstart |
| | 161 | assert fct.location.geometry.coordinates == newcoords |
| 156 | 162 | |
| 157 | 163 | def test_edit_core(self): |