Skip to content
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
13 changes: 1 addition & 12 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
Django settings for config project.

Generated by 'django-admin startproject' using Django 4.1.2.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -37,6 +25,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'files_management'
]

MIDDLEWARE = [
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 files_management.controller import post_controller

api=NinjaAPI()
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/' , post_controller)

]

24 changes: 23 additions & 1 deletion files_management/controller.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
from django.shortcuts import render
# Create your views here.
from ninja import Router
import re
from files_management.utils import list_posts,save_post,get_post,del_post

post_controller = Router()
@post_controller.get('')
def list_posts(request):
return list_posts()


@post_controller.get('/{title}')
def get_post(request ,title):
return get_post(title)

@post_controller.post('')
def save_post(request , title , content):
return save_post(title,content)



@post_controller.delete('/{title}')
def del_post(request ,title):
return del_post(title)
9 changes: 8 additions & 1 deletion files_management/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def list_posts():
for filename in filenames if filename.endswith(".md")))



def save_post(title, content):
"""
Saves a blog post, given its title and Markdown
Expand All @@ -23,6 +24,7 @@ def save_post(title, content):
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(content))
return{'Message':f'Post {title} Created Successfully'}


def get_post(title):
Expand All @@ -38,4 +40,9 @@ def get_post(title):


def del_post(title):
pass
filename = f"posts/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
else:
return {'Message':'this post doesnt exist'}
return {'Message':f'Post {title} deleted Successfully'}