From 0ee684a4b5597b385d7d496fd0fad666fcfa644f Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 29 Mar 2013 15:57:02 -0400 Subject: [PATCH] [schema] [manual] Add colors to the subscription model. This is preparatory for removing the StreamColor model, so we also set things up so anything changing the StreamColor model changes the Subscription model too. The manual task is to run the copy_colors.py management command after deployment to each of staging and prod. (imported from commit 1be7523ca59f5266eb2c4dc2009e31209ed49635) --- zephyr/lib/actions.py | 5 ++++- zephyr/management/commands/colorize_streams.py | 2 ++ zephyr/management/commands/copy_colors.py | 11 +++++++++++ zephyr/management/commands/reset_colors.py | 2 ++ zephyr/models.py | 3 +++ 5 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 zephyr/management/commands/copy_colors.py diff --git a/zephyr/lib/actions.py b/zephyr/lib/actions.py index 134ffb6a83..72cf692246 100644 --- a/zephyr/lib/actions.py +++ b/zephyr/lib/actions.py @@ -431,13 +431,16 @@ def set_stream_color_backend(user_profile, subscription, color=None): if not created: stream_color.color = color stream_color.save(update_fields=["color"]) + subscription.color = color + subscription.save(update_fields=["color"]) return color def do_add_subscription(user_profile, stream, no_log=False): recipient = get_recipient(Recipient.STREAM, stream.id) + color = pick_color(user_profile) (subscription, created) = Subscription.objects.get_or_create( user_profile=user_profile, recipient=recipient, - defaults={'active': True}) + defaults={'active': True, 'color': color}) did_subscribe = created if not subscription.active: did_subscribe = True diff --git a/zephyr/management/commands/colorize_streams.py b/zephyr/management/commands/colorize_streams.py index 8bfb639d94..e78ca9a2b7 100644 --- a/zephyr/management/commands/colorize_streams.py +++ b/zephyr/management/commands/colorize_streams.py @@ -63,3 +63,5 @@ class Command(BaseCommand): continue StreamColor(subscription=subscription, color=color).save() + subscription.color = color + subscription.save(update_fields=["color"]) diff --git a/zephyr/management/commands/copy_colors.py b/zephyr/management/commands/copy_colors.py new file mode 100644 index 0000000000..607db9e1f9 --- /dev/null +++ b/zephyr/management/commands/copy_colors.py @@ -0,0 +1,11 @@ +from django.core.management.base import BaseCommand +from zephyr.models import StreamColor, Subscription + +class Command(BaseCommand): + help = """Copies all colors from the StreamColor table to the Subscription table.""" + + def handle(self, *args, **options): + for stream_color in StreamColor.objects.all(): + subscription = stream_color.subscription + subscription.color = stream_color.color + subscription.save(update_fields=["color"]) diff --git a/zephyr/management/commands/reset_colors.py b/zephyr/management/commands/reset_colors.py index 93439f226a..1312d00387 100644 --- a/zephyr/management/commands/reset_colors.py +++ b/zephyr/management/commands/reset_colors.py @@ -20,3 +20,5 @@ class Command(BaseCommand): stream_color, _ = StreamColor.objects.get_or_create(subscription=sub) stream_color.color = StreamColor.DEFAULT_STREAM_COLOR stream_color.save() + sub.color = Subscription.DEFAULT_STREAM_COLOR + sub.save(update_fields=["color"]) diff --git a/zephyr/models.py b/zephyr/models.py index a72876acab..fbe01e089b 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -324,6 +324,9 @@ class Subscription(models.Model): active = models.BooleanField(default=True) in_home_view = models.NullBooleanField(default=True) + DEFAULT_STREAM_COLOR = "#c2c2c2" + color = models.CharField(max_length=10, default=DEFAULT_STREAM_COLOR) + class Meta: unique_together = ("user_profile", "recipient")