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 watch(request, table, filtre, page=None, nbparpage=None, nomClasser=None, plusOuMoins=None):
    table = int(table)  
    FILTRE = data.filtre(table)
    filtre = int(filtre)
    listAffich = data.listTable(table)
    listeliste = data.listinside(table)
    addmulti = data.mulipleajout(table)
    if page == None:
        page = 1
    else:
        page = int(page)
    if nomClasser == None:
        nomClasser = 100
    if plusOuMoins == None:
        plusOuMoins = 0   
    if nbparpage == None:
        nbparpage = 40
    else:
        nbparpage = int(nbparpage)       
    allP = False
    
    if nbparpage == 10000:
        allP = True 
    nomClasser = int(nomClasser)
    if int(plusOuMoins) == 0:
        plus = ''
    else:
        plus = '-'
    conditions = []
    cond = []
    if filtre > 0:
        if request.method == 'POST':
           
            form = data.form(table, 0, request.POST)
                
            if form.is_valid():
                filtre = 2
                
                for f in FILTRE:
                    clean = form.cleaned_data[f[0]]
                    
                    if f[4] == 1:
                        cleancond = int(clean)
                    else:
                        cleancond = clean
                    if cleancond != f[2]:
                        
                        if f[4] == 2:
                            cleanf = clean.strftime('%Y-%m-%d')
                        else:
                            cleanf = clean
                        
                        conditions.append((f[3], cleanf))
                        cond.append((f[1], 0))
                request.session['conditions'] = conditions
                request.session['cond'] = cond
                
        else:
            form = data.form(table, 0)
    if filtre == 2 and conditions == []:
        try:
            conditions = request.session['conditions']
            cond = request.session['cond']
            
            entier = 0
            for l in conditions:
                if cond[entier][1] == 1:
                    form.fields[cond[entier][0]].initial = (datetime.strptime(l[1], '%Y-%m-%d').date()).strftime('%d/%m/%Y')
                else:   
                    
                    form.fields[cond[entier][0]].initial = l[1]
                entier = entier + 1
        except:
            filtre = 0
    if nomClasser != 100:
        column = data.classer(table, nomClasser)
        if allP:
            if filtre == 2:
                rep = select(data.table(table), plus, column, listFiltre=conditions)
            else:
                rep = select(data.table(table), plus, column)   
        else:
            if filtre == 2:
                rep = select(data.table(table), plus, column, page, nbparpage, listFiltre=conditions)
            else:    
                rep = select(data.table(table), plus, column, page, nbparpage)
    else:    
        if allP:
            if filtre == 2:
                rep = select(data.table(table), listFiltre=conditions)
            else:
                rep = select(data.table(table))
        else :      
            if filtre == 2:     
                rep = select(data.table(table), page=page, nbparpage=nbparpage, listFiltre=conditions)
            else:
                rep = select(data.table(table), page=page, nbparpage=nbparpage)
    reponserecherche = rep[0]
    n = rep[1]    
    pagemax = nbparpage * page >= n
    pagemoins = page - 1
    pageplus = page + 1
    i = 1
    pages = []
    while nbparpage * (i) < n:
        pages.append(i)
        i = i + 1
    pages.append(i)
    return render(request, 'BDD/ADMIN/watch.html', locals())    
Esempio n. 3
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. 4
0
def watch(request, table, filtre, page=None, nbparpage=None, nomClasser=None, plusOuMoins=None):
    """
        It defines the variables used in the template watch.html, which print 
        data. You can filter and sort data.
    
    :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 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:
    
    >> watch(request, 1, 0, 1, 30, 1, 0)
    Will return the http response of the first page that print 30 groups per page (table=1),
    sorted by his first atribute (the name) in ascending order
    Filters forms wont be printed because filtre=0
    All the other parameters (after 3) are the previous parameter of the view watch.
    
    
    """
    isProf= request.user.personne.type==PROF_STATUT

    table = int(table)
    if not request.user.is_superuser and table != 6:
        return index(request)
    FILTRE = data.filtre(table)
    filtre = int(filtre)
    listAffich = data.listTable(table)
    listeliste = data.listinside(table)
    
    allllll = 'all'
    if page == None:
        page = 1
    else:
        page = int(page)
    if nomClasser == None:
        nomClasser = 100
    if plusOuMoins == None:
        plusOuMoins = 0   
    if nbparpage == None:
        nbparpage = 20
    else:
        nbparpage = int(nbparpage)       
    allP = False
    
    if nbparpage == 10000:
        allP = True 
    nomClasser = int(nomClasser)
    if int(plusOuMoins) == 0:
        plus = ''
    else:
        plus = '-'
    conditions = []
    cond = []
    #===========================================================================
    # if filters form are printed
    #===========================================================================
    if filtre > 0:
        #=======================================================================
        # if filter form has been validated
        #=======================================================================
        if request.method == 'POST':
           
            form = data.form(request.user, table, 0, request.POST)
                
            if form.is_valid():
                filtre = 2
                
                for f in FILTRE:
                    clean = form.cleaned_data[f[0]]
                    
                    if f[4] == 1:
                        cleancond = int(clean)
                    else:
                        cleancond = clean
                    if cleancond != f[2]:
                        
                        if f[4] == 2:
                            cleanf = clean.strftime('%Y-%m-%d')
                        elif f[4] == 3:
                            cleanf = str(clean)
                        elif f[4] == 4:
                            cleanf = clean.id
                        else:
                            cleanf = clean
                        
                        conditions.append((f[3], cleanf))
                        cond.append((f[1], 0))
                request.session['conditions'] = conditions
                request.session['cond'] = cond
                
        else:
            form = data.form(request.user, table, 0)
    #===========================================================================
    # filter forms are printed and the user has filtered the data
    #===========================================================================
    if filtre == 2 and conditions == []:
        try:
            conditions = request.session['conditions']
            
            cond = request.session['cond']
            if table == 6 and not request.user.is_superuser:
                    conditions.append(("prof", request.user.personne))
                    cond.append(("prof", 0))
            entier = 0
            for l in conditions:
                if cond[entier][1] == 1:
                    form.fields[cond[entier][0]].initial = (datetime.strptime(l[1], '%Y-%m-%d').date()).strftime('%d/%m/%Y')
                else:   
                    
                    form.fields[cond[entier][0]].initial = l[1]
                entier = entier + 1
        except:
            filtre = 0
    #===========================================================================
    # if nothing is sorted
    #===========================================================================
    if nomClasser != 100:
        column = data.classer(table, nomClasser)
        if allP:
            if filtre == 2 and conditions != []:
                rep = select(data.table(table), plus, column, listFiltre=conditions)
            else:
                if table == 6 and not request.user.is_superuser:
                    conditions = []
                    conditions.append(("prof", request.user.personne))
                    rep = select(data.table(table), plus, column, listFiltre=conditions)  
                else:
                    rep = select(data.table(table), plus, column)   
        else:
            if filtre == 2 and conditions != [] :
                rep = select(data.table(table), plus, column, page, nbparpage, listFiltre=conditions)
            else:    
                if table == 6 and not request.user.is_superuser:
                    conditions = []
                    conditions.append(("prof", request.user.personne))
                    rep = select(data.table(table), plus, column, page, nbparpage, listFiltre=conditions)   
                else:
                    rep = select(data.table(table), plus, column, page, nbparpage)
    #===========================================================================
    # something is sorted
    #===========================================================================
    else:    
        if allP:
            if filtre == 2 and conditions != []:
                rep = select(data.table(table), listFiltre=conditions)
            else:
                if table == 6 and not request.user.is_superuser:
                    conditions = []
                    conditions.append(("prof", request.user.personne))
                    rep = select(data.table(table), listFiltre=conditions) 
                else:
                    rep = select(data.table(table))
        else :      
            if filtre == 2 and conditions != []:     
                rep = select(data.table(table), page=page, nbparpage=nbparpage, listFiltre=conditions)
            else:
                if table == 6 and not request.user.is_superuser:
                    conditions = []
                    conditions.append(("prof", request.user.personne))
                    rep = select(data.table(table), page=page, nbparpage=nbparpage, listFiltre=conditions)
                else:
                    rep = select(data.table(table), page=page, nbparpage=nbparpage)
    reponserecherche = rep[0]
    n = rep[1]    
    pagemax = nbparpage * page >= n
    pagemoins = page - 1
    pageplus = page + 1
    i = 1
    pages = []
    while nbparpage * (i) < n:
        pages.append(i)
        i = i + 1
    pages.append(i)
    return render(request, 'BDD/ADMIN/watch.html', locals())    
Esempio n. 5
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())   
Esempio n. 6
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())