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
2 changes: 1 addition & 1 deletion .idea/digitalize_task2_backend.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
22 changes: 7 additions & 15 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
"""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 ninja import NinjaAPI
from files_management.FileManController import files_controller

api = NinjaAPI()

api.add_router('/', files_controller)

urlpatterns = [
path('admin/', admin.site.urls),
path('api/', api.urls)
]
76 changes: 76 additions & 0 deletions files_management/FileManController.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from files_management.utils import list_posts, get_post, save_post, del_post, exist_post
# from django.shortcuts import render
from ninja import Router, Schema


# Create your views here.

class FileBody(Schema):
"""this Schema for posting new post"""
file_title: str
file_content: str = None


class FileBodyUpdate(Schema):
"""this Schema for update post"""
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'}

# this method for posting new file
@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'}


# this method for updating existing file
@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'}
2 changes: 0 additions & 2 deletions files_management/controller.py

This file was deleted.

19 changes: 18 additions & 1 deletion files_management/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,21 @@ def get_post(title):


def del_post(title):
pass
"""
delete file if exists
"""
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):
"""Check if file exists"""
filename = f"posts/{title}.md"
if default_storage.exists(filename):
return True
else:
return False
2 changes: 1 addition & 1 deletion posts/post_1.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is a content of post 1
This is the content of post 1
6 changes: 0 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
asgiref==3.5.2
Django==4.1.2
django-ninja==0.19.1
pydantic==1.10.2
sqlparse==0.4.3
typing_extensions==4.4.0