# -*- coding: utf-8 -*-

import logging

from celery.task import task,periodic_task
from celery.schedules import crontab
from cuestionario.models import Cuestionario,Evaluacion

from django.http import Http404
from time import localtime, strftime

#logging
logger = logging.getLogger('tareas_info')
log_error = logging.getLogger('tareas_error')

@task(name="tasks.cuestionarios",default_retry_delay=45,max_retries=10)
def cuestionarios(autor=None,id=None,slug=None):
    """
        task : Cuestionarios
            Permite ver los cuestionarios
            :autor Char : Autor del cuestionario, por defecto es Null
            :id  Char : Id del cuestionario, por defecto es Null
            :slug Char: Slug del cuestionario por defecto es Null
        Note: Si no se especifica nada obtiene todos los cuestionarios
    """
    try:
        if autor:
            logger.info(strftime("%d/%m/%Y %H:%M:%S", localtime()) + "\tCUESTIONARIO: [%s] \tAutor:%s" % (cuestionarios.request.id,autor))
            query = Cuestionario.objects.filter(autor=profile.user)
            return query

        elif id:
            logger.info(strftime("%d/%m/%Y %H:%M:%S", localtime()) + "\tCUESTIONARIO: [%s] Id:%s" % (cuestionarios.request.id,id))
            query = Cuestionario.objects.get(id__iexact=id)
            return query

        elif slug:
            logger.info(strftime("%d/%m/%Y %H:%M:%S", localtime()) + "\tCUESTIONARIO: [%s] Slug:%s" % (cuestionarios.request.id,slug))
            query = Cuestionario.objects.get(slug__iexact=slug)
            return query

    except Cuestionario.DoesNotExist:
        raise Http404
    except Exception,exc:
        log_error.error(strftime("%d/%m/%Y %H:%M:%S", localtime()) + "\tCUESTIONARIO: [%s] \tAutor:%s Reintentando" % (cuestionarios.request.id,autor))
        cuestionarios.retry(exc=exc,countdown=15)


