diff --git a/config/settings.py b/config/settings.py index e332a2b..20129d9 100644 --- a/config/settings.py +++ b/config/settings.py @@ -1,14 +1,4 @@ -""" -Django settings for config project. -Generated by 'django-admin startproject' using Django 4.1.2. - -For more information on this file, see -https://docs.djangoproject.com/en/4.1/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/4.1/ref/settings/ -""" from pathlib import Path @@ -37,6 +27,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..ea28f29 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() urlpatterns = [ path('admin/', admin.site.urls), + path('posts/' , post_controller) + ] diff --git a/files_management/controller.py b/files_management/controller.py index 45a329d..bf77f2d 100644 --- a/files_management/controller.py +++ b/files_management/controller.py @@ -1,2 +1,25 @@ from django.shortcuts import render -# Create your views here. +from ninja import Router +import re +from files_management.utils import list_posts,save_post,get_post,del_post + +post_controller = Router() +@post_controller.get('') +def list_posts(request): + return list_posts() + + +@post_controller.get('/{title}') +def get_post(request ,title): + return get_post(title) + +@post_controller.post('') +def save_post(request , title , content): + return save_post(title,content) + + + +@post_controller.delete('/{title}') +def del_post(request ,title): + return del_post(title) + diff --git a/files_management/utils.py b/files_management/utils.py index fbcb059..9bbba6e 100644 --- a/files_management/utils.py +++ b/files_management/utils.py @@ -13,6 +13,7 @@ def list_posts(): for filename in filenames if filename.endswith(".md"))) + def save_post(title, content): """ Saves a blog post, given its title and Markdown @@ -23,6 +24,7 @@ def save_post(title, content): if default_storage.exists(filename): default_storage.delete(filename) default_storage.save(filename, ContentFile(content)) + return{'Message':f'Post {title} Created Successfully'} def get_post(title): @@ -38,4 +40,9 @@ def get_post(title): def del_post(title): - pass + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) + else: + return {'Message':'this post doesnt exist'} + return {'Message':f'Post {title} deleted Successfully'}