trentpalmerdotorg/audio/episode_views.py

81 lines
2.7 KiB
Python
Raw Normal View History

2021-02-24 20:13:54 -08:00
from django.shortcuts import render, redirect
2021-03-13 08:50:04 -08:00
from django.contrib import messages
2021-02-24 20:13:54 -08:00
from .forms import EpisodeForm
from .models import Feed, Episode, rand_slug
from django.utils.text import slugify
2021-02-24 20:13:54 -08:00
def edit_episode(request, pk, title_slug):
if not request.user.is_authenticated:
return redirect('audio:home')
slug = title_slug
2021-02-24 20:13:54 -08:00
episode = Episode.objects.get(id=pk)
otitle = episode.title
2021-02-24 20:13:54 -08:00
if not episode.user == request.user:
return redirect('audio:home')
if request.method == "POST":
form = EpisodeForm(request.POST, request.FILES, instance=episode)
if form.is_valid():
nepisode = form.save(commit=False)
if nepisode.title != otitle:
nepisode.slug = slugify(rand_slug() + "-" + nepisode.title)
slug = nepisode.slug
nepisode.save()
return redirect('audio:episode', pk=pk, slug=slug)
2021-02-24 20:13:54 -08:00
else:
form = EpisodeForm(instance=episode)
return render(
request, 'base_form.html',
{
'form': form,
'heading': 'Edit Episode?',
'title': 'Edit Episode?',
'submit': 'save',
'form_data': 'TRUE',
2021-03-13 08:50:04 -08:00
'edit_episode': True,
'episode_pk': pk,
2021-02-24 20:13:54 -08:00
})
2021-03-13 08:50:04 -08:00
def confirm_delete_episode(request, pk):
if not request.user.is_authenticated:
return redirect('audio:home')
episode = Episode.objects.get(id=pk)
if not episode.user == request.user:
return redirect('audio:home')
if request.method == 'POST':
episode.delete()
messages.success(request, f'Episode {episode.title} deleted!', extra_tags='mb-0')
return redirect('audio:feed', pk=episode.feed.pk, slug=episode.feed.slug)
return render(request, 'audio/delete_episode_confirmation.html', {
'title': 'Delete Episode?',
'heading': 'Delete Episode?',
'episode': episode})
2021-02-24 20:13:54 -08:00
def new_episode(request, feed_pk, feed_title_slug):
if not request.user.is_authenticated:
return redirect('audio:home')
feed = Feed.objects.get(id=feed_pk)
if not feed.user == request.user:
return redirect('audio:home')
if request.method == "POST":
form = EpisodeForm(request.POST, request.FILES)
if form.is_valid():
episode = form.save(commit=False)
episode.user = request.user
episode.feed = feed
episode.save()
2021-02-24 22:25:38 -08:00
return redirect('audio:feed', pk=feed_pk, slug=feed_title_slug)
2021-02-24 20:13:54 -08:00
else:
form = EpisodeForm()
return render(
request, 'base_form.html',
{
'form': form,
'heading': 'New Episode?',
'title': 'New Episode?',
'submit': 'submit',
'form_data': 'TRUE',
})