Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace timezonefinder with tzfpy #176

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions osmcal/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.views import View
from osmcal import views
from pytz import timezone as tzp
from timezonefinder import TimezoneFinder
from tzfpy import get_tz

from . import serializers
from .decorators import ALLOWED_HEADERS, cors_any, language_from_header
Expand All @@ -13,8 +13,6 @@
"application/json; charset=" + settings.DEFAULT_CHARSET
) # This shall be utf-8, otherwise we're not friends anymore.

tf = TimezoneFinder()


class CORSOptionsMixin(object):
def options(self, request, *args, **kwargs):
Expand All @@ -31,7 +29,9 @@ def get_serializer(self):
@cors_any
@language_from_header
def get(self, request, *args, **kwargs):
es = self.get_serializer()(self.get_queryset(request.GET), context={"request": request})
es = self.get_serializer()(
self.get_queryset(request.GET), context={"request": request}
)
return HttpResponse(es.json, content_type=JSON_CONTENT_TYPE)


Expand All @@ -49,7 +49,7 @@ class Timezone(View):
def get(self, request, *args, **kwargs):
lat = float(request.GET["lat"])
lon = float(request.GET["lon"])
tz = tf.timezone_at(lng=lon, lat=lat)
tz = get_tz(lon, lat)
if tz is None:
return HttpResponse("", status=400)
return HttpResponse(tzp(tz))
17 changes: 6 additions & 11 deletions osmcal/migrations/0027_timezone_data.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from django.db import migrations
from pytz import timezone
from timezonefinder import TimezoneFinder
from tzfpy import get_tz


def set_timezones(apps, schema_editor):
tf = TimezoneFinder()
Event = apps.get_model('osmcal', 'Event')
Event = apps.get_model("osmcal", "Event")
for event in Event.objects.filter(timezone=None):
if event.location:
tz = tf.timezone_at(lng=event.location.x, lat=event.location.y)
tz = get_tz(event.location.x, event.location.y)
if tz is not None:
event.timezone = tz
if not event.timezone:
# Either time zone could not be determined from location or no location is available.
event.timezone = 'UTC'
event.timezone = "UTC"
tz = timezone(event.timezone)
event.start = tz.localize(event.start.replace(tzinfo=None))

Expand All @@ -24,10 +23,6 @@ def set_timezones(apps, schema_editor):

class Migration(migrations.Migration):

dependencies = [
('osmcal', '0026_event_timezone'),
]
dependencies = [("osmcal", "0026_event_timezone")]

operations = [
migrations.RunPython(set_timezones)
]
operations = [migrations.RunPython(set_timezones)]
51 changes: 38 additions & 13 deletions osmcal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
from django.utils.text import Truncator
from pytz import timezone
from sentry_sdk import add_breadcrumb
from timezonefinder import TimezoneFinder

tf = TimezoneFinder()
from tzfpy import get_tz


class EventType(Enum):
Expand All @@ -38,7 +36,9 @@ class Event(models.Model):
location_address = models.JSONField(blank=True, null=True)

link = models.URLField(blank=True, null=True)
kind = models.CharField(max_length=4, choices=[(x.name, x.value) for x in EventType])
kind = models.CharField(
max_length=4, choices=[(x.name, x.value) for x in EventType]
)
description = models.TextField(
blank=True,
null=True,
Expand Down Expand Up @@ -70,7 +70,12 @@ def geocode_location(self):
def _geocode_location(self):
nr = requests.get(
"https://nominatim.openstreetmap.org/reverse",
params={"format": "jsonv2", "lat": self.location.y, "lon": self.location.x, "accept-language": "en"},
params={
"format": "jsonv2",
"lat": self.location.y,
"lon": self.location.x,
"accept-language": "en",
},
headers={"User-Agent": "osmcal"},
)
nr.raise_for_status()
Expand All @@ -93,7 +98,13 @@ def location_text(self):
return ", ".join(
filter(
lambda x: x is not None,
[addr.get("village"), addr.get("town"), addr.get("city"), addr.get("state"), addr.get("country")],
[
addr.get("village"),
addr.get("town"),
addr.get("city"),
addr.get("state"),
addr.get("country"),
],
)
)

Expand Down Expand Up @@ -163,25 +174,33 @@ class AnswerType(Enum):


class ParticipationQuestion(models.Model):
event = models.ForeignKey("Event", null=True, on_delete=models.SET_NULL, related_name="questions")
event = models.ForeignKey(
"Event", null=True, on_delete=models.SET_NULL, related_name="questions"
)
question_text = models.CharField(max_length=200)
answer_type = models.CharField(max_length=4, choices=[(x.name, x.value) for x in AnswerType])
answer_type = models.CharField(
max_length=4, choices=[(x.name, x.value) for x in AnswerType]
)
mandatory = models.BooleanField(default=True)

class Meta:
ordering = ("event", "id")


class ParticipationQuestionChoice(models.Model):
question = models.ForeignKey(ParticipationQuestion, related_name="choices", on_delete=models.CASCADE)
question = models.ForeignKey(
ParticipationQuestion, related_name="choices", on_delete=models.CASCADE
)
text = models.CharField(max_length=200)

class Meta:
ordering = ("question", "id")


class EventParticipation(models.Model):
event = models.ForeignKey("Event", null=True, on_delete=models.SET_NULL, related_name="participation")
event = models.ForeignKey(
"Event", null=True, on_delete=models.SET_NULL, related_name="participation"
)
user = models.ForeignKey("User", null=True, on_delete=models.SET_NULL)
added_on = models.DateTimeField(auto_now_add=True, null=True)

Expand All @@ -190,12 +209,18 @@ class Meta:


class ParticipationAnswer(models.Model):
question = models.ForeignKey(ParticipationQuestion, on_delete=models.CASCADE, related_name="answers")
question = models.ForeignKey(
ParticipationQuestion, on_delete=models.CASCADE, related_name="answers"
)
user = models.ForeignKey("User", null=True, on_delete=models.SET_NULL)
answer = models.CharField(max_length=200)

class Meta:
constraints = (models.UniqueConstraint(fields=("question", "user"), name="unique_question_answer"),)
constraints = (
models.UniqueConstraint(
fields=("question", "user"), name="unique_question_answer"
),
)


class EventLog(models.Model):
Expand All @@ -217,7 +242,7 @@ class User(AbstractUser):
def home_timezone(self):
if not self.home_location:
return None
return tf.timezone_at(lng=self.home_location.x, lat=self.home_location.y)
return tf.get_tz(self.home_location.x, self.home_location.y)

def save(self, *args, **kwargs):
if not self.username:
Expand Down
Loading
Loading