diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 0bfbd82af5..1ace2d7374 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -54,6 +54,7 @@ from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime from zerver.lib.url_encoding import append_url_query_string from zerver.lib.utils import assert_is_not_none from zerver.models import Realm, RealmAuditLog, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_org_type_display_name, get_realm from zerver.models.users import get_system_bot from zilencer.lib.remote_counts import MissingDataError @@ -3833,15 +3834,15 @@ class RealmBillingSession(BillingSession): elif event_type is BillingSessionEventType.CUSTOMER_PLAN_CREATED: return RealmAuditLog.CUSTOMER_PLAN_CREATED elif event_type is BillingSessionEventType.DISCOUNT_CHANGED: - return RealmAuditLog.REALM_DISCOUNT_CHANGED + return AuditLogEventType.REALM_DISCOUNT_CHANGED elif event_type is BillingSessionEventType.CUSTOMER_PROPERTY_CHANGED: return RealmAuditLog.CUSTOMER_PROPERTY_CHANGED elif event_type is BillingSessionEventType.SPONSORSHIP_APPROVED: - return RealmAuditLog.REALM_SPONSORSHIP_APPROVED + return AuditLogEventType.REALM_SPONSORSHIP_APPROVED elif event_type is BillingSessionEventType.SPONSORSHIP_PENDING_STATUS_CHANGED: - return RealmAuditLog.REALM_SPONSORSHIP_PENDING_STATUS_CHANGED + return AuditLogEventType.REALM_SPONSORSHIP_PENDING_STATUS_CHANGED elif event_type is BillingSessionEventType.BILLING_MODALITY_CHANGED: - return RealmAuditLog.REALM_BILLING_MODALITY_CHANGED + return AuditLogEventType.REALM_BILLING_MODALITY_CHANGED elif event_type is BillingSessionEventType.CUSTOMER_PLAN_PROPERTY_CHANGED: return RealmAuditLog.CUSTOMER_PLAN_PROPERTY_CHANGED # nocoverage elif event_type is BillingSessionEventType.CUSTOMER_SWITCHED_FROM_MONTHLY_TO_ANNUAL_PLAN: diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index e4d8960ac9..56804e8791 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -972,7 +972,7 @@ class StripeTest(StripeTestCase): (RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now), ], ) - self.assertEqual(audit_log_entries[3][0], RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(audit_log_entries[3][0], AuditLogEventType.REALM_PLAN_TYPE_CHANGED) first_audit_log_entry = ( RealmAuditLog.objects.filter(event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED) .values_list("extra_data", flat=True) @@ -1099,10 +1099,10 @@ class StripeTest(StripeTestCase): timestamp_to_datetime(stripe_customer.created), ), (RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now), - (RealmAuditLog.REALM_PLAN_TYPE_CHANGED, self.now), + (AuditLogEventType.REALM_PLAN_TYPE_CHANGED, self.now), ], ) - self.assertEqual(audit_log_entries[2][0], RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(audit_log_entries[2][0], AuditLogEventType.REALM_PLAN_TYPE_CHANGED) first_audit_log_entry = ( RealmAuditLog.objects.filter(event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED) .values_list("extra_data", flat=True) @@ -1242,7 +1242,7 @@ class StripeTest(StripeTestCase): (RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now), ], ) - self.assertEqual(audit_log_entries[3][0], RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(audit_log_entries[3][0], AuditLogEventType.REALM_PLAN_TYPE_CHANGED) first_audit_log_entry = ( RealmAuditLog.objects.filter(event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED) .values_list("extra_data", flat=True) @@ -1382,10 +1382,10 @@ class StripeTest(StripeTestCase): timestamp_to_datetime(stripe_customer.created), ), (RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now), - (RealmAuditLog.REALM_PLAN_TYPE_CHANGED, self.now), + (AuditLogEventType.REALM_PLAN_TYPE_CHANGED, self.now), ], ) - self.assertEqual(audit_log_entries[2][0], RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(audit_log_entries[2][0], AuditLogEventType.REALM_PLAN_TYPE_CHANGED) first_audit_log_entry = ( RealmAuditLog.objects.filter(event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED) .values_list("extra_data", flat=True) @@ -1500,10 +1500,10 @@ class StripeTest(StripeTestCase): self.now, ), (RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now), - (RealmAuditLog.REALM_PLAN_TYPE_CHANGED, self.now), + (AuditLogEventType.REALM_PLAN_TYPE_CHANGED, self.now), ], ) - self.assertEqual(audit_log_entries[3][0], RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(audit_log_entries[3][0], AuditLogEventType.REALM_PLAN_TYPE_CHANGED) first_audit_log_entry = ( RealmAuditLog.objects.filter(event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED) .values_list("extra_data", flat=True) @@ -1729,10 +1729,10 @@ class StripeTest(StripeTestCase): timestamp_to_datetime(stripe_customer.created), ), (RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now), - (RealmAuditLog.REALM_PLAN_TYPE_CHANGED, self.now), + (AuditLogEventType.REALM_PLAN_TYPE_CHANGED, self.now), ], ) - self.assertEqual(audit_log_entries[2][0], RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(audit_log_entries[2][0], AuditLogEventType.REALM_PLAN_TYPE_CHANGED) first_audit_log_entry = ( RealmAuditLog.objects.filter(event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED) .values_list("extra_data", flat=True) @@ -1887,10 +1887,10 @@ class StripeTest(StripeTestCase): timestamp_to_datetime(stripe_customer.created), ), (RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now), - (RealmAuditLog.REALM_PLAN_TYPE_CHANGED, self.now), + (AuditLogEventType.REALM_PLAN_TYPE_CHANGED, self.now), ], ) - self.assertEqual(audit_log_entries[2][0], RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(audit_log_entries[2][0], AuditLogEventType.REALM_PLAN_TYPE_CHANGED) first_audit_log_entry = ( RealmAuditLog.objects.filter(event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED) .values_list("extra_data", flat=True) @@ -3003,7 +3003,7 @@ class StripeTest(StripeTestCase): (20, 20), ) realm_audit_log = RealmAuditLog.objects.latest("id") - self.assertEqual(realm_audit_log.event_type, RealmAuditLog.REALM_PLAN_TYPE_CHANGED) + self.assertEqual(realm_audit_log.event_type, AuditLogEventType.REALM_PLAN_TYPE_CHANGED) self.assertEqual(realm_audit_log.acting_user, None) # Verify that we don't write LicenseLedger rows once we've downgraded @@ -6175,7 +6175,7 @@ class TestSupportBillingHelpers(StripeTestCase): annual_discounted_price=1200, ) realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_DISCOUNT_CHANGED + event_type=AuditLogEventType.REALM_DISCOUNT_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -6249,7 +6249,7 @@ class TestSupportBillingHelpers(StripeTestCase): [invoice, _, _] = iter(stripe.Invoice.list(customer=stripe_customer_id)) self.assertEqual([4000 * self.seat_count], [item.amount for item in invoice.lines]) realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_DISCOUNT_CHANGED + event_type=AuditLogEventType.REALM_DISCOUNT_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -6463,7 +6463,7 @@ class TestSupportBillingHelpers(StripeTestCase): assert customer is not None self.assertTrue(customer.sponsorship_pending) realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_SPONSORSHIP_PENDING_STATUS_CHANGED + event_type=AuditLogEventType.REALM_SPONSORSHIP_PENDING_STATUS_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = {"sponsorship_pending": True} @@ -6488,7 +6488,7 @@ class TestSupportBillingHelpers(StripeTestCase): plan.refresh_from_db() self.assertEqual(plan.charge_automatically, True) realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_BILLING_MODALITY_CHANGED + event_type=AuditLogEventType.REALM_BILLING_MODALITY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = {"charge_automatically": plan.charge_automatically} @@ -6499,7 +6499,7 @@ class TestSupportBillingHelpers(StripeTestCase): plan.refresh_from_db() self.assertEqual(plan.charge_automatically, False) realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_BILLING_MODALITY_CHANGED + event_type=AuditLogEventType.REALM_BILLING_MODALITY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = {"charge_automatically": plan.charge_automatically} diff --git a/zerver/actions/create_realm.py b/zerver/actions/create_realm.py index c180773324..8ee283dd26 100644 --- a/zerver/actions/create_realm.py +++ b/zerver/actions/create_realm.py @@ -37,6 +37,7 @@ from zerver.models import ( ) from zerver.models.groups import SystemGroups from zerver.models.presence import PresenceSequence +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import ( CommonPolicyEnum, InviteToRealmPolicyEnum, @@ -76,7 +77,7 @@ def do_change_realm_subdomain( realm.save(update_fields=["string_id", "demo_organization_scheduled_deletion_date"]) RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_SUBDOMAIN_CHANGED, + event_type=AuditLogEventType.REALM_SUBDOMAIN_CHANGED, event_time=timezone_now(), acting_user=acting_user, extra_data={"old_subdomain": old_subdomain, "new_subdomain": new_subdomain}, diff --git a/zerver/actions/realm_icon.py b/zerver/actions/realm_icon.py index c44fc03a08..28a3d589be 100644 --- a/zerver/actions/realm_icon.py +++ b/zerver/actions/realm_icon.py @@ -3,6 +3,7 @@ from django.utils.timezone import now as timezone_now from zerver.lib.realm_icon import realm_icon_url from zerver.models import Realm, RealmAuditLog, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.users import active_user_ids from zerver.tornado.django_api import send_event_on_commit @@ -18,7 +19,7 @@ def do_change_icon_source( event_time = timezone_now() RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_ICON_SOURCE_CHANGED, + event_type=AuditLogEventType.REALM_ICON_SOURCE_CHANGED, extra_data={"icon_source": icon_source, "icon_version": realm.icon_version}, event_time=event_time, acting_user=acting_user, diff --git a/zerver/actions/realm_logo.py b/zerver/actions/realm_logo.py index 272169b528..24fc56caae 100644 --- a/zerver/actions/realm_logo.py +++ b/zerver/actions/realm_logo.py @@ -3,6 +3,7 @@ from django.utils.timezone import now as timezone_now from zerver.lib.realm_logo import get_realm_logo_data from zerver.models import Realm, RealmAuditLog, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.users import active_user_ids from zerver.tornado.django_api import send_event_on_commit @@ -22,7 +23,7 @@ def do_change_logo_source( realm.save(update_fields=["night_logo_source", "night_logo_version"]) RealmAuditLog.objects.create( - event_type=RealmAuditLog.REALM_LOGO_CHANGED, + event_type=AuditLogEventType.REALM_LOGO_CHANGED, realm=realm, event_time=timezone_now(), acting_user=acting_user, diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.py index a00fd3bdfc..f09f63d7d3 100644 --- a/zerver/actions/realm_settings.py +++ b/zerver/actions/realm_settings.py @@ -46,6 +46,7 @@ from zerver.models import ( UserProfile, ) from zerver.models.groups import SystemGroups +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_default_max_invites_for_realm_plan_type, get_realm from zerver.models.users import active_user_ids from zerver.tornado.django_api import send_event_on_commit @@ -99,7 +100,7 @@ def do_set_realm_property( event_time = timezone_now() RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time=event_time, acting_user=acting_user, extra_data={ @@ -138,7 +139,7 @@ def do_set_push_notifications_enabled_end_timestamp( event_time = timezone_now() RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time=event_time, acting_user=acting_user, extra_data={ @@ -203,7 +204,7 @@ def do_change_realm_permission_group_setting( event_time = timezone_now() RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time=event_time, acting_user=acting_user, extra_data={ @@ -350,7 +351,7 @@ def do_set_realm_authentication_methods( updated_value = realm.authentication_methods_dict() RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time=timezone_now(), acting_user=acting_user, extra_data={ @@ -409,7 +410,7 @@ def do_set_realm_stream( event_time = timezone_now() RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time=event_time, acting_user=acting_user, extra_data={ @@ -526,7 +527,7 @@ def do_deactivate_realm( event_time = timezone_now() RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_DEACTIVATED, + event_type=AuditLogEventType.REALM_DEACTIVATED, event_time=event_time, acting_user=acting_user, extra_data={ @@ -584,7 +585,7 @@ def do_reactivate_realm(realm: Realm) -> None: # know which user initiated the change. acting_user=None, realm=realm, - event_type=RealmAuditLog.REALM_REACTIVATED, + event_type=AuditLogEventType.REALM_REACTIVATED, event_time=event_time, extra_data={ RealmAuditLog.ROLE_COUNT: realm_user_count_by_role(realm), @@ -671,7 +672,7 @@ def do_scrub_realm(realm: Realm, *, acting_user: UserProfile | None) -> None: realm=realm, event_time=timezone_now(), acting_user=acting_user, - event_type=RealmAuditLog.REALM_SCRUBBED, + event_type=AuditLogEventType.REALM_SCRUBBED, ) @@ -709,7 +710,7 @@ def do_change_realm_max_invites(realm: Realm, max_invites: int, acting_user: Use realm.save(update_fields=["_max_invites"]) RealmAuditLog.objects.create( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, realm=realm, event_time=timezone_now(), acting_user=acting_user, @@ -766,7 +767,7 @@ def do_change_realm_plan_type( realm.plan_type = plan_type realm.save(update_fields=["plan_type"]) RealmAuditLog.objects.create( - event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED, + event_type=AuditLogEventType.REALM_PLAN_TYPE_CHANGED, realm=realm, event_time=timezone_now(), acting_user=acting_user, @@ -806,7 +807,7 @@ def do_send_realm_reactivation_email(realm: Realm, *, acting_user: UserProfile | RealmAuditLog.objects.create( realm=realm, acting_user=acting_user, - event_type=RealmAuditLog.REALM_REACTIVATION_EMAIL_SENT, + event_type=AuditLogEventType.REALM_REACTIVATION_EMAIL_SENT, event_time=timezone_now(), ) context = { diff --git a/zerver/lib/export.py b/zerver/lib/export.py index 497feb286f..4032747dc9 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -73,6 +73,7 @@ from zerver.models import ( UserTopic, ) from zerver.models.presence import PresenceSequence +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_realm from zerver.models.users import get_system_bot, get_user_profile_by_id @@ -2489,7 +2490,7 @@ def get_realm_exports_serialized(user: UserProfile) -> list[dict[str, Any]]: # appropriate way to express for who issued them; this requires an # API change. all_exports = RealmAuditLog.objects.filter( - realm=user.realm, event_type=RealmAuditLog.REALM_EXPORTED + realm=user.realm, event_type=AuditLogEventType.REALM_EXPORTED ).exclude(acting_user=None) exports_dict = {} for export in all_exports: diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.py index dc80d09ff4..7605fa164f 100644 --- a/zerver/lib/import_realm.py +++ b/zerver/lib/import_realm.py @@ -85,6 +85,7 @@ from zerver.models import ( ) from zerver.models.groups import SystemGroups from zerver.models.presence import PresenceSequence +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_realm from zerver.models.recipients import get_direct_message_group_hash from zerver.models.users import get_system_bot, get_user_profile_by_id @@ -1684,7 +1685,7 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea # 'zulip_update_announcements_level' is set to None by default. # Set it to the latest level to avoid receiving older update messages. is_realm_imported_from_other_zulip_server = RealmAuditLog.objects.filter( - realm=realm, event_type=RealmAuditLog.REALM_EXPORTED, acting_user=None + realm=realm, event_type=AuditLogEventType.REALM_EXPORTED, acting_user=None ).exists() if not is_realm_imported_from_other_zulip_server: send_zulip_update_announcements_to_realm( diff --git a/zerver/lib/zulip_update_announcements.py b/zerver/lib/zulip_update_announcements.py index 8de61fe672..1c9c83dc2c 100644 --- a/zerver/lib/zulip_update_announcements.py +++ b/zerver/lib/zulip_update_announcements.py @@ -15,7 +15,7 @@ from zerver.actions.message_send import ( ) from zerver.lib.message import SendMessageRequest, remove_single_newlines from zerver.lib.topic import messages_for_topic -from zerver.models.realm_audit_logs import RealmAuditLog +from zerver.models.realm_audit_logs import AuditLogEventType, RealmAuditLog from zerver.models.realms import Realm from zerver.models.users import UserProfile, get_system_bot @@ -250,7 +250,7 @@ configuration change), or [turn this feature off]({organization_settings_url}) a def get_level_none_to_initial_auditlog(realm: Realm) -> RealmAuditLog | None: return RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, extra_data__contains={ # Note: We're looking for the transition away from None, # which usually will be to level 0, but can be to a higher @@ -304,7 +304,7 @@ def send_messages_and_update_level( RealmAuditLog.objects.create( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time=timezone_now(), extra_data={ RealmAuditLog.OLD_VALUE: realm.zulip_update_announcements_level, diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py index 327a937459..97063a8cb9 100644 --- a/zerver/management/commands/export.py +++ b/zerver/management/commands/export.py @@ -12,6 +12,7 @@ from zerver.actions.realm_settings import do_deactivate_realm from zerver.lib.export import export_realm_wrapper from zerver.lib.management import ZulipBaseCommand from zerver.models import Message, Reaction, RealmAuditLog, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType class Command(ZulipBaseCommand): @@ -215,7 +216,7 @@ class Command(ZulipBaseCommand): RealmAuditLog.objects.create( acting_user=None, realm=realm, - event_type=RealmAuditLog.REALM_EXPORTED, + event_type=AuditLogEventType.REALM_EXPORTED, event_time=timezone_now(), ) diff --git a/zerver/models/realm_audit_logs.py b/zerver/models/realm_audit_logs.py index e08054733b..a24ee5bea4 100644 --- a/zerver/models/realm_audit_logs.py +++ b/zerver/models/realm_audit_logs.py @@ -36,6 +36,21 @@ class AuditLogEventType(IntEnum): USER_SETTING_CHANGED = 132 USER_DIGEST_EMAIL_CREATED = 133 + REALM_DEACTIVATED = 201 + REALM_REACTIVATED = 202 + REALM_SCRUBBED = 203 + REALM_PLAN_TYPE_CHANGED = 204 + REALM_LOGO_CHANGED = 205 + REALM_EXPORTED = 206 + REALM_PROPERTY_CHANGED = 207 + REALM_ICON_SOURCE_CHANGED = 208 + REALM_DISCOUNT_CHANGED = 209 + REALM_SPONSORSHIP_APPROVED = 210 + REALM_BILLING_MODALITY_CHANGED = 211 + REALM_REACTIVATION_EMAIL_SENT = 212 + REALM_SPONSORSHIP_PENDING_STATUS_CHANGED = 213 + REALM_SUBDOMAIN_CHANGED = 214 + class AbstractRealmAuditLog(models.Model): """Defines fields common to RealmAuditLog and RemoteRealmAuditLog.""" @@ -56,20 +71,6 @@ class AbstractRealmAuditLog(models.Model): extra_data = models.JSONField(default=dict, encoder=DjangoJSONEncoder) # Event types - REALM_DEACTIVATED = 201 - REALM_REACTIVATED = 202 - REALM_SCRUBBED = 203 - REALM_PLAN_TYPE_CHANGED = 204 - REALM_LOGO_CHANGED = 205 - REALM_EXPORTED = 206 - REALM_PROPERTY_CHANGED = 207 - REALM_ICON_SOURCE_CHANGED = 208 - REALM_DISCOUNT_CHANGED = 209 - REALM_SPONSORSHIP_APPROVED = 210 - REALM_BILLING_MODALITY_CHANGED = 211 - REALM_REACTIVATION_EMAIL_SENT = 212 - REALM_SPONSORSHIP_PENDING_STATUS_CHANGED = 213 - REALM_SUBDOMAIN_CHANGED = 214 REALM_CREATED = 215 REALM_DEFAULT_USER_SETTINGS_CHANGED = 216 REALM_ORG_TYPE_CHANGED = 217 @@ -159,8 +160,8 @@ class AbstractRealmAuditLog(models.Model): AuditLogEventType.USER_DEACTIVATED, AuditLogEventType.USER_REACTIVATED, AuditLogEventType.USER_ROLE_CHANGED, - REALM_DEACTIVATED, - REALM_REACTIVATED, + AuditLogEventType.REALM_DEACTIVATED, + AuditLogEventType.REALM_REACTIVATED, REALM_IMPORTED, ] diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index fcdc322734..cd775396b8 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -426,7 +426,7 @@ class TestRealmAuditLog(ZulipTestCase): realm, acting_user=user, deactivation_reason="owner_request", email_owners=False ) log_entry = RealmAuditLog.objects.get( - realm=realm, event_type=RealmAuditLog.REALM_DEACTIVATED, acting_user=user + realm=realm, event_type=AuditLogEventType.REALM_DEACTIVATED, acting_user=user ) extra_data = log_entry.extra_data @@ -437,7 +437,7 @@ class TestRealmAuditLog(ZulipTestCase): do_reactivate_realm(realm) log_entry = RealmAuditLog.objects.get( - realm=realm, event_type=RealmAuditLog.REALM_REACTIVATED + realm=realm, event_type=AuditLogEventType.REALM_REACTIVATED ) extra_data = log_entry.extra_data self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT]) @@ -502,7 +502,7 @@ class TestRealmAuditLog(ZulipTestCase): do_set_realm_authentication_methods(realm, auth_method_dict, acting_user=user) realm_audit_logs = RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=user, ) @@ -537,7 +537,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=user, extra_data=value_expected, @@ -557,7 +557,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=user, extra_data=value_expected, @@ -577,7 +577,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=user, extra_data={ @@ -601,7 +601,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=user, extra_data={ @@ -625,7 +625,7 @@ class TestRealmAuditLog(ZulipTestCase): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=user, extra_data={ @@ -645,7 +645,7 @@ class TestRealmAuditLog(ZulipTestCase): do_change_icon_source(realm, icon_source, acting_user=user) audit_entries = RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_ICON_SOURCE_CHANGED, + event_type=AuditLogEventType.REALM_ICON_SOURCE_CHANGED, acting_user=user, event_time__gte=test_start, ) diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 3be8b13138..acd5b4d4a0 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -3368,7 +3368,7 @@ class NormalActionsTest(BaseAction): # Now we check the deletion of the export. audit_log_entry = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_EXPORTED + event_type=AuditLogEventType.REALM_EXPORTED ).first() assert audit_log_entry is not None audit_log_entry_id = audit_log_entry.id @@ -3514,7 +3514,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=self.user_profile.realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, ).count(), @@ -3539,7 +3539,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=self.user_profile.realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, extra_data={ @@ -3584,7 +3584,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=self.user_profile.realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, ).count(), @@ -3636,7 +3636,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=self.user_profile.realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, extra_data={ @@ -3676,7 +3676,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, ).count(), @@ -3700,7 +3700,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, extra_data={ @@ -3743,7 +3743,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, extra_data={ @@ -3779,7 +3779,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, event_time__gte=now, acting_user=self.user_profile, extra_data={ @@ -3931,7 +3931,7 @@ class RealmPropertyActionTest(BaseAction): self.assertEqual( RealmAuditLog.objects.filter( realm=realm, - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED, acting_user=None, extra_data={ RealmAuditLog.OLD_VALUE: old_timestamp, diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py index d990cd9875..82acf4d88a 100644 --- a/zerver/tests/test_import_export.py +++ b/zerver/tests/test_import_export.py @@ -371,7 +371,9 @@ class RealmImportExportTest(ExportFile): public_only: bool = False, ) -> None: RealmAuditLog.objects.create( - realm=original_realm, event_type=RealmAuditLog.REALM_EXPORTED, event_time=timezone_now() + realm=original_realm, + event_type=AuditLogEventType.REALM_EXPORTED, + event_time=timezone_now(), ) self.export_realm(original_realm, exportable_user_ids, consent_message_id, public_only) @@ -995,7 +997,9 @@ class RealmImportExportTest(ExportFile): new_realm_emoji.save() RealmAuditLog.objects.create( - realm=original_realm, event_type=RealmAuditLog.REALM_EXPORTED, event_time=timezone_now() + realm=original_realm, + event_type=AuditLogEventType.REALM_EXPORTED, + event_time=timezone_now(), ) getters = self.get_realm_getters() @@ -1319,7 +1323,7 @@ class RealmImportExportTest(ExportFile): def get_realm_audit_log_event_type(r: Realm) -> set[int]: realmauditlogs = RealmAuditLog.objects.filter(realm=r).exclude( event_type__in=[ - RealmAuditLog.REALM_PLAN_TYPE_CHANGED, + AuditLogEventType.REALM_PLAN_TYPE_CHANGED, RealmAuditLog.STREAM_CREATED, RealmAuditLog.REALM_IMPORTED, ] @@ -1871,7 +1875,7 @@ class RealmImportExportTest(ExportFile): self.assertEqual(imported_realm.message_visibility_limit, 10000) self.assertTrue( RealmAuditLog.objects.filter( - realm=imported_realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED + realm=imported_realm, event_type=AuditLogEventType.REALM_PLAN_TYPE_CHANGED ).exists() ) @@ -1888,7 +1892,7 @@ class RealmImportExportTest(ExportFile): self.assertEqual(imported_realm.message_visibility_limit, None) self.assertTrue( RealmAuditLog.objects.filter( - realm=imported_realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED + realm=imported_realm, event_type=AuditLogEventType.REALM_PLAN_TYPE_CHANGED ).exists() ) diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index edf6f5ee28..5630b111b3 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -1617,7 +1617,7 @@ class AnalyticsBouncerTest(BouncerTestCase): RealmAuditLog.objects.create( realm=user.realm, modified_user=user, - event_type=RealmAuditLog.REALM_LOGO_CHANGED, + event_type=AuditLogEventType.REALM_LOGO_CHANGED, event_time=end_time, extra_data=orjson.dumps({"foo": "bar"}).decode(), ) @@ -1826,7 +1826,7 @@ class AnalyticsBouncerTest(BouncerTestCase): RealmAuditLog.objects.create( realm=user.realm, modified_user=user, - event_type=RealmAuditLog.REALM_LOGO_CHANGED, + event_type=AuditLogEventType.REALM_LOGO_CHANGED, event_time=end_time, extra_data={"data": "foo"}, ) @@ -2194,7 +2194,7 @@ class AnalyticsBouncerTest(BouncerTestCase): RealmAuditLog.objects.create( realm=user.realm, modified_user=user, - event_type=RealmAuditLog.REALM_LOGO_CHANGED, + event_type=AuditLogEventType.REALM_LOGO_CHANGED, event_time=self.TIME_ZERO, extra_data=orjson.dumps({"foo": "bar"}).decode(), ) @@ -2210,7 +2210,7 @@ class AnalyticsBouncerTest(BouncerTestCase): else: # Test that we're respecting SYNCED_BILLING_EVENTS self.assertIn(f'"event_type":{AuditLogEventType.USER_REACTIVATED}', str(args)) - self.assertNotIn(f'"event_type":{RealmAuditLog.REALM_LOGO_CHANGED}', str(args)) + self.assertNotIn(f'"event_type":{AuditLogEventType.REALM_LOGO_CHANGED}', str(args)) # Test that we're respecting REALMAUDITLOG_PUSHED_FIELDS self.assertIn("backfilled", str(args)) self.assertNotIn("modified_user", str(args)) diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index 028a96ba57..11152e0a5a 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -61,6 +61,7 @@ from zerver.models import ( UserProfile, ) from zerver.models.groups import SystemGroups +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import ( CommonMessagePolicyEnum, CommonPolicyEnum, @@ -354,7 +355,7 @@ class RealmTest(ZulipTestCase): self.assertEqual(placeholder_realm.deactivated_redirect, user.realm.url) realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_SUBDOMAIN_CHANGED, acting_user=iago + event_type=AuditLogEventType.REALM_SUBDOMAIN_CHANGED, acting_user=iago ).last() assert realm_audit_log is not None expected_extra_data = {"old_subdomain": "zulip", "new_subdomain": "newzulip"} @@ -437,7 +438,7 @@ class RealmTest(ZulipTestCase): assert log_entry is not None self.assertEqual(log_entry.realm, realm) - self.assertEqual(log_entry.event_type, RealmAuditLog.REALM_REACTIVATED) + self.assertEqual(log_entry.event_type, AuditLogEventType.REALM_REACTIVATED) log_entry_id = log_entry.id with self.assertLogs(level="WARNING") as m: @@ -575,7 +576,7 @@ class RealmTest(ZulipTestCase): self.assertFalse(realm.deactivated) self.assertEqual( RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_REACTIVATION_EMAIL_SENT, acting_user=iago + event_type=AuditLogEventType.REALM_REACTIVATION_EMAIL_SENT, acting_user=iago ).count(), 1, ) @@ -1025,7 +1026,7 @@ class RealmTest(ZulipTestCase): do_change_realm_max_invites(realm, 1, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -1041,7 +1042,7 @@ class RealmTest(ZulipTestCase): do_change_realm_max_invites(realm, 0, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = {"old_value": 1, "new_value": None, "property": "max_invites"} @@ -1056,7 +1057,7 @@ class RealmTest(ZulipTestCase): do_change_realm_max_invites(realm, 0, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -1075,7 +1076,7 @@ class RealmTest(ZulipTestCase): do_change_realm_max_invites(realm, 0, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -1094,7 +1095,7 @@ class RealmTest(ZulipTestCase): do_change_realm_max_invites(realm, 0, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -1113,7 +1114,7 @@ class RealmTest(ZulipTestCase): do_change_realm_max_invites(realm, 50000, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -1129,7 +1130,7 @@ class RealmTest(ZulipTestCase): do_change_realm_max_invites(realm, 0, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PROPERTY_CHANGED + event_type=AuditLogEventType.REALM_PROPERTY_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { @@ -1173,7 +1174,7 @@ class RealmTest(ZulipTestCase): do_change_realm_plan_type(realm, Realm.PLAN_TYPE_STANDARD, acting_user=iago) realm = get_realm("zulip") realm_audit_log = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED + event_type=AuditLogEventType.REALM_PLAN_TYPE_CHANGED ).last() assert realm_audit_log is not None expected_extra_data = { diff --git a/zerver/tests/test_realm_export.py b/zerver/tests/test_realm_export.py index 340e8406eb..f94b29df04 100644 --- a/zerver/tests/test_realm_export.py +++ b/zerver/tests/test_realm_export.py @@ -17,6 +17,7 @@ from zerver.lib.test_helpers import ( use_s3_backend, ) from zerver.models import Realm, RealmAuditLog +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.views.realm_export import export_realm @@ -63,7 +64,7 @@ class RealmExportTest(ZulipTestCase): # Get the entry and test that iago initiated it. audit_log_entry = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_EXPORTED + event_type=AuditLogEventType.REALM_EXPORTED ).first() assert audit_log_entry is not None self.assertEqual(audit_log_entry.acting_user_id, admin.id) @@ -88,7 +89,7 @@ class RealmExportTest(ZulipTestCase): self.assert_length( export_dict, RealmAuditLog.objects.filter( - realm=admin.realm, event_type=RealmAuditLog.REALM_EXPORTED + realm=admin.realm, event_type=AuditLogEventType.REALM_EXPORTED ).count(), ) @@ -163,7 +164,7 @@ class RealmExportTest(ZulipTestCase): # Get the entry and test that iago initiated it. audit_log_entry = RealmAuditLog.objects.filter( - event_type=RealmAuditLog.REALM_EXPORTED + event_type=AuditLogEventType.REALM_EXPORTED ).first() assert audit_log_entry is not None self.assertEqual(audit_log_entry.id, data["id"]) @@ -186,7 +187,7 @@ class RealmExportTest(ZulipTestCase): self.assert_length( export_dict, RealmAuditLog.objects.filter( - realm=admin.realm, event_type=RealmAuditLog.REALM_EXPORTED + realm=admin.realm, event_type=AuditLogEventType.REALM_EXPORTED ).count(), ) @@ -275,13 +276,13 @@ class RealmExportTest(ZulipTestCase): admin = self.example_user("iago") self.login_user(admin) - current_log = RealmAuditLog.objects.filter(event_type=RealmAuditLog.REALM_EXPORTED) + current_log = RealmAuditLog.objects.filter(event_type=AuditLogEventType.REALM_EXPORTED) self.assert_length(current_log, 0) exports = [ RealmAuditLog( realm=admin.realm, - event_type=RealmAuditLog.REALM_EXPORTED, + event_type=AuditLogEventType.REALM_EXPORTED, event_time=timezone_now(), ) for i in range(5) diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py index 13e430067e..798aea05eb 100644 --- a/zerver/tests/test_slack_importer.py +++ b/zerver/tests/test_slack_importer.py @@ -50,6 +50,7 @@ from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_helpers import read_test_image_file from zerver.lib.topic import EXPORT_TOPIC_NAME from zerver.models import Message, Realm, RealmAuditLog, Recipient, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType from zerver.models.realms import get_realm @@ -1355,8 +1356,8 @@ class SlackImporter(ZulipTestCase): realmauditlog_event_type, { RealmAuditLog.SUBSCRIPTION_CREATED, - RealmAuditLog.REALM_PLAN_TYPE_CHANGED, - RealmAuditLog.REALM_PROPERTY_CHANGED, + AuditLogEventType.REALM_PLAN_TYPE_CHANGED, + AuditLogEventType.REALM_PROPERTY_CHANGED, RealmAuditLog.REALM_CREATED, RealmAuditLog.REALM_IMPORTED, RealmAuditLog.USER_GROUP_CREATED, diff --git a/zerver/views/realm_export.py b/zerver/views/realm_export.py index 28614e1140..e7f5679fca 100644 --- a/zerver/views/realm_export.py +++ b/zerver/views/realm_export.py @@ -14,13 +14,14 @@ from zerver.lib.export import get_realm_exports_serialized from zerver.lib.queue import queue_json_publish from zerver.lib.response import json_success from zerver.models import RealmAuditLog, UserProfile +from zerver.models.realm_audit_logs import AuditLogEventType @transaction.atomic(durable=True) @require_realm_admin def export_realm(request: HttpRequest, user: UserProfile) -> HttpResponse: # Currently only supports public-data-only exports. - event_type = RealmAuditLog.REALM_EXPORTED + event_type = AuditLogEventType.REALM_EXPORTED event_time = timezone_now() realm = user.realm EXPORT_LIMIT = 5 @@ -97,7 +98,7 @@ def get_realm_exports(request: HttpRequest, user: UserProfile) -> HttpResponse: def delete_realm_export(request: HttpRequest, user: UserProfile, export_id: int) -> HttpResponse: try: audit_log_entry = RealmAuditLog.objects.get( - id=export_id, realm=user.realm, event_type=RealmAuditLog.REALM_EXPORTED + id=export_id, realm=user.realm, event_type=AuditLogEventType.REALM_EXPORTED ) except RealmAuditLog.DoesNotExist: raise JsonableError(_("Invalid data export ID"))