Skip to content

Commit 15e0352

Browse files
author
Mohamed207470
committed
3. Adding an API to an Existing Project -- videos 1 a 5
1 parent 663b066 commit 15e0352

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+105
-21
lines changed

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/todowoo-project.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/__pycache__/admin.cpython-37.pyc

199 Bytes
Binary file not shown.

api/__pycache__/models.cpython-37.pyc

196 Bytes
Binary file not shown.
713 Bytes
Binary file not shown.

api/__pycache__/urls.cpython-37.pyc

405 Bytes
Binary file not shown.

api/__pycache__/views.cpython-37.pyc

1.76 KB
Binary file not shown.

api/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

api/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ApiConfig(AppConfig):
5+
name = 'api'

api/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

api/serializers.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from rest_framework import serializers
2+
from todo.models import Todo
3+
4+
class TodoSerializer(serializers.ModelSerializer):
5+
created = serializers.ReadOnlyField()
6+
datecompleted = serializers.ReadOnlyField()
7+
8+
class Meta:
9+
model = Todo
10+
fields = ['id','title','memo','created','datecompleted','important']

api/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

api/urls.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.urls import path
2+
from . import views
3+
4+
urlpatterns = [
5+
path('todos', views.TodoListCreate.as_view()),
6+
path('todos/<int:pk>', views.TodoRetrieveUpdateDestroy.as_view()),
7+
path('todos/completed', views.TodoCompletedList.as_view()),
8+
]

api/views.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from rest_framework import generics, permissions
2+
3+
from todo.models import Todo
4+
from .serializers import TodoSerializer
5+
6+
class TodoCompletedList(generics.ListAPIView):
7+
serializer_class = TodoSerializer
8+
permission_classes = [permissions.IsAuthenticated]
9+
10+
def get_queryset(self):
11+
user = self.request.user
12+
return Todo.objects.filter(user=user, datecompleted__isnull=False).order_by('-datecompleted')
13+
14+
class TodoListCreate(generics.ListCreateAPIView):
15+
serializer_class = TodoSerializer
16+
permission_classes = [permissions.IsAuthenticated]
17+
18+
def get_queryset(self):
19+
user = self.request.user
20+
return Todo.objects.filter(user=user, datecompleted__isnull=True)
21+
22+
def perform_create(self, serializer):
23+
serializer.save(user=self.request.user)
24+
25+
26+
class TodoRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView):
27+
serializer_class = TodoSerializer
28+
permission_classes = [permissions.IsAuthenticated]
29+
30+
def get_queryset(self):
31+
user = self.request.user
32+
return Todo.objects.filter(user=user)
33+
34+

db.sqlite3

-4 KB
Binary file not shown.

todo/__init__.py

Whitespace-only changes.
-148 Bytes
Binary file not shown.

todo/__pycache__/admin.cpython-37.pyc

472 Bytes
Binary file not shown.

todo/__pycache__/admin.cpython-38.pyc

-465 Bytes
Binary file not shown.

todo/__pycache__/forms.cpython-37.pyc

589 Bytes
Binary file not shown.

todo/__pycache__/forms.cpython-38.pyc

-586 Bytes
Binary file not shown.

todo/__pycache__/views.cpython-37.pyc

3.57 KB
Binary file not shown.

todo/__pycache__/views.cpython-38.pyc

-3.56 KB
Binary file not shown.

todo/migrations/0001_initial.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.0 on 2020-01-31 01:28
1+
# Generated by Django 3.1.6 on 2021-02-16 12:06
22

33
from django.conf import settings
44
from django.db import migrations, models
@@ -21,7 +21,7 @@ class Migration(migrations.Migration):
2121
('title', models.CharField(max_length=100)),
2222
('memo', models.TextField(blank=True)),
2323
('created', models.DateTimeField(auto_now_add=True)),
24-
('datecompleted', models.DateTimeField(null=True)),
24+
('datecompleted', models.DateTimeField(blank=True, null=True)),
2525
('important', models.BooleanField(default=False)),
2626
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
2727
],

todo/migrations/0002_auto_20200131_0131.py

-18
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.
170 Bytes
Binary file not shown.
-159 Bytes
Binary file not shown.

todowoo/__init__.py

Whitespace-only changes.
-151 Bytes
Binary file not shown.
2.27 KB
Binary file not shown.
-2.27 KB
Binary file not shown.

todowoo/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
'django.contrib.messages',
3939
'django.contrib.staticfiles',
4040
'todo',
41+
'api',
42+
'rest_framework',
4143
]
4244

4345
MIDDLEWARE = [

todowoo/urls.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
1616
from django.contrib import admin
17-
from django.urls import path
17+
from django.urls import path, include
1818
from todo import views
1919

20+
2021
urlpatterns = [
2122
path('admin/', admin.site.urls),
2223

@@ -33,4 +34,7 @@
3334
path('todo/<int:todo_pk>', views.viewtodo, name='viewtodo'),
3435
path('todo/<int:todo_pk>/complete', views.completetodo, name='completetodo'),
3536
path('todo/<int:todo_pk>/delete', views.deletetodo, name='deletetodo'),
37+
38+
#API
39+
path('api/', include('api.urls')),
3640
]

0 commit comments

Comments
 (0)