add delete to edit_feed

This commit is contained in:
Trent Palmer 2021-03-13 07:49:20 -08:00
parent 5d6ae12550
commit bc41ba62b0
5 changed files with 100 additions and 52 deletions

68
audio/feed_views.py Normal file
View File

@ -0,0 +1,68 @@
from django.shortcuts import render, redirect
from .forms import FeedForm
from .models import Feed, rand_slug
from django.contrib import messages
from django.utils.text import slugify
def new_feed(request):
if not request.user.is_authenticated:
return redirect('audio:home')
if request.method == "POST":
form = FeedForm(request.POST, request.FILES)
if form.is_valid():
feed = form.save(commit=False)
feed.user = request.user
feed.save()
return redirect('audio:home')
else:
form = FeedForm()
return render(request, 'base_form.html', {'form': form})
def confirm_delete_feed(request, pk):
if not request.user.is_authenticated:
return redirect('audio:home')
feed = Feed.objects.get(id=pk)
if not feed.user == request.user:
return redirect('audio:home')
if request.method == 'POST':
feed.delete()
messages.success(request, f'Feed {feed.title} deleted!', extra_tags='mb-0')
return redirect('audio:feeds')
return render(request, 'audio/delete_feed_confirmation.html', {
'title': 'Delete Feed?',
'heading': 'Delete Feed?',
'feed': feed})
def edit_feed(request, pk, title_slug):
if not request.user.is_authenticated:
return redirect('audio:home')
slug = title_slug
feed = Feed.objects.get(id=pk)
otitle = feed.title
if not feed.user == request.user:
return redirect('audio:home')
if request.method == "POST":
form = FeedForm(request.POST, request.FILES, instance=feed)
if form.is_valid():
nfeed = form.save(commit=False)
if nfeed.title != otitle:
nfeed.slug = slugify(rand_slug() + "-" + nfeed.title)
slug = nfeed.slug
nfeed.save()
return redirect('audio:feed', pk=pk, slug=slug)
else:
form = FeedForm(instance=feed)
return render(
request, 'base_form.html',
{
'form': form,
'heading': 'Edit Feed?',
'title': 'Edit Feed?',
'submit': 'save',
'form_data': 'TRUE',
'edit_feed': True,
'feed_pk': pk,
})

View File

@ -0,0 +1,20 @@
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
{% include "base_navbar.html" %}
<div style="height: 10vh;"></div>
{% include "base_heading.html" %}
<div style="height: 30vh;" class="">
<h4 class="text-center mb-0 mt-5">Delete {{ feed.title }}?</h4>
<div class="h-100 col-sm-12 col-xs-12 d-flex justify-content-center align-content-center row p-0 mx-0">
<form method="POST">
{% csrf_token %}
<input type="submit" class="btn btn-dark btn-lg mb-4" value="Delete"><br>
<a type="button" class="btn btn-lg btn-dark mt-3" href="{% url 'audio:feeds' %}">Cancel</a>
</form>
</div>
</div>
{% endblock %}

View File

@ -1,15 +1,19 @@
from django.urls import path from django.urls import path
from . import views from . import views
from .episode_views import new_episode, edit_episode from .episode_views import new_episode, edit_episode
from .feed_views import new_feed, edit_feed, confirm_delete_feed
from .audiorssfeed import AudioRssFeed from .audiorssfeed import AudioRssFeed
app_name = "audio" app_name = "audio"
urlpatterns = [ urlpatterns = [
path('', views.home, name='home'), path('', views.home, name='home'),
path('new-feed/', views.new_feed, name='new_feed'), path('new-feed/', new_feed, name='new_feed'),
path('feeds/', views.feeds, name='feeds'), path('feeds/', views.feeds, name='feeds'),
path('edit-feed/<str:pk>/<str:title_slug>', views.edit_feed, name='edit_feed'), path('edit-feed/<str:pk>/<str:title_slug>', edit_feed, name='edit_feed'),
path('confirm-delete-feed/<str:pk>', confirm_delete_feed, name='confirm_delete_feed'),
path('new-episode/<str:feed_pk>/<str:feed_title_slug>', new_episode, name='new_episode'), path('new-episode/<str:feed_pk>/<str:feed_title_slug>', new_episode, name='new_episode'),
path('edit-episode/<str:pk>/<str:title_slug>', edit_episode, name='edit_episode'), path('edit-episode/<str:pk>/<str:title_slug>', edit_episode, name='edit_episode'),
path('rss/<str:slug>.xml', AudioRssFeed(), name='rss'), path('rss/<str:slug>.xml', AudioRssFeed(), name='rss'),

View File

@ -1,8 +1,6 @@
from django.shortcuts import render, redirect, reverse from django.shortcuts import render, reverse
from django.contrib.sites.shortcuts import get_current_site from django.contrib.sites.shortcuts import get_current_site
from .forms import FeedForm from .models import Feed, Episode
from django.utils.text import slugify
from .models import Feed, Episode, rand_slug
from tp.settings import IMAGES_URL, MP3_URL from tp.settings import IMAGES_URL, MP3_URL
@ -47,48 +45,3 @@ def feeds(request):
request, request,
'audio/feeds.html', 'audio/feeds.html',
{'feeds': feeds, 'IMAGES_URL': IMAGES_URL}) {'feeds': feeds, 'IMAGES_URL': IMAGES_URL})
def new_feed(request):
if not request.user.is_authenticated:
return redirect('audio:home')
if request.method == "POST":
form = FeedForm(request.POST, request.FILES)
if form.is_valid():
feed = form.save(commit=False)
feed.user = request.user
feed.save()
return redirect('audio:home')
else:
form = FeedForm()
return render(request, 'base_form.html', {'form': form})
def edit_feed(request, pk, title_slug):
if not request.user.is_authenticated:
return redirect('audio:home')
slug = title_slug
feed = Feed.objects.get(id=pk)
otitle = feed.title
if not feed.user == request.user:
return redirect('audio:home')
if request.method == "POST":
form = FeedForm(request.POST, request.FILES, instance=feed)
if form.is_valid():
nfeed = form.save(commit=False)
if nfeed.title != otitle:
nfeed.slug = slugify(rand_slug() + "-" + nfeed.title)
slug = nfeed.slug
nfeed.save()
return redirect('audio:feed', pk=pk, slug=slug)
else:
form = FeedForm(instance=feed)
return render(
request, 'base_form.html',
{
'form': form,
'heading': 'Edit Feed?',
'title': 'Edit Feed?',
'submit': 'save',
'form_data': 'TRUE',
})

View File

@ -36,8 +36,11 @@
{% endif %} {% endif %}
{% csrf_token %} {% csrf_token %}
{{ form | crispy }} {{ form | crispy }}
<div class="mt-3"> <div class="mt-3 d-flex justify-content-between">
<input type="submit" class="btn btn-dark btn-lg" value="{{ submit }}"> <input type="submit" class="btn btn-dark btn-lg" value="{{ submit }}">
{% if edit_feed %}
<a type="button" class="btn btn-lg btn-dark" href="{% url 'audio:confirm_delete_feed' pk=feed_pk %}">Delete Feed?</a>
{% endif %}
</div> </div>
</form><br> </form><br>
{% if request.path == edit_profile_url %} {% if request.path == edit_profile_url %}