class Condition(EmbeddedDocument): collected_at = ComplexDateTimeField(required=True) name = StringField(max_length=1000, required=True) tag = StringField(max_length=1000, required=True) type = StringField(max_length=1000) valid_since = ComplexDateTimeField() valid_until = ComplexDateTimeField() values = DynamicField()
class Record(Document): title = StringField(max_length=100) comment = StringField() created_time = ComplexDateTimeField(default=now) finished_time = ComplexDateTimeField(default=now) modified_time = ComplexDateTimeField(default=now) hidden = BooleanField(default=False) children = ListField(ReferenceField('Record')) tags = ListField(StringField(max_length=50)) loc = StringField() imagefield = FileField(collection_name='images') work = ReferenceField('CodeSnippet') notebook = ReferenceField('Notebook') notebook_index = IntField(min_value=0)
class Usuario(DynamicDocument): dni = StringField(required=True, unique=True, min_length=9, max_length=9, regex="^[0-9]{8}[A-Z]{1}$") nombre = StringField(required=True) primerApellido = StringField(required=True) segundoApellido = StringField(required=False) fechaNacimiento = DateTimeField(required=True, separator="-") fechaUltimosDiezAccesos = ListField( ComplexDateTimeField(required=False, separator="-")) listaTarjetasCredito = ListField( EmbeddedDocumentField(TarjetaCredito, required=False)) listaReferenciasPedidos = ListField( ReferenceField(Pedido, required=False, reverse_delete_rule=PULL)) def clean( self ): # Se lanza al llamar a .save(), permite hacer comprobaciones personalizadas. numero = int(self.dni[0:8]) letras = "TRWAGMYFPDXBNJZSQVHLCKE" if self.dni[8] != letras[numero % 23]: raise ValidationError( f"El DNI {self.dni} es incorrecto, fallo en el calculo de su letra." )
class Record(Document): title = StringField(max_length=100) comment = StringField() created_time = ComplexDateTimeField(default=now) finished_time = ComplexDateTimeField(default=now) modified_time = ComplexDateTimeField(default=now) hidden = BooleanField(default=False) children = ListField(ReferenceField('Record')) config=DictField() setting=DictField() tags = ListField(StringField(max_length=50)) datafield = FileField(collection_name='data') imagefield = FileField(collection_name='images') imagefields = ListField(BinaryField()) work = ReferenceField('CodeSnippet') notebook = ReferenceField('Notebook') notebook_index = IntField(min_value=0) def __repr__(self): return 'Record(title=%s, finished_time=%s, tags=%s)' % ( self.title, self.finished_time, self.tags) @property def data(self): return from_pickle(self.datafield) @property @functools.lru_cache(maxsize=1) def image(self): return from_pickle(self.imagefield) def set_data(self, obj, content_type='application/octet-stream'): if self.datafield is None: self.datafield.put(to_pickle(obj), content_type=content_type) else: self.datafield.replace(to_pickle(obj), content_type=content_type) def set_image(self, img, content_type='image/png'): if self.imagefield is None: self.imagefield.put(to_pickle(img), content_type=content_type) else: self.imagefield.replace(to_pickle(img), content_type=content_type)
class Series(Document, BaseModel): title = StringField() number_of_chapters = IntField() active = BooleanField() rating = FloatField() info = EmbeddedDocumentField(DataSheet) characters = ListField() secret_code = StringField() extras = StringField() additional_info = DictField() created_at = DateTimeField(default=datetime.now) updated_at = ComplexDateTimeField(default=datetime.now)
class Usuario(Document): # er: 1 digito o letra('X','Y','Z') + 7 digitos + # 1 letra mayuscula o minuscula dni = StringField(primary_key=True,\ regex='(\d{8}|[X-Z]\d{7})[A-Z]$|'\ + '(\d{8}|[x-z]\d{7})[a-z]$') nombre = StringField(required=True) primer_apellido=StringField(required=True) segundo_apellido=StringField() # er: Rangos de fecha [1900-2017]-[01-12]-[01-31] fecha_nacimiento=StringField(required=True,\ regex='(19\d{2}-|20[0-1][0-7]-)'\ + '(0[1-9]-|1[0-2]-)'\ + '(0[1-9]$|(1|2)\d{1}$|3[0-1]$)'\ ) fecha_ultimo_acceso=ComplexDateTimeField() tarjetas_credito=ListField(EmbeddedDocumentField(Tarjeta_Credito)) pedidos=ListField(ReferenceField(Pedido,reverse_delete_rule=PULL)) #---- verificacion de DNI o NIE: ---- def clean(self): letras=['T','R','W','A','G','M','Y','F','P','D','X','B',\ 'N','J','Z','S','Q','V','H','L','C','K','E' ] dni=self.dni.upper() id_number=0 offset=0 last_letter=dni[len(dni)-1] # calcular offset en caso de NIE if(dni[0].isalpha() and dni[0]!='X'): offset=offset+10000000 if(dni[0]=='Z'): offset=offset+10000000 else: offset=0 # obtener la cifra para calcular el digito de control dni='' i=0 for i in range(len(self.dni)): if(self.dni[i].isalpha()): continue else: dni=dni+self.dni[i] id_number=int(dni) id_number=id_number+offset #calculo y comprobacion correct_index=id_number%23 correct_letter=letras[correct_index] if(correct_letter != last_letter): raise ValidationError("El DNI o NIE introducido No Existe")
class CodeSnippet(Document): text = StringField() filename = StringField() created_time = ComplexDateTimeField(default=now) meta = { 'indexes': [ '#text', # hashed index ] } def __repr__(self): return "< CodeSnippet(id='%s') >" % self.id
class Pedido(Document): # er: float con dos cifras decimales: total=StringField(required=True,regex='\d*\.\d{2}$') fecha=ComplexDateTimeField(required=True) lineas_pedido=ListField(EmbeddedDocumentField(Linea_Pedido,required=True)\ ,required=True) def clean(self): total=float(self.total) total_from_lines=0.00 i=0 for i in range(len(self.lineas_pedido)): total_linea=float(self.lineas_pedido[i].precio_total) total_from_lines=total_from_lines+total_linea if(total != total_from_lines): raise ValidationError("ERROR: No concuerdan los totales")
class Pedido(Document): precioTotalPedido = FloatField(required=True, min_value=0) fechaPedido = ComplexDateTimeField(required=True, separator="-") listaLineasPedido = ListField( EmbeddedDocumentField(LineaPedido, required=True)) def clean( self ): # Se lanza al llamar a .save(), permite hacer comprobaciones personalizadas. totalLineas = 0 for linea in self.listaLineasPedido: totalLineas += linea.precioTotalLinea if totalLineas != self.precioTotalPedido: raise ValidationError( f"El precio total {self.precioTotalPedido} no coincide con la suma de las lineas {totalLineas}" )
class Pedido(Document): total = FloatField(required=True, min_value=0) #Suma precios de sus lineas de pedido fecha = ComplexDateTimeField(required=True) lineas = ListField(EmbeddedDocumentField(Linea, required=True)) def clean(self): self.validate(clean=False) #Queremos comprobar que el precio total es la suma de precios de la lista de lineas precio = 0 correcto = True productos = [] for lineaPedido in self.lineas: if lineaPedido.name not in productos: #Comprobamos si cada linea corresponde a un producto único (no hay varias lineas sobre un mismo producto) productos.append(lineaPedido.name) precio += lineaPedido.total else: correcto = False break if correcto == False: raise ValidationError("Hay varias lineas sobre el mismo producto") if round(precio,2) != self.total: #Volvemos a redondear por un fallo que nos da 30.9399999 en lugar de 30.94 raise ValidationError("No se corresponde el precio del pedido con la suma de los totales de las lineas de pedidos")
class Notebook(Document): name = StringField() created_time = ComplexDateTimeField(default=now) modified_time = ComplexDateTimeField(default=now) inputCells = ListField(ReferenceField('CodeSnippet'))
class UASZonesFilter(EmbeddedDocument): airspace_volume = EmbeddedDocumentField(AirspaceVolume, db_field='airspaceVolume') regions = ListField() start_date_time = ComplexDateTimeField(db_field='startDateTime') end_date_time = ComplexDateTimeField(db_field='endDateTime')
class TimePeriod(EmbeddedDocument): permanent = StringField(choices=CodeYesNoType.choices(), required=True) start_date_time = ComplexDateTimeField(db_field='startDateTime', required=True) end_date_time = ComplexDateTimeField(db_field='endDateTime', required=True) schedule = EmbeddedDocumentListField(DailyPeriod)
class DailyPeriod(EmbeddedDocument): day = StringField(choices=CodeWeekDay.choices()) start_time = ComplexDateTimeField(db_field='startTime', required=True) end_time = ComplexDateTimeField(db_field='endTime', required=True)
class Challenge(Document): name = StringField(required=True) number_of_steps = IntField(required=True) reward = IntField(required=True) start_date = ComplexDateTimeField(required=True) end_date = ComplexDateTimeField(required=True)