phantasmobile/app/item/views.py

151 lines
4.9 KiB
Python
Raw Normal View History

2023-07-25 19:03:54 +00:00
import xml.etree.ElementTree as ET
2023-07-15 07:30:36 +00:00
from datetime import date, datetime, timedelta
2023-07-24 11:05:45 +00:00
import json
from django.utils import timezone
2023-07-15 07:30:36 +00:00
from django.shortcuts import render
2023-07-24 11:05:45 +00:00
from django.db.models import Q
from django.utils.html import mark_safe
from django.conf import settings
2023-07-25 19:03:54 +00:00
from django.http import HttpResponse
2023-07-15 07:30:36 +00:00
from . import models
2023-07-24 11:05:45 +00:00
from . import tasks
from ..signalbot.rpc import send_reaction
from .utils import render_to_json
2023-07-15 07:30:36 +00:00
def index(request):
2023-07-25 19:03:54 +00:00
context = {"settings": settings}
2023-07-15 07:30:36 +00:00
week, archive = models.Item.public()
context['items'] = week
context['archive'] = archive.exists()
return render(request, 'index.html', context)
def archive(request):
2023-07-25 19:03:54 +00:00
context = {"settings": settings}
2023-07-15 07:30:36 +00:00
qs = models.Item.public()
week, archive = models.Item.public()
context['items'] = archive
return render(request, 'archive.html', context)
2023-07-24 11:05:45 +00:00
2023-07-15 07:30:36 +00:00
def item(request, id):
2023-07-25 19:03:54 +00:00
context = {"settings": settings}
2023-07-15 07:30:36 +00:00
item = models.Item.objects.get(id=id)
context['item'] = item
2023-07-24 11:05:45 +00:00
qs = item.comments.order_by('created')
if not request.user.is_staff:
q = ~Q(published=None)
if request.user.is_authenticated:
q |= Q(user=request.user)
if request.session and request.session.session_key:
q |= Q(session_key=request.session.session_key)
qs = qs.filter(q)
comments = []
for comment in qs:
2023-07-24 21:00:43 +00:00
comments.append(comment.json())
2023-07-24 11:05:45 +00:00
context['comments'] = mark_safe(json.dumps(comments))
user = {}
if request.user.is_staff:
user['is_moderator'] = True
if request.user.is_authenticated:
user['username'] = request.user.username
2023-07-15 07:30:36 +00:00
2023-07-24 11:05:45 +00:00
context['user'] = mark_safe(json.dumps(user))
request.session['item'] = id
return render(request, 'item.html', context)
2023-07-15 07:30:36 +00:00
def comment(request):
response = {}
data = json.loads(request.body)
comment = models.Comment()
comment.item = models.Item.objects.get(id=data['item'])
if request.user.is_authenticated:
comment.user = request.user
2023-07-24 11:05:45 +00:00
if comment.user.has_perm('app.item.can_post_comment'):
comment.published = timezone.now()
2023-07-15 07:30:36 +00:00
else:
comment.name = data['name']
comment.email = data['email']
2023-07-24 11:05:45 +00:00
comment.session_key = request.session.session_key
2023-07-15 07:30:36 +00:00
comment.text = data['text']
comment.save()
2023-07-25 19:03:54 +00:00
link = request.build_absolute_uri(comment.item.get_absolute_url())
tasks.notify_moderators.delay(comment.id, link)
2023-07-15 07:30:36 +00:00
response = comment.json()
return render_to_json(response)
2023-07-24 11:05:45 +00:00
def publish_comment(request):
response = {}
data = json.loads(request.body)
if request.user.is_staff:
comment = models.Comment.objects.get(id=data['comment'])
comment.published = timezone.now()
comment.save()
if comment.data.get("moderator_ts"):
account = settings.SIGNAL_ACCOUNT
group = settings.SIGNAL_MODERATORS_GROUP
2023-07-25 19:03:54 +00:00
send_reaction(
account, comment.data["moderator_ts"], "🎉", group=group
)
2023-07-24 11:05:45 +00:00
response['status'] = 'ok'
else:
response['error'] = 'permission denied'
return render_to_json(response)
2023-07-25 19:03:54 +00:00
def atom_xml(request):
feed = ET.Element("feed")
feed.attrib['xmlns'] = 'http://www.w3.org/2005/Atom'
feed.attrib['xmlns:media'] = 'http://search.yahoo.com/mrss/'
feed.attrib['xml:lang'] = 'en'
title = ET.SubElement(feed, "title")
title.text = settings.SITENAME
title.attrib['type'] = 'text'
link = ET.SubElement(feed, "link")
link.attrib['rel'] = 'self'
link.attrib['type'] = 'application/atom+xml'
atom_link = request.build_absolute_uri('/atom.xml')
link.attrib['href'] = atom_link
el = ET.SubElement(feed, 'id')
el.text = atom_link
week, archive = models.Item.public()
for item in week:
page_link = request.build_absolute_uri(item.get_absolute_url())
entry = ET.Element("entry")
title = ET.SubElement(entry, "title")
title.text = item.title
link = ET.SubElement(entry, "link")
link.attrib['rel'] = 'alternate'
link.attrib['href'] = page_link
updated = ET.SubElement(entry, "updated")
updated.text = item.modified.strftime("%Y-%m-%dT%H:%M:%SZ")
published = ET.SubElement(entry, "published")
published.text = item.created.strftime("%Y-%m-%dT%H:%M:%SZ")
el = ET.SubElement(entry, "id")
el.text = page_link
print(item.data)
if 'title' in item.data and 'thumbnail' in item.data:
html = f'''
<p>
{item.data['title']}
</p>
<img src={ item.data['thumbnail'] }>
'''.strip()
content = ET.SubElement(entry, "content")
content.attrib['type'] = 'html'
content.text = html
feed.append(entry)
return HttpResponse(
'<?xml version="1.0" encoding="utf-8" ?>\n' + ET.tostring(feed).decode(),
'application/atom+xml'
)