Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

backend: Add `org_type` to realm settings updates and events.

Lauryn Menardd2207d4ad587
diff --git a/templates/zerver/api/changelog.md b/templates/zerver/api/changelog.md
index 5fdbb079646..f1d5043fc32 100644
--- a/templates/zerver/api/changelog.md
+++ b/templates/zerver/api/changelog.md
@@ -20,6 +20,12 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 6.0
+**Feature level 128**
+
+* [`POST /register`](/api/register-queue), [`GET
+ /events`](/api/get-events), `PATCH /realm`: Added
+ `org_type` realm setting.
+
**Feature level 127**
* [`GET /user_groups`](/api/get-user-groups),[`POST
diff --git a/version.py b/version.py
index 5c8d8698f06..8b47b06a902 100644
--- a/version.py
+++ b/version.py
@@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.4.3"
# Changes should be accompanied by documentation explaining what the
# new level means in templates/zerver/api/changelog.md, as well as
# "**Changes**" entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 127
+API_FEATURE_LEVEL = 128
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.py
index 93f412942e9..62c8d13ec18 100644
--- a/zerver/actions/realm_settings.py
+++ b/zerver/actions/realm_settings.py
@@ -403,6 +403,9 @@ def do_change_realm_org_type(
extra_data={"old_value": old_value, "new_value": org_type},
)
+ event = dict(type="realm", op="update", property="org_type", value=org_type)
+ transaction.on_commit(lambda: send_event(realm, event, active_user_ids(realm.id)))
+
@transaction.atomic(savepoint=False)
def do_change_realm_plan_type(
diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py
index eea01c47ef6..4ebea066ee2 100644
--- a/zerver/lib/event_schema.py
+++ b/zerver/lib/event_schema.py
@@ -883,7 +883,7 @@ def check_realm_update(
assert "extra_data" not in event.keys()
- if prop in ["notifications_stream_id", "signup_notifications_stream_id"]:
+ if prop in ["notifications_stream_id", "signup_notifications_stream_id", "org_type"]:
assert isinstance(value, int)
return
diff --git a/zerver/lib/events.py b/zerver/lib/events.py
index fdbffee019d..198d2404198 100644
--- a/zerver/lib/events.py
+++ b/zerver/lib/events.py
@@ -286,6 +286,7 @@ def fetch_initial_state_data(
state["server_generation"] = settings.SERVER_GENERATION
state["realm_is_zephyr_mirror_realm"] = realm.is_zephyr_mirror_realm
state["development_environment"] = settings.DEVELOPMENT
+ state["realm_org_type"] = realm.org_type
state["realm_plan_type"] = realm.plan_type
state["zulip_plan_is_not_limited"] = realm.plan_type != Realm.PLAN_TYPE_LIMITED
state["upgrade_text_for_wide_organization_logo"] = str(Realm.UPGRADE_TEXT_STANDARD)
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index 4b79fb14039..b7ec0ca814d 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -3929,6 +3929,26 @@ paths:
Since these notifications are sent by the server, this field is
primarily relevant to clients containing UI for changing it.
+ org_type:
+ type: integer
+ description: |
+ The type of the organization.
+
+ - 0 = Unspecified
+ - 10 = Business
+ - 20 = Open-source project
+ - 30 = Education (non-profit)
+ - 35 = Education (for-profit)
+ - 40 = Research
+ - 50 = Event or conference
+ - 60 = Non-profit (registered)
+ - 70 = Government
+ - 80 = Political group
+ - 90 = Community
+ - 100 = Personal
+ - 1000 = Other
+
+ **Changes**: New in Zulip 6.0 (feature level 128).
plan_type:
type: integer
description: |
@@ -11463,6 +11483,28 @@ paths:
**Changes**: New in Zulip 5.0 (feature level 72). Previously,
this was called `realm_upload_quota`.
+ realm_org_type:
+ type: integer
+ description: |
+ Present if `realm` is present in `fetch_event_types`.
+
+ The type of the organization.
+
+ - 0 = Unspecified
+ - 10 = Business
+ - 20 = Open-source project
+ - 30 = Education (non-profit)
+ - 35 = Education (for-profit)
+ - 40 = Research
+ - 50 = Event or conference
+ - 60 = Non-profit (registered)
+ - 70 = Government
+ - 80 = Political group
+ - 90 = Community
+ - 100 = Personal
+ - 1000 = Other
+
+ **Changes**: New in Zulip 6.0 (feature level 128).
realm_plan_type:
type: integer
description: |
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 52e77259233..3f1b7a5dcdd 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -69,6 +69,7 @@ from zerver.actions.realm_linkifiers import (
from zerver.actions.realm_logo import do_change_logo_source
from zerver.actions.realm_playgrounds import do_add_realm_playground, do_remove_realm_playground
from zerver.actions.realm_settings import (
+ do_change_realm_org_type,
do_change_realm_plan_type,
do_deactivate_realm,
do_set_realm_authentication_methods,
@@ -1739,6 +1740,22 @@ class NormalActionsTest(BaseAction):
check_user_settings_update("events[0]", events[0])
check_update_global_notifications("events[1]", events[1], 1)
+ def test_realm_update_org_type(self) -> None:
+ realm = self.user_profile.realm
+
+ state_data = fetch_initial_state_data(self.user_profile)
+ self.assertEqual(state_data["realm_org_type"], Realm.ORG_TYPES["business"]["id"])
+
+ events = self.verify_action(
+ lambda: do_change_realm_org_type(
+ realm, Realm.ORG_TYPES["government"]["id"], acting_user=self.user_profile
+ )
+ )
+ check_realm_update("events[0]", events[0], "org_type")
+
+ state_data = fetch_initial_state_data(self.user_profile)
+ self.assertEqual(state_data["realm_org_type"], Realm.ORG_TYPES["government"]["id"])
+
def test_realm_update_plan_type(self) -> None:
realm = self.user_profile.realm
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index 5edc3d9c6ff..fe4343e06e7 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -165,6 +165,7 @@ class HomeTest(ZulipTestCase):
"realm_night_logo_url",
"realm_non_active_users",
"realm_notifications_stream_id",
+ "realm_org_type",
"realm_password_auth_enabled",
"realm_plan_type",
"realm_playgrounds",
diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py
index 54b2c22ad42..ee94d143db0 100644
--- a/zerver/tests/test_realm.py
+++ b/zerver/tests/test_realm.py
@@ -1120,6 +1120,26 @@ class RealmAPITest(ZulipTestCase):
with self.subTest(property=prop):
self.do_test_realm_update_api(prop)
+ # Not in Realm.property_types because org_type has
+ # a unique RealmAuditLog event_type.
+ def test_update_realm_org_type(self) -> None:
+ vals = [t["id"] for t in Realm.ORG_TYPES.values()]
+
+ self.set_up_db("org_type", vals[0])
+
+ for val in vals[1:]:
+ realm = self.update_with_api("org_type", val)
+ self.assertEqual(getattr(realm, "org_type"), val)
+
+ realm = self.update_with_api("org_type", vals[0])
+ self.assertEqual(getattr(realm, "org_type"), vals[0])
+
+ # Now we test an invalid org_type id.
+ invalid_org_type = 1
+ assert invalid_org_type not in vals
+ result = self.client_patch("/json/realm", {"org_type": invalid_org_type})
+ self.assert_json_error(result, "Invalid org_type")
+
def update_with_realm_default_api(self, name: str, val: Any) -> None:
if not isinstance(val, str):
val = orjson.dumps(val).decode()
diff --git a/zerver/views/realm.py b/zerver/views/realm.py
index 6887e76edbf..ed0cba7ad5f 100644
--- a/zerver/views/realm.py
+++ b/zerver/views/realm.py
@@ -1,4 +1,4 @@
-from typing import Any, Dict, Optional, Union
+from typing import Any, Dict, List, Optional, Union
from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
@@ -9,6 +9,7 @@ from django.views.decorators.http import require_safe
from confirmation.models import Confirmation, ConfirmationKeyException, get_object_from_key
from zerver.actions.create_realm import do_change_realm_subdomain
from zerver.actions.realm_settings import (
+ do_change_realm_org_type,
do_deactivate_realm,
do_reactivate_realm,
do_set_realm_authentication_methods,
@@ -40,6 +41,8 @@ from zerver.lib.validator import (
from zerver.models import Realm, RealmUserDefault, UserProfile
from zerver.views.user_settings import check_settings_values
+ORG_TYPE_IDS: List[int] = [t["id"] for t in Realm.ORG_TYPES.values()]
+
@require_realm_admin
@has_request_variables
@@ -138,6 +141,7 @@ def update_realm(
str_validator=check_capped_string(Realm.MAX_REALM_SUBDOMAIN_LENGTH),
default=None,
),
+ org_type: Optional[int] = REQ(json_validator=check_int_in(ORG_TYPE_IDS), default=None),
enable_spectator_access: Optional[bool] = REQ(json_validator=check_bool, default=None),
) -> HttpResponse:
realm = user_profile.realm
@@ -294,6 +298,10 @@ def update_realm(
do_change_realm_subdomain(realm, string_id, acting_user=user_profile)
data["realm_uri"] = realm.uri
+ if org_type is not None:
+ do_change_realm_org_type(realm, org_type, acting_user=user_profile)
+ data["org_type"] = org_type
+
return json_success(request, data)