Django

Code

Changeset 6708

Show
Ignore:
Timestamp:
11/20/07 09:13:55 (8 months ago)
Author:
jbronn
Message:

gis: Fixed DecimalField? verification in LayerMapping?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/tests/layermap/models.py

    r6687 r6708  
    1111class Interstate(models.Model): 
    1212    name = models.CharField(max_length=20) 
    13     length = models.DecimalField(max_digits=7, decimal_places=2) 
     13    length = models.DecimalField(max_digits=6, decimal_places=2) 
    1414    path = models.LineStringField() 
    1515    objects = models.GeoManager() 
  • django/branches/gis/django/contrib/gis/tests/layermap/tests.py

    r6687 r6708  
    8989        lm.save() 
    9090 
    91         # Only one interstate should have imported correctly. 
    92         self.assertEqual(1, Interstate.objects.count()) 
     91        # Two interstate should have imported correctly. 
     92        self.assertEqual(2, Interstate.objects.count()) 
    9393         
    94         # Verifying the values in the single feature w/the model. 
     94        # Verifying the values in the layer w/the model. 
    9595        ds = DataSource(inter_shp) 
    96         feat = ds[0][0] 
    97         istate = Interstate.objects.get(name=feat['Name'].value) 
    98         self.assertEqual(Decimal(str(feat['Length'])), istate.length) 
    99         for p1, p2 in zip(feat.geom, istate.path): 
    100             self.assertAlmostEqual(p1[0], p2[0], 6) 
    101             self.assertAlmostEqual(p1[1], p2[1], 6) 
     96 
     97        # Only the first two features of this shapefile are valid. 
     98        valid_feats = ds[0][:2] 
     99        for feat in valid_feats: 
     100            istate = Interstate.objects.get(name=feat['Name'].value) 
     101             
     102            if feat.fid == 0: 
     103                self.assertEqual(Decimal(str(feat['Length'])), istate.length) 
     104            elif feat.fid == 1: 
     105                # Everything but the first two decimal digits were truncated, 
     106                # because the Interstate model's `length` field has decimal_places=2. 
     107                self.assertAlmostEqual(feat.get('Length'), float(istate.length), 2) 
     108 
     109            for p1, p2 in zip(feat.geom, istate.path): 
     110                self.assertAlmostEqual(p1[0], p2[0], 6) 
     111                self.assertAlmostEqual(p1[1], p2[1], 6) 
    102112 
    103113def suite(): 
  • django/branches/gis/django/contrib/gis/utils/layermapping.py

    r6687 r6708  
    395395            except: 
    396396                raise InvalidDecimal('Could not construct decimal from: %s' % fld) 
     397 
     398            # Getting the decimal value as a tuple. 
    397399            dtup = d.as_tuple() 
    398             if len(dtup[1]) > field_class.max_digits: 
    399                 raise InvalidDecimal('More than the maximum # of digits encountered.') 
    400             elif len(dtup[1][dtup[2]:]) > field_class.decimal_places: 
    401                 raise InvalidDecimal('More than the maximum # of decimal places encountered.') 
     400            digits = dtup[1] 
     401            d_idx = dtup[2] # index where the decimal is 
     402 
     403            # Maximum amount of precision, or digits to the left of the decimal. 
     404            max_prec = field_class.max_digits - field_class.decimal_places 
     405 
     406            # Getting the digits to the left of the decimal place for the  
     407            # given decimal. 
     408            if d_idx < 0: 
     409                n_prec = len(digits[:d_idx]) 
     410            else: 
     411                n_prec = len(digits) + d_idx 
     412 
     413            # If we have more than the maximum digits allowed, then throw an  
     414            # InvalidDecimal exception. 
     415            if n_prec > max_prec: 
     416                raise InvalidDecimal('A DecimalField with max_digits %d, decimal_places %d must round to an absolute value less than 10^%d.' % 
     417                                     (field_class.max_digits, field_class.decimal_places, max_prec)) 
    402418            val = d 
    403419        else: 
     
    468484                        print 'Ignoring Feature ID %s because: %s' % (feat.fid, msg) 
    469485                else: 
    470                     # Constructing the model using the constructed keyword args 
     486                    # Constructing the model using the keyword args 
    471487                    if all_prepped: 
    472488                        m = self.model(**kwargs)