Commit 9920f560 authored by fred's avatar fred

allow episode owner to add diffusions

parent 353c8c88
......@@ -263,6 +263,23 @@ class SoundFileEditForm(forms.ModelForm):
self.fields.keyOrder = ['title', 'format', 'podcastable', 'fragment', 'license']
class DiffusionDateTimeWidget(forms.Select):
def __init__(self, emission, *args, **kwargs):
super().__init__(*args, **kwargs)
self.choices = list(self.options(emission))
def options(self, emission):
dates = []
for i in range(10):
if dates:
since = dates[-1] + datetime.timedelta(minutes=10)
else:
since = datetime.datetime.now() - datetime.timedelta(days=1)
dates.append(emission.get_next_planned_date(since))
for date in dates:
yield (date, date.strftime('%d/%m/%Y %H:%M'))
class DiffusionForm(forms.ModelForm):
class Meta:
model = Diffusion
......@@ -272,6 +289,18 @@ class DiffusionForm(forms.ModelForm):
'datetime': DateTimeWidget(),
}
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
episode = kwargs.get('initial', {}).get('episode')
emission = episode.emission
if (user and user.has_perm('emissions.add_diffusion')) or not emission.schedule_set.exists():
date = emission.get_next_planned_date()
self.fields['datetime'].initial = date
self.fields['datetime'].widget = DateTimeWidget()
else:
self.fields['datetime'].widget = DiffusionDateTimeWidget(emission)
class NewsItemForm(forms.ModelForm):
class Meta:
......
......@@ -396,14 +396,17 @@ class EpisodeAddDiffusionView(CreateView):
}
def get_form(self, *args, **kwargs):
if not self.request.user.has_perm('emissions.add_diffusion'):
raise PermissionDenied()
episode = Episode.objects.get(slug=self.kwargs.get('slug'),
emission__slug=self.kwargs.get('emission_slug'))
if not self.request.user.can_manage(episode):
raise PermissionDenied()
return super(EpisodeAddDiffusionView, self).get_form(*args, **kwargs)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def get_success_url(self):
messages.success(self.request, SUCCESS_MESSAGE)
return self.object.episode.get_absolute_url()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment