Go back to Django
This commit is contained in:
parent
d7be14f5a2
commit
7eee113cdf
22 changed files with 1481 additions and 172 deletions
|
|
@ -1,35 +1,92 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from django.contrib import messages
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.template import loader
|
||||
from django.views import View
|
||||
from django.views.generic.list import ListView
|
||||
|
||||
from feeds.add_feeds import add_feed
|
||||
from feeds.models import Entry, Feed
|
||||
from feeds.stats import get_db_size
|
||||
|
||||
|
||||
class IndexView(View):
|
||||
"""Index path."""
|
||||
|
||||
def get(self, request: HttpRequest) -> HttpResponse:
|
||||
"""GET request for index path."""
|
||||
"""Load the index page."""
|
||||
template = loader.get_template(template_name="index.html")
|
||||
context = {}
|
||||
context = {
|
||||
"db_size": get_db_size(),
|
||||
"amount_of_feeds": Feed.objects.count(),
|
||||
}
|
||||
return HttpResponse(content=template.render(context=context, request=request))
|
||||
|
||||
|
||||
class FeedView(View):
|
||||
"""A single feed."""
|
||||
|
||||
def get(self, request: HttpRequest, feed_id: int) -> HttpResponse:
|
||||
"""GET request for index path."""
|
||||
template = loader.get_template(template_name="feed.html")
|
||||
context = {"feed_id": feed_id}
|
||||
return HttpResponse(content=template.render(context=context, request=request))
|
||||
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: # noqa: ANN002, ANN003, ARG002
|
||||
"""Load the feed page."""
|
||||
feed_id = kwargs.get("feed_id", None)
|
||||
if not feed_id:
|
||||
return HttpResponse(content="No id", status=400)
|
||||
|
||||
feed = get_object_or_404(Feed, id=feed_id)
|
||||
entries = Entry.objects.filter(feed=feed).order_by("-created_parsed")[:100]
|
||||
|
||||
context = {"feed": feed, "entries": entries, "db_size": get_db_size(), "amount_of_feeds": Feed.objects.count()}
|
||||
return render(request, "feed.html", context)
|
||||
|
||||
|
||||
class FeedsView(View):
|
||||
class FeedsView(ListView):
|
||||
"""All feeds."""
|
||||
|
||||
model = Feed
|
||||
paginate_by = 100
|
||||
template_name = "feeds.html"
|
||||
context_object_name = "feeds"
|
||||
|
||||
def get_context_data(self, **kwargs) -> dict: # noqa: ANN003
|
||||
"""Get the context data."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["db_size"] = get_db_size()
|
||||
context["amount_of_feeds"] = Feed.objects.count()
|
||||
return context
|
||||
|
||||
|
||||
class AddView(View):
|
||||
"""Add a feed."""
|
||||
|
||||
def get(self, request: HttpRequest) -> HttpResponse:
|
||||
"""GET request for index path."""
|
||||
template = loader.get_template(template_name="feeds.html")
|
||||
context = {}
|
||||
"""Load the index page."""
|
||||
template = loader.get_template(template_name="index.html")
|
||||
context = {
|
||||
"db_size": get_db_size(),
|
||||
"amount_of_feeds": Feed.objects.count(),
|
||||
}
|
||||
return HttpResponse(content=template.render(context=context, request=request))
|
||||
|
||||
def post(self, request: HttpRequest) -> HttpResponse:
|
||||
"""Add a feed."""
|
||||
urls: str | None = request.POST.get("urls", None)
|
||||
if not urls:
|
||||
return HttpResponse(content="No urls", status=400)
|
||||
|
||||
# Split the urls by newline.
|
||||
for url in urls.split("\n"):
|
||||
feed: None | Feed = add_feed(url)
|
||||
if not feed:
|
||||
messages.error(request, f"{url} - Failed to add")
|
||||
continue
|
||||
# Check if bozo is true.
|
||||
if feed.bozo:
|
||||
messages.warning(request, f"{feed.feed_url} - Bozo: {feed.bozo_exception}")
|
||||
|
||||
messages.success(request, f"{feed.feed_url} added")
|
||||
|
||||
# Render the index page.
|
||||
template = loader.get_template(template_name="index.html")
|
||||
return HttpResponse(content=template.render(context={}, request=request))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue