force empty strings to NULL on save (bug 645436, 636824)

This commit is contained in:
Jeff Balogh 2011-03-29 15:45:42 -07:00
Родитель bb26b7a17d
Коммит 19f7f7a8e2
2 изменённых файлов: 18 добавлений и 4 удалений

Просмотреть файл

@ -34,7 +34,7 @@ class BlocklistDetail(amo.models.ModelBase):
return self.name
class BlockId:
class BlocklistBase(object):
@property
def block_id(self):
@ -43,8 +43,15 @@ class BlockId:
def get_url_path(self):
return reverse('blocked.detail', args=[self.block_id])
def save(self, *args, **kw):
for field in self._meta.fields:
if isinstance(field, models.fields.CharField) and field.null:
if getattr(self, field.attname, None) == '':
setattr(self, field.attname, None)
return super(BlocklistBase, self).save(*args, **kw)
class BlocklistItem(BlockId, amo.models.ModelBase):
class BlocklistItem(BlocklistBase, amo.models.ModelBase):
_type = 'i'
guid = models.CharField(max_length=255, blank=True, null=True)
min = models.CharField(max_length=255, blank=True, null=True)
@ -63,7 +70,7 @@ class BlocklistItem(BlockId, amo.models.ModelBase):
return ['/blocklist*'] # no lang/app
class BlocklistPlugin(BlockId, amo.models.ModelBase):
class BlocklistPlugin(BlocklistBase, amo.models.ModelBase):
_type = 'p'
name = models.CharField(max_length=255, blank=True, null=True)
guid = models.CharField(max_length=255, blank=True, null=True)
@ -87,7 +94,7 @@ class BlocklistPlugin(BlockId, amo.models.ModelBase):
return ['/blocklist*'] # no lang/app
class BlocklistGfx(BlockId, amo.models.ModelBase):
class BlocklistGfx(BlocklistBase, amo.models.ModelBase):
_type = 'g'
guid = models.CharField(max_length=255, blank=True, null=True)
os = models.CharField(max_length=255, blank=True, null=True)

Просмотреть файл

@ -51,6 +51,13 @@ class BlocklistItemTest(BlocklistTest):
self.app = BlocklistApp.objects.create(blitem=self.item,
guid=amo.FIREFOX.guid)
def test_empty_string_goes_null_on_save(self):
b = BlocklistItem(guid='guid', min='', max='', os='')
b.save()
assert b.min is None
assert b.max is None
assert b.os is None
def test_lastupdate(self):
bl = self.dom(self.fx4_url).getElementsByTagName('blocklist')[0]
t = datetime.fromtimestamp(int(bl.getAttribute('lastupdate')) / 1000)