Changeset 485:a4eac18cbd43

Show
Ignore:
Timestamp:
01/14/10 22:38:16 (8 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:
2 modified

Legend:

Unmodified
Added
Removed
  • microfacts/controllers/factlet.py

    r481 r485  
    11import logging 
    2 import simplejson as sj 
     2 
     3try: 
     4    import json as sj 
     5except ImportError: 
     6    import simplejson as sj 
    37import genshi 
     8import geojson 
    49 
    510logger = logging.getLogger(__name__) 
     
    178183        iscommit = request.params.has_key('commit') 
    179184         
    180         c.edit_core = self.edit_core(id) 
    181          
    182185        if not (iscommit or ispreview): 
     186            c.edit_core = self.edit_core(id) 
    183187            return render('factlet/edit.html') 
    184188         
    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: 
    195194            mode = EntityPut('/factlet/%s' % id, 
    196195                request_data=entity_data, 
     
    201200            else: # errors 
    202201                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 ... 
    209211        # 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') 
    213214 
    214215    def unflatten_form_data(self, formdict): 
     216        # have multiple values in formdict - it's a multidict 
     217        out = dict(formdict) 
    215218        # convert back to json string format (not actual domain object) 
    216219        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 
    228227 
    229228    def template(self, id): 
     
    266265            factlet.id = fctid 
    267266        return factlet  
     267 
  • microfacts/tests/functional/test_factlet.py

    r463 r485  
    143143        newtitle = 'Battle of Austerlitz Modified but Not Saved' 
    144144        newstart = '2008-08-22' 
     145        newcoords = [0.0,1.0] 
    145146        form['title'] = newtitle 
    146147        form['start'] = newstart 
     148        form.set('location[geometry][coordinates][]', newcoords[0], index=0) 
     149        form.set('location[geometry][coordinates][]', newcoords[1], index=1) 
    147150        response = form.submit('preview') 
    148151        assert newtitle in response, response 
    149         assert newstart in response 
     152        assert newstart in response, response 
     153        assert newcoords[1] in response, response 
    150154        form = response.forms[0] 
    151155        response = form.submit('commit', status=[200,404,302]) 
     
    154158        fct = model.Factlet.query.filter_by(id=factletId).first() 
    155159        assert fct.title == newtitle 
     160        assert fct.start == newstart 
     161        assert fct.location.geometry.coordinates == newcoords 
    156162 
    157163    def test_edit_core(self):