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

Baneen-Ahmed-task2 #94

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
4 changes: 4 additions & 0 deletions config/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ninja import NinjaAPI

api = NinjaAPI()

8 changes: 8 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django import urls
from django.contrib import admin
from django.urls import path
from ninja import NinjaAPI
from config import api
from headless.controllers import config_controller

api = NinjaAPI()
api.add_router('headless', config_controller)

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


config_controller = Router()

import re

from django.core.files.base import ContentFile
from django.core.files.storage import default_storage

@config_controller.get("posts")
def list_posts(request):
_, filenames = default_storage.listdir("posts")
return list(sorted(re.sub(r"\.md$", "", filename)
for filename in filenames if filename.endswith(".md")))



@config_controller.get("title")
def get_post(request, title):
try:
f = default_storage.open(f"posts/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None


@config_controller.post("post")
def update_post(request, title, content):
filename = f"posts/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(content))


1 change: 1 addition & 0 deletions posts/Python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Python is cool