Skip to content

Instantly share code, notes, and snippets.

@antunesleo
Last active May 12, 2023 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antunesleo/bcb70b6ceec4b04d238575fa03182239 to your computer and use it in GitHub Desktop.
Save antunesleo/bcb70b6ceec4b04d238575fa03182239 to your computer and use it in GitHub Desktop.
import json
from django.db import transaction
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .kafka_producer import publish_note_created
from .models import Note
@csrf_exempt
def note_view(request):
if request.method == "POST":
data = json.loads(request.body)
title = data.get("title", "")
content = data.get("content", "")
if title and content:
with transaction.atomic():
note = Note.objects.create(title=title, content=content)
publish_note_created(note.as_dict())
return JsonResponse(
{"id": note.id, "title": note.title, "content": note.content}
)
else:
return JsonResponse({"error": "Missing title or content"}, status=400)
else:
return JsonResponse({"error": "Method not allowed"}, status=405)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment