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

Aya Jafar #87

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
24 changes: 9 additions & 15 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
"""config URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from ninja import NinjaAPI
from headless.utils import router


api = NinjaAPI()

api.add_router("/posts", router)


urlpatterns = [
path('admin/', admin.site.urls),
path('api/', api.urls)
]
40 changes: 34 additions & 6 deletions headless/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage

from ninja import Router

def list_posts():

router = Router()


@router.get('/view-all')
def list_posts(request):
"""
Returns a list of all names of blog posts.
"""
Expand All @@ -13,7 +19,8 @@ def list_posts():
for filename in filenames if filename.endswith(".md")))


def save_post(title, content):
@router.post('/create')
def save_post(request, title: str, content: str):
"""
Saves a blog post, given its title and Markdown
content. If an existing post with the same title already exists,
Expand All @@ -23,9 +30,11 @@ def save_post(title, content):
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(content))
return "New File was created successfully"


def get_post(title):
@router.get('/get-one-post/{title}')
def get_post(request, title: str):
"""
Retrieves a post by its title. If no such
post exists, the function returns None.
Expand All @@ -34,8 +43,27 @@ def get_post(title):
f = default_storage.open(f"posts/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None
return f"posts/{title}.md does not exist"


def del_post(title):
pass
@router.put('/update/{title}')
def update_post(request, title: str, new_content: str):
filename = f"posts/{title}.md"

if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(new_content))
return f"The file {filename} was updated"
else:
return f"{filename} does not exist"


@router.delete('/delete/{title}')
def del_post(request, title: str):
filename = f"posts/{title}.md"

if default_storage.exists(filename):
default_storage.delete(filename)
return f"{title} File was deleted"
else:
return f"{filename} does not exist"
1 change: 1 addition & 0 deletions posts/Laith-motto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Django lives , Php sucks