[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
|
||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import re
|
||||
import re, sys
|
||||
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):
|
||||
"""Vérifie que attr peut se voir attribuer les valeurs vals"""
|
||||
a = eval('%s()' % attr)
|
||||
new_vals = a.normalize(vals, ctxt)
|
||||
a.validate(new_vals, ctxt)
|
||||
return new_vals
|
||||
sys.path.append("/usr/scripts/gestion")
|
||||
import config, annuaires_pg
|
||||
|
||||
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):
|
||||
"""Transforme un n'importe quoi en Attr.
|
||||
|
@ -92,6 +79,10 @@ class Attr(object):
|
|||
def __str__(self):
|
||||
return unicode(self).encode('utf-8')
|
||||
|
||||
def __unicode__(self):
|
||||
assert isinstance(self.value, unicode)
|
||||
return self.value
|
||||
|
||||
def validate(self, ldif):
|
||||
"""validates:
|
||||
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,
|
||||
etc...)"""
|
||||
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)))
|
||||
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):
|
||||
"""Vérifie les restrictions supplémentaires imposées selon les
|
||||
|
@ -128,25 +122,33 @@ class objectClass(Attr):
|
|||
optional = False
|
||||
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):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
legend = "Nom"
|
||||
def _check_type(self, values):
|
||||
return [ validate_name(v) for v in values]
|
||||
|
||||
def __unicode__(self):
|
||||
return self.value
|
||||
def parse_value(self, val):
|
||||
self.value = validate_name(val)
|
||||
|
||||
|
||||
class prenom(Attr):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
legend = u"Prénom"
|
||||
def _check_type(self, values):
|
||||
return [ validate_name(v) for v in values ]
|
||||
|
||||
def __unicode__(self):
|
||||
return self.value
|
||||
def parse_value(self, values):
|
||||
self.value = validate_name(val)
|
||||
|
||||
|
||||
class tel(Attr):
|
||||
singlevalue = True
|
||||
|
@ -158,47 +160,89 @@ class tel(Attr):
|
|||
if len(self.value) == 0:
|
||||
raise ValueError("Numéro de téléphone invalide ('%s')" % val)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.value
|
||||
|
||||
class paiement(Attr):
|
||||
singlevalue = False
|
||||
optional = True
|
||||
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):
|
||||
return self.value # XXX - To implement
|
||||
return unicode(self.value)
|
||||
|
||||
|
||||
class carteEtudiant(Attr):
|
||||
singlevalue = False
|
||||
optional = True
|
||||
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):
|
||||
singlevalue = False
|
||||
optional = True
|
||||
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):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
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):
|
||||
singlevalue = False
|
||||
optional = False
|
||||
legend = u"Études"
|
||||
|
||||
def parse_value(self, val):
|
||||
# who cares
|
||||
self.value = val
|
||||
|
||||
|
||||
class chbre(Attr):
|
||||
singlevalue = True
|
||||
optional = False
|
||||
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):
|
||||
singlevalue = False
|
||||
optional = True
|
||||
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):
|
||||
singlevalue = True
|
||||
optional = True
|
||||
|
|
|
@ -87,3 +87,12 @@ def format_tel(tel):
|
|||
tel_f = re.sub(r'\D', '', 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