From ce5987de9528da4d891a9c4870b7c61eb632a551 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH] Attach establishment timestamps as command --- apps/establishment/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../commands/attach_establishments_tz.py | 23 +++++++++++++++++++ .../migrations/0040_establishment_tz.py | 12 ---------- 4 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 apps/establishment/management/__init__.py create mode 100644 apps/establishment/management/commands/__init__.py create mode 100644 apps/establishment/management/commands/attach_establishments_tz.py diff --git a/apps/establishment/management/__init__.py b/apps/establishment/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/establishment/management/commands/__init__.py b/apps/establishment/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/establishment/management/commands/attach_establishments_tz.py b/apps/establishment/management/commands/attach_establishments_tz.py new file mode 100644 index 00000000..58be04cb --- /dev/null +++ b/apps/establishment/management/commands/attach_establishments_tz.py @@ -0,0 +1,23 @@ +from django.core.management.base import BaseCommand +from pytz import timezone as py_tz +from timezonefinder import TimezoneFinder +from establishment.models import Establishment + + +class Command(BaseCommand): + help = 'Attach correct timestamps according to coordinates to existing establishments' + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.tf = TimezoneFinder(in_memory=True) + + def handle(self, *args, **options): + for establishment in Establishment.objects.prefetch_related('address').all(): + if establishment.address and establishment.address.latitude and establishment.address.longitude: + establishment.tz = py_tz(self.tf.certain_timezone_at(lng=establishment.address.longitude, + lat=establishment.address.latitude)) + establishment.save() + self.stdout.write(self.style.SUCCESS(f'Attached timezone {establishment.tz} to {establishment}')) + else: + self.stdout.write(self.style.WARNING(f'Establishment {establishment} has no coordinates' + f'passing...')) diff --git a/apps/establishment/migrations/0040_establishment_tz.py b/apps/establishment/migrations/0040_establishment_tz.py index d5a383d8..75f54c06 100644 --- a/apps/establishment/migrations/0040_establishment_tz.py +++ b/apps/establishment/migrations/0040_establishment_tz.py @@ -3,17 +3,6 @@ import timezone_field.fields from django.db import migrations from django.conf import settings -from timezonefinder import TimezoneFinder -from establishment.models import Establishment - - -def fill_timezones(apps, schema_editor): - tf = TimezoneFinder(in_memory=True) - for establishment in Establishment.objects.prefetch_related('address').all(): - if establishment.address and establishment.address.latitude and establishment.address.longitude: - establishment.tz = tf.certain_timezone_at(lng=establishment.address.longitude, - lat=establishment.address.latitude) - establishment.save() class Migration(migrations.Migration): @@ -29,5 +18,4 @@ class Migration(migrations.Migration): name='tz', field=timezone_field.fields.TimeZoneField(default=settings.TIME_ZONE), ), - migrations.RunPython(fill_timezones, migrations.RunPython.noop), ]