From 0627ed48f41d6cd7ab615fe8fb93827db4019e74 Mon Sep 17 00:00:00 2001 From: Rongrong Date: Mon, 29 Jul 2024 03:27:09 +0800 Subject: [PATCH] style(db.model): improve readability and maintainability Signed-off-by: Rongrong --- src/db/models.py | 159 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 109 insertions(+), 50 deletions(-) diff --git a/src/db/models.py b/src/db/models.py index 5f93bb417a..891bca3373 100644 --- a/src/db/models.py +++ b/src/db/models.py @@ -40,11 +40,16 @@ class User(Model, Base): Stores users, their info and their default feed options. """ id = fields.BigIntField(pk=True, description='Telegram user id, 8Bytes') - state = fields.SmallIntField(default=0, description='User state: ' - '-1=banned, 0=guest, 1=user, 50=channel, 51=group, 100=admin') + state = fields.SmallIntField( + default=0, + description='User state: ' + '-1=banned, 0=guest, 1=user, 50=channel, 51=group, 100=admin', + ) lang = fields.CharField(default='zh-Hans', max_length=16, description='Preferred language, lang code') - admin = fields.BigIntField(null=True, description='One of the admins of the channel or group, ' - 'can be null if this "user" is not a channel or a group') + admin = fields.BigIntField( + null=True, + description='One of the admins of the channel or group, can be null if this "user" is not a channel or a group', + ) sub_limit = fields.SmallIntField(null=True, description='Subscription number limit') subs: fields.ReverseRelation['Sub'] @@ -76,26 +81,41 @@ class Feed(Model, Base): Stores feeds and their info. """ id = fields.IntField(pk=True) - state = fields.SmallIntField(default=1, description='Feed state: ' - '0=deactivated, 1=activated') - link = fields.CharField(max_length=4096, # hey, is there really any rss feed url > 4096B? - unique=True, # will also be indexed - description='Feed link') + state = fields.SmallIntField( + default=1, + description='Feed state: ' + '0=deactivated, 1=activated', + ) + link = fields.CharField( + max_length=4096, # hey, is there really any rss feed url > 4096B? + unique=True, # will also be indexed + description='Feed link', + ) title = fields.CharField(max_length=1024, description='Feed title') - interval = fields.SmallIntField(null=True, description='Monitoring task interval. ' - 'Should be the minimal interval of all subs to the feed,' - 'default interval will be applied if null') + interval = fields.SmallIntField( + null=True, + description='Monitoring task interval. ' + 'Should be the minimal interval of all subs to the feed,' + 'default interval will be applied if null', + ) entry_hashes = fields.JSONField(null=True, description='Hashes (CRC32) of entries') - etag = fields.CharField(max_length=128, null=True, - description='The etag of webpage, will be changed each time the feed is updated. ' - 'Can be null because some websites do not support') + etag = fields.CharField( + max_length=128, + null=True, + description='The etag of webpage, will be changed each time the feed is updated. ' + 'Can be null because some websites do not support', + ) last_modified = fields.DatetimeField(null=True, description='The last modified time of webpage. ') - error_count = fields.SmallIntField(default=0, - description='Error counts. If too many, deactivate the feed. ' - '+1 if an error occurs, reset to 0 if feed fetched successfully') - next_check_time = fields.DatetimeField(null=True, - description='Next checking time. ' - 'If too many errors occur, let us wait sometime') + error_count = fields.SmallIntField( + default=0, + description='Error counts. If too many, deactivate the feed. ' + '+1 if an error occurs, reset to 0 if feed fetched successfully', + ) + next_check_time = fields.DatetimeField( + null=True, + description='Next checking time. ' + 'If too many errors occur, let us wait sometime', + ) subs: fields.ReverseRelation['Sub'] class Meta: @@ -114,41 +134,80 @@ class Sub(Model, Base): Stores subscriptions (who subscribed which feed) and their options, many to many relation. """ id = fields.IntField(pk=True) - state = fields.SmallIntField(default=1, description='Sub state: ' - '0=deactivated, 1=activated') - user: fields.ForeignKeyRelation['User'] = \ - fields.ForeignKeyField('models.User', related_name='subs', to_field='id', on_delete=fields.CASCADE) + state = fields.SmallIntField( + default=1, + description='Sub state: ' + '0=deactivated, 1=activated', + ) + user: fields.ForeignKeyRelation['User'] = fields.ForeignKeyField( + 'models.User', + related_name='subs', + to_field='id', + on_delete=fields.CASCADE, + ) user_id: int # type hint stub - feed: fields.ForeignKeyRelation['Feed'] = \ - fields.ForeignKeyField('models.Feed', related_name='subs', to_field='id', on_delete=fields.CASCADE) + feed: fields.ForeignKeyRelation['Feed'] = fields.ForeignKeyField( + 'models.Feed', + related_name='subs', + to_field='id', + on_delete=fields.CASCADE, + ) feed_id: int # type hint stub title = fields.CharField(max_length=1024, null=True, description='Sub title, overriding feed title if set') tags = fields.CharField(max_length=255, null=True, description='Tags of the sub') - interval = fields.SmallIntField(null=True, description='Interval of the sub monitor task, ' - 'default interval will be applied if null') - notify = fields.SmallIntField(default=1, description='Enable notification or not? ' - '0: disable, 1=enable') - send_mode = fields.SmallIntField(default=0, description='Send mode: ' - '-1=force link, 0=auto, 1=force Telegraph, 2=force message') - length_limit = fields.SmallIntField(default=0, description='Telegraph length limit, valid when send_mode==0. ' - 'If exceed, send via Telegraph; ' - 'If is 0, send via Telegraph ' - 'when a post cannot be send in a single message') - link_preview = fields.SmallIntField(default=0, description='Enable link preview or not? ' - '0=auto, 1=force enable') - display_author = fields.SmallIntField(default=0, description='Display author or not?' - '-1=disable, 0=auto, 1=force display') - display_via = fields.SmallIntField(default=0, description='Display via or not?' - '-2=completely disable, -1=disable but display link, ' - '0=auto, 1=force display') - display_title = fields.SmallIntField(default=0, description='Display title or not?' - '-1=disable, 0=auto, 1=force display') + interval = fields.SmallIntField( + null=True, + description='Interval of the sub monitor task, ' + 'default interval will be applied if null', + ) + notify = fields.SmallIntField( + default=1, + description='Enable notification or not? ' + '0: disable, 1=enable', + ) + send_mode = fields.SmallIntField( + default=0, + description='Send mode: ' + '-1=force link, 0=auto, 1=force Telegraph, 2=force message', + ) + length_limit = fields.SmallIntField( + default=0, + description='Telegraph length limit, valid when send_mode==0. ' + 'If exceed, send via Telegraph; ' + 'If is 0, send via Telegraph when a post cannot be send in a single message', + ) + link_preview = fields.SmallIntField( + default=0, + description='Enable link preview or not? ' + '0=auto, 1=force enable', + ) + display_author = fields.SmallIntField( + default=0, + description='Display author or not?' + '-1=disable, 0=auto, 1=force display', + ) + display_via = fields.SmallIntField( + default=0, + description='Display via or not?' + '-2=completely disable, -1=disable but display link, 0=auto, 1=force display', + ) + display_title = fields.SmallIntField( + default=0, + description='Display title or not?' + '-1=disable, 0=auto, 1=force display', + ) # new field, use the de facto default value (-100) and with description unset to avoid future migration display_entry_tags = fields.SmallIntField(default=-100) - style = fields.SmallIntField(default=0, description='Style of posts: ' - '0=RSStT, 1=flowerss') - display_media = fields.SmallIntField(default=0, description='Display media or not?' - '-1=disable, 0=enable') + style = fields.SmallIntField( + default=0, + description='Style of posts: ' + '0=RSStT, 1=flowerss', + ) + display_media = fields.SmallIntField( + default=0, + description='Display media or not?' + '-1=disable, 0=enable', + ) class Meta: table = 'sub'