2021-01-17 09:37:11 +01:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2022-01-01 14:49:42 +01:00
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
2021-01-17 09:37:11 +01:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Emmanuel Viennet emmanuel.viennet@viennet.net
#
##############################################################################
""" Fonctions sur les entreprises
"""
# codes anciens déplacés de ZEntreprise
import datetime
2021-07-09 23:19:30 +02:00
from operator import itemgetter
2021-01-17 09:37:11 +01:00
2021-06-19 23:21:37 +02:00
import app . scodoc . sco_utils as scu
import app . scodoc . notesdb as ndb
2021-07-12 15:13:10 +02:00
from app . scodoc . notesdb import ScoDocCursor , EditableTable , DateISOtoDMY , DateDMYtoISO
2021-01-17 09:37:11 +01:00
def _format_nom ( nom ) :
" formatte nom (filtre en entree db) d ' une entreprise "
if not nom :
return nom
2021-07-12 15:13:10 +02:00
return nom [ 0 ] . upper ( ) + nom [ 1 : ]
2021-01-17 09:37:11 +01:00
class EntreprisesEditor ( EditableTable ) :
def delete ( self , cnx , oid ) :
" delete correspondants and contacts, then self "
# first, delete all correspondants and contacts
cursor = cnx . cursor ( cursor_factory = ScoDocCursor )
cursor . execute (
" delete from entreprise_contact where entreprise_id= %(entreprise_id)s " ,
{ " entreprise_id " : oid } ,
)
cursor . execute (
" delete from entreprise_correspondant where entreprise_id= %(entreprise_id)s " ,
{ " entreprise_id " : oid } ,
)
cnx . commit ( )
EditableTable . delete ( self , cnx , oid )
def list (
self ,
cnx ,
args = { } ,
operator = " and " ,
test = " = " ,
sortkey = None ,
sort_on_contact = False ,
limit = " " ,
offset = " " ,
) :
# list, then sort on date of last contact
R = EditableTable . list (
self ,
cnx ,
args = args ,
operator = operator ,
test = test ,
sortkey = sortkey ,
limit = limit ,
offset = offset ,
)
if sort_on_contact :
for r in R :
c = do_entreprise_contact_list (
args = { " entreprise_id " : r [ " entreprise_id " ] } ,
disable_formatting = True ,
)
if c :
r [ " date " ] = max ( [ x [ " date " ] or datetime . date . min for x in c ] )
else :
r [ " date " ] = datetime . date . min
# sort
2021-07-09 23:19:30 +02:00
R . sort ( key = itemgetter ( " date " ) )
2021-01-17 09:37:11 +01:00
for r in R :
r [ " date " ] = DateISOtoDMY ( r [ " date " ] )
return R
def list_by_etud (
self , cnx , args = { } , sort_on_contact = False , disable_formatting = False
) :
" cherche rentreprise ayant eu contact avec etudiant "
cursor = cnx . cursor ( cursor_factory = ScoDocCursor )
cursor . execute (
" select E.*, I.nom as etud_nom, I.prenom as etud_prenom, C.date from entreprises E, entreprise_contact C, identite I where C.entreprise_id = E.entreprise_id and C.etudid = I.etudid and I.nom ~* %(etud_nom)s ORDER BY E.nom " ,
args ,
)
_ , res = [ x [ 0 ] for x in cursor . description ] , cursor . dictfetchall ( )
R = [ ]
for r in res :
r [ " etud_prenom " ] = r [ " etud_prenom " ] or " "
d = { }
for key in r :
v = r [ key ]
# format value
2021-07-09 17:47:06 +02:00
if not disable_formatting and key in self . output_formators :
2021-01-17 09:37:11 +01:00
v = self . output_formators [ key ] ( v )
d [ key ] = v
R . append ( d )
# sort
if sort_on_contact :
2021-07-09 23:19:30 +02:00
R . sort ( key = lambda x : ( x [ " date " ] or datetime . date . min ) )
2021-01-17 09:37:11 +01:00
for r in R :
r [ " date " ] = DateISOtoDMY ( r [ " date " ] or datetime . date . min )
return R
_entreprisesEditor = EntreprisesEditor (
" entreprises " ,
" entreprise_id " ,
(
" entreprise_id " ,
" nom " ,
" adresse " ,
" ville " ,
" codepostal " ,
" pays " ,
" contact_origine " ,
" secteur " ,
" privee " ,
" localisation " ,
" qualite_relation " ,
" plus10salaries " ,
" note " ,
" date_creation " ,
) ,
2021-08-13 00:34:58 +02:00
filter_dept = True ,
2021-01-17 09:37:11 +01:00
sortkey = " nom " ,
2021-08-11 00:36:07 +02:00
input_formators = {
" nom " : _format_nom ,
" plus10salaries " : bool ,
} ,
2021-01-17 09:37:11 +01:00
)
# ----------- Correspondants
_entreprise_correspEditor = EditableTable (
" entreprise_correspondant " ,
" entreprise_corresp_id " ,
(
" entreprise_corresp_id " ,
" entreprise_id " ,
" civilite " ,
" nom " ,
" prenom " ,
" fonction " ,
" phone1 " ,
" phone2 " ,
" mobile " ,
" fax " ,
" mail1 " ,
" mail2 " ,
" note " ,
) ,
sortkey = " nom " ,
)
# ----------- Contacts
_entreprise_contactEditor = EditableTable (
" entreprise_contact " ,
" entreprise_contact_id " ,
(
" entreprise_contact_id " ,
" date " ,
" type_contact " ,
" entreprise_id " ,
" entreprise_corresp_id " ,
" etudid " ,
" description " ,
" enseignant " ,
) ,
sortkey = " date " ,
output_formators = { " date " : DateISOtoDMY } ,
input_formators = { " date " : DateDMYtoISO } ,
)
2021-08-21 00:24:51 +02:00
def do_entreprise_create ( args ) :
2021-01-17 09:37:11 +01:00
" entreprise_create "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
r = _entreprisesEditor . create ( cnx , args )
return r
2021-08-21 00:24:51 +02:00
def do_entreprise_delete ( oid ) :
2021-01-17 09:37:11 +01:00
" entreprise_delete "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
_entreprisesEditor . delete ( cnx , oid )
2021-08-21 00:24:51 +02:00
def do_entreprise_list ( * * kw ) :
2021-01-17 09:37:11 +01:00
" entreprise_list "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
return _entreprisesEditor . list ( cnx , * * kw )
2021-08-21 00:24:51 +02:00
def do_entreprise_list_by_etud ( * * kw ) :
2021-01-17 09:37:11 +01:00
" entreprise_list_by_etud "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
return _entreprisesEditor . list_by_etud ( cnx , * * kw )
2021-08-21 00:24:51 +02:00
def do_entreprise_edit ( * args , * * kw ) :
2021-01-17 09:37:11 +01:00
" entreprise_edit "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
_entreprisesEditor . edit ( cnx , * args , * * kw )
2021-08-21 00:24:51 +02:00
def do_entreprise_correspondant_create ( args ) :
2021-01-17 09:37:11 +01:00
" entreprise_correspondant_create "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
r = _entreprise_correspEditor . create ( cnx , args )
return r
2021-08-21 00:24:51 +02:00
def do_entreprise_correspondant_delete ( oid ) :
2021-01-17 09:37:11 +01:00
" entreprise_correspondant_delete "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
_entreprise_correspEditor . delete ( cnx , oid )
2021-08-21 00:24:51 +02:00
def do_entreprise_correspondant_list ( * * kw ) :
2021-01-17 09:37:11 +01:00
" entreprise_correspondant_list "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
return _entreprise_correspEditor . list ( cnx , * * kw )
2021-08-21 00:24:51 +02:00
def do_entreprise_correspondant_edit ( * args , * * kw ) :
2021-01-17 09:37:11 +01:00
" entreprise_correspondant_edit "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
_entreprise_correspEditor . edit ( cnx , * args , * * kw )
2021-08-21 00:24:51 +02:00
def do_entreprise_correspondant_listnames ( args = { } ) :
2021-01-17 09:37:11 +01:00
" -> liste des noms des correspondants (pour affichage menu) "
2021-08-21 00:24:51 +02:00
C = do_entreprise_correspondant_list ( args = args )
2021-01-17 09:37:11 +01:00
return [ ( x [ " prenom " ] + " " + x [ " nom " ] , str ( x [ " entreprise_corresp_id " ] ) ) for x in C ]
2021-08-21 00:24:51 +02:00
def do_entreprise_contact_delete ( oid ) :
2021-01-17 09:37:11 +01:00
" entreprise_contact_delete "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
_entreprise_contactEditor . delete ( cnx , oid )
2021-08-21 00:24:51 +02:00
def do_entreprise_contact_list ( * * kw ) :
2021-01-17 09:37:11 +01:00
" entreprise_contact_list "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
return _entreprise_contactEditor . list ( cnx , * * kw )
2021-08-21 00:24:51 +02:00
def do_entreprise_contact_edit ( * args , * * kw ) :
2021-01-17 09:37:11 +01:00
" entreprise_contact_edit "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
_entreprise_contactEditor . edit ( cnx , * args , * * kw )
2021-08-21 00:24:51 +02:00
def do_entreprise_contact_create ( args ) :
2021-01-17 09:37:11 +01:00
" entreprise_contact_create "
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
r = _entreprise_contactEditor . create ( cnx , args )
return r
2021-08-21 00:24:51 +02:00
def do_entreprise_check_etudiant ( etudiant ) :
2021-01-17 09:37:11 +01:00
""" Si etudiant est vide, ou un ETUDID valide, ou un nom unique,
retourne ( 1 , ETUDID ) .
Sinon , retourne ( 0 , ' message explicatif ' )
"""
etudiant = etudiant . strip ( ) . translate (
2021-07-12 10:51:45 +02:00
str . maketrans ( " " , " " , " ' () " )
2021-01-17 09:37:11 +01:00
) # suppress parens and quote from name
if not etudiant :
return 1 , None
2021-06-15 13:59:56 +02:00
cnx = ndb . GetDBConnexion ( )
2021-01-17 09:37:11 +01:00
cursor = cnx . cursor ( cursor_factory = ScoDocCursor )
cursor . execute (
" select etudid, nom, prenom from identite where upper(nom) ~ upper( %(etudiant)s ) or etudid= %(etudiant)s " ,
{ " etudiant " : etudiant } ,
)
r = cursor . fetchall ( )
if len ( r ) < 1 :
return 0 , ' Aucun etudiant ne correspond à " %s " ' % etudiant
elif len ( r ) > 10 :
return (
0 ,
" <b> %d etudiants</b> correspondent à ce nom (utilisez le code) " % len ( r ) ,
)
elif len ( r ) > 1 :
e = [ ' <ul class= " entreprise_etud_list " > ' ]
for x in r :
e . append (
2021-08-21 00:24:51 +02:00
" <li> %s %s (code %s )</li> " % ( ( x [ 1 ] ) . upper ( ) , x [ 2 ] or " " , x [ 0 ] . strip ( ) )
2021-01-17 09:37:11 +01:00
)
e . append ( " </ul> " )
return (
0 ,
" Les étudiants suivants correspondent: préciser le nom complet ou le code \n "
+ " \n " . join ( e ) ,
)
else : # une seule reponse !
return 1 , r [ 0 ] [ 0 ] . strip ( )