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.
This commit is contained in:
Abhishek-Balaji 2020-04-26 23:23:53 +05:30 committed by Tim Abbott
parent c11de1e3a7
commit 2ff1527be8
3 changed files with 62 additions and 9 deletions

View File

@ -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.',

View File

@ -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),
),
]

View File

@ -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")