class ModelIterPreIter(Model): Field1 = IntegerField("a") Field2 = IntegerField("b") def pre_iter(self): # noinspection PyAttributeOutsideInit self.field1 = 5
class ChannelConfigModel(Model): WITH_OID = False # Votes needed to promote a member to be moderator VotePromoMod = IntegerField("v-m", default=ChannelConfig.VotesToPromoteMod) # Votes needed to promote a member to be admin VotePromoAdmin = IntegerField("v-a", default=ChannelConfig.VotesToPromoteAdmin) EnableAutoReply = BooleanField("e-ar", default=True) EnableTimer = BooleanField("e-tmr", default=True) EnableCalculator = BooleanField("e-calc", default=True) EnableBotCommand = BooleanField("e-bot", default=True) InfoPrivate = BooleanField("prv", default=False) DefaultProfileOid = ObjectIDField("d-prof", allow_none=True) DefaultName = TextField("d-name", allow_none=True)
class ModelOnInvalid(Model): Field1 = IntegerField("i") Field2 = BooleanField("b") def perform_validity_check(self) -> ModelValidityCheckResult: if self.field1 > 0: return ModelValidityCheckResult.X_UNCATEGORIZED else: return ModelValidityCheckResult.O_OK def on_invalid(self, reason=ModelValidityCheckResult.X_UNCATEGORIZED): raise TestBaseModel.TempInvalidException()
class ChannelProfileModel(Model): # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # !!! Check `ProfileManager.process_create_profile_kwargs` when changing the variable name of this class. !!! # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ChannelOid = ObjectIDField("c", default=ModelDefaultValueExt.Required) Name = TextField("n", default="-", must_have_content=True) Color = ColorField("col") # 0 means no need to vote, > 0 means # votes needed to get this profile PromoVote = IntegerField("promo", positive_only=True) Permission = DictionaryField( "perm", default=ProfilePermissionDefault.get_default_code_str_dict(), allow_none=False) PermissionLevel = PermissionLevelField("plv") EmailKeyword = ArrayField("e-kw", str) @property def is_mod(self): return self.permission_level >= PermissionLevel.MOD @property def is_admin(self): return self.permission_level >= PermissionLevel.ADMIN @property def permission_list(self) -> List[ProfilePermission]: ret = set() for cat, permitted in self.permission.items(): if permitted: perm = ProfilePermission.cast(cat, silent_fail=True) if perm: ret.add(perm) ret = ret.union( ProfilePermissionDefault.get_overridden_permissions( self.permission_level)) return list(sorted(ret, key=lambda x: x.code)) def pre_iter(self): # Will be used when the data will be passed to MongoDB d = [ p.code_str for p in ProfilePermissionDefault.get_overridden_permissions( self.permission_level) ] for perm_cat in ProfilePermission: k = perm_cat.code_str if k not in self.permission: self.permission[k] = k in d
class ModelTest(Model): FModel = ModelField("m", SubModel) FInt = IntegerField("i", default=ModelDefaultValueExt.Required) FModelArray = ModelArrayField("ma", SubModel, default=ModelDefaultValueExt.Optional) FDict = DictionaryField("d") FFloat = FloatField("f", default=7.5) FBool = BooleanField("b") FUrl = UrlField("u", default=ModelDefaultValueExt.Optional) FText = TextField("t") FOid = ObjectIDField("o") FGeneral = GeneralField("g") FDatetime = DateTimeField("dt") FColor = ColorField("c") FArray = ArrayField("a", int, default=[17, 21]) FArContent = AutoReplyContentTypeField("ac")
class TimerModel(Model): ChannelOid = ObjectIDField("ch", default=ModelDefaultValueExt.Required) Keyword = TextField("k", default=ModelDefaultValueExt.Required) Title = TextField("t", default=ModelDefaultValueExt.Required) TargetTime = DateTimeField("tt", default=ModelDefaultValueExt.Required) DeletionTime = DateTimeField("del", default=ModelDefaultValueExt.Optional) Countup = BooleanField("c", default=False) PeriodSeconds = IntegerField("p") Notified = BooleanField("nt", default=False) NotifiedExpired = BooleanField("nt-e", default=False) @property def is_periodic(self) -> bool: return self.period_seconds > 0 def get_target_time_diff(self, dt: datetime): return abs(self.target_time - make_tz_aware(dt)) def is_after(self, dt: datetime) -> bool: """Check if the target time of the timer is after the given ``dt``.""" return self.target_time >= make_tz_aware(dt)
class ModelHasOid(Model): Field1 = IntegerField("i")
class ModelIterNoPreIter(Model): Field1 = IntegerField("a") Field2 = IntegerField("b")
class DuplicateJsonModel(Model): Field1 = IntegerField("i") Field2 = BooleanField("i")
class TestModel(Model): Field1 = IntegerField("f1") Field2 = IntegerField("f2", default=ModelDefaultValueExt.Required) Field3 = IntegerField("f3", default=ModelDefaultValueExt.Optional) Field4 = IntegerField("f4", default=5)
def get_field(self) -> BaseField: return IntegerField("k")
class SubModel(Model): WITH_OID = False FInt = IntegerField("i2", default=8)
def get_field(self) -> BaseField: return IntegerField("k", allow_none=True)
def get_field(self) -> BaseField: return IntegerField("k", auto_cast=False)
class ModelNoOid(Model): WITH_OID = False Field1 = IntegerField("i")
class ModelTest2(Model): Field1 = IntegerField("f1") Field2 = BooleanField("f2")
def get_field(self) -> BaseField: return IntegerField("k", positive_only=True)