<gh_stars>0
from django.shortcuts import render
from django.views.generic import View
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, JsonResponse
from django.utils import timezone
from .forms import PhotoForm, DeleteForm
from .models import PhotoModel
import os
import shutil
from .effects import double, nigeria, france, usa, kenya, russia
from django.contrib import messages


# Mixins
class LoginRequiredMixin(object):
    @classmethod
    def as_view(cls, **initkwargs):
        view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
        return login_required(view)


# helper functions
def get_photos(user):
    '''returns all photos of a user'''

    photos = PhotoModel.objects.filter(owner=user)
    return photos


def create_duplicate_file(image_file_path, image_file_url):
    '''creates a duplicate file for editting'''
    # create temporary file path
    root, ext = os.path.splitext(image_file_path)
    root = root + '_temp'
    temp_file_path = root + ext

    # create temporary file url
    root, ext = os.path.splitext(image_file_url)
    root = root + '_temp'
    temp_file_url = root + ext

    # create a duplicate of the image file
    shutil.copy2(image_file_path, temp_file_path)

    return temp_file_path, temp_file_url


def photo_effect(effect, temp_file_path, temp_file_url):
    '''applies a photo effect on a temporary file'''

    if effect == 'double':
        double(temp_file_path)
        return_value = temp_file_url
    elif effect == 'france':
        france(temp_file_path)
        return_value = temp_file_url
    elif effect == 'kenya':
        kenya(temp_file_path)
        return_value = temp_file_url
    elif effect == 'nigeria':
        nigeria(temp_file_path)
        return_value = temp_file_url
    elif effect == 'russia':
        russia(temp_file_path)
        return_value = temp_file_url
    elif effect == 'usa':
        usa(temp_file_path)
        return_value = temp_file_url
    else:
        return_value = None
    return return_value


# Views
class HomePageView(View):
    '''handles homepage requests'''
    template_name = 'photo/index.html'

    def get(self, request):
        # redirect to user dashboard if user is logged in
        if request.user and not request.user.is_anonymous:
            return HttpResponseRedirect(reverse('photo:user_home'))
        else:
            return render(request, self.template_name)


class DashboardView(LoginRequiredMixin, View):
    '''displays dashboard'''
    template_name = 'photo/dashboard.html'

    def get(self, request):
        # get the user's social details
        social_user = request.user.social_auth.filter(provider='facebook').first()
        message = {}

        # check if any photo is to be editted
        staged_photo = None
        if 'photo' in request.GET:
            # get object of the photo to be editted
            staged_photo = PhotoModel.objects.get(id=request.GET['photo'])

            # set original image as image to be displayed
            staged_photo.display_image = staged_photo.photo.url

        if 'effect' in request.GET:
            # create a temporary image to use for editting
            staged_photo.temp_file_path, staged_photo.temp_file_url = create_duplicate_file(staged_photo.photo.path, staged_photo.photo.url)
            staged_photo.temp_file_url = photo_effect(request.GET['effect'], staged_photo.temp_file_path, staged_photo.temp_file_url)

            # set temporary image as image to be displayed
            staged_photo.display_image = staged_photo.temp_file_url
            message = {'staged_photo': staged_photo.display_image + '? %s' % (timezone.now())}
            return JsonResponse(message)

        photos = get_photos(request.user)
        context = {'social_user': social_user,
                   'photos': photos,
                   'staged_photo': staged_photo}
        return render(request, self.template_name, context)


class ImageUploadView(LoginRequiredMixin, View):
    '''handles image upload'''

    def post(self, request):
        form = PhotoForm(request.POST, request.FILES)
        if form.is_valid():
            # set the current user as the owner of the photo before saving the photo
            photo = form.save(commit=False)
            photo.owner = request.user
            photo.save()
            return HttpResponseRedirect(reverse('photo:user_home'))
        else:
            message = 'Error! Both Photo Caption and Select Photo are required'
            messages.add_message(request, messages.WARNING, message)
            return HttpResponseRedirect(reverse('photo:user_home'))


class DeleteImageView(LoginRequiredMixin, View):
    ''' handle deletion of images'''

    def post(self, request):
        form = DeleteForm(request.POST)

        if form.is_valid():
            photo_id = request.POST['photo_id']
            original_photo_path = request.POST['orig']
            temporary_photo_path = request.POST['temp']

            # delete image from database
            photo = PhotoModel.objects.get(pk=photo_id)
            photo.delete()

            # delete original and temporary images
            os.remove(original_photo_path)

            # first check if a temporary image exists
            if temporary_photo_path != 'None':
                os.remove(temporary_photo_path)

            message = {'content': 'Photo was deleted successfully', 'status': True}
            return JsonResponse(message)
        else:
            message = {'content': 'Error deleting file', 'status': False}
            return JsonResponse(message)

from django.shortcuts import render
from django.views.generic import View
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, JsonResponse
from django.utils import timezone
from .forms import PhotoForm, DeleteForm
from .models import PhotoModel
import os
import shutil
from .effects import double, nigeria, france, usa, kenya, russia
from django.contrib import messages


# Mixins
class LoginRequiredMixin(object):
    @classmethod
    def as_view(cls, **initkwargs):
        view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
        return login_required(view)


# helper functions
def get_photos(user):
    '''returns all photos of a user'''

    photos = PhotoModel.objects.filter(owner=user)
    return photos


def create_duplicate_file(image_file_path, image_file_url):
    '''creates a duplicate file for editting'''
    # create temporary file path
    root, ext = os.path.splitext(image_file_path)
    root = root + '_temp'
    temp_file_path = root + ext

    # create temporary file url
    root, ext = os.path.splitext(image_file_url)
    root = root + '_temp'
    temp_file_url = root + ext

    # create a duplicate of the image file
    shutil.copy2(image_file_path, temp_file_path)

    return temp_file_path, temp_file_url


def photo_effect(effect, temp_file_path, temp_file_url):
    '''applies a photo effect on a temporary file'''

    if effect == 'double':
        double(temp_file_path)
        return_value = temp_file_url
    elif effect == 'france':
        france(temp_file_path)
        return_value = temp_file_url
    elif effect == 'kenya':
        kenya(temp_file_path)
        return_value = temp_file_url
    elif effect == 'nigeria':
        nigeria(temp_file_path)
        return_value = temp_file_url
    elif effect == 'russia':
        russia(temp_file_path)
        return_value = temp_file_url
    elif effect == 'usa':
        usa(temp_file_path)
        return_value = temp_file_url
    else:
        return_value = None
    return return_value


# Views
class HomePageView(View):
    '''handles homepage requests'''
    template_name = 'photo/index.html'

    def get(self, request):
        # redirect to user dashboard if user is logged in
        if request.user and not request.user.is_anonymous:
            return HttpResponseRedirect(reverse('photo:user_home'))
        else:
            return render(request, self.template_name)


class DashboardView(LoginRequiredMixin, View):
    '''displays dashboard'''
    template_name = 'photo/dashboard.html'

    def get(self, request):
        # get the user's social details
        social_user = request.user.social_auth.filter(provider='facebook').first()
        message = {}

        # check if any photo is to be editted
        staged_photo = None
        if 'photo' in request.GET:
            # get object of the photo to be editted
            staged_photo = PhotoModel.objects.get(id=request.GET['photo'])

            # set original image as image to be displayed
            staged_photo.display_image = staged_photo.photo.url

        if 'effect' in request.GET:
            # create a temporary image to use for editting
            staged_photo.temp_file_path, staged_photo.temp_file_url = create_duplicate_file(staged_photo.photo.path, staged_photo.photo.url)
            staged_photo.temp_file_url = photo_effect(request.GET['effect'], staged_photo.temp_file_path, staged_photo.temp_file_url)

            # set temporary image as image to be displayed
            staged_photo.display_image = staged_photo.temp_file_url
            message = {'staged_photo': staged_photo.display_image + '? %s' % (timezone.now())}
            return JsonResponse(message)

        photos = get_photos(request.user)
        context = {'social_user': social_user,
                   'photos': photos,
                   'staged_photo': staged_photo}
        return render(request, self.template_name, context)


class ImageUploadView(LoginRequiredMixin, View):
    '''handles image upload'''

    def post(self, request):
        form = PhotoForm(request.POST, request.FILES)
        if form.is_valid():
            # set the current user as the owner of the photo before saving the photo
            photo = form.save(commit=False)
            photo.owner = request.user
            photo.save()
            return HttpResponseRedirect(reverse('photo:user_home'))
        else:
            message = 'Error! Both Photo Caption and Select Photo are required'
            messages.add_message(request, messages.WARNING, message)
            return HttpResponseRedirect(reverse('photo:user_home'))


class DeleteImageView(LoginRequiredMixin, View):
    ''' handle deletion of images'''

    def post(self, request):
        form = DeleteForm(request.POST)

        if form.is_valid():
            photo_id = request.POST['photo_id']
            original_photo_path = request.POST['orig']
            temporary_photo_path = request.POST['temp']

            # delete image from database
            photo = PhotoModel.objects.get(pk=photo_id)
            photo.delete()

            # delete original and temporary images
            os.remove(original_photo_path)

            # first check if a temporary image exists
            if temporary_photo_path != 'None':
                os.remove(temporary_photo_path)

            message = {'content': 'Photo was deleted successfully', 'status': True}
            return JsonResponse(message)
        else:
            message = {'content': 'Error deleting file', 'status': False}
            return JsonResponse(message)
# Mixins # helper functions returns all photos of a user creates a duplicate file for editting # create temporary file path # create temporary file url # create a duplicate of the image file applies a photo effect on a temporary file # Views handles homepage requests # redirect to user dashboard if user is logged in displays dashboard # get the user's social details # check if any photo is to be editted # get object of the photo to be editted # set original image as image to be displayed # create a temporary image to use for editting # set temporary image as image to be displayed handles image upload # set the current user as the owner of the photo before saving the photo handle deletion of images # delete image from database # delete original and temporary images # first check if a temporary image exists