From 1e8647d478da384c7d0c7dda18ebd97137fd41ba Mon Sep 17 00:00:00 2001 From: Emanuel Rodriguez Date: Sun, 7 Aug 2022 20:16:06 -0700 Subject: [PATCH] adds model serializer and urls for interacting with api --- requirements.txt | 20 ++++ snippets/serializers.py | 12 +-- snippets/urls.py | 7 ++ snippets/views.py | 47 ++++++++- subl-proj.sublime-project | 8 ++ subl-proj.sublime-workspace | 184 ++++++++++++++++++++++++++++++++++++ tutorial/urls.py | 5 +- 7 files changed, 272 insertions(+), 11 deletions(-) create mode 100644 requirements.txt create mode 100644 snippets/urls.py create mode 100644 subl-proj.sublime-project create mode 100644 subl-proj.sublime-workspace diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1181f75 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,20 @@ +asgiref==3.5.2 +certifi==2022.6.15 +charset-normalizer==2.1.0 +colorama==0.4.5 +commonmark==0.9.1 +defusedxml==0.7.1 +Django==4.1 +djangorestframework==3.13.1 +httpie==3.2.1 +idna==3.3 +multidict==6.0.2 +Pygments==2.12.0 +PySocks==1.7.1 +pytz==2022.1 +requests==2.28.1 +requests-toolbelt==0.9.1 +rich==12.5.1 +sqlparse==0.4.2 +tzdata==2022.1 +urllib3==1.26.11 diff --git a/snippets/serializers.py b/snippets/serializers.py index 7f0e01a..8ec0781 100644 --- a/snippets/serializers.py +++ b/snippets/serializers.py @@ -1,13 +1,10 @@ from rest_framework import serializers from snippets.models import Snippet, LANG_CHOICES, STYLE_CHOICES -class SnippetSerializer(serializers.Serializer): - id = serializers.IntegerField(read_only=True) - title = serializers.CharField(required=False, allow_blank=True, max_length=100) - code = serializers.CharField(style={"base_template", "textarea.html"}) - line_numbers = serializers.BooleanField() - language = serializers.ChoiceField(choices=LANG_CHOICES, default="python") - style = serializers.ChoiceField(choices=STYLE_CHOICES, default="friendly") +class SnippetSerializer(serializers.ModelSerializer): + class Meta: + model = Snippet + fields = ["id", "title", "code", "language", "line_numbers", "style"] def create(self, validated_data): return Snippet.objects.create(**validated_data) @@ -20,6 +17,7 @@ class SnippetSerializer(serializers.Serializer): instance.style = validated_data.get("style", instance.style) instance.save() + return instance diff --git a/snippets/urls.py b/snippets/urls.py new file mode 100644 index 0000000..5a360d7 --- /dev/null +++ b/snippets/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from snippets import views + +urlpatterns = [ + path("snippets/", views.snippet_list), + path("snippets//", views.snippet_detail) +] \ No newline at end of file diff --git a/snippets/views.py b/snippets/views.py index 91ea44a..47b4c2e 100644 --- a/snippets/views.py +++ b/snippets/views.py @@ -1,3 +1,46 @@ -from django.shortcuts import render +from django.http import HttpResponse, JsonResponse +from django.views.decorators.csrf import csrf_exempt +from rest_framework.parsers import JSONParser +from snippets.models import Snippet +from snippets.serializers import SnippetSerializer -# Create your views here. +@csrf_exempt +def snippet_list(request): + + if request.method == "GET": + snippets = Snippet.objects.all() + serializer = SnippetSerializer(snippets, many=True) + return JsonResponse(data=serializer.data, safe=False) + + elif request.method == "POST": + data = JSONParser().parse(request) + serializer = SnippetSerializer(data=data) + if serializer.is_valid(): + serializer.save() + return JsonResponse(serializer.data, status=201) + return JsonResponse(serializer.errors, status=400) + + +@csrf_exempt +def snippet_detail(request, pk): + + try: + snippet = Snippet.objects.get(pk=pk) + except Snippet.DoesNotExist: + return HttpResponse(status=404) + + if request.method == "GET": + serializer = SnippetSerializer(snippet) + return JsonResponse(data=serializer.data) + + elif request.method == "PUT": + data = JSONParser().parse(request) + serializer = SnippetSerializer(snippet, data=data) + if serializer.is_valid(): + serializer.save() + return JsonResponse(serializer.data) + return JsonResponse(serializer.errors, status=400) + + elif request.method == "DELETE": + snippet.delete() + return HttpResponse(status=204) diff --git a/subl-proj.sublime-project b/subl-proj.sublime-project new file mode 100644 index 0000000..24db303 --- /dev/null +++ b/subl-proj.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "." + } + ] +} diff --git a/subl-proj.sublime-workspace b/subl-proj.sublime-workspace new file mode 100644 index 0000000..d329588 --- /dev/null +++ b/subl-proj.sublime-workspace @@ -0,0 +1,184 @@ +{ + "auto_complete": + { + "selected_items": + [ + ] + }, + "build_system": "", + "build_system_choices": + [ + ], + "build_varint": "", + "command_palette": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + [ + "index", + "Help: Indexing Status" + ], + [ + "settings", + "Preferences: Settings" + ], + [ + "theme", + "UI: Select Theme" + ], + [ + "install", + "Package Control: Install Package" + ], + [ + "select col", + "UI: Select Color Scheme" + ], + [ + "color", + "UI: Select Color Scheme" + ], + [ + "c++", + "Set Syntax: C++" + ], + [ + "set syntx: c++", + "Set Syntax: C++" + ], + [ + "select color", + "UI: Select Color Scheme" + ] + ], + "width": 0.0 + }, + "console": + { + "height": 0.0, + "history": + [ + ] + }, + "distraction_free": + { + "menu_visible": true, + "show_minimap": false, + "show_open_files": false, + "show_tabs": false, + "side_bar_visible": false, + "status_bar_visible": false + }, + "file_history": + [ + "/C/Users/eeman/learning/django-rest-tutorial/snippets/models.py", + "/C/Users/eeman/learning/django-rest-tutorial/snippets/serializers.py", + "/C/Users/eeman/learning/django-rest-tutorial/.gitignore", + "/C/Users/eeman/AppData/Roaming/Sublime Text/Packages/User/Preferences.sublime-settings", + "/C/Users/eeman/apps/scripts/activate-vc-vars.bat.txt" + ], + "find": + { + "height": 32.0 + }, + "find_in_files": + { + "height": 0.0, + "where_history": + [ + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + ], + "highlight": true, + "in_selection": false, + "preserve_case": false, + "regex": false, + "replace_history": + [ + ], + "reverse": false, + "scrollbar_highlights": true, + "show_context": true, + "use_buffer2": true, + "use_gitignore": true, + "whole_word": false, + "wrap": true + }, + "incremental_find": + { + "height": 32.0 + }, + "input": + { + "height": 0.0 + }, + "menu_visible": true, + "output.find_results": + { + "height": 0.0 + }, + "pinned_build_system": "", + "replace": + { + "height": 60.8 + }, + "save_all_on_build": true, + "select_file": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + [ + "gitig", + ".gitignore" + ], + [ + "seria", + "snippets\\serializers.py" + ], + [ + "mdoel", + "snippets\\models.py" + ] + ], + "width": 0.0 + }, + "select_project": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "select_symbol": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "settings": + { + }, + "show_minimap": true, + "show_open_files": false, + "show_tabs": true, + "side_bar_visible": true, + "side_bar_width": 183.0, + "status_bar_visible": true, + "template_settings": + { + } +} diff --git a/tutorial/urls.py b/tutorial/urls.py index fc897b3..1e5e1e4 100644 --- a/tutorial/urls.py +++ b/tutorial/urls.py @@ -14,8 +14,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ - path('admin/', admin.site.urls), + path("", include("snippets.urls")), + path('admin/', admin.site.urls) ]