class Source(Model): ''' Sub-model of Error. ''' pointer = Field() ''' ''' parameter = Field() '''
class Model(MemoryModel): username = Field() password = Field() @classmethod async def find_one(cls, query): for id in cls.db: data = cls.db[id].copy() del data['id'] if data == query: return cls(cls.db[id]) return None
class Course(APIModel): __rate__ = ( 10, 'secondly' ) __acl__ = { 'administrator': ['all'], 'other': ['read', 'read_all'], #'unauthorized': ['read', 'read_all'] } name = Field(required=True) sections = Field(type=[ Section ]) description = Field(required=True)
class Equipment(Model): head = Field(type=Item) back = Field(type=Item) shoulders = Field(type=Item) chest = Field(type=Item) waist = Field(type=Item) legs = Field(type=Item) left = Field(type=Item) right = Field(type=Item) feet = Field(type=Item)
class Profile(MongoDBModel, JSONAPIMixin): username = Field(required=True) name = Field(type=Name) email = Field(required=True) password = Field(required=True, computed='encrypt_password') def encrypt_password(self): if self.password == 'hashed-': raise Exception('Invalid password.') if self.password.startswith('hashed-'): return self.password return f'hashed-{hashlib.sha256(self.password.encode()).hexdigest()}'
class User(MongoDBModel, JSONAPIMixin): __acl__ = { 'self': ['read', 'update', 'subscribe'], 'administrator': ['all'], 'other': ['read'] } __set__ = {'groups': ['administrator']} username = Field(required=True) password = Field(required=True) groups = Field(type=list)
class Post(MongoDBModel, JSONAPIMixin): __acl__ = { 'administrator': ['all'], 'other': ['read', 'read_all'], 'unauthorized': ['read', 'read_all'] } __database_options__ = {'name': 'sugar-blog'} title = Field(required=True) owner = Field(required=True) created = Field(required=True) updated = Field() content = Field(required=True)
class User(MongoDBModel, JSONAPIMixin): __acl__ = { 'self': ['read', 'update'], 'administrator': ['all'], 'other': ['read'] } __database_options__ = { 'name': 'sugar-blog' } username = Field(required=True) password = Field(required=True) group = Field(required=True)
class Links(Model): ''' Sub-model of Error. ''' about = Field() '''
class Group(MongoDBModel, JSONAPIMixin): __acl__ = {'administrator': ['all']} __database_options__ = {'name': 'sugar-blog'} name = Field(required=True)
class Post(MongoDBModel, JSONAPIMixin, TimestampMixin): __rate__ = ( 10, 'secondly' ) __acl__ = { 'administrator': [ 'all' ], '$owner': [ 'read', 'update', 'delete' ], 'user': [ 'read_all', 'read', 'create' ], 'unauthorized': [ 'read_all', 'read' ] } __index__ = [ { 'keys': [('created', ASCENDING)] }, { 'keys': [('slug', ASCENDING)], 'options': { 'unique': True } } ] __connection__ = { 'host': os.getenv('MONGODB_URI', 'mongodb://localhost:27017'), 'retrywrites': False } __database__ = { 'name': os.getenv('MONGODB_DB', 'sugar-blog') } created = Field(type='timestamp', required=True) owner = Field(required=True) slug = Field(required=True) title = Field(required=True) content = Field(required=True, computed='format_content') def format_content(self): return re.sub('\n\n\n', '\n\n', self.content)
class Attributes(Model): strength = Field(type=int) dexterity = Field(type=int) constitution = Field(type=int) intelligence = Field(type=int) wisdom = Field(type=int) charisma = Field(type=int)
class User(APIModel): __rate__ = (1, 'secondly') __acl__ = { 'self': ['read', 'update', 'delete'], 'administrator': ['all'], #'other': ['read'], 'unauthorized': ['create'] } __get__ = {'groups': ['self', 'administrator']} __set__ = { 'username': ['self', 'administrator', 'unauthorized'], 'password': ['self', 'administrator', 'unauthorized'], 'groups': ['administrator'] } __database__ = {'name': 'vue-sugar-template'} username = Field(required=True) password = Field(required=True, computed='encrypt_password') groups = Field(type=list, computed='default_groups', computed_empty=True) def on_render(self, data, token): del data['attributes']['password'] def default_groups(self): return ['users'] def encrypt_password(self): if self.password == 'hashed-': raise Exception('Invalid password.') if self.password.startswith('hashed-'): return self.password return f'hashed-{hashlib.sha256(self.password.encode()).hexdigest()}'
class Item(Model): title = Field() slot = Field() attributes = Field(type=Attributes) resistances = Field(type=Resistances) armor = Field(type=int) count = Field(type=int)
class Resistances(Model): fire = Field(type=int) frost = Field(type=int) poison = Field(type=int) shadow = Field(type=int) magic = Field(type=int) holy = Field(type=int)
class Error(Model, Exception): ''' An JSONAPI compliant error model which can be raised or serialized. It can be constructed from a `dictionary`, `**kargs` or a combination of both. ''' id = Field() ''' ''' links = Field(type=Links) ''' ''' status = Field(type=int) ''' ''' code = Field() ''' ''' title = Field() ''' ''' detail = Field() ''' ''' source = Field(type=Source) ''' ''' meta = Field(type=dict) '''
class Section(Model): name = Field(required=True) instructor = Field(required=True) students = Field(type=[ str ])
class Mixin(MongoDBModel, JSONAPIMixin): field = Field()
class Location(Model): type = Field() coordinates = Field(type=list)
class Name(Model): first = Field(required=True) last = Field() title = Field()
class Alpha(MemoryModel): gamma = Field(type=Gamma)
class Gamma(Model): owner = Field()
class User(MongoDBModel, JSONAPIMixin): __rate__ = (10, 'secondly') __acl__ = { 'self': ['read', 'update', 'delete'], 'administrator': ['all'], 'other': ['read'], 'unauthorized': ['read'] } __get__ = {'groups': ['self', 'administrator']} __set__ = { 'username': ['self', 'administrator', 'unauthorized'], 'password': ['self', 'administrator', 'unauthorized'], 'groups': ['administrator'] } __index__ = [{ 'keys': [('username', ASCENDING)], 'options': { 'unique': True } }, { 'keys': [('username', ASCENDING), ('password', ASCENDING)] }] __connection__ = { 'host': os.getenv('MONGODB_URI', 'mongodb://localhost:27017'), 'retrywrites': False } __database__ = {'name': os.getenv('MONGODB_DB', 'sugar-blog')} username = Field(required=True, validated='validate_username') password = Field(required=True, computed='encrypt_password', validated='validate_password', validated_before_computed=True) groups = Field(type=list, computed='default_groups', computed_empty=True) def on_render(self, data, token): del data['attributes']['password'] def default_groups(self): return ['user'] def encrypt_password(self): if self.password == 'hashed-': raise Exception('Invalid password.') if self.password.startswith('hashed-'): return self.password return f'hashed-{hashlib.sha256(self.password.encode()).hexdigest()}' def validate_username(self, value): if len(value) < 8: raise Exception('Username cannot be less than eight characters.') def validate_password(self, value): if len(value) < 8: raise Exception('Password cannot be less than eight characters.')
class Character(MongoDBModel, JSONAPIMixin, TimestampMixin): __index__ = [{'keys': [('location', GEOSPHERE)]}] __set__ = { 'shard': [], 'monster_id': [], 'profile': [], 'name': [], 'title': [], 'profession': [], 'attributes': [], 'resistances': [], 'equipment': [], 'inventory': [], 'state': [], 'health': [], 'level': [], 'location': [], 'touched': [] } shard = Field() monster_id = Field() profile = Field() name = Field(type=Name, required=True) title = Field() race = Field(required=True) profession = Field(required=True) attributes = Field(type=Attributes, required=True) equipment = Field(type=Equipment) inventory = Field(type=[Item]) state = Field(type=State, required=True) health = Field(type=int, required=True) level = Field(type=Level, required=True) location = Field(type=Location) touched = Field(type='timestamp') @property def socket(self): return Connections.socket_by_character_id(self.id) @property def connected(self): return self.socket async def set_location(self, longitude, latitude): redis = await Redis.connect(host='redis://localhost', minsize=1, maxsize=1) await redis.geoadd('location', longitude, latitude, f'{self.shard}:{self.id}') self.longitude = longitude self.latitude = latitude await self.save() async def remove_location(self): redis = await Redis.connect(host='redis://localhost', minsize=1, maxsize=1) await redis.zrem('location', f'{self.shard}:{self.id}') self.longitude = None self.latitude = None await self.save() async def distance_to_character(self, character): redis = await Redis.connect(host='redis://localhost', minsize=1, maxsize=1) return await redis.geodist('location', f'{self.shard}:{self.id}', f'{character.shard}:{character.id}')
class State(Model): target = Field() hostile = Field(type=bool) retaliate = Field(type=bool) dead = Field(type=int) casting = Field(type=bool)
class Gamma(Model): owners = Field(type=list)
class Level(Model): current = Field(type=int, required=True) experience = Field(type=int, required=True) next = Field(type=int)
class Alpha(MemoryModel): owners = Field(type=list)
class Alpha(MemoryModel): owners = Field()