class Point(Document): meta = {'collection': 'point'} name = StringField() latitude = StringField() longitud = StringField() phone = StringField() due = DateTimeField(default=datetime.now)
class MarketModel(DynamicDocument): blockchain = StringField() host = StringField() date = DateTimeField() base = FloatField() quote = FloatField() basecurr = StringField() quotecurr = StringField() cost = FloatField() is1min = BooleanField() is5min = BooleanField() is15min = BooleanField() is30min = BooleanField() is60min = BooleanField() is4hour = BooleanField() is1day = BooleanField() is1week = BooleanField() meta = { 'collection': 'markets', 'ordering': ['-date'], 'indexes': [ 'blockchain', 'host', 'date', 'is1min', 'is5min', 'is15min', 'is30min', 'is60min', 'is4hour', 'is1day', 'is1week' ], 'auto_create_index': True, 'index_background': True }
class User(Document): meta = {"collection": "users"} username = StringField() email = StringField() posts = ListField(ReferenceField('Post')) friends = ListField(ReferenceField('User')) datetime_created = DateTimeField()
class QueryOntology(Document): """ Ontology of queries to generate the navigation tree """ name = StringField(required=True, unique=True) status = IntField(default=0, required=True) # 0: Uploaded; 1: Active; 2: Blank; -1: Deleted last_modif = DateTimeField(required=False) content = StringField(required=True)
class Guy(DynamicDocument): meta = {'collection': 'guys'} ref_id = StringField() ref_name = StringField() name = StringField() email = EmailField() pages_today = IntField(default=0) pages_total = IntField(default=0) shares_today = IntField(default=0) updated = DateTimeField(default=datetime.now()) earnings = FloatField() level = IntField() ref = ReferenceField('Guy') refs = ListField(ReferenceField('Guy')) def next_ref_name(self, n): return '{}_{}'.format(self.ref_name, n) def next_email(self, n): name, domain = self.email.split('@') return '@'.join(['{}+{}'.format(name, n), domain]) def __repr__(self): return 'Guy({})'.format(str(self.to_json()))
class MediaFile(Document): # 文件id fileId = StringField() # 文件名称 fileName = StringField() # 文件原始名称 fileOriginalName = StringField() # 文件路径 filePath = StringField() # 文件url fileUrl = StringField() # 文件类型 fileType = StringField() # mimetype mimeType = StringField() # 文件大小 fileSize = StringField() # 文件状态 fileStatus = StringField() # 上传时间 uploadTime = DateTimeField() # 处理状态 processStatus = StringField() # tag标签用于查询 tag = StringField() # hls处理 mediaFileProcess_m3u8 = ReferenceField(MediaFileProcess_m3u8) # 根据file_id获取MediaFile @classmethod def find_by_id(cls, file_id): return cls.objects.filter(fileId=file_id).first()
class Submission(Document): group_name = StringField(required=True) student_id = IntField(required=True) module_name = StringField(required=True) submission_date = DateTimeField(required=True) submission_files = EmbeddedDocumentListField(SubmissionFile) submission_score = FloatField(required=True) def load(self, filepath, results): """ Initiate a new Submission entry from a filepath and a results dict :filepath: A fileparth ending in "group_name/student_id/module_name/submission_date' e.g. data/cohort1/14/pre2-programming/20180724_152918/ :results: A dictionary populated by the contents of results.json in filepath """ # Populated from filepath group, student, module, time = filepath.split("/")[-4:] self.group_name = group self.student_id = int(student) self.module_name = module self.submission_date = datetime.datetime.strptime( time, config.INPUT_DATE_FORMAT) # Populated from json (results.json) self.submission_score = results["overall_score"] for filestats in results["data"]: self.submission_files.append(SubmissionFile().load(filestats)) return self
class EmailJob(EmbeddedDocument): job_id = ObjectIdField() subject = StringField() emails = EmbeddedDocumentListField(Email) type = StringField(choices=["Manual", "Scheduled"]) initiated_at = DateTimeField(default=datetime.utcnow) included_feedback = BooleanField()
class Review(Document): meta = {"collection": "review"} date = DateTimeField(default=datetime.now) rating = DecimalField() organization = ReferenceField(Organization) user = ReferenceField(UserAccount)
class Category(Document): uuid = UUIDField(default=uuid4, unique=True) name = StringField(max_length=120, unique=True, required=True) created_at = DateTimeField(default=datetime.now) def to_dict(self): return {'uuid': str(self.uuid), 'name': self.name}
class SpecChange(EmbeddedDocument): date = DateTimeField() changes = StringField() @property def json_data(self): return {"date": self.date.isoformat(), "changes": self.changes}
class MntCostSnap(Document): '''托管计划快照''' rpt_date = DateTimeField(verbose_name='报表快照日期', required=True) shop_id = IntField(verbose_name="店铺ID", required=True) campaign_id = IntField(verbose_name="推广计划ID", primary_key=True) cost = IntField(verbose_name="总花费", default=0) category = StringField( verbose_name="业务类型") # 对应 customer表中的 current_highest_version 字段 consult_id = IntField(verbose_name='当前顾问ID') xfgroup_id = IntField(verbose_name='当前销服组ID') meta = { 'collection': 'timer_mntcostsnap', 'indexes': ['rpt_date', 'xfgroup_id'], "shard_key": ('shop_id', ) } @classmethod def bulk_update_db(cls, update_list): total_updated_num = 0 for temp_list in genr_sublist(update_list, 1000): # bulk一次最多1000个 bulk = cls._get_collection().initialize_unordered_bulk_op() for update_tuple in temp_list: bulk.find(update_tuple[0]).update(update_tuple[1]) try: result = bulk.execute() total_updated_num += result['nModified'] except BulkWriteError, e: log.error('bulk_update_kw2db, detail=%s' % e.details) total_updated_num += e.details['nModified'] return total_updated_num
class ObjectLog(Document): meta = { "collection": "noc.objectlog", "strict": False, "auto_create_index": False, "indexes": ["object"] } # Inventory object reference object = ObjectIdField() # Timestamp ts = DateTimeField() # Username user = StringField() # NOC subsystem system = StringField() # Managed object name managed_object = StringField() # Operation # * CREATE - Object created # * CHANGE - Object changed # * CONNECT - Object connected # * DISCONNECT - Object disconnected # * INSERT - Object moved into container # * REMOVE - Object removed from container op = StringField() # Message message = StringField()
class EmbeddedArticle(EmbeddedDocument): meta = {'collection': 'test_embedded_article'} headline = StringField(required=True) pub_date = DateTimeField(default=datetime.now) editor = ReferenceField(Editor) reporter = ReferenceField('Reporter')
class BlogPost(DynamicDocument): # pass title = StringField() content = StringField() # Reference field to a user # If we have not defimed User schema (line 36-69) that would not work author = ReferenceField(User) date_created = DateTimeField(default=datetime.utcnow) meta = { "indexes": ["title"], # ordering in descending fashion "ordering": ["-date_created"] } # Querying the database # http://docs.mongoengine.org/guide/querying.html # Return everything from the user collection # Pylint warning - ignore # https://github.com/MongoEngine/mongoengine/issues/858 users = User.objects() for user in users: print(user.username, user.email, user.bio)
class PyRule(Document): meta = { "collection": "pyrules", "strict": False, "auto_create_index": False } # Relative modules name # i.e. test.mod1 for noc.pyrules.test.mod1 name = StringField( unique=True, regex="^([a-zA-Z_][a-zA-Z0-9_]*)(\.[a-zA-Z_][a-zA-Z0-9_]*)*$") # description = StringField() # Source code source = StringField() # last_changed = DateTimeField() def __unicode__(self): return self.name @property def full_name(self): return "noc.pyrules.%s" % self.name def clean(self): # Check source code try: compile(str(self.source), "<string>", "exec") except SyntaxError as e: raise ValidationError("Cannot compile pyRule: %s" % e) # Update last_changed self.last_changed = datetime.datetime.now()
class ObjectFile(Document): """ Inventory object """ meta = { "collection": "noc.objectfiles", "allow_inheritance": False, "indexes": ["object"] } object = ObjectIdField() name = StringField() file = FileField() ts = DateTimeField() description = StringField() size = IntField() mime_type = StringField() def __unicode__(self): return unicode(self.name or self.id) def delete_file(self): if self.file: self.file.delete() @classmethod def delete_files(cls, sender, document, target=None): for o in ObjectFile.objects.filter(object=document.id): o.delete_file() o.delete() @classmethod def on_delete(cls, sender, document, target=None): document.delete_file()
class User(Custom_Document): nickname = StringField(required=True, unique=True, max_length=20) email = EmailField(required=True, unique=True, max_length=50) password_hash = StringField(max_length=200, required=True) about_me = StringField(max_length=200) skills = ListField(EmbeddedDocumentField(Skill)) last_seen = DateTimeField(required=True) @property def password(self): raise AttributeError('password is not a readable') @password.setter @tornado.gen.coroutine def password(self, password): self.password_hash = yield self._executor.submit( bcrypt.hashpw, password, bcrypt.gensalt()) @tornado.gen.coroutine def verify_password(self, password): password_hash = yield self._executor.submit( bcrypt.hashpw, password, self.password_hash) return password_hash == self.password_hash def avatar(self, size): return avatar(self.email, size) def __repr__(self): return '<User %r>' % (self.nickname)
class Contest(Document): post_id = StringField(required=True, unique=True) text = StringField(required=True) date = DateTimeField(required=True) city = ListField(IntField()) category = ListField(StringField()) is_tagged = BooleanField(required=True, default=False) meta = { 'collection': 'contest', 'indexes': [ '#post_id', 'date', 'city', 'category', { 'fields': ['$text'], 'default_language': 'russian' }, 'is_tagged' ] } @staticmethod def create(post_id, text, date, city, category, is_tagged=False): contest = Contest() contest.post_id = post_id contest.text = text contest.date = date contest.city = city contest.category = category contest.is_tagged = is_tagged return contest
class User(Document): # http://docs.mongoengine.org/guide/defining-documents.html#fields username = StringField(unique=True, required=True) email = EmailField(unique=True) password = BinaryField(required=True) age = IntField() bio = StringField(max_length=100) # Will accepet strings # categoreis = ListField(StringField()) categories = ListField() admin = BooleanField(default=False) registered = BooleanField(default=False) date_created = DateTimeField(default=datetime.utcnow) @classmethod def json(self): user_dict = { "username": self.username, "email": self.email, "age": self.age, "bio": self.bio, "categories": self.categories, "admin": self.admin, "registered": self.registered } return json.dumps(user_dict) # Create meta information about our document # Check out docs for more meta = { "indexes": ["username", "email"], # ordering in descending fashion "ordering": ["-date_created"] }
class Outage(Document): meta = { "collection": "noc.fm.outages", "strict": False, "auto_create_index": False, "indexes": ["object", ("object", "-start")] } object = IntField() start = DateTimeField() stop = DateTimeField() # None for active outages def __unicode__(self): return u"%d" % self.object @property def is_active(self): return self.stop is None @classmethod def register_outage(cls, object, status, ts=None): """ Change current outage status :param cls: :param object: Managed Object :param status: True - if object is down, False - otherwise :param ts: Effective event timestamp. None for current time :return: """ ts = ts or datetime.datetime.now() col = Outage._get_collection() lo = col.find_one({ "object": object.id, "start": { "$lte": ts } }, { "_id": 1, "stop": 1 }, sort=[("object", 1), ("start", -1)]) if not status and lo and not lo.get("stop"): # Close interval col.update({"_id": lo["_id"]}, {"$set": {"stop": ts}}) elif status and (not lo or lo.get("stop")): # New outage col.insert({"object": object.id, "start": ts, "stop": None})
class Timelog(Document): time = DateTimeField(required=True) kind = StringField(required=True) source = StringField() employee = ReferenceField(Employee, dbref=False)
class Operation(Document): meta = {'collection': 'operation'} text = StringField(required=True) textType = StringField(default='input') result = StringField() dateCr = DateTimeField(default=datetime.now) user = ReferenceField(User) operation_type = ReferenceField(OperationType)
class Links(Document): meta = {'collection': 'linkedin_links'} url = StringField() last_fetch_at = DateTimeField(default=datetime.min) @staticmethod def to_crawl(): return Links.objects(last_fetch_at__exists=False).count()
class Message(Document): id = UUIDField(primary_key=True, default=lambda: uuid4()) user = ReferenceField('User') room = ReferenceField('Room') message = StringField(max_length=500, required=True) timestamp = DateTimeField(required=True, default=lambda: datetime.datetime.now()) location = PointField()
class Users(Document): role = EnumField(Role, default=Role.USER) first_name = StringField(min_length=1, max_length=200) last_name = StringField(min_length=1, max_length=200) email = EmailField(allow_utf8_user=True, unique=True) phone_number = StringField(regex=r"\d{3,}") password = DynamicField(max_length=100) created = DateTimeField(default=datetime.utcnow)
class ObjectFact(Document): meta = { "collection": "noc.objectfacts", "strict": False, "auto_create_index": False, "indexes": ["object", "attrs.rule"], } uuid = UUIDField(binary=True, primary_key=True) object = ForeignKeyField(ManagedObject) cls = StringField() label = StringField() attrs = DictField() introduced = DateTimeField(default=datetime.datetime.now) changed = DateTimeField(default=datetime.datetime.now) def __str__(self): return self.object.name
class ApiSpecification(Document): server_url = StringField(required=True, min_length=3) title = StringField(required=False) version = StringField(required=False) consumes = ListField(field=StringField(required=False), required=False) produces = ListField(field=StringField(required=False), required=False) operations = ListField(field=StringField(required=False), required=False) created_at = DateTimeField(default=datetime.datetime.utcnow())
class Historique(Document): meta = {'collection': 'historique'} text = StringField() result = StringField() dateCreation = DateTimeField(default=datetime.now) user = ReferenceField(User) operation = ReferenceField(Operation)
class Text(EmbeddedDocument): title = StringField() content = StringField(required=True) type = StringField() dateCr = DateTimeField(default=datetime.now) emotion = IntField(default=-1) fakeNews = IntField(default=-1) operation = ListField(EmbeddedDocumentField(Operation))
def __init__(self, *args, **kwargs): kwargs.setdefault("default", datetime.datetime.now) DateTimeField.__init__(self, *args, **kwargs)