from django.shortcuts import render
from django.http import JsonResponse,HttpResponse
from django.core import serializers
from django.views.decorators.http import require_http_methods
from home import models
import json

# Create your views here.
def index(request):
    return render(request,'index.html')

def project(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.Project.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.Project._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def assetmanager(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.AssetManager.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.AssetManager._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def assetinfo(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.AssetInfo.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.AssetInfo._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def assetreport(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.AssetReport.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.AssetReport._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def operatereport(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.OperationReport.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.OperationReport._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def indexdb(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.Project.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.Project._meta._get_fields()))[1:-1]
    return JsonResponse(json.loads(serializers.serialize("json", models.Index.objects.all())),safe=False)

def assetbase(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.AssetBase.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.AssetBase._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def assetconstruct(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.AssetConstruct.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.AssetConstruct._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def assetinvest(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.AssetInvest.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.AssetInvest._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)

def operation(request):
    data = {}
    data["data"] = json.loads(serializers.serialize("json", models.Operation.objects.all()))
    data["fieldname"]=list(map(lambda x:{"name":x.verbose_name,"value":x.name},models.Operation._meta._get_fields()))[1:-1]
    return JsonResponse(data,safe=False)