Skip to content
Draft
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
27 changes: 14 additions & 13 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", get_random_secret_key())
DYNAMIC_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "127.0.0.1,localhost").split(",")
DYNAMIC_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS",
"127.0.0.1,localhost").split(",")

if DEVELOPMENT_MODE:
STATIC_HOSTS = ['localhost']
Expand Down Expand Up @@ -178,14 +179,14 @@
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'authentication.middleware.HttpOnlyTokenAuthentication'
],
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
],
],
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 100,
}
Expand All @@ -196,11 +197,11 @@
CORS_ALLOWED_ORIGINS = [os.getenv('CLIENT_ORIGIN')]

CORS_ALLOW_HEADERS = [
'accept-encoding',
'authorization',
'content-disposition',
'content-type', 'accept',
'origin',
'accept-encoding',
'authorization',
'content-disposition',
'content-type', 'accept',
'origin',
]

SESSION_COOKIE_PATH = '/;HttpOnly'
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ services:
- [email protected]
depends_on:
- db
platform: linux/amd64
23 changes: 23 additions & 0 deletions events/migrations/0019_event_latitude_event_longitude.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.3 on 2022-09-15 02:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('events', '0018_alter_event_image_url'),
]

operations = [
migrations.AddField(
model_name='event',
name='latitude',
field=models.DecimalField(decimal_places=4, max_digits=6, null=True),
),
migrations.AddField(
model_name='event',
name='longitude',
field=models.DecimalField(decimal_places=4, max_digits=7, null=True),
),
]
22 changes: 20 additions & 2 deletions events/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from django.db import models
from django.conf import settings
from django.utils.html import format_html
from django.db.models.signals import pre_save
from django.dispatch import receiver
from geopy.geocoders import Nominatim


def get_coordinates(location, region):
geolocator = Nominatim(user_agent='data-umbrella/event-board-api')
geolocation = geolocator.geocode(f'{location},{region}')
return (geolocation.latitude, geolocation.longitude)


class Event(models.Model):
Expand All @@ -26,6 +35,8 @@ class Event(models.Model):
end_time = models.CharField(max_length=200, blank=True, null=True)
location = models.TextField(blank=True, null=True)
region = models.CharField(max_length=200, blank=True, null=True)
latitude = models.DecimalField(max_digits=6, decimal_places=4, null=True)
longitude = models.DecimalField(max_digits=7, decimal_places=4, null=True)
in_person = models.BooleanField(blank=True, null=True)
virtual = models.BooleanField(blank=True, null=True)
cfp_due_date = models.DateField(blank=True, null=True)
Expand All @@ -44,7 +55,14 @@ class Event(models.Model):

def __str__(self):
return self.event_name

def review_link(self):
url = f"https://events.dataumbrella.org/events/{self.id}/details"
return format_html(f'<a href="{url}" target="_blank">Review</a>', url=url)
return format_html(f'<a href="{url}" target="_blank">Review</a>', url=url)


@receiver(pre_save, sender=Event)
def save_coordinates(sender, instance, **kwargs):
coords = get_coordinates(instance.location, instance.region)
instance.latitude = coords[0]
instance.longitude = coords[1]
19 changes: 19 additions & 0 deletions events/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from events.models import Event
from django.db.models.signals import pre_save
from django.dispatch import receiver
from geopy.geocoders import Nominatim


def get_coordinates(location, region):
geolocator = Nominatim(user_agent='data-umbrella/event-board-api')
geolocation = geolocator.geocode(f'{location},{region}')
return (geolocation.latitude, geolocation.longitude)


@receiver(pre_save, sender=Event)
def save_coordinates(sender, instance, **kwargs):
# import pdb
# pdb.set_trace()
coords = get_coordinates(instance.location, instance.region)
instance.latitude = coords[0]
instance.longitude = coords[1]
14 changes: 13 additions & 1 deletion events/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def test_event_initialization(self):
event.save()
self.assertEqual(event.organization_name, 'Example org name')


def test_event_validations(self):
event = Event.objects.create(
event_name=None,
Expand All @@ -34,3 +33,16 @@ def test_event_validations(self):
error.exception.message_dict['event_name'],
['This field cannot be blank.'],
)

def test_event_initialization(self):
event = Event.objects.create(
event_name='Example Title',
description='Example Description',
featured=False,
organization_name='Example org name',
location="Orlando",
region="Florida"
)

event.save()
self.assertEqual(event.latitude, 'Example org name')
9 changes: 5 additions & 4 deletions events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class ListEvent(generics.ListCreateAPIView):
queryset = Event.objects.all()
serializer_class = EventSerializer
permission_classes = [
IsAuthenticatedOrReadOnly
IsAuthenticatedOrReadOnly
]
filter_backends = [
SearchFilter,
DjangoFilterBackend
SearchFilter,
DjangoFilterBackend
]
search_fields = [
'event_name',
Expand All @@ -30,7 +30,7 @@ class ListEvent(generics.ListCreateAPIView):
'featured': ['exact'],
'published': ['exact'],
'region': ['exact'],
'start_date':['gte', 'lte', 'exact', 'gt', 'lt'],
'start_date': ['gte', 'lte', 'exact', 'gt', 'lt'],
}


Expand All @@ -43,6 +43,7 @@ class DetailEvent(generics.RetrieveUpdateDestroyAPIView):
"""
Concrete view for updating a model instance.
"""

def put(self, request, *args, **kwargs):
try:
instance = self.get_object()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Faker==13.15.1
boto3==1.18.36
django-storages==1.11.1
Pillow==9.2.0
geopy==2.2.0