diff --git a/config/settings.py b/config/settings.py index e332a2b..df8f25d 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..c3f00d0 100644 --- a/config/urls.py +++ b/config/urls.py @@ -1,21 +1,11 @@ -"""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 posts_controller +api = NinjaAPI() +api.add_router('/posts', posts_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..59505ef 100644 --- a/files_management/controller.py +++ b/files_management/controller.py @@ -1,2 +1,45 @@ from django.shortcuts import render # Create your views here. + +# Create your views here. +import re +from ninja import Router +posts_controller=Router() + +from django.core.files.base import ContentFile +from django.core.files.storage import default_storage + + +@posts_controller.get('') +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"))) + +@posts_controller.post('/{title}') + +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)) + +@posts_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 diff --git a/files_management/utils.py b/files_management/utils.py index fbcb059..e69de29 100644 --- a/files_management/utils.py +++ b/files_management/utils.py @@ -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/san.md b/posts/san.md new file mode 100644 index 0000000..9afddfc --- /dev/null +++ b/posts/san.md @@ -0,0 +1 @@ +sara \ No newline at end of file