126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
from django.shortcuts import render
|
|
|
|
# Create your views here.
|
|
from django.http import HttpResponse
|
|
from django.views.decorators.http import require_http_methods
|
|
from przyrost.models import *
|
|
from django.http import JsonResponse
|
|
from django.core import serializers
|
|
import json
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
def index(request, path=''):
|
|
"""
|
|
Renders the Angular2 SPA
|
|
"""
|
|
return render(request, 'home.html')
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
@require_http_methods(["POST", "GET", "DELETE"])
|
|
def Category_Handler(request):
|
|
def Category_POST():
|
|
"""Expects {"name":"name"}"""
|
|
req = json.loads(request.body)
|
|
i = Category.objects.create(name=req['name'])
|
|
i.save()
|
|
return JsonResponse(serializers.serialize("python", [i]), safe=False)
|
|
def Category_GET():
|
|
all = Category.objects.all()
|
|
s = serializers.serialize("json", all)
|
|
return JsonResponse(s,safe=False)
|
|
def Category_DELETE():
|
|
req = json.loads(request.body)
|
|
Category.objects.get(id=req['id']).delete();
|
|
return JsonResponse({"status": "ok"})
|
|
return {"GET":Category_GET, "POST":Category_POST, "DELETE":Category_DELETE}[request.method]()
|
|
|
|
@csrf_exempt
|
|
@require_http_methods(["POST", "GET", "DELETE"])
|
|
def Post_Handler(request):
|
|
def Post_POST():
|
|
"""Expects {"text":"tesxt", "category":category_id, "authorNickname":"blalbal"}"""
|
|
req = json.loads(request.body)
|
|
category = Category.objects.get(pk=int(req['category_id']))
|
|
p = Post.objects.create(
|
|
created_at=timezone.datetime.now(),
|
|
category=category,
|
|
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)
|
|
return JsonResponse(res, safe=False)
|
|
def Post_DELETE():
|
|
id = request.GET.get("id", None)
|
|
if id:
|
|
o:Post=Post.objects.filter(lambda d: d.id == id)[0]
|
|
o.delete()
|
|
|
|
return {"GET":Post_GET, "POST":Post_POST, "DELETE":Post_DELETE}[request.method]()
|
|
|
|
@csrf_exempt
|
|
@require_http_methods(["POST", "GET", "DELETE"])
|
|
def Comments_Handler(request):
|
|
def Comments_POST():
|
|
"""Expects {"text":"text", "author_id"=12, "unregistered_author"="mamam", "post_id"=12 }"""
|
|
req = json.loads(request.body)
|
|
post = Post.objects.get(pk=int(req['post_id']))
|
|
# author = User.objects.get(pk=int(req['author_id']))
|
|
p = Comment.objects.create(
|
|
post =post,
|
|
unregistered_author=req['unregistered_author'],
|
|
text=req['text']
|
|
)
|
|
p.save()
|
|
ser = serializers.serialize("python" , [p])
|
|
return JsonResponse(ser, safe=False)
|
|
def Comments_GET():
|
|
entry = Post.objects.get(pk=request.GET.get('post',0))
|
|
comments = Comment.objects.filter(post=entry)
|
|
|
|
return JsonResponse(serializers.serialize("python",comments ), safe=False)
|
|
def Comments_DELETE():
|
|
pass
|
|
|
|
return {"GET":Comments_GET, "POST":Comments_POST, "DELETE":Comments_DELETE}[request.method]()
|
|
|
|
@csrf_exempt
|
|
@require_http_methods(["POST", "GET", "DELETE"])
|
|
def Menu_Handler(request):
|
|
def Menu_POST():
|
|
"""Expects {"text":"text", "link":"link"}"""
|
|
req = json.loads(request.body)
|
|
# author = User.objects.get(pk=int(req['author_id']))
|
|
p = MenuEntry.objects.create(
|
|
link = req['link'],
|
|
alt=req['text']
|
|
)
|
|
p.save()
|
|
ser = serializers.serialize("python" , [p])
|
|
return JsonResponse(ser, safe=False)
|
|
def Menu_GET():
|
|
menu = MenuEntry.objects.all()
|
|
return JsonResponse(serializers.serialize("python", menu ), safe=False)
|
|
def Menu_DELETE():
|
|
req = json.loads(request.body)
|
|
MenuEntry.objects.get(id=req['id']).delete()
|
|
return JsonResponse({"status":"ok"})
|
|
|
|
return {"GET":Menu_GET, "POST":Menu_POST, "DELETE":Menu_DELETE}[request.method]()
|
|
|
|
|
|
|