base menu, about page, atom feed
This commit is contained in:
parent
013ca30765
commit
0b64fd58a1
21 changed files with 284 additions and 47 deletions
|
|
@ -9,6 +9,10 @@ class ItemAdmin(admin.ModelAdmin):
|
|||
list_filter = (
|
||||
("published", admin.EmptyFieldListFilter),
|
||||
)
|
||||
raw_id_fields = ['user']
|
||||
|
||||
def get_changeform_initial_data(self, request):
|
||||
return {'user': request.user}
|
||||
|
||||
admin.site.register(models.Item, ItemAdmin)
|
||||
|
||||
|
|
@ -25,5 +29,6 @@ class CommentAdmin(admin.ModelAdmin):
|
|||
list_filter = (
|
||||
("published", admin.EmptyFieldListFilter),
|
||||
)
|
||||
raw_id_fields = ['item', 'user']
|
||||
|
||||
admin.site.register(models.Comment, CommentAdmin)
|
||||
|
|
|
|||
|
|
@ -26,14 +26,14 @@ class Item(models.Model):
|
|||
created = models.DateTimeField(auto_now_add=True)
|
||||
modified = models.DateTimeField(auto_now=True)
|
||||
|
||||
user = models.ForeignKey(User, null=True, related_name='items', on_delete=models.CASCADE)
|
||||
|
||||
url = models.CharField(max_length=1024, unique=True)
|
||||
title = models.CharField(max_length=1024)
|
||||
description = models.TextField(default="", blank=True)
|
||||
url = models.CharField(max_length=1024, unique=True)
|
||||
description = models.TextField(default="", blank=True, editable=False)
|
||||
published = models.DateTimeField(default=timezone.now, null=True, blank=True)
|
||||
announced = models.DateTimeField(null=True, default=None, blank=True)
|
||||
announced = models.DateTimeField(null=True, default=None, blank=True, editable=False)
|
||||
data = models.JSONField(default=dict, editable=False)
|
||||
user = models.ForeignKey(User, null=True, related_name='items', on_delete=models.CASCADE)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.url and not self.data:
|
||||
|
|
@ -105,14 +105,16 @@ class Comment(models.Model):
|
|||
modified = models.DateTimeField(auto_now=True)
|
||||
|
||||
item = models.ForeignKey(Item, related_name='comments', on_delete=models.CASCADE)
|
||||
user = models.ForeignKey(User, null=True, related_name='comments', on_delete=models.CASCADE, blank=True)
|
||||
session_key = models.CharField(max_length=60, null=True, default=None, blank=True)
|
||||
text = models.TextField(default="")
|
||||
|
||||
name = models.CharField(max_length=1024, blank=True)
|
||||
email = models.CharField(max_length=1024, blank=True)
|
||||
|
||||
user = models.ForeignKey(User, null=True, related_name='comments', on_delete=models.CASCADE, blank=True)
|
||||
session_key = models.CharField(max_length=60, null=True, default=None, blank=True, editable=False)
|
||||
|
||||
name = models.CharField(max_length=1024)
|
||||
email = models.CharField(max_length=1024)
|
||||
text = models.TextField(default="", blank=True)
|
||||
data = models.JSONField(default=dict, editable=False)
|
||||
published = models.DateTimeField(null=True, default=None)
|
||||
published = models.DateTimeField(null=True, default=None, blank=True)
|
||||
|
||||
class Meta:
|
||||
permissions = [
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ from ..celery import app
|
|||
from . import models
|
||||
|
||||
|
||||
@app.task(queue="default")
|
||||
@app.task(queue="default", ignore_results=True)
|
||||
def announce_items():
|
||||
if not getattr(settings, 'SIGNAL_ANNOUNCE_GROUP'):
|
||||
return
|
||||
|
|
@ -42,18 +42,27 @@ def announce_items():
|
|||
os.unlink(f.name)
|
||||
|
||||
|
||||
@app.task(queue="default")
|
||||
@app.task(queue="default", ignore_results=True)
|
||||
def notify_moderators(id, link):
|
||||
comment = models.Comment.objects.filter(id=id).first()
|
||||
if comment:
|
||||
message = "%s commented on %s (%s)\n\n%s" % (comment.name, comment.item.title, link, comment.text)
|
||||
message = "%s commented on %s (%s)\n\n%s" % (
|
||||
comment.name, comment.item.title, link, comment.text
|
||||
)
|
||||
r = rpc.send(message, group=settings.SIGNAL_MODERATORS_GROUP)
|
||||
if r and "timestamp" in r:
|
||||
comment.data["moderator_ts"] = r["timestamp"]
|
||||
comment.save()
|
||||
if comment.published:
|
||||
account = settings.SIGNAL_ACCOUNT
|
||||
group = settings.SIGNAL_MODERATORS_GROUP
|
||||
rpc.send_reaction(
|
||||
account, comment.data["moderator_ts"], "🎉", group=group
|
||||
)
|
||||
else:
|
||||
print("failed to notify", r)
|
||||
|
||||
|
||||
@app.on_after_finalize.connect
|
||||
def setup_periodic_tasks(sender, **kwargs):
|
||||
sender.add_periodic_task(crontab(minute="*/2"), announce_items.s())
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import xml.etree.ElementTree as ET
|
||||
|
||||
from datetime import date, datetime, timedelta
|
||||
import json
|
||||
|
||||
|
|
@ -6,6 +8,7 @@ from django.shortcuts import render
|
|||
from django.db.models import Q
|
||||
from django.utils.html import mark_safe
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponse
|
||||
|
||||
from . import models
|
||||
from . import tasks
|
||||
|
|
@ -14,7 +17,7 @@ from .utils import render_to_json
|
|||
|
||||
|
||||
def index(request):
|
||||
context = {}
|
||||
context = {"settings": settings}
|
||||
week, archive = models.Item.public()
|
||||
context['items'] = week
|
||||
context['archive'] = archive.exists()
|
||||
|
|
@ -22,7 +25,7 @@ def index(request):
|
|||
|
||||
|
||||
def archive(request):
|
||||
context = {}
|
||||
context = {"settings": settings}
|
||||
qs = models.Item.public()
|
||||
week, archive = models.Item.public()
|
||||
context['items'] = archive
|
||||
|
|
@ -30,7 +33,7 @@ def archive(request):
|
|||
|
||||
|
||||
def item(request, id):
|
||||
context = {}
|
||||
context = {"settings": settings}
|
||||
item = models.Item.objects.get(id=id)
|
||||
context['item'] = item
|
||||
qs = item.comments.order_by('created')
|
||||
|
|
@ -71,9 +74,8 @@ def comment(request):
|
|||
comment.session_key = request.session.session_key
|
||||
comment.text = data['text']
|
||||
comment.save()
|
||||
if not comment.published:
|
||||
link = request.build_absolute_uri(comment.item.get_absolute_url())
|
||||
tasks.notify_moderators.delay(comment.id, link)
|
||||
link = request.build_absolute_uri(comment.item.get_absolute_url())
|
||||
tasks.notify_moderators.delay(comment.id, link)
|
||||
response = comment.json()
|
||||
return render_to_json(response)
|
||||
|
||||
|
|
@ -88,8 +90,61 @@ def publish_comment(request):
|
|||
if comment.data.get("moderator_ts"):
|
||||
account = settings.SIGNAL_ACCOUNT
|
||||
group = settings.SIGNAL_MODERATORS_GROUP
|
||||
send_reaction(account, comment.data["moderator_ts"], "🎉", group=group)
|
||||
send_reaction(
|
||||
account, comment.data["moderator_ts"], "🎉", group=group
|
||||
)
|
||||
response['status'] = 'ok'
|
||||
else:
|
||||
response['error'] = 'permission denied'
|
||||
return render_to_json(response)
|
||||
|
||||
|
||||
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'
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue