File tree Expand file tree Collapse file tree 4 files changed +29
-4
lines changed
Expand file tree Collapse file tree 4 files changed +29
-4
lines changed Original file line number Diff line number Diff 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+
Original file line number Diff line number Diff 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
1722class 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 ):
Original file line number Diff line number Diff line change 11import React , { Component } from 'react'
2+ import 'whatwg-fetch'
3+ import cookie from 'react-cookies'
24import { Link } from 'react-router-dom'
35
46class 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 >
You can’t perform that action at this time.
0 commit comments