class Role(ExtendedDocument): """ ... """ meta = {"collection": "roles"} name = db.StringField(required=True, unique=True) description = db.StringField() # TODO rethink the structure group = db.IntField(required=True) # , unique_with=["priority_level"]) priority_level = db.IntField(required=True) # , unique_with=["group"]) # Embedded Document Version _embedded = None @classmethod def as_embedded(cls, *args, **kwargs): if not cls._embedded: cls._embedded = type("EmbeddedRole", (ExtendedEmbeddedDocument,), {"name": db.StringField(required=True), "description": cls.description, "group": cls.group, "priority_level": cls.priority_level}) if args or kwargs: return cls._embedded(*args, **kwargs) return cls._embedded
class Scenario(db.EmbeddedDocument): order = db.IntField() display_text = db.DictField() available_time = db.IntField() expire_time = db.IntField() used = db.IntField() disabled = db.StringField() countdown = db.IntField() attr = db.DictField()
class PuzzleStatus(db.Document): puzzle = db.StringField() quantity = db.IntField(default=0) currency = db.IntField(default=0) meta = { 'indexes': [ 'puzzle' ] }
class Attendee(db.Document): event_id = db.StringField() token = db.StringField(unique=True) user_id = db.StringField() scenario = db.DictField() attr = db.DictField() first_use = db.IntField() role = db.StringField() meta = {'indexes': ['event_id', 'token', 'role']} @property def public_token(self): return sha1(self.token.encode('utf-8')).hexdigest() def to_json(self): data = self.to_mongo() scenarios = [] for k, v in data['scenario'].items(): v.pop('_cls') v['id'] = k scenarios.append(v) data.pop('scenario') data['scenarios'] = sorted(scenarios, key=lambda k: k['order']) return bson.json_util.dumps(data)
class PuzzleBucket(db.Document): attendee = db.ReferenceField(Attendee) public_token = db.StringField(unique=True) puzzle = db.ListField() valid = db.IntField() coupon = db.IntField() deliverer = db.ListField() meta = { 'indexes': [ 'public_token' ] } @classmethod def init(cls, attendee): try: return PuzzleBucket.objects.create(attendee=attendee, public_token=attendee.public_token) except NotUniqueError: return PuzzleBucket.objects(public_token=attendee.public_token).get()
class Task(ExtendedDocument): """ ... """ meta = {"collection": "tasks", "allow_inheritance": True} name = db.StringField(required=True) priority = db.IntField(required=True) description = db.StringField() subject = db.ObjectIdField(required=True)
class Announcement(db.Document): datetime = db.IntField() msg_zh = db.StringField() msg_en = db.StringField() uri = db.StringField() role = db.ListField()
class Team(db.Document): group_id = db.IntField(unique=True) name = db.StringField() coin = db.IntField(default=0) meta = {'indexes': ['group_id']}
class Coupon(db.Document): coin = db.IntField() description = db.StringField() own_team = db.ReferenceField(Team) producer = db.StringField()