[attributs] Moar parsing functions
This commit is contained in:
parent
236115ea1a
commit
84a455ec63
2 changed files with 82 additions and 29 deletions
102
attributs.py
102
attributs.py
|
@ -30,26 +30,13 @@
|
||||||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
import re
|
import re, sys
|
||||||
from unicodedata import normalize
|
from unicodedata import normalize
|
||||||
from crans_utils import format_tel
|
from crans_utils import format_tel, mailexist, validate_name
|
||||||
|
|
||||||
def normalize_and_validate(attr, vals, ctxt):
|
sys.path.append("/usr/scripts/gestion")
|
||||||
"""Vérifie que attr peut se voir attribuer les valeurs vals"""
|
import config, annuaires_pg
|
||||||
a = eval('%s()' % attr)
|
|
||||||
new_vals = a.normalize(vals, ctxt)
|
|
||||||
a.validate(new_vals, ctxt)
|
|
||||||
return new_vals
|
|
||||||
|
|
||||||
def validate_name(value, more_chars=""):
|
|
||||||
"""Valide un nom: ie un unicode qui contient lettres, espaces et
|
|
||||||
apostrophes, et éventuellement des caractères additionnels"""
|
|
||||||
return re.match("[A-Za-z][-' A-Za-z%s]*" % more_chars,
|
|
||||||
normalize('NFKD', value).encode('ASCII', 'ignore'))
|
|
||||||
|
|
||||||
def validate_mac(value):
|
|
||||||
"""Vérifie qu'une adresse mac est valide"""
|
|
||||||
return True
|
|
||||||
|
|
||||||
def attrify(val, attr, ldif, conn, ctxt_check = True):
|
def attrify(val, attr, ldif, conn, ctxt_check = True):
|
||||||
"""Transforme un n'importe quoi en Attr.
|
"""Transforme un n'importe quoi en Attr.
|
||||||
|
@ -92,6 +79,10 @@ class Attr(object):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return unicode(self).encode('utf-8')
|
return unicode(self).encode('utf-8')
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
assert isinstance(self.value, unicode)
|
||||||
|
return self.value
|
||||||
|
|
||||||
def validate(self, ldif):
|
def validate(self, ldif):
|
||||||
"""validates:
|
"""validates:
|
||||||
vérifie déjà que ce qu'on a rentré est parsable"""
|
vérifie déjà que ce qu'on a rentré est parsable"""
|
||||||
|
@ -112,8 +103,11 @@ class Attr(object):
|
||||||
"""Vérifie l'unicité dans la base de la valeur (mailAlias, chbre,
|
"""Vérifie l'unicité dans la base de la valeur (mailAlias, chbre,
|
||||||
etc...)"""
|
etc...)"""
|
||||||
attr = self.__class__.__name__
|
attr = self.__class__.__name__
|
||||||
if attr in [ "mid", "uid", "cid", "fid",]: #... etc
|
if attr in [ "mid", "uid", "cid", "fid"]: #... etc
|
||||||
assert not self.conn.search('%s=%s' % (attr, str(self)))
|
assert not self.conn.search('%s=%s' % (attr, str(self)))
|
||||||
|
if attr in [ "mailAlias", "canonicalAlias"]:
|
||||||
|
assert not self.conn.search('|(mailAlias=%s)(canonicalAlias=%s)' % (str(self),)*2)
|
||||||
|
assert not mailexist(str(self))
|
||||||
|
|
||||||
def _check_users_restrictions(self, values):
|
def _check_users_restrictions(self, values):
|
||||||
"""Vérifie les restrictions supplémentaires imposées selon les
|
"""Vérifie les restrictions supplémentaires imposées selon les
|
||||||
|
@ -128,25 +122,33 @@ class objectClass(Attr):
|
||||||
optional = False
|
optional = False
|
||||||
legend = "entité"
|
legend = "entité"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
if val not in [ 'top', 'posixAccount', 'shadowAccount', 'proprio',
|
||||||
|
'adherent', 'club', 'machine', 'machineCrans',
|
||||||
|
'borneWifi', 'machineWifi', 'machineFixe',
|
||||||
|
'cransAccount', 'service', 'facture', 'freeMid' ]:
|
||||||
|
raise ValueError("Pourquoi insérer un objectClass=%s ?" % val)
|
||||||
|
else:
|
||||||
|
self.value = val
|
||||||
|
|
||||||
|
|
||||||
class nom(Attr):
|
class nom(Attr):
|
||||||
singlevalue = True
|
singlevalue = True
|
||||||
optional = False
|
optional = False
|
||||||
legend = "Nom"
|
legend = "Nom"
|
||||||
def _check_type(self, values):
|
|
||||||
return [ validate_name(v) for v in values]
|
|
||||||
|
|
||||||
def __unicode__(self):
|
def parse_value(self, val):
|
||||||
return self.value
|
self.value = validate_name(val)
|
||||||
|
|
||||||
|
|
||||||
class prenom(Attr):
|
class prenom(Attr):
|
||||||
singlevalue = True
|
singlevalue = True
|
||||||
optional = False
|
optional = False
|
||||||
legend = u"Prénom"
|
legend = u"Prénom"
|
||||||
def _check_type(self, values):
|
|
||||||
return [ validate_name(v) for v in values ]
|
|
||||||
|
|
||||||
def __unicode__(self):
|
def parse_value(self, values):
|
||||||
return self.value
|
self.value = validate_name(val)
|
||||||
|
|
||||||
|
|
||||||
class tel(Attr):
|
class tel(Attr):
|
||||||
singlevalue = True
|
singlevalue = True
|
||||||
|
@ -158,47 +160,89 @@ class tel(Attr):
|
||||||
if len(self.value) == 0:
|
if len(self.value) == 0:
|
||||||
raise ValueError("Numéro de téléphone invalide ('%s')" % val)
|
raise ValueError("Numéro de téléphone invalide ('%s')" % val)
|
||||||
|
|
||||||
def __unicode__(self):
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
class paiement(Attr):
|
class paiement(Attr):
|
||||||
singlevalue = False
|
singlevalue = False
|
||||||
optional = True
|
optional = True
|
||||||
legend = u"Paiement"
|
legend = u"Paiement"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
if int(val) < 1998 or int(val) > config.ann_scol:
|
||||||
|
raise ValueError("Année de cotisation invalide (%s)" % val)
|
||||||
|
self.value = int(val)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.value # XXX - To implement
|
return unicode(self.value)
|
||||||
|
|
||||||
|
|
||||||
class carteEtudiant(Attr):
|
class carteEtudiant(Attr):
|
||||||
singlevalue = False
|
singlevalue = False
|
||||||
optional = True
|
optional = True
|
||||||
legend = u"Carte d'étudiant"
|
legend = u"Carte d'étudiant"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
if int(val) < 1998 or int(val) > config.ann_scol:
|
||||||
|
raise ValueError("Année de cotisation invalide (%s)" % val)
|
||||||
|
self.value = int(val)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return unicode(self.value)
|
||||||
|
|
||||||
|
|
||||||
class mailAlias(Attr):
|
class mailAlias(Attr):
|
||||||
singlevalue = False
|
singlevalue = False
|
||||||
optional = True
|
optional = True
|
||||||
legend = u"Alias mail"
|
legend = u"Alias mail"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
val = val.lower()
|
||||||
|
if not re.match('[a-z][-_.0-9a-z]+', val):
|
||||||
|
raise ValueError("Alias mail invalide (%s)" % val)
|
||||||
|
self.value = val
|
||||||
|
|
||||||
|
|
||||||
class canonicalAlias(Attr):
|
class canonicalAlias(Attr):
|
||||||
singlevalue = True
|
singlevalue = True
|
||||||
optional = False
|
optional = False
|
||||||
legend = u"Alias mail canonique"
|
legend = u"Alias mail canonique"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
val = val.lower()
|
||||||
|
if not re.match('[a-z][-_.0-9a-z]+', val):
|
||||||
|
raise ValueError("Alias mail invalide (%s)" % val)
|
||||||
|
self.value = val
|
||||||
|
|
||||||
|
|
||||||
class etudes(Attr):
|
class etudes(Attr):
|
||||||
singlevalue = False
|
singlevalue = False
|
||||||
optional = False
|
optional = False
|
||||||
legend = u"Études"
|
legend = u"Études"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
# who cares
|
||||||
|
self.value = val
|
||||||
|
|
||||||
|
|
||||||
class chbre(Attr):
|
class chbre(Attr):
|
||||||
singlevalue = True
|
singlevalue = True
|
||||||
optional = False
|
optional = False
|
||||||
legend = u"Chambre sur le campus"
|
legend = u"Chambre sur le campus"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
annuaires_pg.chbre_prises(val[0], val[1:])
|
||||||
|
self.value = val
|
||||||
|
|
||||||
class droits(Attr):
|
class droits(Attr):
|
||||||
singlevalue = False
|
singlevalue = False
|
||||||
optional = True
|
optional = True
|
||||||
legend = u"Droits sur les serveurs"
|
legend = u"Droits sur les serveurs"
|
||||||
|
|
||||||
|
def parse_value(self, val):
|
||||||
|
if val not in ['apprenti', 'nounou', 'cableur', 'tresorier', 'bureau',
|
||||||
|
'webmaster', 'webradio', 'imprimeur', 'multimachines', 'victime']:
|
||||||
|
raise ValueError("Ces droits n'existent pas ('%s')" % val)
|
||||||
|
self.value = val
|
||||||
|
|
||||||
class solde(Attr):
|
class solde(Attr):
|
||||||
singlevalue = True
|
singlevalue = True
|
||||||
optional = True
|
optional = True
|
||||||
|
|
|
@ -87,3 +87,12 @@ def format_tel(tel):
|
||||||
tel_f = re.sub(r'\D', '', tel_f)
|
tel_f = re.sub(r'\D', '', tel_f)
|
||||||
return tel_f
|
return tel_f
|
||||||
|
|
||||||
|
def validate_name(value, more_chars=""):
|
||||||
|
"""Valide un nom: ie un unicode qui contient lettres, espaces et
|
||||||
|
apostrophes, et éventuellement des caractères additionnels"""
|
||||||
|
if re.match("[A-Za-z]([-' %s]?[A-Za-z])*" % more_chars,
|
||||||
|
normalize('NFKD', value).encode('ASCII', 'ignore')):
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
raise ValueError("Nom invalide ('%s')" % value)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue