Esempio n. 1
0
def ajouter(request, table, nbajout, filtre, page, nbparpage, nomClasser, plusOuMoins, first=True):
    
    nbajout = int(nbajout)
    table = int(table)
    ficheAfter = data.ficheAfter(table)
    if nbajout > 0:
        
        listeform = range(0, nbajout)
        lquery = data.quiry(table)
        table = int(table)
        envoi = False
        Formset = formset_factory(data.form(table, 1), extra=nbajout)
        if request.method == 'POST' and first:
            
            formset = Formset(request.POST, request.FILES)
            if formset.is_valid():
                nbajout = 0  
                envoi = True
                
                for form in formset:
                    
                    idP = form.save()
                
                if not ficheAfter:
                    
                    return change(request, table, idP, 0, filtre, page, nbparpage, nomClasser, plusOuMoins, 0, False)
            form = nbAjout()
                       
        else:
            formset = Formset() 
            
            
    else:
        if request.method == 'POST':
            
            form = nbAjout(request.POST)  
            if form.is_valid():
                nb = form.cleaned_data['nb']
                
                return ajouter(request, table, nb, filtre, page, nbparpage, nomClasser, plusOuMoins, False)

        else:
            
            form = nbAjout() 
            
    return render(request, 'BDD/ADMIN/ajouter.html', locals())
Esempio n. 2
0
def change(request, table, idP, what, filtre, page, nbparpage, nomClasser, plusOuMoins, nbajout, first=True):
    table = int(table)
    soustable = data.soustable(table)
    TABBLE = data.table(table)
    obj = TABBLE.objects.get(id=int(idP))
    links = data.links(table)
    titrest = []
    ii = 0
    changed = False
    if (first and request.method == 'POST'  and int(nbajout) == 0):
        
        nbajoutf = nbAjout(request.POST)
    
        if nbajoutf.is_valid():
            nbajout = int(nbajoutf.cleaned_data['nb'])
            first=False
            
            
            
    else:
        nbajoutf = nbAjout()
    
    if (first and request.method == 'POST' and int(what) > 0 and int(nbajout) > 0):
        
        stforms = data.formsoustable(table, request.POST, int(what))
        
        for frm in stforms:
            llll=[o.id for o in getattr(obj, frm[5]).all()]
            frm[0].fields[frm[3]].queryset = frm[4].objects.all().exclude(id__in=llll)
        for frm in stforms:
            
            if frm[0].is_valid():
                frm[0].save(obj)
                changed = True
                return http.HttpResponseRedirect('')
    else:
        
        stforms = data.formsoustable(table)
        
        
        for frm in stforms:
            llll=[o.id for o in getattr(obj, frm[5]).all()]
            frm[0].fields[frm[3]].queryset = frm[4].objects.all().exclude(id__in=llll)
            Formset = formset_factory(frm[0], int(nbajout))
            
    
    if (first and request.method == 'POST' and int(what) == 0 and int(nbajout) > 0):
        
        form = data.form(table, 0, request.POST) 
        if form.is_valid():
            form.modif(idP)    
            changed = True
            
            
    else:
       
        cond = []
        conditions = []
        
        form = data.form(table, 0)
        
        data.changecond(table, cond, conditions, obj)
         
        entier = 0
        for l in conditions:
            if cond[entier][1] == 1:
                form.fields[cond[entier][0]].initial = l.strftime('%d/%m/%Y')
            else:   
                form.fields[cond[entier][0]].initial = l
            entier = entier + 1 
    
    for st in soustable:
        
        if st[0] == 0:
            titrest.append([stforms[ii], st[0], st[2], [getattr(obj, st[3]).all(), st[4]], st[1]])
        else:
            titrest.append([stforms[ii], st[0], st[2], [getattr(obj, st[3], st[1]).all()], st[1]])
            
        ii = ii + 1  
    return render(request, 'BDD/ADMIN/change.html', locals())       
Esempio n. 3
0
def ajouter(request, table, nbajout, filtre, page, nbparpage, nomClasser, plusOuMoins, first=True):
    """
        It defines the variables used in the template ajouter.html, which print 
        forms to add one or mutltiple objects.
    
    :param request: Class that give many information like POST, GET and user data.
    :type request: Request    
    :param table: define in which model the object is.
    :type table: int
    :param first: Is it the first time ajouter() has been call ?
    :type first: boolean
    :param nbajout: if 0, print forms to know how many object the user want to add.
                    if 100, add object for a manyto many relation ex: notes for a group
                    if >100, the objcts are added in a session after =100
                    if not, it adds nbajout forms to add nbajout objects.
    :type nbajout: int
            
            the variable bellow are there so when the user press the buttom back
            he has the same specification as before in the view watch.
            
    :param filtre: if = 1, filter forms are printed. if =2, filter forms are printed
                    and the user has filtered the data. Else, forms no printed, no filters.
    :type filtre: int
    :param page: defines which page is printed. If None, it is the first page.
    :type page: int
    :param nbparpage: defines how many object are printed every pages. if None it is 40. 
                      If 10000 its all of them in one page
    :type nbparpage: int 
    :param nomClasser: Which column is sorted. if None or = 100, nothing is sorted.
    :type nomClasser: int
    :param plusOuMoins:  if None or 0, data is sorted in ascending order, else in descending order.
    :type plusOuMoins: int 
    :return: What the user is going to view on his screen 
    :rtype: HttpResponse
    
    :Exemple:
    
    >> ajouter(request, 1, 3, 0, 1, 12, 0, 0)
    Will return the http response of the page that print 3 forms to add 3
    groups in the database (table = 1 represents group database).
    All the other parameters (after 3) are the previous parameter of the view watch.
    
    .. warnings:: if the user play with nbajout in the url, he will be redirected to index.hmlt
    if he doesn't do it correctly (so no errors occurs)
    
    """
    
    isProf= request.user.personne.type==PROF_STATUT
    nbajout = int(nbajout)
    table = int(table)
    MODEL=data.table(table)()
    if hasattr(MODEL,'ajouterPlusieurs'):
       
       
        AJJ=MODEL.ajouterPlusieurs()
        
    else:
        AJJ=None
    if not request.user.is_superuser and table != 6:  # case of teacher
        return http.HttpResponseRedirect('/')
    #ajj = data.ajouterA(table)
    ficheAfter = False
    if nbajout > 0 and nbajout != 100:
        
        listeform = range(0, nbajout)
        if hasattr(MODEL,'quiery'):
            lquery = MODEL.quiery()
        else:
            lquery =[]
        table = int(table)
        envoi = False
        #===========================================================================
        #      Nb ajout normal. No many to many relations
        #===========================================================================
        if nbajout < 100:
            
            if data.form(request.user, table, 3) == None:
                Formset = formset_factory(data.form(request.user, table, 1), extra=nbajout)
            else:
                
                Formset = formset_factory(data.form(request.user, table, 1), extra=nbajout, formset=data.form(request.user, table, 3))
            if request.method == 'POST' and first:
             
                formset = Formset(request.POST, request.FILES)
                if formset.is_valid():
                    
                    nbajout = 0  
                    envoi = True
                    
                    
                    for form in formset:
                        
                        idP = form.save(request.user.personne)
                        
                    
                form = nbAjout()
                           
            else:
                formset = Formset() 
        #===========================================================================
        #      Many to many relations, the user has already write the forms
        #      to know which many to many relations exactly
        #===========================================================================
        else:
            if 'stock' not in request.session:
                return http.HttpResponseRedirect('/')
            stock = request.session['stock']
            solo = []
            multi = []
            i = 0
            
            if AJJ!=None:
                
                for x in AJJ.listFieldForm:
                    if i==0:
                        kwargs = {
                            AJJ.QCode: stock[i]
                        }
                        a = AJJ.listModel[i].objects.filter(**kwargs)
                        nb = a.count()
                        Formset = formset_factory(AJJ.form, extra=nb, formset=AJJ.baseForm)
                        
                        if request.method == 'POST' and first:
                           
                            formset = Formset(request.POST, request.FILES)
                        
                                
                        else:
                            formset = Formset()
                        b = []
                        ii = 0
                        for aa in a:
                            b.append([aa, formset[ii]])
                            ii += 1
                            
                        multi.append(b)
                        
                        
                    else:
                        solo.append(AJJ.listModel[i].objects.get(id=stock[i]))
                    i += 1
                    
            if request.method == 'POST' and first:
                if formset.is_valid():
                    jj = 0
                    for f in formset:
                        if table == 6:
                            f.save(request.user.personne, solo, [x[jj] for x in multi], request.user.personne)
                        else:
                            f.save(request.user.personne, solo, [x[jj] for x in multi])
                        jj += 1
                    nbajout = 0  
                    return http.HttpResponseRedirect('/watch/' + str(table) + '/' + str(filtre))
            
    else:
        if request.method == 'POST':
            #===========================================================================
            #      Normal nb ajout. no many to many relations. 
            #      print forms to know how much
            #===========================================================================
            if AJJ == None or nbajout < 100:
                
                form = nbAjout(request.POST)
                
                if form.is_valid():
                    nb = form.cleaned_data['nb']
                    
                    return ajouter(request, table, nb, filtre, page, nbparpage, nomClasser, plusOuMoins, False)
                else:
                    if AJJ != None:
                        formAjj = AJJ.formInit()
            #===========================================================================
            #      Many to many relations,print the forms
            #      to know which many to many relations 
            #===========================================================================
            else:
                formAjj = AJJ.formInit(request.POST)
                stock = []
                if formAjj.is_valid():
                    i=0
                    for x in AJJ.listFieldForm:
                        if i == 0:
                            stock.append(formAjj.cleaned_data[x])
                        else:
                            stock.append(formAjj.cleaned_data[x].id)
                        i=i+1
                    request.session['stock'] = stock
                    return ajouter(request, table, 101, filtre, page, nbparpage, nomClasser, plusOuMoins, False)
        else:
            if AJJ != None:
                
                formAjj = AJJ.formInit()
            form = nbAjout() 
            
            
    return render(request, 'BDD/ADMIN/ajouter.html', locals())