Skip to content

Commit

Permalink
Merge pull request #1128 from flibbertigibbet/feature/kak/multiple-ev…
Browse files Browse the repository at this point in the history
…ent-destinations#1107

Feature/kak/multiple event destinations#1107
  • Loading branch information
flibbertigibbet authored Aug 21, 2019
2 parents 51673de + 2879df3 commit 58fa786
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 9 deletions.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion python/cac_tripplanner/destinations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class EventAdmin(ImageCroppingMixin, admin.ModelAdmin):

fields = ('name', 'website_url', 'description', 'image', 'image_raw', 'wide_image',
'wide_image_raw', 'published', 'priority', 'accessible', 'activities',
'start_date', 'end_date', 'destination')
'start_date', 'end_date', 'destinations')
list_display = ('name', 'published', 'priority', )
actions = ('make_published', 'make_unpublished', )
ordering = ('name', )
Expand Down
6 changes: 3 additions & 3 deletions python/cac_tripplanner/destinations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class Meta:

def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
self.fields['destination'].widget.can_delete_related = False
self.fields['destination'].widget.can_add_related = False
self.fields['destination'].widget.can_change_related = False
self.fields['destinations'].widget.can_delete_related = False
self.fields['destinations'].widget.can_add_related = False
self.fields['destinations'].widget.can_change_related = False

def clean(self):
"""Validate start date is less than end date"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-07-29 18:05
from __future__ import unicode_literals

from django.db import migrations, models


def move_event_destinations_to_multiple(apps, schema_editor):
"""Move event destination to new set of multiple destinations."""
Event = apps.get_model('destinations', 'Event')
events = Event.objects.all()
for event in events:
if event.destination:
event.destinations.add(event.destination)
event.save()


def move_event_destinations_to_single(apps, schema_editor):
"""Move event destination from new set of multiple destinations.
Use the first destination for the event.
"""
Event = apps.get_model('destinations', 'Event')
events = Event.objects.all()
for event in events:
if event.destinations.count() > 0:
event.destination = event.destinations.first()
event.save()


class Migration(migrations.Migration):

dependencies = [
('destinations', '0046_prepopulate_events'),
]

operations = [
migrations.AddField(
model_name='event',
name='destinations',
field=models.ManyToManyField(blank=True, to='destinations.Destination'),
),
migrations.RunPython(move_event_destinations_to_multiple, move_event_destinations_to_single),
migrations.RemoveField(
model_name='event',
name='destination',
),
]
2 changes: 1 addition & 1 deletion python/cac_tripplanner/destinations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Meta:
start_date = models.DateTimeField()
end_date = models.DateTimeField()

destination = models.ForeignKey('Destination', on_delete=models.SET_NULL, null=True, blank=True)
destinations = models.ManyToManyField('Destination', blank=True)

objects = EventManager()

Expand Down
10 changes: 6 additions & 4 deletions python/cac_tripplanner/destinations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,13 @@ def set_event_properties(event):

extra_images = ExtraEventPicture.objects.filter(event=event)
obj = set_attraction_properties(obj, event, extra_images)
# add properties of related destination, if any
obj = set_location_properties(obj, event.destination)
# add properties of first related destination, if any
obj = set_location_properties(obj, event.destinations.first())

# if related destination belongs to Watershed Alliance, so does this event
obj['watershed_alliance'] = event.destination.watershed_alliance if event.destination else False
obj['destinations'] = [set_destination_properties(x) for x in event.destinations.all()]

# if the first related destination belongs to Watershed Alliance, so does this event
obj['watershed_alliance'] = event.destinations.first().watershed_alliance if event.destinations.count() else False
return obj


Expand Down

0 comments on commit 58fa786

Please sign in to comment.