diff --git a/.idea/digitalize_task2_backend.iml b/.idea/digitalize_task2_backend.iml index e3bd986..f602895 100644 --- a/.idea/digitalize_task2_backend.iml +++ b/.idea/digitalize_task2_backend.iml @@ -16,7 +16,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 3b7bbcc..671bc3f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ - + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/config/settings.py b/config/settings.py index e332a2b..157e82a 100644 --- a/config/settings.py +++ b/config/settings.py @@ -37,6 +37,7 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'files_management', ] MIDDLEWARE = [ diff --git a/config/urls.py b/config/urls.py index b6601c1..519f702 100644 --- a/config/urls.py +++ b/config/urls.py @@ -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), ] diff --git a/files_management/controller.py b/files_management/controller.py index 45a329d..b8b919f 100644 --- a/files_management/controller.py +++ b/files_management/controller.py @@ -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. diff --git a/files_management/utils.py b/files_management/utils.py deleted file mode 100644 index fbcb059..0000000 --- a/files_management/utils.py +++ /dev/null @@ -1,41 +0,0 @@ -import re - -from django.core.files.base import ContentFile -from django.core.files.storage import default_storage - - -def list_posts(): - """ - 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"))) - - -def save_post(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)) - - -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") - except FileNotFoundError: - return None - - -def del_post(title): - pass diff --git a/posts/post_3.md b/posts/post_3.md new file mode 100644 index 0000000..b12b749 --- /dev/null +++ b/posts/post_3.md @@ -0,0 +1 @@ +This is a content of post 3 \ No newline at end of file diff --git a/posts/post_4.md b/posts/post_4.md new file mode 100644 index 0000000..01c27bc --- /dev/null +++ b/posts/post_4.md @@ -0,0 +1 @@ +This is a content of post 4