Skip to content

Commit 3636c83

Browse files
24 - Post Owner
1 parent 909e007 commit 3636c83

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/posts/serializers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class PostSerializer(serializers.ModelSerializer):
2828
)
2929
user = UserPublicSerializer(read_only=True)
3030
publish = serializers.DateField(default=timezone.now())
31+
owner = serializers.SerializerMethodField(read_only=True)
3132

3233
class Meta:
3334
model = Post
@@ -40,5 +41,14 @@ class Meta:
4041
'draft',
4142
'publish',
4243
'updated',
44+
'owner',
4345
'timestamp',
44-
]
46+
]
47+
def get_owner(self, obj):
48+
request = self.context['request']
49+
if request.user.is_authenticated:
50+
if obj.user == request.user:
51+
return True
52+
return False
53+
54+

src/posts/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ class PostDetailAPIView(generics.RetrieveUpdateDestroyAPIView):
1313
lookup_field = 'slug'
1414
permission_classes = [IsOwnerOrReadOnly]
1515

16+
# def get_serializer_context(self):
17+
# context = super().get_serializer_context()
18+
# context['request'] = self.request
19+
# return context
20+
1621

1722
class PostListCreateAPIView(generics.ListCreateAPIView):
1823
queryset = Post.objects.all()
19-
serializer_class = PostSerializer
24+
serializer_class = PostSerializer
2025
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
2126

2227
def perform_create(self, serializer):

src/reactify-ui/src/posts/PostDetail.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React, {Component} from 'react'
2+
import 'whatwg-fetch'
3+
import cookie from 'react-cookies'
24
import { Link } from 'react-router-dom'
35

46
class PostDetail extends Component {
@@ -21,6 +23,12 @@ class PostDetail extends Component {
2123
}
2224
}
2325

26+
const csrfToken = cookie.load('csrftoken')
27+
if (csrfToken !== undefined) {
28+
lookupOptions['credentials'] = 'include'
29+
lookupOptions['headers']['X-CSRFToken'] = csrfToken
30+
}
31+
2432
fetch(endpoint, lookupOptions)
2533
.then(function(response){
2634
if (response.status == 404){
@@ -71,6 +79,8 @@ class PostDetail extends Component {
7179
pathname: `/posts`,
7280
state: { fromDashboard: false }
7381
}}>Posts</Link></p>
82+
83+
{post.owner === true ? <div>Update Post</div> : ""}
7484
</div>
7585
}
7686
</div> : "Loading..."}</p>

src/staticfiles/js/reactify-django.ui.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)