from django.http import HttpResponse
from django.core.mail import send_mail
import json
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from GestiRED.models import User
from GestiRED.models import QualityControl, Phase, Resource, ResourceType,PhaseType
from django.core import serializers
from django.db.models import Q

# Create your views here.

def index(request):
    return HttpResponse("GestiRED app UP")


@csrf_exempt
def quality_review_notification(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        qualityControl_id = data["qualityControl_id"]
        resource_name = data["resource_name"]
        responsible_name = data["responsible_name"]
        qualityControl = QualityControl.objects.get(pk=qualityControl_id)
        user = qualityControl.responsible

        send_mail('Revision Calidad',
                  'Recurso: ' + resource_name + '\n Observaciones: Se ha asignado para control de calidad a: ' + responsible_name,
                  'clipstaragil6@gmail.com',
                  [user.email],
                  fail_silently=False)

    res = {"status": "Ok", "Content:": "Email enviado"}
    return HttpResponse(json.dumps(res), content_type="application/json")

@csrf_exempt
def resources_filters(request):
    qs_json={}
    if request.method == 'GET':

        phaseType = request.GET.get('phaseType')
        if phaseType != None : phaseType= phaseType.split(',')

        resourceType = request.GET.get('resourceType')
        if resourceType != None : resourceType = resourceType.split(',')

        responsible = request.GET.get('responsible')
        if responsible != None: responsible = responsible.split(',')

        labels = request.GET.get('labels')

        my_dict = {'phase__phaseType__in':phaseType,
                   'resourceType__in': resourceType,
                   'responsibles__in':responsible,
                   'labels__icontains': labels}  # Your dict with fields
        or_condition = Q()
        for key, value in my_dict.items():
            if value != None:
                or_condition.add(Q(**{key: value}), Q.AND)
        lp = set()
        lp=Resource.objects.filter(or_condition).all().distinct()
        data = list([res.json() for res in lp])
        qs_json =json.dumps({'objects':data})
    return HttpResponse( qs_json, content_type='application/json')


[((14, 11, 14, 42), 'django.http.HttpResponse', 'HttpResponse', ({(14, 24, 14, 41): '"""GestiRED app UP"""'}, {}), "('GestiRED app UP')", False, 'from django.http import HttpResponse\n'), ((64, 11, 64, 66), 'django.http.HttpResponse', 'HttpResponse', (), '', False, 'from django.http import HttpResponse\n'), ((20, 15, 20, 39), 'json.loads', 'json.loads', ({(20, 26, 20, 38): 'request.body'}, {}), '(request.body)', False, 'import json\n'), ((24, 25, 24, 73), 'GestiRED.models.QualityControl.objects.get', 'QualityControl.objects.get', (), '', False, 'from GestiRED.models import QualityControl, Phase, Resource, ResourceType, PhaseType\n'), ((27, 8, 31, 38), 'django.core.mail.send_mail', 'send_mail', (), '', False, 'from django.core.mail import send_mail\n'), ((34, 24, 34, 39), 'json.dumps', 'json.dumps', ({(34, 35, 34, 38): 'res'}, {}), '(res)', False, 'import json\n'), ((56, 23, 56, 26), 'django.db.models.Q', 'Q', ({}, {}), '()', False, 'from django.db.models import Q\n'), ((63, 17, 63, 45), 'json.dumps', 'json.dumps', ({(63, 28, 63, 44): "{'objects': data}"}, {}), "({'objects': data})", False, 'import json\n'), ((59, 33, 59, 50), 'django.db.models.Q', 'Q', ({}, {}), '(**{key: value})', False, 'from django.db.models import Q\n'), ((61, 11, 61, 48), 'GestiRED.models.Resource.objects.filter', 'Resource.objects.filter', ({(61, 35, 61, 47): 'or_condition'}, {}), '(or_condition)', False, 'from GestiRED.models import QualityControl, Phase, Resource, ResourceType, PhaseType\n')]