From 2ff1527be8e7828cfba703e2858f9fa064c56501 Mon Sep 17 00:00:00 2001 From: Abhishek-Balaji Date: Sun, 26 Apr 2020 23:23:53 +0530 Subject: [PATCH] models: Switch from NullBooleanField to BooleanField. In Django 2.1, the preferred way to express a nullable BooleanField changed from NullBooleanField to passing null=True to BooleanField. This updates our codebase to use the preferred API. Tweaked by tabbott to update the linter rules. The migration is a noop for Django accounting only. Part of #11341. --- tools/linter_lib/custom_check.py | 2 +- .../0274_nullbooleanfield_to_booleanfield.py | 53 +++++++++++++++++++ zerver/models.py | 16 +++--- 3 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 zerver/migrations/0274_nullbooleanfield_to_booleanfield.py diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py index 06eb89a020..9027ce94c0 100644 --- a/tools/linter_lib/custom_check.py +++ b/tools/linter_lib/custom_check.py @@ -444,7 +444,7 @@ python_rules = RuleList( 'bad_lines': ['desc = models.CharField(null=True) # type: Text', 'stream = models.ForeignKey(Stream, null=True, on_delete=CASCADE) # type: Stream'], }, - {'pattern': r' = models[.](?!NullBoolean).*\) # type: Optional', # Optional tag, except NullBoolean(Field) + {'pattern': r' = models[.].* # type: Optional', # Optional tag 'exclude_pattern': 'null=True', 'include_only': {"zerver/models.py"}, 'description': 'Model variable annotated with Optional but variable does not have null=true.', diff --git a/zerver/migrations/0274_nullbooleanfield_to_booleanfield.py b/zerver/migrations/0274_nullbooleanfield_to_booleanfield.py new file mode 100644 index 0000000000..5f03b8b6a1 --- /dev/null +++ b/zerver/migrations/0274_nullbooleanfield_to_booleanfield.py @@ -0,0 +1,53 @@ +# Generated by Django 2.2.12 on 2020-04-26 17:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('zerver', '0273_migrate_old_bot_messages'), + ] + + operations = [ + migrations.AlterField( + model_name='stream', + name='invite_only', + field=models.BooleanField(default=False, null=True), + ), + migrations.AlterField( + model_name='subscription', + name='audible_notifications', + field=models.BooleanField(default=None, null=True), + ), + migrations.AlterField( + model_name='subscription', + name='desktop_notifications', + field=models.BooleanField(default=None, null=True), + ), + migrations.AlterField( + model_name='subscription', + name='email_notifications', + field=models.BooleanField(default=None, null=True), + ), + migrations.AlterField( + model_name='subscription', + name='is_muted', + field=models.BooleanField(default=False, null=True), + ), + migrations.AlterField( + model_name='subscription', + name='push_notifications', + field=models.BooleanField(default=None, null=True), + ), + migrations.AlterField( + model_name='subscription', + name='wildcard_mentions_notify', + field=models.BooleanField(default=None, null=True), + ), + migrations.AlterField( + model_name='userprofile', + name='enter_sends', + field=models.BooleanField(default=False, null=True), + ), + ] diff --git a/zerver/models.py b/zerver/models.py index 71ebb70e8d..8a25f2f06f 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -944,7 +944,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin): default_all_public_streams: bool = models.BooleanField(default=False) # UI vars - enter_sends: Optional[bool] = models.NullBooleanField(default=False) + enter_sends: Optional[bool] = models.BooleanField(null=True, default=False) left_side_userlist: bool = models.BooleanField(default=False) # display settings @@ -1365,7 +1365,7 @@ class Stream(models.Model): # Foreign key to the Recipient object for STREAM type messages to this stream. recipient = models.ForeignKey(Recipient, null=True, on_delete=models.SET_NULL) - invite_only: Optional[bool] = models.NullBooleanField(default=False) + invite_only: Optional[bool] = models.BooleanField(null=True, default=False) history_public_to_subscribers: bool = models.BooleanField(default=False) # Whether this stream's content should be published by the web-public archive features @@ -2088,7 +2088,7 @@ class Subscription(models.Model): active: bool = models.BooleanField(default=True) # Whether this user had muted this stream. - is_muted: Optional[bool] = models.NullBooleanField(default=False) + is_muted: Optional[bool] = models.BooleanField(null=True, default=False) DEFAULT_STREAM_COLOR = "#c2c2c2" color: str = models.CharField(max_length=10, default=DEFAULT_STREAM_COLOR) @@ -2097,11 +2097,11 @@ class Subscription(models.Model): # These fields are stream-level overrides for the user's default # configuration for notification, configured in UserProfile. The # default, None, means we just inherit the user-level default. - desktop_notifications: Optional[bool] = models.NullBooleanField(default=None) - audible_notifications: Optional[bool] = models.NullBooleanField(default=None) - push_notifications: Optional[bool] = models.NullBooleanField(default=None) - email_notifications: Optional[bool] = models.NullBooleanField(default=None) - wildcard_mentions_notify: Optional[bool] = models.NullBooleanField(default=None) + desktop_notifications: Optional[bool] = models.BooleanField(null=True, default=None) + audible_notifications: Optional[bool] = models.BooleanField(null=True, default=None) + push_notifications: Optional[bool] = models.BooleanField(null=True, default=None) + email_notifications: Optional[bool] = models.BooleanField(null=True, default=None) + wildcard_mentions_notify: Optional[bool] = models.BooleanField(null=True, default=None) class Meta: unique_together = ("user_profile", "recipient")