Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Django Project - Social Media Blog
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Georgios Xonikis
Django Project - Social Media Blog
Commits
59017527
Commit
59017527
authored
Jun 14, 2019
by
Georgios Xonikis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial Views Completed
parent
a5cf0c4c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
64 additions
and
19 deletions
+64
-19
app/blog/models.py
app/blog/models.py
+9
-3
app/blog/serializers.py
app/blog/serializers.py
+7
-1
app/blog/urls.py
app/blog/urls.py
+2
-0
app/blog/views.py
app/blog/views.py
+33
-6
instructions.txt
instructions.txt
+13
-9
No files found.
app/blog/models.py
View file @
59017527
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
app/blog/serializers.py
View file @
59017527
...
...
@@ -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']
app/blog/urls.py
View file @
59017527
...
...
@@ -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
()),
]
app/blog/views.py
View file @
59017527
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
instructions.txt
View file @
59017527
...
...
@@ -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)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment