class Player(Document): blizz_id = IntField(required=True) battletag = StringField(required=True, max_length=12) def __repr__(self): return '{} ({})'.format(self.battletag, self.blizz_id) def get_player_details(self, game_mode=None): if game_mode: player_details = [ detail for detail in PlayerModeDetails.objects(player=self) if detail.mode == game_mode ] if len(player_details) == 1: return player_details[0] else: msg = 'Could not find PlayerDetails for Player {} in {}'.format( self.blizz_id, game_mode) logger.error(msg) raise AttributeError(msg) else: player_details = PlayerModeDetails.objects(player=self) return player_details def get_player_replay(self, replay, game_mode=None): from server.db.replay import PlayerReplay if game_mode: found_replay = [ r for r in self.get_player_details(game_mode).get_replays() if r.replay == replay ] else: found_replay = [ r for r in PlayerReplay.objects(player=self) if r.replay == replay ] if len(found_replay) == 1: return found_replay[0] else: msg = 'Could not find PlayerReplay for Player {} and Replay {}'.format( self.blizz_id, replay.id) logger.error(msg) raise AttributeError(msg)
class User(gj.Document): meta = {"collection": "user"} meta = {'db_alias': 'db1'} email = StringField() password = StringField() username = StringField() address = StringField() dl_state = StringField(max_length=2, choices=STATE) dl_number = StringField() #dl_info = ListField((dl_state, dl_number), unique = True) mobile_phone = StringField() age = IntField() registration_date = DateTimeField() credit_card_info = ReferenceField('CreditCardInfo') membership = ReferenceField('Membership') role = StringField(max_length=8, choices=ROLE) reservation = ListField(ReferenceField( Reservation, reverse_delete_rule=PULL)) is_active = BooleanField(default=True)
class DNALocation(Location): strand = IntField(required=True) def relative_to(self, otherLocation): loc = Location.relative_to(self, otherLocation) loc.strand = self.strand return loc def sense(self, sequence): if self.strand == 1: return sequence.seq[self.start:self.end] else: return str( Seq(sequence.seq[self.start:self.end]).reverse_complement()) def __str__(self): return self.base + ":" if self.base else "" + str( self.start) + "-" + str( self.end) + "(" + ("+" if self.strand == 1 else "-") + ")"
class Review(Document): # Review documents are saved in the collection 'reviews' meta = {'collection': 'reviews'} # The review references the user who created the review and the restaurant the review was created for. # Both references include a rule that deletes the review should either of the references be deleted themselves. user = LazyReferenceField('Patron', required=True, reverse_delete_rule=CASCADE) restaurant = LazyReferenceField('Restaurant', required=True, reverse_delete_rule=CASCADE) # General content values of the review rating = IntField(required=True, validation=_validate_rating) date = DateTimeField(required=True) content = StringField(required=True, max_length=1500) # Images contains a list of urls used to access images from the s3 bucket images = ListField(StringField())
class Contributor(db.DynamicDocument): name = StringField() id = IntField(primary_key=True) label = StringField() url = StringField() header_image_url = StringField() image_url = StringField() meta = {'collection': 'contributor'} def contributor_doc(self): return { 'id': self.id, 'name': self.name, 'label': self.label, 'url': self.url, 'header_image_url': self.header_image_url, 'image_url': self.image_url }
class VDisk(ModelBase, Document): machineguid = StringField(required=True) diskid = IntField() fs = StringField(default='') size = IntField(default=0) free = IntField() sizeondisk = IntField() mounted = BooleanField() path = StringField(default='') description = StringField(default='') mountpoint = StringField(default='') role = ListField(StringField()) type = ListField(StringField()) order = IntField() devicename = StringField(default='') #if known device name in vmachine lastcheck = IntField(default=j.data.time.getTimeEpoch()) backup = BooleanField() backuplocation = StringField() backuptime = IntField(default=j.data.time.getTimeEpoch()) backupexpiration = IntField()
class AlarmRootCauseCondition(EmbeddedDocument): meta = {"strict": False, "auto_create_index": False} name = StringField(required=True) root = PlainReferenceField("fm.AlarmClass") window = IntField(required=True) condition = StringField(default="True") match_condition = DictField(required=True) def __str__(self): return self.name def __eq__(self, other): return (self.name == other.name and ((self.root is None and other.root is None) or (self.root and other.root and self.root.id == other.root.id)) and self.window == other.window and self.condition == other.condition and self.match_condition == other.match_condition)
class Instructor(User): class_ids = ListField(IntField(), default=[]) def __init__(self, *args, **kwargs): super(self.__class__, self).__init__(*args, **kwargs) if 'class_ids' in kwargs: self.class_ids = kwargs['class_ids'] @staticmethod def init_instructor(first_name, last_name, username, password): uid = User.objects.count() + 1 type = 'p' encrypt_pw = generate_password_hash(password) return Instructor(uid=uid, type=type, first_name=first_name, last_name=last_name, username=username, encrypt_pw=encrypt_pw)
class Alerts(EmbeddedDocument): """Alerts generated by a Study.""" _timestamp = DateTimeField(default=arrow.now(Config.TZ).datetime) active = ListField(StringField(max_length=120)) # Meta schema_version = IntField(default=1) meta = {'strict': False} @property def timestamp(self): """Preprocess the timestamp to ensure consistency.""" return arrow.get(self._timestamp).to(Config.TZ) @timestamp.setter def timestamp(self, newdt): """Process the timestamp for entry.""" if isinstance(newdt, arrow.Arrow): newdt = newdt.datetime self._timestamp = newdt
class GeneralRuleSet(Rules): ''' A collection for all possible rule sets. (Different expensions, or other rule variations we might want to try out) This colleciton holds most of the game-play related information. ''' ''' name is a key field, all specific rule sets know which general \ rule set is their parent by using this. ''' name = StringField() number_of_piles = IntField() used_cards = ListField(ReferenceField(CARD_COLLECTION)) starting_deck = ListField(ReferenceField(CARD_COLLECTION)) _is_general = BooleanField(default=True) no_copy_fields = Rules.no_copy_fields + ['_is_general']
class ActivatorModel(Document): """ ActivatorModel An abstract base class model that provides activate and deactivate fields. """ STATUS_CHOICES = ( (0, _('Inactive')), (1, _('Active')), ) status = IntField(choices=STATUS_CHOICES, default=1) activate_date = DateTimeField(blank=True, null=True, help_text=_('keep empty for an immediate activation')) deactivate_date = DateTimeField(blank=True, null=True, help_text=_('keep empty for indefinite activation')) objects = ActivatorModelManager() class Meta: abstract = True def save(self, *args, **kwargs): if not self.activate_date: self.activate_date = datetime.datetime.now() super(ActivatorModel, self).save(*args, **kwargs)
class BaseModel(Document): created = DateTimeField(default=datetime.now) modified = DateTimeField(default=datetime.now) created_by = ObjectIdField() modified_by = ObjectIdField() status = IntField(default=Status.VALID) meta = {'abstract': True, 'queryset_class': BaseQuerySetMixin} #=============================================================================== # we can add operate log here too. #=============================================================================== def save(self, *args, **kwargs): try: if not self.created_by: self.created_by = current_user.get_id() self.modified_by = current_user.get_id() except: pass self.modified = datetime.now() Document.save(self, **kwargs) def update(self, *args, **kwargs): try: if not self.created_by: self.created_by = current_user.get_id() self.modified_by = current_user.get_id() except: pass self.modified = datetime.now() return Document.update(self, *args, **kwargs) def delete(self, *args, **kwargs): # we can custom the delete # example: set the status to 0 instead of physical delete return Document.delete(self, *args, **kwargs)
class User(Document, CommonEqualityMixin, UserMixin): """Base User Class. Inherited by Student and Instructor Contains base functionality and fields for both classes """ meta = {'allow_inheritance': True, 'collection': 'users'} uid = SequenceField(primary_key=True, required=True, unique=True) first_name = StringField(required=True) last_name = StringField(required=True) username = StringField(required=True) type = StringField(required=True, max_length=1, choices=('u', 'p')) message_ids = ListField(IntField(), default=[]) encrypt_pw = StringField(required=True) def __init__(self, *args, **kwargs): Document.__init__(self, *args, **kwargs) if 'uid' in kwargs: self.uid = kwargs['uid'] if 'first_name' in kwargs: self.first_name = kwargs['first_name'] if 'last_name' in kwargs: self.last_name = kwargs['last_name'] if 'username' in kwargs: self.username = kwargs['username'] if 'type' in kwargs: self.type = kwargs['type'] if 'message_ids' in kwargs: self.message_ids = kwargs['message_ids'] if 'encrypt_pw' in kwargs: self.encrypt_pw = kwargs['encrypt_pw'] @property def full_name(self): return self.first_name + " " + self.last_name def verify_password(self, password): return check_password_hash(self.encrypt_pw, password) def get_id(self): return self.uid
class Glyph(Document): meta = { "collection": "glyphs", "strict": False, "auto_create_index": False, "json_collection": "main.glyphs", } name = StringField(unique=True) uuid = UUIDField(unique=True, binary=True) font = PlainReferenceField(Font) code = IntField() _id_cache = cachetools.TTLCache(maxsize=100, ttl=60) def __str__(self): return self.name @classmethod @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock) def get_by_id(cls, id: Union[str, bson.ObjectId]) -> "Glyph": return Glyph.objects.filter(id=id).first() @property def json_data(self): return { "name": self.name, "$collection": self._meta["json_collection"], "uuid": str(self.uuid), "font__name": self.font.name, "code": self.code, } def to_json(self): return to_json( self.json_data, order=["name", "$collection", "uuid", "font__name", "code"]) def get_json_path(self): p = [quote_safe_path(n.strip()) for n in self.name.split("|")] return os.path.join(*p) + ".json"
class Assignment(Document): # Set the collection name used in the database meta = {'allow_inheritance': True, 'collection': 'assignments'} aid = SequenceField(required=True, primary_key=True, unique=True) class_id = IntField(required=True) due = DateTimeField(required=True) description = StringField() def __init__(self, *args, **kwargs): Document.__init__(self, *args, **kwargs) if 'aid' in kwargs: self.aid = kwargs['aid'] if 'class_id' in kwargs: self.class_id = kwargs['class_id'] if 'due' in kwargs: self.due = kwargs['due'] if 'description' in kwargs: self.description = kwargs['description']
class ForbidWord(EmbeddedDocument): word = StringField(verbose_name='违禁词') type = IntField(verbose_name='违禁词类型,1为C店违规,2为BC店都违规', default=2) @staticmethod def get_all_forbid_list(cat_id): ''' .获取单个类目的违禁词列表 ''' fobid_list = [] try: cat_id_list = Cat.get_attr_by_cat(cat_id, "cat_path_id").split(' ') + [0] for cat_id in cat_id_list: fobid_list.extend([ fbd.word for fbd in Cat.get_attr_by_cat(cat_id, "forbid_list") ]) except Exception, e: log.error("get forbid list error and the error = %s" % e) return fobid_list
class Connection(EmbeddedDocument): dbType = StringField( choices=( "mysql", "postgresql", "sqlite", "mssql", "csvTextFile", "xlsXlsxFile", "s3BucketFile", ), required=True, ) host = StringField() port = IntField() database = StringField() user = StringField() password = StringField() query = StringField() bucket = StringField() files = EmbeddedDocumentListField(File)
class Tables(DynamicDocument): name = StringField(required=True, help_text="name") columns = ListField(StringField(), required=True, help_text="column names") data = ListField(ListField(StringField()), required=True, help_text="table rows") md5 = StringField(regex=r"^[a-z0-9]{32}$", unique=True, help_text="md5 sum") total_data_rows = IntField(help_text="total number of rows") meta = {"collection": "tables", "indexes": ["name", "columns", "md5"]} @classmethod def pre_save_post_validation(cls, sender, document, **kwargs): from mpcontribs.api.tables.views import TablesResource resource = TablesResource() d = resource.serialize(document, fields=["columns", "data"]) s = json.dumps(d, sort_keys=True).encode("utf-8") document.md5 = md5(s).hexdigest() document.total_data_rows = len(document.data)
class ReportSnap(Document): '''全网数据报表快照''' OBJECT_TYPE_CHOICES = (('shop', '帐户'), ('camp', '计划')) MNT_TYPE_CHOICES = (('all', '所有'), ('mnt', '托管'), ('unmnt', '未托管')) SUM_DAYS_CHOICES = (1, 7, 15, 30) CONV_DAYS_CHOICES = (1, 3) object_type = StringField(verbose_name='报表对象类型', choices=OBJECT_TYPE_CHOICES) mnt_type = StringField(verbose_name='托管类型', choices=MNT_TYPE_CHOICES) count = IntField(verbose_name='统计数量') date = DateTimeField(verbose_name='快照日期') create_time = DateTimeField(verbose_name='创建日期', default=datetime.datetime.now) sum_days = IntField(verbose_name='报表天数', choices=SUM_DAYS_CHOICES) conv_days = IntField(verbose_name='报表转化天数', choices=CONV_DAYS_CHOICES, default=3) # search_type = IntField(verbose_name = "报表类型", default = 3) # source = IntField(verbose_name = "数据来源", default = 3) # 基础数据 impressions = IntField(verbose_name="展现量", default=0) click = IntField(verbose_name="点击量", default=0) cost = IntField(verbose_name="总花费", default=0) # 效果数据 pay = IntField(verbose_name="成交金额", default=0) paycount = IntField(verbose_name="成交笔数", default=0) ctr = FloatField(verbose_name='', default=0) cpc = FloatField(verbose_name='', default=0) roi = FloatField(verbose_name='', default=0) meta = { 'db_alias': 'crm-db', 'collection': 'timer_reportsnap', 'indexes': ['date', 'object_type'] }
class TableColumns(EmbeddedDocument): label = StringField(required=True) name = StringField(required=True) type = StringField(default="text") description = StringField(default=None) unit = StringField(default=None) order_by = StringField(default='') is_orderable = BooleanField(default=False) is_searchable = BooleanField(default=False) is_editable = BooleanField(default=False) is_required = BooleanField(default=False) is_global_searchable = BooleanField(default=False) rounded = BooleanField(default=False) is_callable = BooleanField(default=False) show_in = EmbeddedDocumentListField( AllowPropertyNamespace, default=[AllowPropertyNamespace(**{ "name": "default", "allow": True })]) order_in = EmbeddedDocumentListField( ValuePropertyNamespace, default=[ValuePropertyNamespace(**{ "name": "default", "value": 0 })]) # is_icon = BooleanField(default=False) icon_name = StringField() is_extended = BooleanField(default=False) colspan = IntField(default=1) style_classes = EmbeddedDocumentListField(TableClasses) # TO REMOVE is_ref = BooleanField(default=False) module = StringField(default=None) model = StringField(default=None)
class Linea(EmbeddedDocument ): #es embedded porque solo puede existir dentro de pedido num_items = IntField(required=True, min_value=0) precio_item = FloatField(required=True, min_value=0) name = StringField(required=True, min_length=2) total = FloatField(required=True, min_value=0) ref = ReferenceField(Producto, required=True) def clean(self): self.validate(clean=False) if not isinstance(self.ref, Producto): raise ValidationError("La referencia a un producto no es correcta") if self.name != self.ref.nombre: raise ValidationError( "El nombre de el producto referenciado no se corresponde con el de la línea" ) if self.total != self.num_items * self.precio_item: raise ValidationError( "El precio total de la línea no se corresponde con el precio individual por número de items" )
class WebAuthn(db.Document, WebAuthnMixin): credential_id = BinaryField(primary_key=True, max_bytes=1024, required=True) public_key = BinaryField(required=True) sign_count = IntField(default=0) transports = ListField(required=False) # a JSON string as returned from registration extensions = StringField(max_length=255) lastuse_datetime = DateTimeField(required=True) # name is provided by user - we make sure it is unique per user name = StringField(max_length=64, required=True) usage = StringField(max_length=64, required=True) # we need to be able to look up a user from a credential_id user = ReferenceField("User") # user_id = ObjectIdField(required=True) meta = {"db_alias": db_name} def get_user_mapping(self) -> t.Dict[str, str]: """ Return the mapping from webauthn back to User """ return dict(id=self.user.id)
class BasicMessage(Document, CommonEqualityMixin): meta = {'allow_inheritance': True, 'collection': 'messages'} mid = SequenceField(required=True, primary_key=True, unique=True) text = StringField(required=True) sender_id = IntField(required=True) seen = BooleanField(default=False) def __init__(self, *args, **kwargs): Document.__init__(*args, **kwargs) if 'mid' in kwargs: self.mid = kwargs['mid'] if 'text' in kwargs: self.text = kwargs['text'] if 'sender' in kwargs: self.sender = kwargs['sender'] if 'seen' in kwargs: self.seen = kwargs['seen'] def mark_as_seen(self): self.seen = True
class Run(EmbeddedDocument): id = ObjectIdField(default=ObjectId) user_id = StringField() date = DateTimeField(default=datetime.now) time = FloatField() distance = FloatField() calories_burned = IntField() def calculate_calories(self, time, distance, user_id): from .user import User if (time <= 0 or distance <= 0 or user_id is None): return 0 user = User.objects.get(pk=user_id) mass_kg = user.mass kph = distance / time vo2 = 2.209 + 3.1633 * kph kcal_per_min = 4.86 * mass_kg * vo2 / 1000 return kcal_per_min
class SpiralModel(DynamicDocument): blockchain = StringField() host = StringField() ahost = StringField() size_of_pool = IntField() overlap = IntField() profit_growth = IntField() base_rate = IntField() loss_percent = IntField() pool_limit = IntField() pool_timeout = IntField() priority_seconds = IntField() meta = { 'collection': 'spirals', 'ordering': ['_id'], 'indexes': [ 'blockchain', 'host', 'ahost', ], 'auto_create_index': True, 'index_background': True }
class RunningMachines(Document): """ Describes a current running vulnerable machine. name: The name of the machine tags: the tags sent to ansible, aka the roles selected operating_systems: Running OS on the machine. ip_address: IP of machine ports: Listening ports on the machine status: the current status of machine (Creating/UP) """ meta = { 'collection': 'running_machines', 'indexes': [ { 'fields': ['name'], 'unique': True } ] } name = StringField(required=True, unique=True, null=False) tags = StringField(required=True, null=False) operating_system = StringField(required=True, null=False) ip_address = StringField(required=False, null=False) ports = ListField(IntField(required=True, null=False), required=False, null=False) status = StringField(required=True, null=False) @property def document(self): return { 'name': self.name, 'tags': self.tags, 'operating_system': self.operating_system, 'ip_address': self.ip_address, 'ports': self.ports, 'status': self.status }
class BaseDynamicModel(DynamicDocument): created = DateTimeField(default=datetime.now) modified = DateTimeField(default=datetime.now) created_by = StringField() modified_by = StringField() status = IntField(default=Status.VALID) meta = {'abstract': True, 'queryset_class': BaseQuerySetMixin} def save(self, *args, **kwargs): if not self.created_by: self.created_by = current_user.get_id() self.modified = datetime.now() self.modified_by = current_user.get_id() Document.save(self, **kwargs) def update(self, *args, **kwargs): self.modified = datetime.now() self.modified_by = current_user.get_id() return Document.update(self, *args, **kwargs)
class ExperimentalStructure(Structure): quaternary = StringField() resolution = FloatField() experiment = StringField() clusters = ListField(EmbeddedDocumentField(Cluster)) tax = IntField(required=False) def cluster(self, cluster_name): rss = [x for x in self.clusters if x.name == cluster_name] if rss: return rss[0] else: return Cluster(name=cluster_name) def file_path(self): return '/data/pdb/divided/' + self.name[ 1:3] + '/pdb' + self.name + '.ent' def __str__(self): return "ExperimentalStructure(" + self.name + ")" def __repr__(self): return self.__str__()
class Alert(ModelBase, Document): username = StringField(default='') description = StringField(default='') descriptionpub = StringField(default='') level = IntField(min_value=1, max_value=3, default=1) # dot notation e.g. machine.start.failed category = StringField(default='') tags = StringField(default='') # e.g. machine:2323 state = StringField(choices=("NEW","ALERT","CLOSED"), default='NEW', required=True) history = ListField(DictField()) # first time there was an error condition linked to this alert inittime = IntField(default=j.data.time.getTimeEpoch()) # last time there was an error condition linked to this alert lasttime = IntField() closetime = IntField() # alert is closed, no longer active # $nr of times this error condition happened nrerrorconditions = IntField() errorconditions = ListField(IntField()) # ids of errorconditions
class TrainedModel(Document): machine_name = StringField(required=True) model_name = StringField(required=True) dataset_name = StringField(required=True) run = IntField(required=True) train_temperature = StringField(required=True) all_history = MapField(EmbeddedDocumentListField(MetricHistoryRecord)) best_history = MapField(EmbeddedDocumentListField(MetricHistoryRecord)) created_at = DateTimeField(required=True, default=datetime.datetime.now) updated_at = DateTimeField(required=True, default=datetime.datetime.now) meta = meta = { 'indexes': [ { 'fields': ['model_name', 'dataset_name', '+run', 'train_temperature'], 'unique': True, }, '#machine_name', '#model_name', '#dataset_name', '+run', '#train_temperature', '-created_at', '-updated_at', ], } def clean(self): self.model_name = self.model_name.lower() self.dataset_name = self.dataset_name.lower() self.train_temperature = self.train_temperature.lower() self.updated_at = datetime.datetime.now() super().clean()