Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']


# Application definition
Expand All @@ -37,6 +37,8 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',


]

MIDDLEWARE = [
Expand Down
56 changes: 56 additions & 0 deletions files_management/controller.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
from django.shortcuts import render
from ninja import Router, Schema
import re
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage


class TandC(Schema):
title: str
content: str = None


# Create your views here.

post_control = Router()


@post_control.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")))


@post_control.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_control.put('')
def save_post(request, paylod: TandC):
"""
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/{paylod.title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(paylod.content))


@post_control.delete('/{title}')
def del_post(request, title):
filename = f"posts/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)