Attach establishment timestamps as command
This commit is contained in:
parent
d5609e4cb8
commit
ce5987de95
0
apps/establishment/management/__init__.py
Normal file
0
apps/establishment/management/__init__.py
Normal file
0
apps/establishment/management/commands/__init__.py
Normal file
0
apps/establishment/management/commands/__init__.py
Normal 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...'))
|
||||||
|
|
@ -3,17 +3,6 @@
|
||||||
import timezone_field.fields
|
import timezone_field.fields
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
from django.conf import settings
|
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):
|
class Migration(migrations.Migration):
|
||||||
|
|
@ -29,5 +18,4 @@ class Migration(migrations.Migration):
|
||||||
name='tz',
|
name='tz',
|
||||||
field=timezone_field.fields.TimeZoneField(default=settings.TIME_ZONE),
|
field=timezone_field.fields.TimeZoneField(default=settings.TIME_ZONE),
|
||||||
),
|
),
|
||||||
migrations.RunPython(fill_timezones, migrations.RunPython.noop),
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user