hrss/web/views.py
2018-10-24 23:57:41 +02:00

87 lines
2.7 KiB
Python

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .utils import *
from .models import Feed
from django.db.models import ObjectDoesNotExist
# Create your views here.
def iframe(request, url):
try:
html = requests.get(url).text
except Exception as e:
html = str(e)
return HttpResponse(html)
def dummy(request):
return HttpResponse("toto")
def homepage(request):
if request.method == 'POST':
if "url" in request.POST and request.POST["url"]:
url = request.POST["url"]
if is_valid_url(url):
return redirect("/setup/"+url)
else:
return render(request, 'homepage.html', {"url": url, "error": url+" is not a valid URL."})
return render(request, 'homepage.html')
def setup(request, url):
if is_valid_url(url):
return render(request, 'setup.html', {"url": url})
else:
return redirect("/")
def newfeed(request):
if request.method == 'POST':
if not "url" in request.POST or not "element" in request.POST or not "title" in request.POST or not "content" in request.POST:
return HttpResponse("Error, missing required element")
url = request.POST["url"]
element = request.POST["element"]
title = request.POST["title"]
content = request.POST["content"]
date = request.POST["date"]
author = request.POST["author"]
link = request.POST["link"]
a = find_longest_common_string(element, title)
title = title[len(a)+3:].strip()
a = find_longest_common_string(element, content)
content = content[len(a)+3:].strip()
a = find_longest_common_string(element, date)
date = date[len(a)+3:].strip()
a = find_longest_common_string(element, author)
author = author[len(a)+3:].strip()
a = find_longest_common_string(element, link)
link = link[len(a)+3:].strip()
feed = Feed(url=url, element=element, title=title, content=content, date=date, author=author, link=link)
feed.save()
return redirect("/feeds")
else:
return redirect("/")
def feeds(request):
feeds = Feed.objects.all()
return render(request, "feeds.html", {"feeds": feeds})
def feed_delete(request, id):
try:
Feed.objects.get(pk=id).delete()
except Exception as e:
pass
finally:
return redirect("/feeds")
def rss(request, uurl):
try:
feed = Feed.objects.get(uurl=uurl)
return render(request, "feed.xml", {"feed": feed, "rss": fetch_feed(feed)}, content_type="application/rss+xml")
except ObjectDoesNotExist:
return HttpResponse("nope")