class Posts(Document): post_id = SequenceField() title = StringField() description = StringField() created_at = DateTimeField(default=datetime.utcnow)
class NurseModel(RidDocument): rid = SequenceField( unique=True ) # Overriding to use own counter rather than the one common to all RidDocument childs. user = ReferenceField(UserModel, required=True, unique=True, reverse_delete_rule=2) name = StringField(min_lenght=1, max_length=255, required=True) treatment_types = ListField(ReferenceField(TreatmentTypeModel, required=False), default=list) agenda = SortedListField(DateTimeField(), required=False, default=list()) def clean(self): errs = dict() self.agenda.sort() for i in range(len(self.agenda) - 1): if self.agenda[i].date() == self.agenda[i + 1].date(): errs["agenda"] = "Can only have one agenda per day!" duplicates = find_duplicates(self.treatment_types) if duplicates: errs[ "treatment_types"] = f"Treatment types list cannot contains duplicate elements." if errs: raise ValidationError(errors=errs)
class MongoJob(Document): """ Document of job in metadata backend storage. """ uuid = SequenceField(db_alias=MONGO_DB_ALIAS_META_SERVICE) name = StringField(max_length=255, required=True, unique=True) workflow_execution_id = IntField() job_id = StringField(max_length=1000) properties = StringField(max_length=1000) start_time = LongField() end_time = LongField() job_state = StringField(max_length=256) log_uri = StringField(max_length=256) signature = StringField(max_length=1000) is_deleted = BooleanField(default=False) meta = {'db_alias': MONGO_DB_ALIAS_META_SERVICE} def __repr__(self): return '<Document Job ({}, {}, {}, {}, {}, {}, {}, {}, {}, {})>'.format( self.uuid, self.job_str_id, self.name, self.properties, self.start_time, self.end_time, self.job_state, self.log_uri, self.signature, self.workflow_execution_id)
class MongoProject(Document): """ Document of project in metadata backend storage. """ uuid = SequenceField(db_alias=MONGO_DB_ALIAS_META_SERVICE) name = StringField(max_length=255, required=True, unique=True) properties = StringField(max_length=1000) project_type = StringField(max_length=1000) user = StringField(max_length=1000) password = StringField(max_length=1000) uri = StringField(max_length=1000) is_deleted = BooleanField(default=False) model_relation = ListField(ReferenceField(MongoModelRelation)) workflow_execution = ListField(ReferenceField(MongoWorkflowExecution)) meta = {'db_alias': MONGO_DB_ALIAS_META_SERVICE} def __repr__(self): return '<Document Project ({}, {}, {}, {}, {}, {}, {})>'.format( self.uuid, self.name, self.properties, self.project_type, self.user, self.password, self.uri)
class Thing(Document, Base): meta = {'indexes': [{'fields': ('company', 'name'), 'unique': True}]} id = SequenceField(primary_key=True) name = StringField(nullable=False) description = StringField(nullable=True) company = ReferenceField('Company', nullable=False) deleted = BooleanField(default=False)
class Company(Document, Base): id = SequenceField(primary_key=True) name = StringField(unique=True) status = StringField(default='trialling') created = DateTimeField(default=datetime.now) owner = ReferenceField('Person', reverse_delete_rule=DENY) def save(self, *args, **kwargs): # Create default Roles admin = Role(name='Administrator', theme='success', company=self, permissions=[ 'edit_events', 'edit_people', 'edit_places', 'edit_system_settings' ]).save() Role(name='Regular User', theme='primary', company=self, permissions=['edit_events', 'edit_people', 'edit_places']).save() Role(name='Read-Only User', theme='warning', company=self, permissions=['view_events', 'view_people', 'view_places']).save() # Set owner as Admin self.owner.update(role=admin, role_name=admin.name, role_theme=admin.theme, active=True) return super(Company, self).save(*args, **kwargs)
class MongoArtifact(Document): """ Document of artifact in metadata backend storage. """ uuid = SequenceField(db_alias=MONGO_DB_ALIAS_META_SERVICE) name = StringField(max_length=255, required=True, unique=True) data_format = StringField(max_length=256) description = StringField(max_length=1000) batch_uri = StringField(max_length=1000) stream_uri = StringField(max_length=1000) create_time = LongField() update_time = LongField() properties = StringField(max_length=1000) is_deleted = BooleanField(default=False) meta = {'db_alias': MONGO_DB_ALIAS_META_SERVICE} def __repr__(self): return '<Document Artifact ({}, {}, {}, {}, {}, {}, {}, {}, {}, {})>'.format( self.pk, self.name, self.data_format, self.description, self.batch_uri, self.stream_uri, self.create_time, self.update_time, self.properties, self.is_deleted)
class User(DynamicDocument): """ 用户信息表 """ uid = SequenceField(primary_key=True, value_decorator=uid_value_decorator) # 用户id username = StringField(max_length=100, required=True) password_hash = StringField(max_length=200, required=True) phone = StringField(max_length=200, unique=True, required=True) create_time = DateTimeField(default=datetime.datetime.now) # 记录的创建时间 update_time = DateTimeField(default=datetime.datetime.now, onupdate=datetime.datetime.now) # 记录的最后更新时间 def hash_password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) meta = { "collection": "user", "db_alias": "default", "shard_key": ("uid", ) } def __init__(self, *args, **kwargs): super(User, self).__init__(*args, **kwargs) @property def id(self): return self.uid
class UserModel(RidDocument): """User mongoengine model. Defines methods to work with password hash. Notes ----- Only set `password` with an hash. """ rid = SequenceField( unique=True ) # Overriding to use own counter rather than the one commun to all RidDocument childs. username = StringField(min_lenght=1, max_length=64, required=True, unique=True) usertype = IntField(choices={0, 1}, required=True) password = StringField(min_lenght=1, required=True) @staticmethod def hash_password(password): """Hash and returns password.""" return sha256.hash(password) def set_password(self, password): """Hash and set password.""" self.password = self.hash_password(password) def verify_password(self, password): """Hash password and compares it to stored value.""" return sha256.verify(password, self.password)
class TaskDocument(Document): counter = SequenceField() title = StringField() project = ReferenceField(ProjectDocument) status = StringField( choices=['new', 'pending', 'pausing', 'waiting', 'done']) tags = ListField(ReferenceField(TagDocument)) created_at = DateTimeField(default=datetime.datetime.utcnow()) updated_at = DateTimeField(default=datetime.datetime.utcnow()) user = StringField() def to_dict(self): rec = { 'id': str(self.id).decode('unicode-escape'), 'counter': str(self.counter).decode('unicode-escape'), 'title': self.title, 'project_id': str(self.project.id).decode('unicode-escape') if self.project is not None else '', 'status': self.status, 'tags_ref': [str(tag.id).decode('unicode-escape') for tag in self.tags], 'created_at': self.created_at.isoformat().decode('unicode-escape'), 'updated_at': self.updated_at.isoformat().decode('unicode-escape'), 'user': self.user } return rec
class TreatmentTypeModel(RidDocument): rid = SequenceField( unique=True ) # Overriding to use own counter rather than the one commun to all RidDocument childs. code = IntField(required=True, unique=True) name = StringField(min_length=1, max_length=255) duration = IntField(required=True)
class MongoExample(Document): """ Document of example in metadata backend storage. """ uuid = SequenceField(db_alias=MONGO_DB_ALIAS_META_SERVICE) name = StringField(max_length=255, required=True, unique=True) support_type = StringField(max_length=256, required=True) data_type = StringField(max_length=256) format = StringField(max_length=256) description = StringField(max_length=1000) batch_uri = StringField(max_length=1000) stream_uri = StringField(max_length=1000) create_time = LongField() update_time = LongField() properties = StringField(max_length=1000) name_list = StringField(max_length=1000) type_list = StringField(max_length=1000) catalog_name = StringField(max_length=1000) catalog_type = StringField(max_length=1000) catalog_database = StringField(max_length=1000) catalog_connection_uri = StringField(max_length=1000) catalog_version = StringField(max_length=1000) catalog_table = StringField(max_length=1000) is_deleted = BooleanField(default=False) meta = {'db_alias': MONGO_DB_ALIAS_META_SERVICE} def __repr__(self): return '<Document Example ({}, {}, {}, {}, {}, {}, {}, {}, {})>'.format( self.uuid, self.name, self.properties, self.support_type, self.name_list, self.type_list, self.format, self.batch_uri, self.stream_uri)
class MongoWorkflowExecution(Document): """ Document of workflow execution in metadata backend storage. """ uuid = SequenceField(db_alias=MONGO_DB_ALIAS_META_SERVICE) name = StringField(max_length=255, required=True, unique=True) project_id = IntField() properties = StringField(max_length=1000) start_time = LongField() end_time = LongField() execution_state = StringField(max_length=256) log_uri = StringField(max_length=1000) workflow_json = StringField() signature = StringField(max_length=1000) is_deleted = BooleanField(default=False) job_info = ListField(ReferenceField(MongoJob)) model_version_relation = ListField( ReferenceField(MongoModelVersionRelation)) meta = {'db_alias': MONGO_DB_ALIAS_META_SERVICE} def __repr__(self): return '<Document WorkflowExecution ({}, {}, {}, {}, {}, {}, {}, {}, {}, {})>'.format( self.uuid, self.name, self.properties, self.start_time, self.end_time, self.execution_state, self.log_uri, self.workflow_json, self.signature, self.project_id)
class TreatmentModel(RidDocument): rid = SequenceField(unique=True) # Overriding to use own counter rather than the one common to all RidDocument childs. patient = ReferenceField(PatientModel, required=True, reverse_delete_rule=2) ttype = ReferenceField(TreatmentTypeModel, required=True, reverse_delete_rule=2) date = DateField(required=True) time = DateTimeField(default=None) nurse = ReferenceField(NurseModel, default=None, reverse_delete_rule=2)
class MongoMetricMeta(Document): """ Document of metric meta """ uuid = SequenceField(db_alias=MONGO_DB_ALIAS_META_SERVICE) name = StringField(max_length=255, required=True, unique=True) dataset_id = IntField() model_name = StringField(max_length=256) model_version = StringField(max_length=500) job_id = IntField() start_time = LongField() end_time = LongField() metric_type = StringField(max_length=256, default=MetricType.DATASET.value) uri = StringField(max_length=1000) tags = StringField(max_length=256) metric_description = StringField(max_length=4096) properties = StringField(max_length=1000) is_deleted = BooleanField(default=False) meta = {'db_alias': MONGO_DB_ALIAS_META_SERVICE} def __repr__(self): return '<Document MetricMeta ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})>' \ .format(self.name, self.dataset_id, self.model_id, self.job_id, self.start_time, self.end_time, self.metric_type, self.metric_description, self.uri, self.tags, self.properties)
class Option(EmbeddedDocument): Id = SequenceField() title = StringField(required=True) value = IntField(default=0) def inc(self, value): self.value += value self.save()
class ImageDoc(Document): id_no = SequenceField() description = StringField() #asfdffimage = ImageField(thumbnail_size=(120, 100, True)) image = FileField() # sdf url = StringField() create = DateTimeField(default=datetime.datetime.now)
class Role(Document, Base): meta = {'indexes': [{'fields': ('company', 'name'), 'unique': True}]} id = SequenceField(primary_key=True) name = StringField(nullable=False) theme = StringField(nullable=False) company = ReferenceField('Company', nullable=False) permissions = ListField(StringField(nullable=False), nullable=False) deleted = BooleanField(default=False)
class Quote(Document): id_no = SequenceField() body = StringField(required=True, unique=True) author = StringField() create = DateTimeField(default=datetime.datetime.now) meta = { 'ordering': ['-create'], }
class Account(Document): id = SequenceField(required=True, primary_key=True) name = StringField(required=True) user = StringField(required=True) timestamp = DateTimeField(default=datetime.utcnow) status = BooleanField(default=True) def validate(self, clean=True): if Utility.check_empty_string(self.name): raise ValidationError("Account Name cannot be empty or blank space")
class Course(Document): '''Course class ''' courseId = SequenceField() courseName = StringField(max_length=100) description = StringField(max_length=8000) suitableFor = StringField(max_length=200) courseLevel = StringField() language = StringField(max_length=100) active = IntField(default=1) # 0 = inactive, 1 = active
class Place(Document, Base): meta = {'indexes': [{'fields': ('company', 'name'), 'unique': True}]} id = SequenceField(primary_key=True) name = StringField(nullable=False) address = StringField(nullable=False) directions = StringField() mail = StringField() phone = StringField() company = ReferenceField('Company', nullable=False) deleted = BooleanField(default=False)
class DBTaskHandler(Document): task_id = SequenceField(primary_key=True) stock_name = StringField(required=True, max_length=200) stock_code = StringField(required=True, max_length=200) obj_amount = StringField(required=True, max_length=200) strategy_name = StringField(required=True, max_length=200) riskctrl_name = StringField(required=True, max_length=200) gateway_name = StringField(required=True, max_length=200)
class Dog(Document): identifier = SequenceField(primary_key=True) name = StringField(min_length=4, max_length=20) age = IntField(min_value=0) race = StringField(min_length=4) gender = StringField(choice=GENDERS_DOG) owner = ReferenceField(User) meta = {'queryset_class': DogQuerySet}
class VoteRecord(BaseDocument): Id = SequenceField() vote = ReferenceField(BlindVote) options = EmbeddedDocumentListField(Option, default=list) track_id = StringField(required=True) @classmethod def get_records_by_vote_id(cls, vote_id): vote = BlindVote.get_by_id(vote_id) records = cls.objects(vote=vote) return records
class Notification(Document, Base): id = SequenceField(primary_key=True) stamp = DateTimeField(default=datetime.now) body = StringField() message = StringField(nullable=True) company = ReferenceField('Company') owner = ReferenceField('Person') def to_dict(self): output = self.to_mongo() output['stamp'] = str(output['stamp']) return output
class Product(Document): product_id = SequenceField(primary_key=True) product_name = StringField() product_description = StringField() product_category = StringField(default="Minerals") product_image_gallery = EmbeddedDocumentListField(ProductImage) product_size_metrics = DictField(default={ 'min_size': 10, 'max_size': 100, 'metrics': 'mm' }) product_created_timestamp = DateTimeField( default=datetime.datetime.utcnow())
class Content(DynamicDocument): """ 日记内容表 """ cid = SequenceField(primary_key=True, value_decorator=con_value_decorator) # 文章id phone = StringField(max_length=200, required=True) uid = SequenceField(max_length=100, required=True) # 用户id username = StringField(max_length=100, required=True) title = StringField(max_length=100, required=True) data = StringField(max_length=100, required=True) # 发布时间 text = StringField(max_length=100000, required=True) status = IntField(required=True) # 是否公开 create_time = DateTimeField(default=datetime.datetime.now) # 记录的创建时间 update_time = DateTimeField(default=datetime.datetime.now, onupdate=datetime.datetime.now) # 记录的最后更新时间 meta = { "collection": "content", "db_alias": "default", "shard_key": ("phone", "cid") }
class BaseArticle(Document): id_no = SequenceField() title = StringField(required=True) create_time = DateTimeField(default=datetime.datetime.now) md_content = StringField() html_content = StringField() img_list = ListField(StringField(), default=[]) is_del = IntField(default=0, choices=(0, 1)) meta = { 'allow_inheritance': True, 'abstract': True, }
class MongoMetricSummary(Document): """ Document of metric summary """ uuid = SequenceField(db_alias=MONGO_DB_ALIAS_META_SERVICE) metric_id = IntField() metric_key = StringField(max_length=128, required=True) metric_value = StringField(max_length=2048, required=True) is_deleted = BooleanField(default=False) meta = {'db_alias': MONGO_DB_ALIAS_META_SERVICE} def __repr__(self): return '<Document MetricSummary ({}, {}, {})>'.format(self.metric_id, self.metric_key, self.metric_value)