pracowniapp2/przyrost/views.py

126 lines
4.3 KiB
Python
Raw Normal View History

2019-11-25 12:18:15 +01:00
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
2020-01-20 14:59:40 +01:00
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
2020-01-20 18:36:06 +01:00
def index(request, path=''):
"""
Renders the Angular2 SPA
"""
return render(request, 'home.html')
2020-01-20 14:59:40 +01:00
@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():
2020-01-20 18:36:06 +01:00
"""Expects {"text":"tesxt", "category":category_id, "authorNickname":"blalbal"}"""
2020-01-20 14:59:40 +01:00
req = json.loads(request.body)
2020-01-20 18:36:06 +01:00
category = Category.objects.get(pk=int(req['category_id']))
2020-01-20 14:59:40 +01:00
p = Post.objects.create(
created_at=timezone.datetime.now(),
category=category,
2020-01-20 18:36:06 +01:00
text=req['text'],
author_anonim_name = req['authorNickname'] if req['authorNickname'] else None
2020-01-20 14:59:40 +01:00
)
p.save()
return JsonResponse(serializers.serialize("python", [p]), safe=False)
def Post_GET():
2020-01-20 18:36:06 +01:00
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)
2020-01-20 14:59:40 +01:00
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))
2020-01-20 18:36:06 +01:00
comments = Comment.objects.filter(post=entry)
return JsonResponse(serializers.serialize("python",comments ), safe=False)
2020-01-20 14:59:40 +01:00
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]()
2019-11-25 12:18:15 +01:00