diff --git a/src/program/admin.py b/src/program/admin.py index 7fc11b05..eebfa209 100644 --- a/src/program/admin.py +++ b/src/program/admin.py @@ -1,6 +1,20 @@ -from django.contrib import admin +from django.contrib import ( + admin, + messages +) +from django.core.exceptions import ValidationError -from .models import Event, Speaker, EventType, EventInstance, EventLocation, SpeakerProposal, EventProposal, Favorite + +from .models import ( + Event, + Speaker, + EventType, + EventInstance, + EventLocation, + SpeakerProposal, + EventProposal, + Favorite +) @admin.register(SpeakerProposal) @@ -18,7 +32,18 @@ class SpeakerProposalAdmin(admin.ModelAdmin): class EventProposalAdmin(admin.ModelAdmin): def mark_eventproposal_as_approved(self, request, queryset): for ep in queryset: - ep.mark_as_approved() + if not ep.speakers.all(): + messages.error( + request, + 'Event cant be approved as it has no speaker(s).' + ) + return False + else: + try: + ep.mark_as_approved() + except ValidationError as e: + messages.error(request, e) + return False mark_eventproposal_as_approved.description = 'Approve and create Event object(s)' actions = ['mark_eventproposal_as_approved'] diff --git a/src/program/models.py b/src/program/models.py index a89197ee..1f8449a8 100644 --- a/src/program/models.py +++ b/src/program/models.py @@ -9,6 +9,7 @@ from datetime import timedelta from django.contrib.postgres.fields import DateTimeRangeField from django.contrib import messages from django.db import models +from django.core.exceptions import ObjectDoesNotExist from django.dispatch import receiver from django.utils.text import slugify from django.utils import timezone @@ -278,8 +279,10 @@ class EventProposal(UserSubmittedModel): event.save() # loop through the speakerproposals linked to this eventproposal and associate any related speaker objects with this event for sp in self.speakers.all(): - if sp.speaker: + try: event.speakers.add(sp.speaker) + except ObjectDoesNotExist: + raise ValidationError('Not all speakers are approved or created yet.') self.proposal_status = eventproposalmodel.PROPOSAL_APPROVED self.save()