Skip to content

Instantly share code, notes, and snippets.

@cityproject
Created August 5, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cityproject/5f77182643625c90f2d7ba17a7d96f74 to your computer and use it in GitHub Desktop.
Save cityproject/5f77182643625c90f2d7ba17a7d96f74 to your computer and use it in GitHub Desktop.
<form id="post_form" method="post" action="">
{% csrf_token %}
<input type="text" name="title" placeholder="Username">
<input type="textarea" name="content" placeholder="content">
<button class="ladda-button button-primary login_button" ng-click="vm.submit()"/><span class="ladda-label">submit</span></button>
</form>
$('#post_form').on('submit', function(e) { e.preventDefault()
$.ajax({
type:"POST",
url: '/api/posts/create/',
data:$('#post_form').serialize(),
error: function(response){
console.log(response);
alert('Not authorized.'); // Or something in a message DIV
},
success: function(response){
console.log(response);
$('.login_bar').html(response)
$('.login_bar').html(response)
$('#logout_form').toggleClass(show)
// do something with response
}
});
});
from rest_framework.serializers import (
HyperlinkedIdentityField,
ModelSerializer,
SerializerMethodField
)
from accounts.api.serializers import UserDetailSerializer
from comments.api.serializers import CommentSerializer
from comments.models import Comment
from posts.models import Post
class PostCreateUpdateSerializer(ModelSerializer):
class Meta:
model = Post
fields = [
#'id',
'title',
#'slug',
'content',
#'publish'
]
post_detail_url = HyperlinkedIdentityField(
view_name='posts-api:detail',
lookup_field='slug'
)
class PostDetailSerializer(ModelSerializer):
url = post_detail_url
user = UserDetailSerializer(read_only=True)
image = SerializerMethodField()
html = SerializerMethodField()
comments = SerializerMethodField()
class Meta:
model = Post
fields = [
'url',
'id',
'user',
'title',
'slug',
'content',
'html',
'publish',
'image',
'comments',
]
def get_html(self, obj):
return obj.get_markdown()
def get_image(self, obj):
try:
image = obj.image.url
except:
image = None
return image
def get_comments(self, obj):
c_qs = Comment.objects.filter_by_instance(obj)
comments = CommentSerializer(c_qs, many=True).data
return comments
class PostListSerializer(ModelSerializer):
url = post_detail_url
user = UserDetailSerializer(read_only=True)
class Meta:
model = Post
fields = [
'url',
'user',
'title',
'content',
'publish',
]
""""
from posts.models import Post
from posts.api.serializers import PostDetailSerializer
data = {
"title": "Yeahh buddy",
"content": "New content",
"publish": "2016-2-12",
"slug": "yeah-buddy",
}
obj = Post.objects.get(id=2)
new_item = PostDetailSerializer(obj, data=data)
if new_item.is_valid():
new_item.save()
else:
print(new_item.errors)
"""
from django.db.models import Q
from rest_framework.filters import (
SearchFilter,
OrderingFilter,
)
from rest_framework.generics import (
CreateAPIView,
DestroyAPIView,
ListAPIView,
UpdateAPIView,
RetrieveAPIView,
RetrieveUpdateAPIView
)
from rest_framework.permissions import (
AllowAny,
IsAuthenticated,
IsAdminUser,
IsAuthenticatedOrReadOnly,
)
from posts.models import Post
from .pagination import PostLimitOffsetPagination, PostPageNumberPagination
from .permissions import IsOwnerOrReadOnly
from .serializers import (
PostCreateUpdateSerializer,
PostDetailSerializer,
PostListSerializer
)
class PostCreateAPIView(CreateAPIView):
queryset = Post.objects.all()
serializer_class = PostCreateUpdateSerializer
#permission_classes = [IsAuthenticated]
def perform_create(self, serializer):
serializer.save(user=self.request.user)
class PostDetailAPIView(RetrieveAPIView):
queryset = Post.objects.all()
serializer_class = PostDetailSerializer
lookup_field = 'slug'
permission_classes = [AllowAny]
#lookup_url_kwarg = "abc"
class PostUpdateAPIView(RetrieveUpdateAPIView):
queryset = Post.objects.all()
serializer_class = PostCreateUpdateSerializer
lookup_field = 'slug'
permission_classes = [IsOwnerOrReadOnly]
#lookup_url_kwarg = "abc"
def perform_update(self, serializer):
serializer.save(user=self.request.user)
#email send_email
class PostDeleteAPIView(DestroyAPIView):
queryset = Post.objects.all()
serializer_class = PostDetailSerializer
lookup_field = 'slug'
permission_classes = [IsOwnerOrReadOnly]
#lookup_url_kwarg = "abc"
class PostListAPIView(ListAPIView):
serializer_class = PostListSerializer
filter_backends= [SearchFilter, OrderingFilter]
permission_classes = [AllowAny]
search_fields = ['title', 'content', 'user__first_name']
pagination_class = PostPageNumberPagination #PageNumberPagination
def get_queryset(self, *args, **kwargs):
#queryset_list = super(PostListAPIView, self).get_queryset(*args, **kwargs)
queryset_list = Post.objects.all() #filter(user=self.request.user)
query = self.request.GET.get("q")
if query:
queryset_list = queryset_list.filter(
Q(title__icontains=query)|
Q(content__icontains=query)|
Q(user__first_name__icontains=query) |
Q(user__last_name__icontains=query)
).distinct()
return queryset_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment