def ProtagonistaEvent(request):
    global protaform
    if request.method == 'POST' or bool(protaform):
        if request.method == 'POST': protaform = NomDataForm(request.POST)
        protaform.is_valid()
        fdict = {}
        try:
            fdict = protaform.cleaned_data
        except:    
            print('Error al cleaned data Protagonista')
        consultaB = "SELECT * from baptisme natural join event_taula"   #Comencem a crear la consulta
        consultaM = "SELECT * from matrimoni natural join event_taula"
        consultaO = "SELECT * from obituari natural join event_taula"
        Buit = True
        for e in fdict:
            if fdict[e]!='-' and fdict[e]!='':
                consultaB += " WHERE";consultaM += " WHERE";consultaO += " WHERE"
                Buit = False
                break
        consultaB,_= Assistant.AddParameters(fdict,consultaB)
        consultaO,_= Assistant.AddParameters(fdict,consultaO)
        consultaM, newform = Assistant.AddParametersMatrimoni(fdict,consultaM)
        
        Btable = []
        if not Buit: Btable = Assistant.getTable(consultaB,'Baptisme')
        tableB = BatejatTable(Btable)
        RequestConfig(request).configure(tableB)
        tableB.paginate(page = request.GET.get('page',1),per_page=10)
        
        Mtable = []
        if not Buit: Mtable = Assistant.getTable(consultaM,'Matrimoni')
        tableM = MatrimoniTable(Mtable)
        RequestConfig(request).configure(tableM)
        tableM.paginate(page = request.GET.get('page',1),per_page=10)
        
        Otable = []
        if not Buit: Otable = Assistant.getTable(consultaO,'Obituari')
        tableO = ObituariTable(Otable)
        RequestConfig(request).configure(tableO)
        tableO.paginate(page = request.GET.get('page',1),per_page=10)
        
        protaform = NomDataForm(newform)
        context = {'baptismes': tableB, 'matrimoni':tableM,'obituari':tableO,'form':protaform}
        render(request,'tfg/general_protagonista.html',context)
        
    else:
        protaform = NomDataForm()
        tableB = BatejatTable([])
        tableM = MatrimoniTable([])
        tableO = ObituariTable([])
        context = {'baptismes': tableB, 'matrimoni':tableM,'obituari':tableO,'form':protaform}
    return render(request,'tfg/general_protagonista.html',context)
def vida(request):
    global vidaform
    if request.method == 'POST' or bool(vidaform):
        if request.method == 'POST': vidaform = NomDataForm(request.POST)
        vidaform.is_valid()
        fdict = {}
        try:
            fdict = vidaform.cleaned_data
        except:    
            print('Error al cleaned data vida')
        consultaB = "";consultaM = "";consultaO = "";consultaP = ""
        Buit = True
        for e in fdict:
            if fdict[e]!='-' and fdict[e]!='':
                consultaB = "select * from  baptisme natural join event_taula WHERE "
                consultaM = "select * from  matrimoni natural join event_taula WHERE "
                consultaO = "select * from  obituari natural join event_taula WHERE "
                consultaP = "select * from  participant natural join event_taula WHERE "
                Buit = False
                break

        consultaB,_= Assistant.AddParameters(fdict,consultaB)
        consultaO,_= Assistant.AddParameters(fdict,consultaO)
        consultaM,_ = Assistant.AddParametersMatrimoni(fdict,consultaM)   
        consultaP,newform = Assistant.AddParameters(fdict,consultaP)
#        newform = dict(list(newform1.items()) + list(newformData.items()))
        tableB=[];tableM=[];tableO=[];tableP=[]
        if not Buit:
            tableB = Assistant.getTable(consultaB,'Baptisme')
            tableM = Assistant.getTable(consultaM,'Matrimoni')
            tableO = Assistant.getTable(consultaO,'Obituari')
            tableP = Assistant.getTable(consultaP,'Participant')

        vides = Assistant.getVides(tableB,tableM,tableO,tableP)
        vidaform = NomDataForm(newform)
        context = {'form':vidaform,'vides':vides}
        return render(request,'tfg/general_vida.html',context)
    else:
        vidaform = NomDataForm()
        vides = []
        context = {'form':vidaform,'vides':vides}
        return render(request,'tfg/general_vida.html',context)
def Matrimonis(request):
    global mform
    url = request.build_absolute_uri()
    if request.method == 'POST' or 'cerca-matrimonis' in url:
        if request.method == 'POST': mform = NomDataForm(request.POST)
        mform.is_valid()
        fdict = mform.cleaned_data
        consulta = "SELECT * from matrimoni natural join event_taula"   #Comencem a crear la consulta
        for e in fdict:
            if fdict[e]!='-' and fdict[e]!='':
                consulta += " WHERE"
                break
        consulta,newform = Assistant.AddParametersMatrimoni(fdict,consulta)
        Mtable = Assistant.getTable(consulta,'Matrimoni')
        tableM = MatrimoniTable(Mtable)
        RequestConfig(request).configure(tableM)
        tableM.paginate(page = request.GET.get('page',1),per_page=20)
        mform = NomDataForm(newform)
        context = {'matrimonis': tableM,'form': mform}
        return render(request,'tfg/base_matrimonis.html',context)

    else:
        arxiutable = [] 
        with connection.cursor() as cursor:
            cursor.execute("SELECT * from arxiu where nom_llibre LIKE 'M%'")
            arxius = cursor.fetchall()
        for a in arxius:
            arxiutable.append({'Arxiu':a[0],'Nom_Parroquia':a[1],
                               'Persona_B':a[2],'Persona_R':a[3]})
        table = ArxiuTable(arxiutable)
        RequestConfig(request).configure(table)
        table.paginate(page = request.GET.get('page',1),per_page=25)
        
        mform = NomDataForm()
        context = {'matrimonis': table,
                   'form': mform}
    return render(request,'tfg/base_matrimonis.html',context)