Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#126297701 Refactor code #17

Merged
merged 5 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .coverage
Original file line number Diff line number Diff line change
@@ -1 +1 @@
!coverage.py: This is a private format, don't read it directly!{"lines": {"/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/__init__.py": [1], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/views.py": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 18, 20, 21, 22, 24, 27, 60], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/urls.py": [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_edit.py": [1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 22, 23, 24, 25, 26, 27, 31, 32, 34, 35, 36, 38, 39, 40, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 109, 110, 112, 113, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 127, 128, 133, 134, 135, 138, 139, 140, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_processor.py": [1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 20, 21, 23, 24, 25, 27, 28, 29, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/permissions.py": [1, 4, 6, 7], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/serializers.py": [1, 2, 3, 9, 10, 13, 15, 16, 18, 19, 20, 21, 22, 23, 25, 27, 28, 29, 30, 32, 33, 34, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 49, 50, 53, 54, 55, 56, 59, 62, 64, 65, 67, 68, 69, 70, 71, 73, 74, 77, 78, 79, 80, 81, 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99, 102, 103, 104, 105, 106, 108, 109, 111, 112, 114, 115, 116, 118, 119], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/views.py": [1, 2, 3, 4, 5, 6, 11, 15, 19, 20, 21, 22, 24, 25, 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 47, 48, 51, 52, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 79, 80, 83, 99, 101, 102, 105, 106, 108, 109, 110, 112, 128, 130, 131, 134, 135, 136, 137, 138, 139, 141, 143, 144, 148, 149, 150, 151, 152, 153, 154, 156, 157, 160, 178, 179, 180, 181, 182, 186, 204, 205, 206, 207, 208, 211, 229, 230, 231, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 245, 246, 247, 248]}}
!coverage.py: This is a private format, don't read it directly!{"lines": {"/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/__init__.py": [1], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/views.py": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 27, 60], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/urls.py": [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_edit.py": [1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 127, 128, 130, 132, 133, 134, 135, 136, 138, 139, 140, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 157, 158, 163, 164, 165, 166, 168, 169, 170, 171, 172, 173, 175], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_processor.py": [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/permissions.py": [1, 4, 6, 7], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/serializers.py": [1, 2, 3, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99, 102, 103, 104, 105, 106, 108, 109, 111, 112, 114, 115, 116, 118, 119], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/views.py": [1, 2, 3, 4, 5, 6, 7, 11, 12, 15, 16, 19, 20, 21, 22, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 88, 99, 101, 102, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 118, 128, 130, 131, 134, 135, 136, 137, 138, 139, 140, 141, 143, 144, 148, 149, 150, 151, 152, 153, 154, 156, 157, 159, 160, 161, 162, 165, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 191, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 222, 223, 225, 226, 227, 229, 230, 231, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 245, 246, 247, 248]}}
33 changes: 3 additions & 30 deletions app/photos/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
HyperlinkedIdentityField,
SerializerMethodField,
)

from photos.models import *


Expand Down Expand Up @@ -58,36 +57,20 @@ def get_photos(self, obj):
return serialized_photos


class PhotoDetailSerializer(ModelSerializer):

class Meta:
model = PhotoDetail
fields = [
'id',
'title',
'effects',
'date_created',
'date_modified',
]
extra_kwargs = {'date_created': {'read_only': True},
'date_modified': {'read_only': True}}


class PhotoSerializer(ModelSerializer):
detail = SerializerMethodField()
folder_name = SerializerMethodField()
uploader = SerializerMethodField()
file_size = SerializerMethodField()

class Meta:
model = Photo
fields = [
'id',
'image',
'title',
'edited_image',
'folder_name',
'share_code',
'detail',
'effects',
'uploader',
'file_size',
'user',
Expand All @@ -104,14 +87,4 @@ def get_folder_name(self, obj):
return 'None'

def get_uploader(self, obj):
return obj.user.username

def get_file_size(self, obj):
return int(obj.image.size/1000)

def get_detail(self, obj):
detail = PhotoDetail.objects.filter(photo=obj).first()
if(not detail):
return {}
serializer = PhotoDetailSerializer(detail)
return serializer.data
return obj.user.username
2 changes: 1 addition & 1 deletion app/photos/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
url(r'^photos/$', PhotoApiView.as_view(), name='list-photo'),
url(r'^photos/(?P<id>\d+)/$', SinglePhotoAPIView.as_view(), name='photo-single'),
url(r'^photos/share/$', PhotoShare.as_view(), name='photo-share'),
url(r'^photos/(?P<id>\d+)/detail/(?P<pk>\d+)/$', PhotoDetailAPIView.as_view(), name='photo-detail'),
#url(r'^photos/(?P<id>\d+)/detail/(?P<pk>\d+)/$', PhotoDetailAPIView.as_view(), name='photo-detail'),
url(r'^photos/(?P<id>\d+)/preview/$', PhotoPreview.as_view(), name='photo-preview'),
]
104 changes: 37 additions & 67 deletions app/photos/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ def get_queryset(self):
return queryset


class SingleFolderAPIView(RetrieveUpdateDestroyAPIView):

"""
Returns individual folder detail if you are doing a GET request.
Updates individual folder detail if you are doing a PUT request.
Deletes individual folder detail if you are doing a DELETE request.

Method: GET
Response: JSON

Method: PUT
Parameters:
name (required)
Response: JSON

Method: DELETE
Response: JSON

"""
queryset = Folder.objects.all()
serializer_class = FolderSerializer
permission_classes = [IsOwner]
lookup_field = 'id'


class PhotoApiView(ListCreateAPIView):

"""
Expand Down Expand Up @@ -147,9 +172,8 @@ def perform_create(self, serializer):
user=self.request.user, folder=folder, share_code=code)
else:
instance = serializer.save(user=self.request.user, share_code=code)

detail = PhotoDetail(photo=instance)
detail.save()
instance.file_size = int(instance.image.size/1000)
instance.save()

def get_queryset(self):
folder_id = self.kwargs.get('id', -1)
Expand All @@ -164,31 +188,6 @@ def get_queryset(self):
return queryset


class SingleFolderAPIView(RetrieveUpdateDestroyAPIView):

"""
Returns individual folder detail if you are doing a GET request.
Updates individual folder detail if you are doing a PUT request.
Deletes individual folder detail if you are doing a DELETE request.

Method: GET
Response: JSON

Method: PUT
Parameters:
name (required)
Response: JSON

Method: DELETE
Response: JSON

"""
queryset = Folder.objects.all()
serializer_class = FolderSerializer
permission_classes = [IsOwner]
lookup_field = 'id'


class SinglePhotoAPIView(RetrieveUpdateDestroyAPIView):

"""
Expand All @@ -213,49 +212,20 @@ class SinglePhotoAPIView(RetrieveUpdateDestroyAPIView):
permission_classes = [IsOwner]
lookup_field = 'id'

def perform_destroy(self, instance):
if(os.path.isfile(instance.image.path)):
os.remove(instance.image.path)

if(os.path.isfile(instance.image.path.replace('main', 'edited'))):
os.remove(instance.image.path.replace('main', 'edited'))

instance.delete()


class PhotoDetailAPIView(RetrieveUpdateDestroyAPIView):

"""
Returns individual photo detail if you are doing a GET request.
Updates individual photo detail if you are doing a PUT request.
Deletes individual photo detail if you are doing a DELETE request.

Method: GET
Response: JSON

Method: PUT
Parameters:
title (required)
Response: JSON

Method: DELETE
Response: JSON

"""
serializer_class = PhotoDetailSerializer
lookup_field = 'pk'

def perform_update(self, serializer):
instance = serializer.save()
photo = instance.photo
image_processor = ImageProcessor(photo)
image_processor = ImageProcessor(instance)
if instance.effects:
effect_obj = json.loads(instance.effects)
image_processor.process(effect_obj)
edited_path = image_processor.save()
photo.edited_image = edited_path
photo.save()
instance.edited_image = edited_path
instance.save()

def get_queryset(self):
photo = Photo.objects.filter(id=self.kwargs.get('id', 0)).first()
return PhotoDetail.objects.filter(photo=photo)
def perform_destroy(self, instance):
if(os.path.isfile(instance.image.path)):
os.remove(instance.image.path)

if(os.path.isfile(instance.image.path.replace('main', 'edited'))):
os.remove(instance.image.path.replace('main', 'edited'))
instance.delete()
5 changes: 4 additions & 1 deletion app/photos/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-07-15 19:49
# Generated by Django 1.9.2 on 2016-07-18 20:48
from __future__ import unicode_literals

from django.conf import settings
Expand Down Expand Up @@ -34,6 +34,9 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, null=True, upload_to='main')),
('title', models.CharField(default='', max_length=100)),
('file_size', models.CharField(default='', max_length=10)),
('effects', models.TextField(default='')),
('edited_image', models.CharField(default='', max_length=255)),
('share_code', models.CharField(default='', max_length=50)),
('date_modified', models.DateTimeField(auto_now=True)),
Expand Down
44 changes: 0 additions & 44 deletions app/photos/migrations/0002_auto_20160716_2231.py

This file was deleted.

20 changes: 0 additions & 20 deletions app/photos/migrations/0003_auto_20160716_2330.py

This file was deleted.

20 changes: 0 additions & 20 deletions app/photos/migrations/0004_photodetail_image_size.py

This file was deleted.

50 changes: 0 additions & 50 deletions app/photos/migrations/0005_auto_20160718_0947.py

This file was deleted.

3 changes: 3 additions & 0 deletions app/photos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Photo(models.Model):
image = models.ImageField(upload_to='main',
null=True,
blank=True)
title = models.CharField(max_length=100, default="")
file_size = models.CharField(max_length=10, default="")
effects = models.TextField(default='')
edited_image = models.CharField(max_length=255, default="")
share_code = models.CharField(max_length=50, default="")
date_modified = models.DateTimeField(auto_now=True, auto_now_add=False)
Expand Down
Loading