diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..0e40fe8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ + +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3999087 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..51540e6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pracowniapp2.iml b/.idea/pracowniapp2.iml new file mode 100644 index 0000000..b84515a --- /dev/null +++ b/.idea/pracowniapp2.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index 744b08d..d15337a 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/przyrost/__pycache__/__init__.cpython-36.pyc b/przyrost/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..7508675 Binary files /dev/null and b/przyrost/__pycache__/__init__.cpython-36.pyc differ diff --git a/przyrost/__pycache__/admin.cpython-36.pyc b/przyrost/__pycache__/admin.cpython-36.pyc new file mode 100644 index 0000000..20dea81 Binary files /dev/null and b/przyrost/__pycache__/admin.cpython-36.pyc differ diff --git a/przyrost/__pycache__/apps.cpython-36.pyc b/przyrost/__pycache__/apps.cpython-36.pyc new file mode 100644 index 0000000..f93be72 Binary files /dev/null and b/przyrost/__pycache__/apps.cpython-36.pyc differ diff --git a/przyrost/__pycache__/models.cpython-36.pyc b/przyrost/__pycache__/models.cpython-36.pyc new file mode 100644 index 0000000..6847d97 Binary files /dev/null and b/przyrost/__pycache__/models.cpython-36.pyc differ diff --git a/przyrost/__pycache__/urls.cpython-36.pyc b/przyrost/__pycache__/urls.cpython-36.pyc new file mode 100644 index 0000000..169b943 Binary files /dev/null and b/przyrost/__pycache__/urls.cpython-36.pyc differ diff --git a/przyrost/__pycache__/views.cpython-36.pyc b/przyrost/__pycache__/views.cpython-36.pyc new file mode 100644 index 0000000..ed46cec Binary files /dev/null and b/przyrost/__pycache__/views.cpython-36.pyc differ diff --git a/przyrost/migrations/0006_auto_20200113_1529.py b/przyrost/migrations/0006_auto_20200113_1529.py new file mode 100644 index 0000000..c8a0f10 --- /dev/null +++ b/przyrost/migrations/0006_auto_20200113_1529.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.2 on 2020-01-13 15:29 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('przyrost', '0005_auto_20191125_1050'), + ] + + operations = [ + migrations.AlterField( + model_name='comment', + name='author', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='przyrost.User'), + ), + ] diff --git a/przyrost/migrations/__pycache__/0001_initial.cpython-36.pyc b/przyrost/migrations/__pycache__/0001_initial.cpython-36.pyc new file mode 100644 index 0000000..8f365f5 Binary files /dev/null and b/przyrost/migrations/__pycache__/0001_initial.cpython-36.pyc differ diff --git a/przyrost/migrations/__pycache__/0002_auto_20191125_1029.cpython-36.pyc b/przyrost/migrations/__pycache__/0002_auto_20191125_1029.cpython-36.pyc new file mode 100644 index 0000000..4139694 Binary files /dev/null and b/przyrost/migrations/__pycache__/0002_auto_20191125_1029.cpython-36.pyc differ diff --git a/przyrost/migrations/__pycache__/0003_auto_20191125_1043.cpython-36.pyc b/przyrost/migrations/__pycache__/0003_auto_20191125_1043.cpython-36.pyc new file mode 100644 index 0000000..fdce6ec Binary files /dev/null and b/przyrost/migrations/__pycache__/0003_auto_20191125_1043.cpython-36.pyc differ diff --git a/przyrost/migrations/__pycache__/0004_auto_20191125_1045.cpython-36.pyc b/przyrost/migrations/__pycache__/0004_auto_20191125_1045.cpython-36.pyc new file mode 100644 index 0000000..691e793 Binary files /dev/null and b/przyrost/migrations/__pycache__/0004_auto_20191125_1045.cpython-36.pyc differ diff --git a/przyrost/migrations/__pycache__/0005_auto_20191125_1050.cpython-36.pyc b/przyrost/migrations/__pycache__/0005_auto_20191125_1050.cpython-36.pyc new file mode 100644 index 0000000..e9a121a Binary files /dev/null and b/przyrost/migrations/__pycache__/0005_auto_20191125_1050.cpython-36.pyc differ diff --git a/przyrost/migrations/__pycache__/0006_auto_20200113_1529.cpython-36.pyc b/przyrost/migrations/__pycache__/0006_auto_20200113_1529.cpython-36.pyc new file mode 100644 index 0000000..0b08421 Binary files /dev/null and b/przyrost/migrations/__pycache__/0006_auto_20200113_1529.cpython-36.pyc differ diff --git a/przyrost/migrations/__pycache__/__init__.cpython-36.pyc b/przyrost/migrations/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..98dfa36 Binary files /dev/null and b/przyrost/migrations/__pycache__/__init__.cpython-36.pyc differ diff --git a/przyrost/models.py b/przyrost/models.py index cf7209f..0402e17 100644 --- a/przyrost/models.py +++ b/przyrost/models.py @@ -12,6 +12,7 @@ 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) @staticmethod def getPostsOfAuthor(author): @@ -25,7 +26,7 @@ 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() + a = Post.objects.all().order_by('created_at') paginator = Paginator(a,5) return paginator.get_page(page) class StaticPage(models.Model): @@ -40,7 +41,7 @@ class Menu(models.Model): class Comment(models.Model): post = models.ForeignKey(Post,on_delete=models.CASCADE) - author=models.ForeignKey(User,on_delete=models.CASCADE) + author=models.ForeignKey(User,on_delete=models.CASCADE, default=None, null=True) unregistered_author = models.CharField(max_length=20) text = models.TextField() class Config(models.Model): diff --git a/przyrost/urls.py b/przyrost/urls.py index 3ef24d9..7e1bb81 100644 --- a/przyrost/urls.py +++ b/przyrost/urls.py @@ -4,4 +4,8 @@ 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") ] \ No newline at end of file diff --git a/przyrost/views.py b/przyrost/views.py index 363c279..6686d95 100644 --- a/przyrost/views.py +++ b/przyrost/views.py @@ -2,7 +2,109 @@ 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): - return HttpResponse("Hello, world. You're at the polls index.") \ No newline at end of file + return "joy" + +@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}""" + req = json.loads(request.body) + category = Category.objects.get(pk=int(req['category'])) + p = Post.objects.create( + created_at=timezone.datetime.now(), + category=category, + text=req['text'] + ) + p.save() + return JsonResponse(serializers.serialize("python", [p]), safe=False) + def Post_GET(): + 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.get(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]() + + + diff --git a/przyrost2/__pycache__/__init__.cpython-36.pyc b/przyrost2/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..59b7515 Binary files /dev/null and b/przyrost2/__pycache__/__init__.cpython-36.pyc differ diff --git a/przyrost2/__pycache__/settings.cpython-36.pyc b/przyrost2/__pycache__/settings.cpython-36.pyc new file mode 100644 index 0000000..e79b17b Binary files /dev/null and b/przyrost2/__pycache__/settings.cpython-36.pyc differ diff --git a/przyrost2/__pycache__/urls.cpython-36.pyc b/przyrost2/__pycache__/urls.cpython-36.pyc new file mode 100644 index 0000000..432d374 Binary files /dev/null and b/przyrost2/__pycache__/urls.cpython-36.pyc differ diff --git a/przyrost2/__pycache__/wsgi.cpython-36.pyc b/przyrost2/__pycache__/wsgi.cpython-36.pyc new file mode 100644 index 0000000..f86bcb7 Binary files /dev/null and b/przyrost2/__pycache__/wsgi.cpython-36.pyc differ diff --git a/przyrost2/settings.py b/przyrost2/settings.py index 69c294e..a70497a 100644 --- a/przyrost2/settings.py +++ b/przyrost2/settings.py @@ -38,9 +38,11 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'przyrost.apps.PrzyrostConfig', +'corsheaders' ] MIDDLEWARE = [ +'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', @@ -121,3 +123,4 @@ USE_TZ = False # https://docs.djangoproject.com/en/2.2/howto/static-files/ STATIC_URL = '/static/' +CORS_ORIGIN_ALLOW_ALL = True \ No newline at end of file