phantasmobile/app/item/models.py

105 lines
3.6 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
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
User = get_user_model()
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)
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)
published = models.DateTimeField(default=timezone.now, null=True, blank=True)
announced = models.DateTimeField(null=True, default=None, blank=True)
data = models.JSONField(default=dict, editable=False)
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)
print(now.tzinfo)
first_post = qs.filter(published__gt=monday).first()
print('!!', first_post.published, now, first_post.published > now)
if first_post.published < now:
print('only this week')
week = qs.filter(published__gt=monday)
else:
print('only last week')
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})
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)
user = models.ForeignKey(User, null=True, related_name='comments', on_delete=models.CASCADE, blank=True)
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)
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):
return self.created.strftime('%Y-%m-%d %H:%M')
def json(self):
data = {}
data['name'] = self.name
data['date'] = self.date
data['text'] = self.text
return data