diff --git a/config/settings.py b/config/settings.py index e332a2b..f9efde6 100644 --- a/config/settings.py +++ b/config/settings.py @@ -11,6 +11,7 @@ """ from pathlib import Path +from typing import Any # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -25,7 +26,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['*'] # Application definition @@ -37,6 +38,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..c2d46dc 100644 --- a/config/urls.py +++ b/config/urls.py @@ -1,21 +1,14 @@ -"""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) +] \ No newline at end of file diff --git a/files_management/controller.py b/files_management/controller.py index 45a329d..aa0ebec 100644 --- a/files_management/controller.py +++ b/files_management/controller.py @@ -1,2 +1,38 @@ +import re from django.shortcuts import render -# Create your views here. +from django.core.files.storage import default_storage +from django.core.files.base import ContentFile +from ninja import Router + +post_controller = Router() + +# function to get all the posts +@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"))) + +# function to create new post and save it +@post_controller.post('') +def save_post(request , title , content): + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) + default_storage.save(filename, ContentFile(content)) + +# function to get the post using the title +@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 +#function to delete a post using title +@post_controller.delete('/{title}') +def del_post(request ,title): + filename = f"posts/{title}.md" + default_storage.delete(filename) + return {'messege': 'deleted.'} + \ No newline at end of file