Last active
January 19, 2020 19:12
-
-
Save mitchtabian/7796918f1ab4b43872bd232ebda4ebfb 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 BlogPostCreateSerializer | |
CREATE_SUCCESS = 'created' | |
# Response: https://gist.github.com/mitchtabian/78d7dcbeab4135c055ff6422238a31f9 | |
# Url: https://<your-domain>/api/blog/create | |
# Headers: Authorization: Token <token> | |
@api_view(['POST']) | |
@permission_classes((IsAuthenticated,)) | |
def api_create_blog_view(request): | |
if request.method == 'POST': | |
data = request.data | |
data['author'] = request.user.pk | |
serializer = BlogPostCreateSerializer(data=data) | |
data = {} | |
if serializer.is_valid(): | |
blog_post = serializer.save() | |
data['response'] = CREATE_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
class BlogPostCreateSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = BlogPost | |
fields = ['title', 'body', 'image', 'date_updated', 'author'] | |
def save(self): | |
try: | |
image = self.validated_data['image'] | |
title = self.validated_data['title'] | |
if len(title) < MIN_TITLE_LENGTH: | |
raise serializers.ValidationError({"response": "Enter a title longer than " + str(MIN_TITLE_LENGTH) + " characters."}) | |
body = self.validated_data['body'] | |
if len(body) < MIN_BODY_LENGTH: | |
raise serializers.ValidationError({"response": "Enter a body longer than " + str(MIN_BODY_LENGTH) + " characters."}) | |
blog_post = BlogPost( | |
author=self.validated_data['author'], | |
title=title, | |
body=body, | |
image=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) | |
blog_post.save() | |
return blog_post | |
except KeyError: | |
raise serializers.ValidationError({"response": "You must have a title, some content, and an image."}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment