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

"Moamel Mohammed" #98

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
7 changes: 7 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
"""
from django.contrib import admin
from django.urls import path
from ninja import NinjaAPI
from headless.controllers import cont

api = NinjaAPI()
api.add_router('/post', cont)


urlpatterns = [
path('admin/', admin.site.urls),
path('api/', api.urls)
]
29 changes: 29 additions & 0 deletions headless/controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from ninja import Router
from headless.utils import *

cont = Router()


@cont.get("")
def list_all_posts(request):
return list_posts()


@cont.get("/get_one_post")
def get_one(request, title: str):
return get_post(title)


@cont.post("/create_post")
def create(request, title: str, content: str):
return save_post(title, content)


@cont.put("/update_post")
def update(request, title: str, content: str):
return save_post(title, content)


@cont.delete("/delete_post")
def delete(request, title: str):
return del_post(title)
10 changes: 8 additions & 2 deletions headless/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def list_posts():
"""
_, filenames = default_storage.listdir("posts")
return list(sorted(re.sub(r"\.md$", "", filename)
for filename in filenames if filename.endswith(".md")))
for filename in filenames if filename.endswith(".md")))


def save_post(title, content):
Expand Down Expand Up @@ -38,4 +38,10 @@ def get_post(title):


def del_post(title):
pass
"""
Retrieves a post by its title. If no such
post exists, the function returns None.
"""
filename = f"posts/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
2 changes: 1 addition & 1 deletion posts/php_sucks.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PHP sucks because Layth said so!
cuz layth said so!
7 changes: 0 additions & 7 deletions requirements.txt

This file was deleted.