#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.utils.decorators import method_decorator
from supra import views as supra
import models
import forms
from django.views.decorators.csrf import csrf_exempt
from usuarios.decorator import check_login
from usuarios import models as usuarios
supra.SupraConf.ACCECC_CONTROL["allow"] = True


"""
Actividad: T053
Requerimientos: R036,R037
Backend Modelo, Formulario, Admin y Servicio: R. Mantenimientos
"""


class FotoMantenimientoInlineFormView(supra.SupraInlineFormView):
    model = models.FotoSolucion

    @method_decorator(check_login)
    def dispatch(self, request, *args, **kwargs):
        return super(FotoMantenimientoInlineFormView, self).dispatch(request, *args, **kwargs)
    # end def
# end class


class FotoFormView(supra.SupraFormView):
    model = models.FotoSolucion

    @method_decorator(check_login)
    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        return super(FotoFormView, self).dispatch(request, *args, **kwargs)
    # end def
# end class


class MantenimientoFormView(supra.SupraFormView):
    model = models.Solucion
    form_class = forms.SolucionForm
    inlines = [FotoMantenimientoInlineFormView]

    @method_decorator(check_login)
    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        return super(MantenimientoFormView, self).dispatch(request, *args, **kwargs)
    # end def
# end class


class MantenimientoListView(supra.SupraListView):
    model = models.Solucion
    list_display = ['id', 'nombre', 'reporte__nombre',
                    'nombreC', 'apellidosC', 'descripcion', 'fecha',
                    'user', 'cliente_id']
    search_fields = ['nombre', 'descripcion', 'reporte__piscina__casa__cliente__first_name',
                     'reporte__piscina__casa__cliente__last_name', 'reporte__nombre']
    list_filter = ('reporte', 'emisor')
    paginate_by = 10

    class Renderer:
        nombreC = 'reporte__piscina__casa__cliente__first_name'
        apellidosC = 'reporte__piscina__casa__cliente__last_name'
        cliente_id = 'reporte__piscina__casa__cliente__id'
        user = 'emisor__username'
    # end class

    def get_queryset(self):
        queryset = super(MantenimientoListView, self).get_queryset()
        piscinero = usuarios.Piscinero.objects.filter(id=self.request.user.id)
        if piscinero:
            return queryset.filter(emisor=self.request.user).order_by('-fecha')
        # end if
        return queryset.order_by('-fecha')
    # end def

    @method_decorator(check_login)
    def dispatch(self, request, *args, **kwargs):
        return super(MantenimientoListView, self).dispatch(request, *args, **kwargs)
    # end def
# end class


class FotoMantenimientoListView(supra.SupraListView):
    list_filter = ['id', 'mantenimiento']
    list_display = ['url', ]
    model = models.FotoSolucion

    @method_decorator(check_login)
    def dispatch(self, request, *args, **kwargs):
        return super(FotoMantenimientoListView, self).dispatch(request, *args, **kwargs)
    # end def
# end class
