Attach establishment timestamps as command

This commit is contained in:
Kuroshini 2019-10-16 14:11:07 +03:00
parent d5609e4cb8
commit ce5987de95
4 changed files with 23 additions and 12 deletions

View File

@ -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...'))

View File

@ -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),
]