def handle(self, *args: Any, **options: str) -> None: if options["entire_server"]: users = UserProfile.objects.filter(is_active=True, is_bot=False, is_mirror_dummy=False, realm__deactivated=False) elif options["marketing"]: # Marketing email sent at most once to each email address for users # who are recently active (!long_term_idle) users of the product. users = UserProfile.objects.filter( is_active=True, is_bot=False, is_mirror_dummy=False, realm__deactivated=False, enable_marketing_emails=True, long_term_idle=False, ).distinct("delivery_email") elif options["all_sponsored_org_admins"]: # Sends at most one copy to each email address, even if it # is an administrator in several organizations. sponsored_realms = Realm.objects.filter( plan_type=Realm.STANDARD_FREE, deactivated=False) admin_roles = [ UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER ] users = UserProfile.objects.filter( is_active=True, is_bot=False, is_mirror_dummy=False, role__in=admin_roles, realm__deactivated=False, realm__in=sponsored_realms, ).distinct("delivery_email") else: realm = self.get_realm(options) try: users = self.get_users(options, realm, is_bot=False) except CommandError as error: if str( error ) == "You have to pass either -u/--users or -a/--all-users.": raise CommandError( "You have to pass -u/--users or -a/--all-users or --entire-server." ) raise error # Only email users who've agreed to the terms of service. if settings.TOS_VERSION is not None: # We need to do a new query because the `get_users` path # passes us a list rather than a QuerySet. users = (UserProfile.objects.select_related().filter( id__in=[u.id for u in users]).exclude(tos_version=None)) send_custom_email(users, options) if options["dry_run"]: print("Would send the above email to:") for user in users: print(f" {user.delivery_email} ({user.realm.string_id})")
def handle(self, *args: Any, **options: str) -> None: if options["entire_server"]: users = UserProfile.objects.filter(is_active=True, is_bot=False, is_mirror_dummy=False) else: realm = self.get_realm(options) try: users = self.get_users(options, realm, is_bot=False) except CommandError as error: if str(error) == "You have to pass either -u/--users or -a/--all-users.": raise CommandError("You have to pass -u/--users or -a/--all-users or --entire-server.") raise error send_custom_email(users, options)
def handle(self, *args: Any, **options: str) -> None: if options["entire_server"]: users = UserProfile.objects.filter(is_active=True, is_bot=False, is_mirror_dummy=False, realm__deactivated=False) elif options["all_sponsored_org_admins"]: # Sends at most one copy to each email address, even if it # is an administrator in several organizations. sponsored_realms = Realm.objects.filter( plan_type=Realm.STANDARD_FREE, deactivated=False) admin_roles = [ UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER ] users = UserProfile.objects.filter( is_active=True, is_bot=False, is_mirror_dummy=False, role__in=admin_roles, realm__deactivated=False, realm__in=sponsored_realms, ).distinct("delivery_email") else: realm = self.get_realm(options) try: users = self.get_users(options, realm, is_bot=False) except CommandError as error: if str( error ) == "You have to pass either -u/--users or -a/--all-users.": raise CommandError( "You have to pass -u/--users or -a/--all-users or --entire-server." ) raise error # Only email users who've agreed to the terms of service. if settings.TOS_VERSION is not None: users = users.exclude(tos_version=None) send_custom_email(users, options) if options["dry_run"]: print("Would send the above email to:") for user in users: print(f" {user.delivery_email} ({user.realm.string_id})")