phantasmobile/app/item/models.py

159 lines
5.3 KiB
Python
Raw Normal View History

2023-07-15 07:30:36 +00:00
from django.utils.timezone import datetime, timedelta
from django.utils import timezone
import json
2023-07-16 05:56:14 +00:00
import requests
import lxml.html
2023-07-15 07:30:36 +00:00
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
2023-07-24 21:00:43 +00:00
from django.utils.timesince import timesince
2023-07-15 07:30:36 +00:00
User = get_user_model()
2023-07-24 11:05:45 +00:00
2023-07-15 07:30:36 +00:00
class Settings(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
key = models.CharField(max_length=1024, unique=True)
value = models.JSONField(default=dict, editable=False)
class Item(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=1024)
2023-07-25 19:03:54 +00:00
url = models.CharField(max_length=1024, unique=True)
description = models.TextField(default="", blank=True, editable=False)
2023-07-15 07:30:36 +00:00
published = models.DateTimeField(default=timezone.now, null=True, blank=True)
2023-07-25 19:03:54 +00:00
announced = models.DateTimeField(null=True, default=None, blank=True, editable=False)
2023-07-15 07:30:36 +00:00
data = models.JSONField(default=dict, editable=False)
2023-07-25 19:03:54 +00:00
user = models.ForeignKey(User, null=True, related_name='items', on_delete=models.CASCADE)
2023-07-15 07:30:36 +00:00
2023-07-16 05:56:14 +00:00
def save(self, *args, **kwargs):
if self.url and not self.data:
self.update_data()
super().save(*args, **kwargs)
2023-07-24 11:05:45 +00:00
2023-07-15 07:30:36 +00:00
def __str__(self):
return '%s (%s)' % (self.title, self.url)
def public_comments(self):
return self.comments.exclude(published=None)
def public_comments_json(self):
comments = []
for comment in self.public_comments():
comments.append({
"name": comment.name,
"date": comment.date,
"text": comment.text,
})
return json.dumps(comments)
@classmethod
def public(cls):
now = timezone.now()
qs = cls.objects.exclude(published=None).filter(published__lte=now).order_by('-published')
cal = now.date().isocalendar()
monday = now.date() - timedelta(days=now.date().isocalendar().weekday - 1)
monday = timezone.datetime(monday.year, monday.month, monday.day, tzinfo=now.tzinfo)
first_post = qs.filter(published__gt=monday).first()
2023-07-24 11:05:45 +00:00
if first_post and first_post.published < now:
week = qs.filter(published__gt=monday)
elif not first_post:
while qs.exists() and not first_post:
monday = monday - timedelta(days=7)
first_post = qs.filter(published__gt=monday).first()
2023-07-15 07:30:36 +00:00
week = qs.filter(published__gt=monday)
else:
last_monday = monday - timedelta(days=7)
week = qs.filter(published__gt=last_monday)
archive = qs.exclude(id__in=week)
return week, archive
def get_absolute_url(self):
return reverse('item', kwargs={'id': self.id})
2023-07-16 05:56:14 +00:00
def update_data(self):
self.data.update(self.parse_url())
def parse_url(self):
content = requests.get(self.url).text
doc = lxml.html.fromstring(content)
data = {}
for meta in doc.cssselect('meta'):
key = meta.attrib.get('name')
if not key:
key = meta.attrib.get('property')
value = meta.attrib.get('content')
if key and value:
if key in ('viewport', ):
continue
key = key.replace('og:', '')
data[key] = value
return data
2023-07-15 07:30:36 +00:00
class Comment(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
item = models.ForeignKey(Item, related_name='comments', on_delete=models.CASCADE)
2023-07-25 19:03:54 +00:00
text = models.TextField(default="")
name = models.CharField(max_length=1024, blank=True)
email = models.CharField(max_length=1024, blank=True)
2023-07-15 07:30:36 +00:00
user = models.ForeignKey(User, null=True, related_name='comments', on_delete=models.CASCADE, blank=True)
2023-07-25 19:03:54 +00:00
session_key = models.CharField(max_length=60, null=True, default=None, blank=True, editable=False)
2023-07-15 07:30:36 +00:00
data = models.JSONField(default=dict, editable=False)
2023-07-25 19:03:54 +00:00
published = models.DateTimeField(null=True, default=None, blank=True)
2023-07-15 07:30:36 +00:00
2023-07-24 11:05:45 +00:00
class Meta:
permissions = [
("can_post_comment", "Can post comments without moderation")
]
2023-07-16 05:56:14 +00:00
@property
def is_published(self):
return bool(self.published)
2023-07-15 07:30:36 +00:00
def __str__(self):
return '%s: %s' % (self.item, self.user)
def save(self, *args, **kwargs):
if self.user:
self.name = self.user.username
self.email = self.user.email
super().save(*args, **kwargs)
@property
def date(self):
2023-07-24 21:00:43 +00:00
now = timezone.now()
difference = now - self.created
if difference <= timedelta(minutes=1):
return "just now"
return '%(time)s ago' % {'time': timesince(self.created).split(', ')[0]}
return self.created.strftime('%B %d, %Y at %H:%M')
2023-07-15 07:30:36 +00:00
return self.created.strftime('%Y-%m-%d %H:%M')
def json(self):
data = {}
2023-07-24 11:05:45 +00:00
if not self.user:
data['name'] = '%s (guest)' % self.name
else:
data['name'] = self.name
2023-07-15 07:30:36 +00:00
data['date'] = self.date
data['text'] = self.text
2023-07-24 21:00:43 +00:00
data['id'] = self.id
data['published'] = self.is_published
2023-07-15 07:30:36 +00:00
return data
2023-07-24 11:05:45 +00:00