Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mortaha Emad #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'headless',
]

Expand Down
6 changes: 6 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
43 changes: 43 additions & 0 deletions headless/controllers.py
Original file line number Diff line number Diff line change
@@ -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}
20 changes: 18 additions & 2 deletions headless/models.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion headless/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ def get_post(title):


def del_post(title):
pass
filename = f"posts/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)