X-Git-Url: https://git.llucax.com/z.facultad/75.52/sercom.git/blobdiff_plain/82677387ef53d211fc481edb9c1b65ec04e18533..37cc917a22431b9e41dbaf38676d6ead3cea69b8:/sercom/model.py?ds=sidebyside diff --git a/sercom/model.py b/sercom/model.py index 56e9853..d06a570 100644 --- a/sercom/model.py +++ b/sercom/model.py @@ -5,9 +5,11 @@ from turbogears.database import PackageHub from sqlobject import * from sqlobject.sqlbuilder import * from sqlobject.inheritance import InheritableSQLObject -from sqlobject.col import PickleValidator +from sqlobject.col import PickleValidator, UnicodeStringValidator from turbogears import identity from turbogears.identity import encrypt_password as encryptpw +from sercom.validators import params_to_list, ParseError +from formencode import Invalid hub = PackageHub("sercom") __connection__ = hub @@ -21,41 +23,72 @@ class TupleValidator(PickleValidator): Validator for tuple types. A tuple type is simply a pickle type that validates that the represented type is a tuple. """ - def to_python(self, value, state): value = super(TupleValidator, self).to_python(value, state) if value is None: return None if isinstance(value, tuple): return value - raise validators.Invalid("expected a tuple in the TupleCol '%s', got %s %r instead" % \ + raise Invalid("expected a tuple in the TupleCol '%s', got %s %r instead" % \ (self.name, type(value), value), value, state) - def from_python(self, value, state): if value is None: return None if not isinstance(value, tuple): - raise validators.Invalid("expected a tuple in the TupleCol '%s', got %s %r instead" % \ + raise Invalid("expected a tuple in the TupleCol '%s', got %s %r instead" % \ (self.name, type(value), value), value, state) return super(TupleValidator, self).from_python(value, state) class SOTupleCol(SOPickleCol): - - def __init__(self, **kw): - super(SOTupleCol, self).__init__(**kw) - def createValidators(self): - return [TupleValidator(name=self.name)] + \ - super(SOPickleCol, self).createValidators() + return [TupleValidator(name=self.name)] \ + + super(SOPickleCol, self).createValidators() class TupleCol(PickleCol): baseClass = SOTupleCol +class ParamsValidator(UnicodeStringValidator): + def to_python(self, value, state): + if isinstance(value, basestring): + value = super(ParamsValidator, self).to_python(value, state) + try: + value = params_to_list(value) + except ParseError, e: + raise Invalid("invalid parameters in the ParamsCol '%s', parse " + "error: %s" % (self.name, e), value, state) + elif not isinstance(value, (list, tuple)): + raise Invalid("expected a tuple, list or valid string in the " + "ParamsCol '%s', got %s %r instead" + % (self.name, type(value), value), value, state) + return value + def from_python(self, value, state): + if isinstance(value, (list, tuple)): + value = ' '.join([repr(p) for p in value]) + elif isinstance(value, basestring): + value = super(ParamsValidator, self).to_python(value, state) + try: + params_to_list(value) + except ParseError, e: + raise Invalid("invalid parameters in the ParamsCol '%s', parse " + "error: %s" % (self.name, e), value, state) + else: + raise Invalid("expected a tuple, list or valid string in the " + "ParamsCol '%s', got %s %r instead" + % (self.name, type(value), value), value, state) + return value + +class SOParamsCol(SOUnicodeCol): + def createValidators(self): + return [ParamsValidator(db_encoding=self.dbEncoding, name=self.name)] \ + + super(SOParamsCol, self).createValidators() + +class ParamsCol(UnicodeCol): + baseClass = SOParamsCol + #}}} #{{{ Tablas intermedias - # BUG en SQLObject, SQLExpression no tiene cálculo de hash pero se usa como # key de un dict. Workarround hasta que lo arreglen. SQLExpression.__hash__ = lambda self: hash(str(self)) @@ -161,10 +194,12 @@ class Usuario(InheritableSQLObject, ByObject): #{{{ def _get_permissions(self): # para identity perms = set() - for g in self.groups: - perms.update(g.permisos) + for r in self.roles: + perms.update(r.permisos) return perms + _get_permisos = _get_permissions + def _set_password(self, cleartext_password): # para identity self.contrasenia = encryptpw(cleartext_password) @@ -368,14 +403,14 @@ class CasoDePrueba(SQLObject): #{{{ pk = DatabaseIndex(enunciado, nombre, unique=True) # Campos # privado = IntCol(default=None) TODO iria en instancia_de_entrega_caso_de_prueba - parametros = TupleCol(notNone=True, default=()) + parametros = ParamsCol(length=255) retorno = IntCol(default=None) tiempo_cpu = FloatCol(default=None) descripcion = UnicodeCol(length=255, default=None) # Joins pruebas = MultipleJoin('Prueba') - def __init__(self, enunciado=None, nombre=None, parametros=(), + def __init__(self, enunciado=None, nombre=None, parametros=None, retorno=None, tiempo_cpu=None, descripcion=None, **kargs): SQLObject.__init__(self, enunciadoID=enunciado and enunciado.id, nombre=nombre, parametros=parametros, retorno=retorno, @@ -384,7 +419,7 @@ class CasoDePrueba(SQLObject): #{{{ def __repr__(self): return 'CasoDePrueba(enunciado=%s, nombre=%s, parametros=%s, ' \ 'retorno=%s, tiempo_cpu=%s, descripcion=%s)' \ - % (self.enunciado.shortrepr(), self.nombre, self.parametros, + % (srepr(self.enunciado), self.nombre, self.parametros, self.retorno, self.tiempo_cpu, self.descripcion) def shortrepr(self):