2016-09-22 13:19:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Generated by Django 1.10.1 on 2016-09-22 11:08
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.db import migrations, models
|
|
|
|
|
|
|
|
|
|
|
|
def convert_duration(apps, schema_editor):
|
2016-10-24 13:09:23 +02:00
|
|
|
"""
|
|
|
|
Converts the values of the old duration CharField to new duration
|
|
|
|
IntegerField. It uses the temporary field for proper renaming the field
|
|
|
|
in the end.
|
|
|
|
"""
|
|
|
|
Item = apps.get_model('agenda', 'Item')
|
2016-09-22 13:19:11 +02:00
|
|
|
for item in Item.objects.all():
|
|
|
|
duration = item.duration
|
|
|
|
item.duration_tmp = None
|
|
|
|
if is_int(duration):
|
2016-10-24 13:09:23 +02:00
|
|
|
# Assuming that these are minutes.
|
2016-09-22 13:19:11 +02:00
|
|
|
item.duration_tmp = int(duration)
|
2016-09-23 14:42:25 +02:00
|
|
|
elif isinstance(duration, str):
|
2016-10-24 13:09:23 +02:00
|
|
|
# Assuming format (h)h:(m)m. If not, new value is None.
|
2016-09-22 13:19:11 +02:00
|
|
|
split = duration.split(':')
|
|
|
|
if len(split) == 2 and is_int(split[0]) and is_int(split[1]):
|
2016-10-24 13:09:23 +02:00
|
|
|
# Calculate new duration: hours * 60 + minutes.
|
2016-09-22 13:19:11 +02:00
|
|
|
item.duration_tmp = int(split[0]) * 60 + int(split[1])
|
2016-10-24 10:42:34 +02:00
|
|
|
item.save(skip_autoupdate=True)
|
2016-09-22 13:19:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_int(s):
|
2016-10-24 13:09:23 +02:00
|
|
|
"""
|
|
|
|
Short helper for duration conversion.
|
|
|
|
"""
|
2016-09-22 13:19:11 +02:00
|
|
|
try:
|
|
|
|
int(s)
|
2016-09-23 14:42:25 +02:00
|
|
|
except (ValueError, TypeError):
|
2016-09-22 13:19:11 +02:00
|
|
|
return False
|
2016-09-22 21:43:49 +02:00
|
|
|
else:
|
|
|
|
return True
|
2016-09-22 13:19:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('agenda', '0001_initial'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.AddField(
|
|
|
|
model_name='item',
|
|
|
|
name='duration_tmp',
|
|
|
|
field=models.IntegerField(blank=True, null=True),
|
|
|
|
),
|
2016-10-24 13:09:23 +02:00
|
|
|
migrations.RunPython(
|
|
|
|
convert_duration
|
|
|
|
),
|
2016-09-22 13:19:11 +02:00
|
|
|
migrations.RemoveField(
|
|
|
|
model_name='item',
|
|
|
|
name='duration',
|
|
|
|
),
|
|
|
|
migrations.RenameField(
|
|
|
|
model_name='item',
|
|
|
|
old_name='duration_tmp',
|
|
|
|
new_name='duration',
|
|
|
|
),
|
|
|
|
]
|