This commit is contained in:
emile 2020-01-20 18:36:06 +01:00
parent 51dc2528b0
commit e9e9c7ba5a
4 changed files with 48 additions and 23 deletions

View File

@ -12,8 +12,8 @@ class Post(models.Model):
text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
authors=models.ManyToManyField(User)
author_anonim_name = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
author_anonim_name = models.TextField(null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
@staticmethod
def getPostsOfAuthor(author):
return Post.objects.filter(authors=author)
@ -26,8 +26,8 @@ class Post(models.Model):
return Post.objects.filter(created_at__day=datetime.datetime.now().date().day)
@staticmethod
def getPage(page:int):
a = Post.objects.all().order_by('created_at')
paginator = Paginator(a,5)
a = Post.objects.all().order_by('-created_at' )
paginator = Paginator(a,3)
return paginator.get_page(page)
class StaticPage(models.Model):
own_text = models.TextField()

View File

@ -1,11 +1,17 @@
from django.urls import path
from django.conf.urls import url
from django.contrib import admin
from django.views.generic.base import TemplateView
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('post', views.Post_Handler, name="post"),
path('category', views.Category_Handler, name="category"),
path('comment', views.Comments_Handler, name="comment"),
path('menu', views.Menu_Handler, name="menu")
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
path('api/post', views.Post_Handler, name="post"),
path('api/category', views.Category_Handler, name="category"),
path('api/comment', views.Comments_Handler, name="comment"),
path('api/menu', views.Menu_Handler, name="menu"),
url(r'^(?P<path>.*)/$', views.index)
]

View File

@ -8,8 +8,15 @@ from django.http import JsonResponse
from django.core import serializers
import json
from django.views.decorators.csrf import csrf_exempt
def index(request):
return "joy"
def index(request, path=''):
"""
Renders the Angular2 SPA
"""
return render(request, 'home.html')
@csrf_exempt
@require_http_methods(["POST", "GET", "DELETE"])
@ -34,17 +41,24 @@ def Category_Handler(request):
@require_http_methods(["POST", "GET", "DELETE"])
def Post_Handler(request):
def Post_POST():
"""Expects {"text":"tesxt", "category":category_id}"""
"""Expects {"text":"tesxt", "category":category_id, "authorNickname":"blalbal"}"""
req = json.loads(request.body)
category = Category.objects.get(pk=int(req['category']))
category = Category.objects.get(pk=int(req['category_id']))
p = Post.objects.create(
created_at=timezone.datetime.now(),
category=category,
text=req['text']
text=req['text'],
author_anonim_name = req['authorNickname'] if req['authorNickname'] else None
)
p.save()
return JsonResponse(serializers.serialize("python", [p]), safe=False)
def Post_GET():
id=request.GET.get('id', None)
if id:
post = Post.objects.get(pk=id)
res = serializers.serialize('python', [post])
return JsonResponse(res, safe=False)
else:
page = request.GET.get('page',0)
posts = Post.getPage(page)
res = serializers.serialize('python',posts)
@ -75,8 +89,9 @@ def Comments_Handler(request):
return JsonResponse(ser, safe=False)
def Comments_GET():
entry = Post.objects.get(pk=request.GET.get('post',0))
comments = Comment.objects.get(post=entry)
return JsonResponse(serializers.serialize("python", comments ), safe=False)
comments = Comment.objects.filter(post=entry)
return JsonResponse(serializers.serialize("python",comments ), safe=False)
def Comments_DELETE():
pass

View File

@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import django_heroku
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -121,6 +121,10 @@ USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
CORS_ORIGIN_ALLOW_ALL = True
django_heroku.settings(locals())