from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import NoArgsCommand, CommandError

class Command(NoArgsCommand):
    help = "Set the user profile settings of every user to the defaults"

    def handle_noargs(self, **options):
        for u in User.objects.all():
            up = u.userprofile
            # Expect user profiles to be there and add all default settings
            up.inverse_mouse_wheel = settings.PROFILE_DEFAULT_INVERSE_MOUSE_WHEEL
            up.show_text_label_tool = settings.PROFILE_SHOW_TEXT_LABEL_TOOL
            up.show_tagging_tool = settings.PROFILE_SHOW_TAGGING_TOOL
            up.show_cropping_tool = settings.PROFILE_SHOW_CROPPING_TOOL
            up.show_segmentation_tool = settings.PROFILE_SHOW_SEGMENTATION_TOOL
            up.show_tracing_tool = settings.PROFILE_SHOW_TRACING_TOOL
            # Save the changes
            up.save()

from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import NoArgsCommand, CommandError

from optparse import make_option

class Command(NoArgsCommand):
    help = "Set the user profile settings of every user to the defaults"

    option_list = NoArgsCommand.option_list + (
        make_option('--update-anon-user', dest='update-anon-user',
            default=False, action='store_true',
            help='Update also the profile of the anonymous user'),
        )

    def handle_noargs(self, **options):
        update_anon_user = 'update-anon-user' in options
        for u in User.objects.all():
            # Ignore the anonymous user by default
            if u.id == settings.ANONYMOUS_USER_ID and not update_anon_user:
                continue
            up = u.userprofile
            # Expect user profiles to be there and add all default settings
            up.inverse_mouse_wheel = settings.PROFILE_DEFAULT_INVERSE_MOUSE_WHEEL
            up.show_text_label_tool = settings.PROFILE_SHOW_TEXT_LABEL_TOOL
            up.show_tagging_tool = settings.PROFILE_SHOW_TAGGING_TOOL
            up.show_cropping_tool = settings.PROFILE_SHOW_CROPPING_TOOL
            up.show_segmentation_tool = settings.PROFILE_SHOW_SEGMENTATION_TOOL
            up.show_tracing_tool = settings.PROFILE_SHOW_TRACING_TOOL
            # Save the changes
            up.save()

Add explicit anonymous user update parameter to user profile command
Add explicit anonymous user update parameter to user profile command

The set_user_profiles_to_default management command should only update
the anonymous user if explicitely stated. This commit adds the
'--update-anon-user' switch to do let the user state that also the
anonymous user's profile should be updated with the current default
values.

  from django.conf import settings
  from django.contrib.auth.models import User
  from django.core.management.base import NoArgsCommand, CommandError
  
+ from optparse import make_option
+ 
  class Command(NoArgsCommand):
      help = "Set the user profile settings of every user to the defaults"
  
+     option_list = NoArgsCommand.option_list + (
+         make_option('--update-anon-user', dest='update-anon-user',
+             default=False, action='store_true',
+             help='Update also the profile of the anonymous user'),
+         )
+ 
      def handle_noargs(self, **options):
+         update_anon_user = 'update-anon-user' in options
          for u in User.objects.all():
+             # Ignore the anonymous user by default
+             if u.id == settings.ANONYMOUS_USER_ID and not update_anon_user:
+                 continue
              up = u.userprofile
              # Expect user profiles to be there and add all default settings
              up.inverse_mouse_wheel = settings.PROFILE_DEFAULT_INVERSE_MOUSE_WHEEL
              up.show_text_label_tool = settings.PROFILE_SHOW_TEXT_LABEL_TOOL
              up.show_tagging_tool = settings.PROFILE_SHOW_TAGGING_TOOL
              up.show_cropping_tool = settings.PROFILE_SHOW_CROPPING_TOOL
              up.show_segmentation_tool = settings.PROFILE_SHOW_SEGMENTATION_TOOL
              up.show_tracing_tool = settings.PROFILE_SHOW_TRACING_TOOL
              # Save the changes
              up.save()
  
Add explicit anonymous user update parameter to user profile command
## Code Before:
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import NoArgsCommand, CommandError

class Command(NoArgsCommand):
    help = "Set the user profile settings of every user to the defaults"

    def handle_noargs(self, **options):
        for u in User.objects.all():
            up = u.userprofile
            # Expect user profiles to be there and add all default settings
            up.inverse_mouse_wheel = settings.PROFILE_DEFAULT_INVERSE_MOUSE_WHEEL
            up.show_text_label_tool = settings.PROFILE_SHOW_TEXT_LABEL_TOOL
            up.show_tagging_tool = settings.PROFILE_SHOW_TAGGING_TOOL
            up.show_cropping_tool = settings.PROFILE_SHOW_CROPPING_TOOL
            up.show_segmentation_tool = settings.PROFILE_SHOW_SEGMENTATION_TOOL
            up.show_tracing_tool = settings.PROFILE_SHOW_TRACING_TOOL
            # Save the changes
            up.save()

## Instruction:
Add explicit anonymous user update parameter to user profile command
## Code After:
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import NoArgsCommand, CommandError

from optparse import make_option

class Command(NoArgsCommand):
    help = "Set the user profile settings of every user to the defaults"

    option_list = NoArgsCommand.option_list + (
        make_option('--update-anon-user', dest='update-anon-user',
            default=False, action='store_true',
            help='Update also the profile of the anonymous user'),
        )

    def handle_noargs(self, **options):
        update_anon_user = 'update-anon-user' in options
        for u in User.objects.all():
            # Ignore the anonymous user by default
            if u.id == settings.ANONYMOUS_USER_ID and not update_anon_user:
                continue
            up = u.userprofile
            # Expect user profiles to be there and add all default settings
            up.inverse_mouse_wheel = settings.PROFILE_DEFAULT_INVERSE_MOUSE_WHEEL
            up.show_text_label_tool = settings.PROFILE_SHOW_TEXT_LABEL_TOOL
            up.show_tagging_tool = settings.PROFILE_SHOW_TAGGING_TOOL
            up.show_cropping_tool = settings.PROFILE_SHOW_CROPPING_TOOL
            up.show_segmentation_tool = settings.PROFILE_SHOW_SEGMENTATION_TOOL
            up.show_tracing_tool = settings.PROFILE_SHOW_TRACING_TOOL
            # Save the changes
            up.save()
