From bd77d5f4020255b5ce357d0f01df52916ecf320f Mon Sep 17 00:00:00 2001 From: MorEmad <69675013+MorEmad@users.noreply.github.com> Date: Fri, 22 Jul 2022 10:17:50 +0300 Subject: [PATCH] Mortaha Emad --- config/settings.py | 1 - config/urls.py | 6 ++++++ headless/controllers.py | 43 +++++++++++++++++++++++++++++++++++++++++ headless/models.py | 20 +++++++++++++++++-- headless/utils.py | 4 +++- 5 files changed, 70 insertions(+), 4 deletions(-) diff --git a/config/settings.py b/config/settings.py index eb18df3f..e11aad5d 100644 --- a/config/settings.py +++ b/config/settings.py @@ -37,7 +37,6 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'headless', ] diff --git a/config/urls.py b/config/urls.py index d52e5777..918eb63d 100644 --- a/config/urls.py +++ b/config/urls.py @@ -15,7 +15,13 @@ """ from django.contrib import admin from django.urls import path +from ninja import NinjaAPI +from headless.controllers import mRouter + +api= NinjaAPI() +api.add_router('/',mRouter) urlpatterns = [ path('admin/', admin.site.urls), + path('api/', api.urls), ] diff --git a/headless/controllers.py b/headless/controllers.py index e69de29b..e4cb862a 100644 --- a/headless/controllers.py +++ b/headless/controllers.py @@ -0,0 +1,43 @@ +from turtle import title +from datetime import date +from typing import List +from django.shortcuts import get_object_or_404 + +from headless.models import PostIN, PostOut, Post +from headless.utils import * +from ninja import Router + +mRouter = Router() + + + + +@mRouter.post("/create a new post") +def create_post(request, payload: PostIN): + post = Post.objects.create(**payload.dict()) + return {"title": post.title} + +@mRouter.get(f"/Retrieves a post/{title()}", response=PostOut) +def get_post(request, title: str): + post = get_object_or_404(Post, title) + return post + +@mRouter.get("/Returns all posts", response=List[PostOut]) +def list_post(request): + qs = Post.objects.all() + return qs + +@mRouter.put(f"/update a post/{title()}") +def update_post(request, title: str, payload: PostIN): + post = get_object_or_404(Post, title) + for attr, value in payload.dict().items(): + setattr(post, attr, value) + post.save() + return {"success": True} + + +@mRouter.delete(f"/delete a certain post/{title()}") +def delete_employee(request, title: str): + post = get_object_or_404(Post, title) + post.delete() + return {"success": True} diff --git a/headless/models.py b/headless/models.py index 71a83623..82f144ce 100644 --- a/headless/models.py +++ b/headless/models.py @@ -1,3 +1,19 @@ -from django.db import models -# Create your models here. +from ninja import NinjaAPI, Schema + + +api=NinjaAPI() + +class Post: + title: str + content: str + + +class PostIN(Schema): + title: str + content: str + + +class PostOut(Schema): + first_name: str + last_name: str diff --git a/headless/utils.py b/headless/utils.py index 20a48f9c..80338683 100644 --- a/headless/utils.py +++ b/headless/utils.py @@ -38,4 +38,6 @@ def get_post(title): def del_post(title): - pass \ No newline at end of file + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) \ No newline at end of file