Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified src/account/__pycache__/views.cpython-37.pyc
Binary file not shown.
Empty file added src/account/api/__init__.py
Empty file.
Binary file added src/account/api/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file not shown.
Binary file added src/account/api/__pycache__/urls.cpython-37.pyc
Binary file not shown.
Binary file added src/account/api/__pycache__/views.cpython-37.pyc
Binary file not shown.
44 changes: 44 additions & 0 deletions src/account/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from rest_framework import serializers

from account.models import Account


# class LoginSerializer(serializers.ModelSerializer):
# class Meta:
# model = Account
# fields = ['email', 'password',]

# extra_kwargs = {'password': {'write_only': True}}

# def validate(self, data):
# password = data.get('password')
# email = data.get('email')


class RegistrationSerializer(serializers.ModelSerializer):

password2 = serializers.CharField(style={'input_type': 'password'}, write_only=True)

class Meta:
model = Account
fields = ['email', 'username', 'password', 'password2']
extra_kwargs = {
'password': {'write_only': True},
}


def save(self):

account = Account(
email=self.validated_data['email'],
username=self.validated_data['username']
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
if password != password2:
raise serializers.ValidationError({'password': 'Passwords must match.'})
account.set_password(password)
account.save()
return account


12 changes: 12 additions & 0 deletions src/account/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path
from account.api.views import(
registration_view,

)

app_name = 'account'

urlpatterns = [
path('register', registration_view, name="register"),
]

21 changes: 21 additions & 0 deletions src/account/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view

from account.api.serializers import RegistrationSerializer


@api_view(['POST', ])
def registration_view(request):

if request.method == 'POST':
serializer = RegistrationSerializer(data=request.data)
data = {}
if serializer.is_valid():
account = serializer.save()
data['response'] = 'successfully registered new user.'
data['email'] = account.email
data['username'] = account.username
else:
data = serializer.errors
return Response(data)
Binary file added src/blog/__pycache__/serializers.cpython-37.pyc
Binary file not shown.
Binary file added src/blog/__pycache__/urls-api.cpython-37.pyc
Binary file not shown.
Binary file modified src/blog/__pycache__/urls.cpython-37.pyc
Binary file not shown.
Binary file added src/blog/__pycache__/urls_api.cpython-37.pyc
Binary file not shown.
Binary file modified src/blog/__pycache__/views.cpython-37.pyc
Binary file not shown.
Binary file added src/blog/__pycache__/views_api.cpython-37.pyc
Binary file not shown.
Empty file added src/blog/api/__init__.py
Empty file.
Binary file added src/blog/api/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added src/blog/api/__pycache__/urls.cpython-37.pyc
Binary file not shown.
Binary file added src/blog/api/__pycache__/views.cpython-37.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions src/blog/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from rest_framework import serializers

from blog.models import BlogPost


class BlogPostSerializer(serializers.ModelSerializer):
class Meta:
model = BlogPost
fields = ['title', 'body', 'image',]







16 changes: 16 additions & 0 deletions src/blog/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.urls import path
from blog.api.views import(
api_detail_blog_view,
api_update_blog_view,
api_delete_blog_view,
api_create_blog_view,
)

app_name = 'blog'

urlpatterns = [
path('<slug>/', api_detail_blog_view, name="detail"),
path('<slug>/update', api_update_blog_view, name="update"),
path('<slug>/delete', api_delete_blog_view, name="delete"),
path('create', api_create_blog_view, name="create"),
]
80 changes: 80 additions & 0 deletions src/blog/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view

from account.models import Account
from blog.models import BlogPost
from blog.api.serializers import BlogPostSerializer

SUCCESS = 'success'
ERROR = 'error'
DELETE_SUCCESS = 'deleted'
UPDATE_SUCCESS = 'updated'
CREATE_SUCCESS = 'created'

@api_view(['GET', ])
def api_detail_blog_view(request, slug):

try:
blog_post = BlogPost.objects.get(slug=slug)
except BlogPost.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'GET':
serializer = BlogPostSerializer(blog_post)
return Response(serializer.data)


@api_view(['PUT',])
def api_update_blog_view(request, slug):

try:
blog_post = BlogPost.objects.get(slug=slug)
except BlogPost.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'PUT':
serializer = BlogPostSerializer(blog_post, data=request.data)
data = {}
if serializer.is_valid():
serializer.save()
data[SUCCESS] = UPDATE_SUCCESS
return Response(data=data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['DELETE',])
def api_delete_blog_view(request, slug):

try:
blog_post = BlogPost.objects.get(slug=slug)
except BlogPost.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'DELETE':
operation = blog_post.delete()
data = {}
if operation:
data[SUCCESS] = DELETE_SUCCESS
return Response(data=data)


@api_view(['POST'])
def api_create_blog_view(request):

account = Account.objects.get(pk=1)

blog_post = BlogPost(author=account)

if request.method == 'POST':
serializer = BlogPostSerializer(blog_post, data=request.data)
data = {}
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)





1 change: 1 addition & 0 deletions src/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
create_blog_view,
detail_blog_view,
edit_blog_view,

)

app_name = 'blog'
Expand Down
1 change: 1 addition & 0 deletions src/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from blog.models import BlogPost
from blog.forms import CreateBlogPostForm, UpdateBlogPostForm

from account.models import Account


Expand Down
4 changes: 4 additions & 0 deletions src/mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
path('logout/', logout_view, name="logout"),
path('must_authenticate/', must_authenticate_view, name="must_authenticate"),
path('register/', registration_view, name="register"),

# REST-framework
path('api/blog/', include('blog.api.urls', 'blog_api')),
path('api/account/', include('account.api.urls', 'account_api')),

# Password reset links (ref: https://github.com/django/django/blob/master/django/contrib/auth/views.py)
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(template_name='registration/password_change_done.html'),
Expand Down