This commit is contained in:
emile 2020-01-12 17:55:22 +01:00
parent d306aad6e2
commit 44e84ef639
3 changed files with 64 additions and 3 deletions

View File

@ -1 +1,7 @@
from django.contrib import admin from django.contrib import admin
from django.contrib import admin
from przyrost.models import *
@admin.register(Category, Post, User, StaticPage, MenuEntry, Menu, Comment, Config)
class AuthorAdmin(admin.ModelAdmin):
pass

View File

@ -14,8 +14,15 @@ class Post(models.Model):
authors=models.ManyToManyField(User) authors=models.ManyToManyField(User)
category = models.ForeignKey(Category, on_delete=models.CASCADE) category = models.ForeignKey(Category, on_delete=models.CASCADE)
@staticmethod @staticmethod
def getPostsOfAuthor(author):
return Post.objects.filter(authors=author)
@staticmethod
def getYesterday():
y=(datetime.datetime.today() - datetime.timedelta(days=1))
return Post.objects.filter(created_at__day=y.day, created_at__month=y.month, created_at__year=y.year)
@staticmethod
def getTodays(): def getTodays():
return Post.objects.filter(created_at=timezone.datetime.today()) return Post.objects.filter(created_at__day=datetime.datetime.now().date().day)
@staticmethod @staticmethod
def getPage(page:int): def getPage(page:int):
a = Post.objects.all() a = Post.objects.all()

View File

@ -1,6 +1,8 @@
from django.test import TestCase from django.test import TestCase
from przyrost.models import * from przyrost.models import *
from django.utils import timezone from django.utils import timezone
from django.core import serializers
class testbasic(TestCase): class testbasic(TestCase):
def setUp(self) -> None: def setUp(self) -> None:
@ -18,6 +20,52 @@ class testbasic(TestCase):
) )
p.authors.set([u]) p.authors.set([u])
p.save() p.save()
d = """[{"model": "przyrost.post","fields": {
"text": "to jest drugi post",
"category": 1, "authors": [1]}}]"""
obj = serializers.deserialize("json", d)
for l in obj:
if not l.object.created_at:
l.object.created_at = datetime.datetime.now()
l.save()
xml = """<?xml version="1.0" encoding="utf-8"?><django-objects version="1.0"><object model="przyrost.post" ><field name="text" type="TextField">Why would I use XML???</field><field name="created_at" type="DateTimeField">2012-12-01T14:02:01.589710</field><field name="category" rel="ManyToOneRel" to="przyrost.category">1</field><field name="authors" rel="ManyToManyRel" to="przyrost.user"><object pk="1"></object></field></object><object model="przyrost.post" pk="2"><field name="text" type="TextField">to jest drugi post</field><field name="created_at" type="DateTimeField">2019-12-08T13:52:18.473000</field><field name="category" rel="ManyToOneRel" to="przyrost.category">1</field><field name="authors" rel="ManyToManyRel" to="przyrost.user"><object pk="1"></object></field></object></django-objects>"""
obj = serializers.deserialize("xml", xml)
for l in obj:
if not l.object.created_at:
l.object.created_at = datetime.datetime.now()
l.save()
print("final values:")
print(Post.objects.all().values())
def test_getPostByDate(self): def test_getPostByDate(self):
self.assertEqual(len(Post.objects.all()),1)
self.assertEqual(len(Post.getTodays()), 1) print(list(Post.objects.all().values()))
self.assertEqual(len(Post.objects.all()),3)
self.assertEqual(len(Post.getTodays()), 2)
self.assertEqual(len(Post.getYesterday()), 0)
def test__serializeXML(self):
print("XML: ")
data = serializers.serialize("xml", Post.objects.all())
print(data)
def test__serializeJSON(self):
print("JSON: ")
data = serializers.serialize("json", Post.objects.all())
print(data)
def test__deserializeJSON(self):
d= """[{"model": "przyrost.post","fields": {
"text": "to jest drugi post",
"created_at": "2019-12-08T13:52:18.473", "category": 1, "authors": [1]}}]"""
obj = serializers.deserialize("json",d)
#for l in obj:
# l.save()
# from time import sleep
#sleep(1)
print("new values")
print(Post.objects.all().values())
def test__getPostsOfAuthor(self):
d = Post.getPostsOfAuthor(User.objects.filter(name="Admin")[0])
self.assertEqual(len(d), 3)