Commit 59017527 authored by Georgios Xonikis's avatar Georgios Xonikis

Initial Views Completed

parent a5cf0c4c
from django.contrib.auth import get_user_model
from django.db import models
from rest_framework.decorators import action
# Create your models here
User = get_user_model()
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(
'auth.User',
on_delete=models.CASCADE,
to=User,
on_delete=models.CASCADE
)
body = models.TextField()
likes_counter = models.IntegerField(blank=True, null=True)
......@@ -16,3 +19,6 @@ class Post(models.Model):
def __str__(self):
return self.title
......@@ -5,7 +5,10 @@ from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'author_id', 'title', 'body', 'created_at']
#fields = ['id', 'author_id']
fields = '__all__'
# read only = cannot be modified:
# read_only_fields = ['id', 'first_name']
# extra_kwargs = {
......@@ -15,3 +18,6 @@ class PostSerializer(serializers.ModelSerializer):
# }
# }
# @staticmethod
# def one_field(self):
# return self.fields['id', 'author_id']
......@@ -13,5 +13,7 @@ urlpatterns = [
path('post-by-post-id/<id>/', BlogPostRetrieveAPIView.as_view()),
path('delete-post-by-post-id/<id>/', BlogPostDeleteAPIView.as_view()),
path('update-post-by-post-id/<id>/', BlogPostUpdateAPIView.as_view()),
path('test/<id>/', BlogPostDeleteAPIView.as_view()),
path('test/<id>/', BlogPostUpdateAPIView.as_view()),
]
from rest_framework import generics
from rest_framework.response import Response
# import the table from the Model
from .models import Post
from .serializers import PostSerializer
'''
"""
Generics Views
'''
"""
class BlogPostsAPIView(generics.ListAPIView):
"""
Class to get all the Posts of the Blog Application
"""
permission_classes = []
authentication_classes = []
queryset = Post.objects.all()
serializer_class = PostSerializer
# def get_queryset(self):
# queryset = Post.objects.filter(author=2)
# return queryset
class BlogCreatePostAPIView(generics.CreateAPIView):
"""
Class to Create a Post for the Blog Application
"""
permission_classes = []
authentication_classes = []
......@@ -26,6 +40,10 @@ class BlogCreatePostAPIView(generics.CreateAPIView):
class UserListPostsAPIView(generics.ListAPIView):
"""
Class to get all Posts of a specific User using
the ID of the User (--> <author_id> in db)
"""
permission_classes = []
authentication_classes = []
......@@ -39,6 +57,9 @@ class UserListPostsAPIView(generics.ListAPIView):
class BlogPostRetrieveAPIView(generics.RetrieveAPIView):
"""
Class to Retrieve a Post using the Post ID (--> <id> in db)
"""
permission_classes = []
authentication_classes = []
......@@ -49,6 +70,9 @@ class BlogPostRetrieveAPIView(generics.RetrieveAPIView):
class BlogPostUpdateAPIView(generics.UpdateAPIView):
"""
Class to Update a Post using the POST ID (--> <id> in db)
"""
permission_classes = []
authentication_classes = []
......@@ -59,6 +83,9 @@ class BlogPostUpdateAPIView(generics.UpdateAPIView):
class BlogPostDeleteAPIView(generics.DestroyAPIView):
"""
Class to Delete a Post using the POST ID (--> <id> in db)
"""
permission_classes = []
authentication_classes = []
......@@ -72,7 +99,7 @@ class BlogPostDeleteAPIView(generics.DestroyAPIView):
'''
"""
Without Serializer
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
......@@ -88,10 +115,10 @@ def view_post_details(response):
post = list(post)
return JsonResponse(post, safe=False)
'''
"""
'''
"""
API View
from rest_framework.views import APIView
from rest_framework.response import Response
......@@ -109,4 +136,4 @@ class PostListSearchAPIView(APIView):
# type(serializer) --> <class 'rest_framework.serializers.ListSerializer'>
return Response(serializer.data)
'''
\ No newline at end of file
"""
\ No newline at end of file
......@@ -176,6 +176,8 @@ class Post(models.Model):
python manage.py makemigrations
python manage.py migrate
# to list migrations --> python manage.py <app name> showmigrations
# to apply a <migration name>.py -->
15. Check that the table has been created -->
table name = <app name>_<model class name in lowercase>
......@@ -260,16 +262,18 @@ class <model class>Serializer(serializers.ModelSerializer):
8. Check that the app's urls are mapped in projects urls --> app/project/urls.py
#### CREATE MIXINS IN ORDER TO DELETE AN UPDATE ONLY THE POSTS OF THE SPECIFIC USER
--> POSTS ARE LISTED --> OPTIONS TO DELETE OR UPDATE
ALSO CHECK HERE: https://momentum.propulsion-home.ch/backend/materials/weeks/week5/day4/permission_classes.html
#### TEST WITH POSTMAN
#### TRY TO RETRIEVE USERS PASSWORD
#### DEBUG VIEWS
#### TRY SERIALIZERS IN PYTHON SHELL
############ CREATE SERIALIZER MODULE
############ CREATE GENERICS VIEWS
############ EXTEND USERS TABLE
############ EXTEND DJANGO'S USERS TABLE
############ CREATE ALL TABLES
HOW TO CHANGE THE TIMEZONE ?? --> use local time from python library
WHAT IS META IN MODEL CLASS ??
WHAT IS from django.contrib.auth import get_user_model ??
WHAT IS THE DIFFERENCE BETWEEN VIEW AND GENERIC VIEW?? --> There are different classes, Generic has more packages
WHAT IS META IN MODEL CLASS ?? --> Just Syndax......
WHAT IS from django.contrib.auth import get_user_model ?? --> You import Django's User Table (<auth_user> in postgres)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment