Пример #1
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())       
Пример #2
0
def change(request, table, idP, what, filtre, page, nbparpage, nomClasser, plusOuMoins, first=True):
    """
        It defines the variables used in the template change.html, which print 
        forms to change an object.
    
    :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 idP: id of the object to change 
    :type idP: int
    :param what: if >0 the user want to change a manytomany relation.  
    :type what: int
    :param first: Is it the first time change() has been call ?
    :type first: boolean
            
            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:
    
    >> change(request, 1, 3, 0, 1, 0, 10, 1, 0)
    Will return the http response of the page that change the group (because table=1) and the id =3.
    what=0 so no many to many relations involved (yet).
    All the other parameters (after 3) are the previous parameter of the view watch.
    
    .. warnings:: admin or teacher in a specific view can change ids in the url.
    
    """
    isProf= request.user.personne.type==PROF_STATUT

    table = int(table)
    if not data.table(table).objects.filter(id=int(idP)).count() > 0 or table > 7:
        return http.HttpResponseRedirect('/')
    if not request.user.is_superuser and table != 6:
        return http.HttpResponseRedirect('/')
    soustable = data.soustable(table)
    TABBLE = data.table(table)
    obj = TABBLE.objects.get(id=int(idP))
    MODEL=data.table(table)()
    if hasattr(MODEL,'links'):
        links = MODEL.links()
    titrest = []
    formseti = None
    ii = 0
    changed = False
    stforms = data.formsoustable(table)
    #===========================================================================
    #  if user wants to change manytomany relations
    #===========================================================================
    if (first and request.method == 'POST' and int(what) > 0):
        for frm in stforms:
            instance = TABBLE.objects.get(id=int(idP))
            frm[0] = frm[0](request.POST, instance=instance)
        j = 1  
        for frm in stforms:
            
            if j == int(what) and frm[0].is_valid():
                frm[0].savePerso(int(idP), request.user.personne)
                changed = True
            j += 1
        return http.HttpResponseRedirect('')
    #===========================================================================
    # no post for manytyomany
    #===========================================================================
    else:
        
        stforms = data.formsoustable(table)
        for frm in stforms:
            instance = TABBLE.objects.get(id=int(idP))
            frm[0] = frm[0](instance=instance)
            
            
    #===========================================================================
    #  if user wants to change value
    #===========================================================================        
    if (first and request.method == 'POST' and int(what) == 0):
        
        form = data.form(request.user, table, 2, request.POST) 
        if form.is_valid():
            form.modif(idP, request.user.personne)    
            changed = True
    #===========================================================================
    #  No POST for value
    #===========================================================================          
    else:
       
        cond = []
        conditions = []
        
        form = data.form(request.user, table, 2)
        
        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 
    taille = len(stforms)
  
    for st in soustable:
        
        if ii < taille:
            
            if st[0] == 0:
                
                titrest.append([stforms[ii], st[0], st[2], [getattr(obj, st[3][0]).all(), st[3][1]], st[1]])
            else:
                
                titrest.append([stforms[ii], st[0], st[2], [getattr(obj, st[3][0], st[1]).all()], st[1]])
        else:
            break        
        ii = ii + 1  
    return render(request, 'BDD/ADMIN/change.html', locals())