From 2e7ae7b763cbfa93b652ab7ec1dd48962694214a Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sat, 9 Feb 2019 00:51:43 +0000 Subject: [PATCH] Created some template http endpoints for the api and a serializer for the Song model class --- api/serializers.py | 7 +++++++ api/urls.py | 10 ++++++++++ api/views.py | 33 ++++++++++++++++++++++++++++++++- database.cnf | 7 +++++++ icarus/settings.py | 7 +++++-- icarus/urls.py | 18 ++---------------- requirements.txt | 1 + 7 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 api/serializers.py create mode 100644 api/urls.py create mode 100644 database.cnf diff --git a/api/serializers.py b/api/serializers.py new file mode 100644 index 0000000..028cfbc --- /dev/null +++ b/api/serializers.py @@ -0,0 +1,7 @@ +from rest_framework import serializers +from .models import Song + +class SongSerialzer(serializers.ModelSerializer): + class Meta: + model = Song + fields = ('id', 'title', 'albumn', 'year', 'genre', 'track_number', 'file') diff --git a/api/urls.py b/api/urls.py new file mode 100644 index 0000000..04c6ec6 --- /dev/null +++ b/api/urls.py @@ -0,0 +1,10 @@ +from django.urls import path +from rest_framework.urlpatterns import format_suffix_patterns +from api import views + +urlpatterns = [ + path('song/', views.UploadList().as_view()), + path('upload/song/', views.UploadList().as_view()) +] + +urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/api/views.py b/api/views.py index 91ea44a..e14b7cf 100644 --- a/api/views.py +++ b/api/views.py @@ -1,3 +1,34 @@ from django.shortcuts import render +from rest_framework import status +from rest_framework.response import Response +from rest_framework.views import APIView +from .models import Song +from .serializers import SongSerialzer + +class UploadList(APIView): + + def post(self, request, format=None): + serializer = SongSerialzer(data=request.data) + if serializer.is_valid(): + serializer.save() + return Response({ + 'demo':'success' + }) + + return Response({ + 'demo': 'failure' + }, status=status.HTTP_400_BAD_REQUEST) + + def get(self, request, format=None): + songs = Song.objects.all() + serializer = SongSerialzer(songs, many=True) + return Response(serializer.data) + +class UploadDetail(APIView): + + def get_object(self, pk): + try: + return Song.objects.get(pk=pk) + except Song.DoesNotExist: + raise Http404 -# Create your views here. diff --git a/database.cnf b/database.cnf new file mode 100644 index 0000000..d1436d8 --- /dev/null +++ b/database.cnf @@ -0,0 +1,7 @@ +[client] +database = +host = +user = +password = +port = +default-character-set = utf8 diff --git a/icarus/settings.py b/icarus/settings.py index 35b5ebd..7f7ee3c 100644 --- a/icarus/settings.py +++ b/icarus/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'api', + 'rest_framework' ] MIDDLEWARE = [ @@ -76,8 +77,10 @@ WSGI_APPLICATION = 'icarus.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'ENGINE': 'django.db.backends.mysql', + 'OPTIONS': { + 'read_default_file': os.path.join(BASE_DIR, 'database.cnf'), + } } } diff --git a/icarus/urls.py b/icarus/urls.py index 45c855c..8d1fb0e 100644 --- a/icarus/urls.py +++ b/icarus/urls.py @@ -1,21 +1,7 @@ -"""icarus URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/2.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 django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), + path('api/', include('api.urls')) ] diff --git a/requirements.txt b/requirements.txt index 8c4417c..d44cff6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ icarus django>=2.1 +djangorestframework>=3.9.1