This commit is contained in:
emilekm2142 2020-01-20 14:59:40 +01:00
parent 44e84ef639
commit 128878c69d
29 changed files with 191 additions and 5 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/workspace.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pracowniapp2.iml" filepath="$PROJECT_DIR$/.idea/pracowniapp2.iml" />
</modules>
</component>
</project>

27
.idea/pracowniapp2.iml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="FacetManager">
<facet type="django" name="Django">
<configuration>
<option name="rootFolder" value="$MODULE_DIR$" />
<option name="settingsModule" value="przyrost2/settings.py" />
<option name="manageScript" value="$MODULE_DIR$/manage.py" />
<option name="environment" value="&lt;map/&gt;" />
<option name="doNotUseTestRunner" value="false" />
<option name="trackFilePattern" value="migrations" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Django" />
</component>
<component name="TestRunnerService">
<option name="projectConfiguration" value="Twisted Trial" />
<option name="PROJECT_TEST_RUNNER" value="Twisted Trial" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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'),
),
]

View File

@ -12,6 +12,7 @@ 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()
category = models.ForeignKey(Category, on_delete=models.CASCADE) category = models.ForeignKey(Category, on_delete=models.CASCADE)
@staticmethod @staticmethod
def getPostsOfAuthor(author): def getPostsOfAuthor(author):
@ -25,7 +26,7 @@ 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() a = Post.objects.all().order_by('created_at')
paginator = Paginator(a,5) paginator = Paginator(a,5)
return paginator.get_page(page) return paginator.get_page(page)
class StaticPage(models.Model): class StaticPage(models.Model):
@ -40,7 +41,7 @@ class Menu(models.Model):
class Comment(models.Model): class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE) 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) unregistered_author = models.CharField(max_length=20)
text = models.TextField() text = models.TextField()
class Config(models.Model): class Config(models.Model):

View File

@ -4,4 +4,8 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), 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")
] ]

View File

@ -2,7 +2,109 @@ from django.shortcuts import render
# Create your views here. # Create your views here.
from django.http import HttpResponse 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): def index(request):
return HttpResponse("Hello, world. You're at the polls index.") 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]()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -38,9 +38,11 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'przyrost.apps.PrzyrostConfig', 'przyrost.apps.PrzyrostConfig',
'corsheaders'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
@ -121,3 +123,4 @@ USE_TZ = False
# https://docs.djangoproject.com/en/2.2/howto/static-files/ # https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
CORS_ORIGIN_ALLOW_ALL = True