From 2f411238e24ce64dac7f56713f2b0fefda11f2de Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Oct 2022 23:27:09 +0300 Subject: [PATCH] solve take 2 --- config/settings.py | 4 +-- config/urls.py | 19 ++++---------- files_management/controller.py | 47 ++++++++++++++++++++++++++++++++++ posts/post_2.md | 1 - posts/post_4.md | 1 + posts/post_5.md | 1 + posts/post_6.md | 1 + 7 files changed, 57 insertions(+), 17 deletions(-) delete mode 100644 posts/post_2.md create mode 100644 posts/post_4.md create mode 100644 posts/post_5.md create mode 100644 posts/post_6.md diff --git a/config/settings.py b/config/settings.py index e332a2b..d2da12d 100644 --- a/config/settings.py +++ b/config/settings.py @@ -25,8 +25,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] - +ALLOWED_HOSTS = ['*'] # Application definition @@ -37,6 +36,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..1f361c7 100644 --- a/config/urls.py +++ b/config/urls.py @@ -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 ninja import NinjaAPI +from files_management.controller import post_controller +api = NinjaAPI() +api.add_router('/title',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..9648189 100644 --- a/files_management/controller.py +++ b/files_management/controller.py @@ -1,2 +1,49 @@ from django.shortcuts import render +from ninja import Router # Create your views here. +import re +from django.core.files.base import ContentFile +from django.core.files.storage import default_storage +post_controller = Router() + +@post_controller.get('') +def list_posts(request): + _, 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}') +def get_post(request,title): + try: + f = default_storage.open(f"posts/{title}.md") + return f.read().decode("utf-8") + except FileNotFoundError: + return None + +@post_controller.post('/{title}/{content}') +def creatNew_post(request,title, content): + filename = f"posts/{title}.md" + if default_storage.exists(filename): + return {'Massage' : 'This title of post already exists if yot want replace the content go 2 replace_post'} + default_storage.save(filename, ContentFile(content)) + +@post_controller.put('/{title}/{content}') +def replace_post(request,title, content): + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) + default_storage.save(filename, ContentFile(content)) + else : return {'Massage' : 'This post isnot exists '} + +@post_controller.delete('/{title}') +def del_post(request,title): + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) + else : return {'Massage' : 'This post isnot exists '} + + + + + diff --git a/posts/post_2.md b/posts/post_2.md deleted file mode 100644 index aeba1ac..0000000 --- a/posts/post_2.md +++ /dev/null @@ -1 +0,0 @@ -This is the content of post 2 \ No newline at end of file diff --git a/posts/post_4.md b/posts/post_4.md new file mode 100644 index 0000000..051386f --- /dev/null +++ b/posts/post_4.md @@ -0,0 +1 @@ +This is a content of post 4 \ No newline at end of file diff --git a/posts/post_5.md b/posts/post_5.md new file mode 100644 index 0000000..70c379b --- /dev/null +++ b/posts/post_5.md @@ -0,0 +1 @@ +Hello world \ No newline at end of file diff --git a/posts/post_6.md b/posts/post_6.md new file mode 100644 index 0000000..9af1a4d --- /dev/null +++ b/posts/post_6.md @@ -0,0 +1 @@ +this is post 6 \ No newline at end of file