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
21 changes: 5 additions & 16 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
"""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.controller import post_controller
api = NinjaAPI()
api.add_router('/posts', post_controller)
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', api.urls),
]
46 changes: 46 additions & 0 deletions files_management/controller.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
from django.shortcuts import render
import re

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

from ninja import Router
post_controller = Router()
@post_controller.get('/posts')
def list_posts(request):
"""
Returns a list of all names of blog posts.
"""
_, filenames = default_storage.listdir("posts")
return list(sorted(re.sub(r"\.md$", "", filename)
for filename in filenames if filename.endswith(".md")))

@post_controller.get('/{title},/{content}')
def save_post(request,title, content):
"""
Saves a blog post, given its title and Markdown
content. If an existing post with the same title already exists,
it is replaced.
"""
filename = f"posts/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(content))

@post_controller.get('/{title}')
def get_post(request,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")
except FileNotFoundError:
return None

@post_controller.get('/{title}')
def del_post(request,title):
filename = f"posts/{title}.md"
default_storage.delete(filename)
return {'messege': 'deleted.'}

# Create your views here.
41 changes: 0 additions & 41 deletions files_management/utils.py

This file was deleted.

1 change: 1 addition & 0 deletions posts/post_3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a content of post 3
1 change: 1 addition & 0 deletions posts/post_4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a content of post 4