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
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'files_management'
]

MIDDLEWARE = [
Expand Down
19 changes: 5 additions & 14 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
"""config URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.1/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 files_management.mycontroller import files_controller
from ninja import NinjaAPI

api = NinjaAPI()
api.add_router('/', files_controller)
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', api.urls),
]
2 changes: 0 additions & 2 deletions files_management/controller.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
from django.shortcuts import render
# Create your views here.
75 changes: 75 additions & 0 deletions files_management/mycontroller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from files_management.utils import list_posts, get_post, save_post, del_post, exist_post

from ninja import Router, Schema



class FileBody(Schema):

file_title: str
file_content: str = None


class FileBodyUpdate(Schema):

file_content: str = None


files_controller = Router()


# list all Posts
@files_controller.get('/posts')
def get_files(request):
return list_posts()


@files_controller.get('/posts/{paraTitle}')
def get_files_by_title(request, paraTitle: str):
try:
if get_post(title=paraTitle):
return get_post(title=paraTitle)
elif get_post(title=paraTitle) is None:
return {'Message': 'There is no post with that name'}
except:
return {'Message': 'There is an error occurs'}


@files_controller.post('/posts')
def post_file(request, payload: FileBody):
try:
if payload.file_title is not None:
save_post(title=payload.file_title, content=payload.file_content)
return {'Message': 'File Saved Successfully'}
else:
return {'Message': 'Please provide file name'}
except:
return {'Message': 'There is an error occurs'}



@files_controller.put('/posts/{paraTitle}')
def update_file(request, payload: FileBodyUpdate, paraTitle):
try:
if exist_post(paraTitle):
if paraTitle is not None and payload.file_content is not None:
save_post(title=paraTitle, content=payload.file_content)
return {'Message': 'File Updated Successfully'}
else:
return {'Message': 'Please provide file name and file content to update'}
else:
return {'Message': 'There is no file with that name'}
except:
return {'Message': 'There is an error occurs'}


@files_controller.delete('/posts/{paraTitle}')
def delete_file(request, paraTitle):
try:
if paraTitle is not None:
if del_post(paraTitle):
return {'Message': 'File deleted Successfully'}
else:
return {'Message': 'There is no file with that name, Please provide a correct file name to delete it'}
except:
return {'Message': 'There is an error occurs'}
22 changes: 17 additions & 5 deletions files_management/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ def save_post(title, content):


def get_post(title):
"""
Retrieves a post by its title. If no such
post exists, the function returns None.
"""

try:
f = default_storage.open(f"posts/{title}.md")
return f.read().decode("utf-8")
Expand All @@ -38,4 +35,19 @@ 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 False
return {'Message': 'File deleted Successfully'}


def exist_post(title):

filename = f"posts/{title}.md"
if default_storage.exists(filename):
return True
else:
return False