class TeamMembers(BaseModel): member = ForeignKeyField(User) team = ForeignKeyField(Team) is_manager = BooleanField(default=False) class Meta: index = ('user', 'team', 'manages')
class ItemModel(BaseSQLModel): id = AutoField() title = CharField(max_length=64) user = ForeignKeyField(UserModel, backref="items", on_delete="CASCADE") category = ForeignKeyField(CategoryModel, backref="items", on_delete="CASCADE") brand = ForeignKeyField(BrandModel, backref="items", on_delete="CASCADE") tags = ManyToManyField(TagModel, backref="items") condition = CharField(choices=ItemCondition) value = DecimalField(decimal_places=2, auto_round=True) currency = CharField(choices=Currency) description = CharField(max_length=128) status = CharField(choices=ItemStatus, default=ItemStatus.AVAILABLE) preference_score_rate = DecimalField(decimal_places=2, auto_round=True, default=0) popularity_rate = DecimalField(decimal_places=2, auto_round=True, default=0) date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "items" def __str__(self): return self.name
class Participation(BaseModel): chat = ForeignKeyField(Chat) user = ForeignKeyField(User) cursor = IntegerField(default=0) is_admin = BooleanField(default=0) class Meta: indexes = ((('chat', 'user'), True), )
class Goals(BaseModel): person = peewee.ForeignKeyField(Person, null=True) title = peewee.CharField(max_length=60) achievement = BooleanField(default=False, null=True) reminder = peewee.DateTimeField(default=datetime.datetime.utcnow) def __str__(self): return "{} {}".format(self.person, self.title)
class ListModel(BaseSQLModel): id = AutoField() # TODO: Restrict author must be role ADMIN author = ForeignKeyField(UserModel, backref="lists", on_delete="CASCADE") title = CharField(max_length=256) date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "lists"
class CardModel(BaseSQLModel): id = AutoField() author = ForeignKeyField(UserModel, backref="cards", on_delete="CASCADE") list = ForeignKeyField(ListModel, backref="cards", on_delete="CASCADE") title = CharField(max_length=256) description = CharField(max_length=1024) date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "cards"
class Price(BaseModel): id = IntegerField(column_name='id') date = DateField(column_name='date') price = DoubleField(column_name='price') product = ForeignKeyField(Product, backref="prices") authorized = BooleanField(column_name="authorized") shop = TextField(column_name="shop") class Meta: table_name = 'price_parser' # print([price.product.__dict__ for price in Price.select(Product).join(Product).execute()])
class CommentModel(BaseSQLModel): id = AutoField() author = ForeignKeyField(UserModel, backref="comments", on_delete="CASCADE") card = ForeignKeyField(CardModel, backref="comments", on_delete="CASCADE") content = CharField(max_length=256) date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "comments"
class UserModel(BaseSQLModel, UserMixin): id = AutoField() username = CharField(unique=True, index=True, max_length=16) email_address = CharField(unique=True, max_length=256) salt = BlobField() key = BlobField() role = CharField(default=UserRole.MEMBER) date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "users"
class MatchModel(BaseSQLModel): id = AutoField() item_a = ForeignKeyField(ItemModel, index=True, related_name="matches_a") item_b = ForeignKeyField(ItemModel, index=True, related_name="matches_b") date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "matches" def __str__(self): return f"{self.item_a} - {self.item_b}"
class ImageModel(BaseSQLModel): id = AutoField() url = CharField(max_length=256) item = ForeignKeyField(ItemModel, backref="images", on_delete="CASCADE") date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "images" def __str__(self): return self.url
class Notification(ModelBase): Types = NotificationTypes type_ = IntegerField(column_name='type') metadata = BinaryJSONField(default={}) read = BooleanField(default=False) created_at = DateTimeField(default=datetime.utcnow) class Meta: table_name = 'notifications' indexes = ( (('created_at', 'read'), False), ) @classmethod def get_unreads(cls, limit=25): return cls.select().where( cls.read == 0, ).order_by( cls.created_at.asc() ).limit(limit) @classmethod def dispatch(cls, typ, **kwargs): obj = cls.create( type_=typ, metadata=kwargs ) rdb.publish('notifications', json.dumps(obj.to_user())) return obj def to_user(self): data = {'id': self.id, 'date': arrow.get(self.created_at).humanize()} if self.type_ == self.Types.GENERIC: data['title'] = self.metadata.get('title', 'Generic Notification') data['content'] = self.metadata.get('content', '').format(m=self.metadata) elif self.type_ == self.Types.CONNECT: data['title'] = '{} connected'.format( 'Production' if self.metadata['env'] == 'prod' else 'Testing') data['content'] = self.metadata.get('content', '').format(m=self.metadata) elif self.type_ == self.Types.RESUME: data['title'] = '{} resumed'.format( 'Production' if self.metadata['env'] == 'prod' else 'Testing') data['content'] = self.metadata.get('content', '').format(m=self.metadata) return data
class User(BaseModel): login = CharField(unique=True) email = CharField(unique=True) pw_hash = TextField() authenticated = BooleanField(default=False) active = BooleanField(default=False) def is_authenticated(self): return self.authenticated def is_active(self): return self.active def is_anonymous(self): return False def get_id(self): return self.login def set_password(self, password): self.pw_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.pw_hash, password)
class CategoryModel(BaseSQLModel): id = AutoField() name = CharField(unique=True, max_length=64) date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "categories" def __str__(self): return self.name def save(self, *args, **kwargs): self.name = self.name.lower() return super().save(*args, **kwargs)
class TagModel(BaseSQLModel): id = AutoField() value = CharField(unique=True, max_length=64) date_created = DateTimeField(null=True) date_updated = DateTimeField(null=True) is_deleted = BooleanField(default=False) class Meta(BaseSQLModel): table_name = "tags" def __str__(self): return self.value def get(self, **kwargs): kwargs["value"] = kwargs["value"].lower() return super().get(**kwargs) def save(self, *args, **kwargs): self.value = self.value.lower() return super().save(*args, **kwargs)
class Sale(BaseModel): id = IntegerField(primary_key=True) city = CharField(null=True) district = CharField(null=True) address = CharField(null=True) latitude = FloatField(null=True) longitude = FloatField(null=True) floor = IntegerField(null=True) number_of_floors = IntegerField(null=True) number_of_rooms = IntegerField(null=True) kitchen_area = FloatField(null=True) living_area = FloatField(null=True) total_area = FloatField(null=True) walling = CharField(null=True) price_amount = FloatField(null=True) price_currency = CharField(null=True) seller = CharField(null=True) resale = BooleanField(null=True) image = CharField(null=True) url = CharField(null=True) created_at = DateTimeField(default=datetime.utcnow) updated_at = DateTimeField(default=datetime.utcnow)