Last active
September 10, 2019 21:57
-
-
Save mitchtabian/bc0351d79d552ee4cda5265d5e135015 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from blog.api.serializers import BlogPostUpdateSerializer | |
# Response: https://gist.github.com/mitchtabian/32507e93c530aa5949bc08d795ba66df | |
# Url: https://<your-domain>/api/blog/<slug>/update | |
# Headers: Authorization: Token <token> | |
@api_view(['PUT',]) | |
@permission_classes((IsAuthenticated,)) | |
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) | |
user = request.user | |
if blog_post.author != user: | |
return Response({'response':"You don't have permission to edit that."}) | |
if request.method == 'PUT': | |
serializer = BlogPostUpdateSerializer(blog_post, data=request.data, partial=True) | |
data = {} | |
if serializer.is_valid(): | |
serializer.save() | |
data['response'] = UPDATE_SUCCESS | |
data['pk'] = blog_post.pk | |
data['title'] = blog_post.title | |
data['body'] = blog_post.body | |
data['slug'] = blog_post.slug | |
data['date_updated'] = blog_post.date_updated | |
image_url = str(request.build_absolute_uri(blog_post.image.url)) | |
if "?" in image_url: | |
image_url = image_url[:image_url.rfind("?")] | |
data['image'] = image_url | |
data['username'] = blog_post.author.username | |
return Response(data=data) | |
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import sys | |
import os | |
from django.conf import settings | |
from django.core.files.storage import default_storage | |
from django.core.files.storage import FileSystemStorage | |
IMAGE_SIZE_MAX_BYTES = 1024 * 1024 * 2 # 2MB | |
MIN_TITLE_LENGTH = 5 | |
MIN_BODY_LENGTH = 50 | |
class BlogPostUpdateSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = BlogPost | |
fields = ['title', 'body', 'image'] | |
def validate(self, blog_post): | |
try: | |
title = blog_post['title'] | |
if len(title) < MIN_TITLE_LENGTH: | |
raise serializers.ValidationError({"response": "Enter a title longer than " + str(MIN_TITLE_LENGTH) + " characters."}) | |
body = blog_post['body'] | |
if len(body) < MIN_BODY_LENGTH: | |
raise serializers.ValidationError({"response": "Enter a body longer than " + str(MIN_BODY_LENGTH) + " characters."}) | |
image = blog_post['image'] | |
url = os.path.join(settings.TEMP , str(image)) | |
storage = FileSystemStorage(location=url) | |
with storage.open('', 'wb+') as destination: | |
for chunk in image.chunks(): | |
destination.write(chunk) | |
destination.close() | |
if sys.getsizeof(image.file) > IMAGE_SIZE_MAX_BYTES: | |
os.remove(url) | |
raise serializers.ValidationError({"response": "That image is too large. Images must be less than 3 MB. Try a different image."}) | |
img = cv2.imread(url) | |
dimensions = img.shape # gives: (height, width, ?) | |
aspect_ratio = dimensions[1] / dimensions[0] # divide w / h | |
if aspect_ratio < 1: | |
os.remove(url) | |
raise serializers.ValidationError({"response": "Image height must not exceed image width. Try a different image."}) | |
os.remove(url) | |
except KeyError: | |
pass | |
return blog_post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEMP = os.path.join(BASE_DIR, 'temp') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment