1 | # -*- coding: utf-8 -*-
|
---|
2 | from __future__ import unicode_literals
|
---|
3 | from django.db import models, migrations, transaction
|
---|
4 | import datetime
|
---|
5 |
|
---|
6 | # Python code to calculate call end times, because we can't do it in SQL
|
---|
7 | CHUNK_SIZE = 1000
|
---|
8 | UPDATE_FIELDS = ['call_end_utc']
|
---|
9 | def calculate_end_time(apps, schema_editor):
|
---|
10 | transaction.set_autocommit(False)
|
---|
11 | Cdr = apps.get_model("phx", "Cdr")
|
---|
12 | last_chunk = 0
|
---|
13 | new_items = True
|
---|
14 | while new_items:
|
---|
15 | new_items = False
|
---|
16 | cdr = None
|
---|
17 | for cdr in Cdr.objects.all()[last_chunk:last_chunk+CHUNK_SIZE]:
|
---|
18 | if cdr.call_end_utc is None:
|
---|
19 | cdr.call_end_utc = cdr.call_utc + datetime.timedelta(seconds=cdr.exact_call_duration)
|
---|
20 | cdr.save(update_fields=UPDATE_FIELDS)
|
---|
21 | transaction.commit()
|
---|
22 | if cdr is not None:
|
---|
23 | new_items = True
|
---|
24 | last_chunk += CHUNK_SIZE
|
---|
25 |
|
---|
26 | def do_nothing(apps, schema_editor):
|
---|
27 | pass # we need a callable for the reverse direction, so we use this.
|
---|
28 |
|
---|
29 |
|
---|
30 | class Migration(migrations.Migration):
|
---|
31 |
|
---|
32 | dependencies = [
|
---|
33 | ('phx', '0007_cdr_call_end_utc'),
|
---|
34 | ]
|
---|
35 |
|
---|
36 | operations = [
|
---|
37 | migrations.RunPython(calculate_end_time, do_nothing, atomic=False)
|
---|
38 |
|
---|
39 | #migrations.AlterField(
|
---|
40 | # model_name='cdr',
|
---|
41 | # name='call_end_utc',
|
---|
42 | # field=models.DateTimeField(db_index=True, help_text='UTC Timestamp when call disconnected.'),
|
---|
43 | #),
|
---|
44 | ]
|
---|