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() text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
authors=models.ManyToManyField(User) authors=models.ManyToManyField(User)
author_anonim_name = models.TextField() author_anonim_name = models.TextField(null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE) category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
@staticmethod @staticmethod
def getPostsOfAuthor(author): def getPostsOfAuthor(author):
return Post.objects.filter(authors=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) 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().order_by('created_at') a = Post.objects.all().order_by('-created_at' )
paginator = Paginator(a,5) paginator = Paginator(a,3)
return paginator.get_page(page) return paginator.get_page(page)
class StaticPage(models.Model): class StaticPage(models.Model):
own_text = models.TextField() own_text = models.TextField()

View File

@ -1,11 +1,17 @@
from django.urls import path 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 from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), url(r'^$', views.index, name='index'),
path('post', views.Post_Handler, name="post"), url(r'^admin/', admin.site.urls),
path('category', views.Category_Handler, name="category"),
path('comment', views.Comments_Handler, name="comment"), path('api/post', views.Post_Handler, name="post"),
path('menu', views.Menu_Handler, name="menu") 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 from django.core import serializers
import json import json
from django.views.decorators.csrf import csrf_exempt 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 @csrf_exempt
@require_http_methods(["POST", "GET", "DELETE"]) @require_http_methods(["POST", "GET", "DELETE"])
@ -34,17 +41,24 @@ def Category_Handler(request):
@require_http_methods(["POST", "GET", "DELETE"]) @require_http_methods(["POST", "GET", "DELETE"])
def Post_Handler(request): def Post_Handler(request):
def Post_POST(): def Post_POST():
"""Expects {"text":"tesxt", "category":category_id}""" """Expects {"text":"tesxt", "category":category_id, "authorNickname":"blalbal"}"""
req = json.loads(request.body) 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( p = Post.objects.create(
created_at=timezone.datetime.now(), created_at=timezone.datetime.now(),
category=category, category=category,
text=req['text'] text=req['text'],
author_anonim_name = req['authorNickname'] if req['authorNickname'] else None
) )
p.save() p.save()
return JsonResponse(serializers.serialize("python", [p]), safe=False) return JsonResponse(serializers.serialize("python", [p]), safe=False)
def Post_GET(): 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) page = request.GET.get('page',0)
posts = Post.getPage(page) posts = Post.getPage(page)
res = serializers.serialize('python',posts) res = serializers.serialize('python',posts)
@ -75,7 +89,8 @@ def Comments_Handler(request):
return JsonResponse(ser, safe=False) return JsonResponse(ser, safe=False)
def Comments_GET(): def Comments_GET():
entry = Post.objects.get(pk=request.GET.get('post',0)) entry = Post.objects.get(pk=request.GET.get('post',0))
comments = Comment.objects.get(post=entry) comments = Comment.objects.filter(post=entry)
return JsonResponse(serializers.serialize("python",comments ), safe=False) return JsonResponse(serializers.serialize("python",comments ), safe=False)
def Comments_DELETE(): def Comments_DELETE():
pass 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 For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/ https://docs.djangoproject.com/en/2.2/ref/settings/
""" """
import django_heroku
import os import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -121,6 +121,10 @@ USE_TZ = False
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/ # https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/' STATIC_URL = '/static/'
CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_ALLOW_ALL = True
django_heroku.settings(locals())