def register_remote_server( request: HttpRequest, zulip_org_id: str = REQ(str_validator=check_string_fixed_length( RemoteZulipServer.UUID_LENGTH)), zulip_org_key: str = REQ(str_validator=check_string_fixed_length( RemoteZulipServer.API_KEY_LENGTH)), hostname: str = REQ(str_validator=check_capped_string( RemoteZulipServer.HOSTNAME_MAX_LENGTH)), contact_email: str = REQ(), new_org_key: Optional[str] = REQ(str_validator=check_string_fixed_length( RemoteZulipServer.API_KEY_LENGTH), default=None), ) -> HttpResponse: # REQ validated the the field lengths, but we still need to # validate the format of these fields. try: # TODO: Ideally we'd not abuse the URL validator this way url_validator = URLValidator() url_validator("http://" + hostname) except ValidationError: raise JsonableError(_("{} is not a valid hostname").format(hostname)) try: validate_email(contact_email) except ValidationError as e: raise JsonableError(e.message) try: validate_uuid(zulip_org_id) except ValidationError as e: raise JsonableError(e.message) with transaction.atomic(): remote_server, created = RemoteZulipServer.objects.get_or_create( uuid=zulip_org_id, defaults={ "hostname": hostname, "contact_email": contact_email, "api_key": zulip_org_key, }, ) if created: RemoteZulipServerAuditLog.objects.create( event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED, server=remote_server, event_time=remote_server.last_updated, ) else: if remote_server.api_key != zulip_org_key: raise InvalidZulipServerKeyError(zulip_org_id) else: remote_server.hostname = hostname remote_server.contact_email = contact_email if new_org_key is not None: remote_server.api_key = new_org_key remote_server.save() return json_success(request, data={"created": created})
def register_remote_server( request: HttpRequest, zulip_org_id: str = REQ(str_validator=check_string_fixed_length( RemoteZulipServer.UUID_LENGTH)), zulip_org_key: str = REQ(str_validator=check_string_fixed_length( RemoteZulipServer.API_KEY_LENGTH)), hostname: str = REQ(str_validator=check_capped_string( RemoteZulipServer.HOSTNAME_MAX_LENGTH)), contact_email: str = REQ(str_validator=check_string), new_org_key: Optional[str] = REQ(str_validator=check_string_fixed_length( RemoteZulipServer.API_KEY_LENGTH), default=None), ) -> HttpResponse: # REQ validated the the field lengths, but we still need to # validate the format of these fields. try: # TODO: Ideally we'd not abuse the URL validator this way url_validator = URLValidator() url_validator('http://' + hostname) except ValidationError: raise JsonableError(_('%s is not a valid hostname') % (hostname, )) try: validate_email(contact_email) except ValidationError as e: raise JsonableError(e.message) remote_server, created = RemoteZulipServer.objects.get_or_create( uuid=zulip_org_id, defaults={ 'hostname': hostname, 'contact_email': contact_email, 'api_key': zulip_org_key }) if not created: if remote_server.api_key != zulip_org_key: raise InvalidZulipServerKeyError(zulip_org_id) else: remote_server.hostname = hostname remote_server.contact_email = contact_email if new_org_key is not None: remote_server.api_key = new_org_key remote_server.save() return json_success({'created': created})