2021-08-13 20:37:15 +02:00
|
|
|
import random
|
|
|
|
import string
|
2022-07-01 02:37:10 +02:00
|
|
|
from typing import TYPE_CHECKING, Any, cast
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
from django.conf import settings
|
2022-05-12 08:07:21 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
|
2019-03-13 18:26:01 +01:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
from confirmation.models import Confirmation, create_confirmation_link
|
2022-01-23 20:37:40 +01:00
|
|
|
from zerver.context_processors import get_realm_from_request
|
2019-01-11 20:56:22 +01:00
|
|
|
from zerver.lib.response import json_success
|
2021-06-24 20:05:06 +02:00
|
|
|
from zerver.models import Realm, UserProfile
|
2019-03-13 18:26:01 +01:00
|
|
|
from zerver.views.auth import create_preregistration_user
|
|
|
|
from zerver.views.registration import accounts_register
|
|
|
|
|
2022-07-01 02:37:10 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from django.http.request import _ImmutableQueryDict
|
2019-01-11 20:56:22 +01:00
|
|
|
|
2023-02-02 04:35:24 +01:00
|
|
|
|
2022-07-01 02:35:27 +02:00
|
|
|
# This is used only by the Puppeteer test in realm-creation.ts.
|
2019-01-11 20:56:22 +01:00
|
|
|
def confirmation_key(request: HttpRequest) -> HttpResponse:
|
2022-07-01 02:35:27 +02:00
|
|
|
return json_success(request, data=request.session["confirmation_key"])
|
2019-03-13 18:26:01 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
def modify_postdata(request: HttpRequest, **kwargs: Any) -> None:
|
2022-07-01 02:37:10 +02:00
|
|
|
new_post = request.POST.copy()
|
2019-03-13 18:26:01 +01:00
|
|
|
for key, value in kwargs.items():
|
2022-07-01 02:37:10 +02:00
|
|
|
new_post[key] = value
|
|
|
|
new_post._mutable = False
|
|
|
|
request.POST = cast("_ImmutableQueryDict", new_post)
|
2019-03-13 18:26:01 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-08-13 20:37:15 +02:00
|
|
|
def generate_demo_realm_name() -> str:
|
|
|
|
letters = "".join(random.SystemRandom().choice(string.ascii_lowercase) for _ in range(4))
|
|
|
|
digits = "".join(random.SystemRandom().choice(string.digits) for _ in range(4))
|
|
|
|
demo_realm_name = f"demo-{letters}{digits}"
|
|
|
|
return demo_realm_name
|
|
|
|
|
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
@csrf_exempt
|
|
|
|
def register_development_user(request: HttpRequest) -> HttpResponse:
|
2022-05-12 08:07:21 +02:00
|
|
|
realm = get_realm_from_request(request)
|
|
|
|
if realm is None:
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
f"{settings.EXTERNAL_URI_SCHEME}{settings.REALM_HOSTS['zulip']}/devtools/register_user/",
|
|
|
|
status=307,
|
|
|
|
)
|
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
count = UserProfile.objects.count()
|
2021-02-12 08:20:45 +01:00
|
|
|
name = f"user-{count}"
|
|
|
|
email = f"{name}@zulip.com"
|
2021-02-12 08:19:30 +01:00
|
|
|
prereg = create_preregistration_user(
|
2022-01-23 20:37:40 +01:00
|
|
|
email, realm, realm_creation=False, password_required=False
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
activation_url = create_confirmation_link(prereg, Confirmation.USER_REGISTRATION)
|
2021-02-12 08:20:45 +01:00
|
|
|
key = activation_url.split("/")[-1]
|
2020-08-11 01:47:44 +02:00
|
|
|
# Need to add test data to POST request as it doesn't originally contain the required parameters
|
2021-02-12 08:20:45 +01:00
|
|
|
modify_postdata(request, key=key, full_name=name, password="test", terms="true")
|
2019-03-13 18:26:01 +01:00
|
|
|
|
|
|
|
return accounts_register(request)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-13 18:26:01 +01:00
|
|
|
@csrf_exempt
|
|
|
|
def register_development_realm(request: HttpRequest) -> HttpResponse:
|
|
|
|
count = UserProfile.objects.count()
|
2021-02-12 08:20:45 +01:00
|
|
|
name = f"user-{count}"
|
|
|
|
email = f"{name}@zulip.com"
|
|
|
|
realm_name = f"realm-{count}"
|
2021-06-24 20:05:06 +02:00
|
|
|
realm_type = Realm.ORG_TYPES["business"]["id"]
|
2022-01-23 20:37:40 +01:00
|
|
|
prereg = create_preregistration_user(email, None, realm_creation=True, password_required=False)
|
2021-02-12 08:19:30 +01:00
|
|
|
activation_url = create_confirmation_link(prereg, Confirmation.REALM_CREATION)
|
2021-02-12 08:20:45 +01:00
|
|
|
key = activation_url.split("/")[-1]
|
2020-08-11 01:47:44 +02:00
|
|
|
# Need to add test data to POST request as it doesn't originally contain the required parameters
|
2021-02-12 08:19:30 +01:00
|
|
|
modify_postdata(
|
|
|
|
request,
|
|
|
|
key=key,
|
|
|
|
realm_name=realm_name,
|
2021-06-24 20:05:06 +02:00
|
|
|
realm_type=realm_type,
|
2021-02-12 08:19:30 +01:00
|
|
|
full_name=name,
|
2021-02-12 08:20:45 +01:00
|
|
|
password="test",
|
2021-02-12 08:19:30 +01:00
|
|
|
realm_subdomain=realm_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
terms="true",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-03-13 18:26:01 +01:00
|
|
|
|
|
|
|
return accounts_register(request)
|
2021-08-13 20:37:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def register_demo_development_realm(request: HttpRequest) -> HttpResponse:
|
|
|
|
count = UserProfile.objects.count()
|
|
|
|
name = f"user-{count}"
|
|
|
|
email = f"{name}@zulip.com"
|
|
|
|
realm_name = generate_demo_realm_name()
|
|
|
|
realm_type = Realm.ORG_TYPES["business"]["id"]
|
2022-01-23 20:37:40 +01:00
|
|
|
prereg = create_preregistration_user(email, None, realm_creation=True, password_required=False)
|
2021-08-13 20:37:15 +02:00
|
|
|
activation_url = create_confirmation_link(prereg, Confirmation.REALM_CREATION)
|
|
|
|
key = activation_url.split("/")[-1]
|
|
|
|
# Need to add test data to POST request as it doesn't originally contain the required parameters
|
|
|
|
modify_postdata(
|
|
|
|
request,
|
|
|
|
key=key,
|
|
|
|
realm_name=realm_name,
|
|
|
|
realm_type=realm_type,
|
|
|
|
full_name=name,
|
|
|
|
password="test",
|
|
|
|
realm_subdomain=realm_name,
|
|
|
|
terms="true",
|
|
|
|
is_demo_organization="true",
|
|
|
|
)
|
|
|
|
|
|
|
|
return accounts_register(request)
|