Parcourir la source

Fix existing data for Django use

Weird issue with empty FloatField
Jocelyn Delande il y a 9 ans
Parent
commit
1ac6ee5d02

+ 16 - 0
wifiwithme/apps/contribmap/fixtures/bottle_data.yaml

@@ -0,0 +1,16 @@
+- model: contribmap.contrib
+  pk: 1
+  fields: {name: Bill, contrib_type: share, latitude: 47.218371, longitude: -1.553621,
+    phone: '', email: a@example.com, access_type: fiber, connect_local: false, connect_internet: false,
+    bandwidth: 3.0, share_part: 2.0, floor: null, floor_total: null, orientations: '[''N'',
+      ''NO'', ''O'', ''SO'', ''S'', ''SE'', ''E'', ''NE'']', roof: false, comment: '',
+    privacy_name: false, privacy_email: false, privacy_coordinates: true, privacy_place_details: true,
+    privacy_comment: false, date: 'Thu, 03 Mar 2016 12:26:39 -0000'}
+- model: contribmap.contrib
+  pk: 2
+  fields: {name: veau, contrib_type: connect, latitude: 47.2195681123155, longitude: -1.5398025512695312,
+    phone: '0101010101', email: '', access_type: '', connect_local: true, connect_internet: false,
+    bandwidth: null, share_part: null, floor: 1, floor_total: 2, orientations: '[''SE'']',
+    roof: false, comment: '', privacy_name: false, privacy_email: false, privacy_coordinates: false,
+    privacy_place_details: false, privacy_comment: false, date: 'Thu, 03 Mar 2016
+      12:27:24 -0000'}

+ 20 - 0
wifiwithme/apps/contribmap/migrations/0003_auto_20160303_1057.py

@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.3 on 2016-03-03 10:57
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+    """Empty floatfield are stored as empty string by sqlite. But Django hates
+    that and refuse to handle it properly until they are stored as NULL."""
+
+    dependencies = [
+        ('contribmap', '0001_initial'),
+    ]
+    float_cols = ['bandwidth', 'share_part', 'floor', 'floor_total']
+    operations = [
+        migrations.RunSQL(
+            "UPDATE contribs SET {col}=NULL WHERE {col}='';".format(col=i))
+        for i in float_cols
+    ]

+ 10 - 1
wifiwithme/apps/contribmap/tests.py

@@ -1,3 +1,12 @@
 from django.test import TestCase
 
-# Create your tests here.
+from contribmap.models import Contrib
+
+
+class TestDataImport(TestCase):
+    fixtures = ['bottle_data.yaml']
+
+    def test_re_save(self):
+        for contrib in Contrib.objects.all():
+            contrib.full_clean()
+            contrib.save()