From 8f145e03cf501b862e59885571ec55bdce2763a2 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Tue, 11 Oct 2016 20:25:20 -0700 Subject: [PATCH] views: split events_register.py from main views file. --- zerver/tests/test_events.py | 2 +- zerver/views/__init__.py | 43 ++------------------------- zerver/views/events_register.py | 51 +++++++++++++++++++++++++++++++++ zproject/urls.py | 2 +- 4 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 zerver/views/events_register.py diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 526fdc3d24..89960b2d45 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -58,7 +58,7 @@ from zerver.lib.validator import ( equals, check_none_or, Validator ) -from zerver.views import _default_all_public_streams, _default_narrow +from zerver.views.events_register import _default_all_public_streams, _default_narrow from zerver.tornadoviews import get_events_backend diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 0e0ff29653..151d5701a4 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import -from typing import Any, List, Dict, Optional, Tuple, Iterable, Sequence +from typing import Any, List, Dict, Optional from django.utils import translation from django.utils.translation import ugettext as _ @@ -35,7 +35,7 @@ from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, ToSF CreateUserForm from zerver.lib.actions import is_inactive from django_auth_ldap.backend import LDAPBackend, _LDAPUser -from zerver.lib.validator import check_string, check_list, check_bool +from zerver.lib.validator import check_string, check_list from zerver.decorator import require_post, authenticated_json_post_view, \ has_request_variables, \ JsonableError, get_user_profile_by_email, REQ, \ @@ -654,45 +654,6 @@ def is_buggy_ua(agent): return ("Humbug Desktop/" in agent or "Zulip Desktop/" in agent or "ZulipDesktop/" in agent) and \ "Mac" not in agent -# Does not need to be authenticated because it's called from rest_dispatch -@has_request_variables -def api_events_register(request, user_profile, - apply_markdown=REQ(default=False, validator=check_bool), - all_public_streams=REQ(default=None, validator=check_bool)): - # type: (HttpRequest, UserProfile, bool, Optional[bool]) -> HttpResponse - return events_register_backend(request, user_profile, - apply_markdown=apply_markdown, - all_public_streams=all_public_streams) - -def _default_all_public_streams(user_profile, all_public_streams): - # type: (UserProfile, Optional[bool]) -> bool - if all_public_streams is not None: - return all_public_streams - else: - return user_profile.default_all_public_streams - -def _default_narrow(user_profile, narrow): - # type: (UserProfile, Iterable[Sequence[text_type]]) -> Iterable[Sequence[text_type]] - default_stream = user_profile.default_events_register_stream - if not narrow and user_profile.default_events_register_stream is not None: - narrow = [['stream', default_stream.name]] - return narrow - -@has_request_variables -def events_register_backend(request, user_profile, apply_markdown=True, - all_public_streams=None, - event_types=REQ(validator=check_list(check_string), default=None), - narrow=REQ(validator=check_list(check_list(check_string, length=2)), default=[]), - queue_lifespan_secs=REQ(converter=int, default=0)): - # type: (HttpRequest, UserProfile, bool, Optional[bool], Optional[Iterable[str]], Iterable[Sequence[text_type]], int) -> HttpResponse - all_public_streams = _default_all_public_streams(user_profile, all_public_streams) - narrow = _default_narrow(user_profile, narrow) - - ret = do_events_register(user_profile, request.client, apply_markdown, - event_types, queue_lifespan_secs, all_public_streams, - narrow=narrow) - return json_success(ret) - @authenticated_json_post_view @has_request_variables def json_set_muted_topics(request, user_profile, diff --git a/zerver/views/events_register.py b/zerver/views/events_register.py new file mode 100644 index 0000000000..e88d978ae9 --- /dev/null +++ b/zerver/views/events_register.py @@ -0,0 +1,51 @@ +from __future__ import absolute_import + +from django.http import HttpRequest, HttpResponse +from six import text_type +from typing import Iterable, Optional, Sequence + +from zerver.lib.actions import do_events_register +from zerver.lib.request import REQ, has_request_variables +from zerver.lib.response import json_success +from zerver.lib.validator import check_string, check_list, check_bool +from zerver.models import UserProfile + +def _default_all_public_streams(user_profile, all_public_streams): + # type: (UserProfile, Optional[bool]) -> bool + if all_public_streams is not None: + return all_public_streams + else: + return user_profile.default_all_public_streams + +def _default_narrow(user_profile, narrow): + # type: (UserProfile, Iterable[Sequence[text_type]]) -> Iterable[Sequence[text_type]] + default_stream = user_profile.default_events_register_stream + if not narrow and user_profile.default_events_register_stream is not None: + narrow = [['stream', default_stream.name]] + return narrow + +# Does not need to be authenticated because it's called from rest_dispatch +@has_request_variables +def api_events_register(request, user_profile, + apply_markdown=REQ(default=False, validator=check_bool), + all_public_streams=REQ(default=None, validator=check_bool)): + # type: (HttpRequest, UserProfile, bool, Optional[bool]) -> HttpResponse + return events_register_backend(request, user_profile, + apply_markdown=apply_markdown, + all_public_streams=all_public_streams) + +@has_request_variables +def events_register_backend(request, user_profile, apply_markdown=True, + all_public_streams=None, + event_types=REQ(validator=check_list(check_string), default=None), + narrow=REQ(validator=check_list(check_list(check_string, length=2)), default=[]), + queue_lifespan_secs=REQ(converter=int, default=0)): + # type: (HttpRequest, UserProfile, bool, Optional[bool], Optional[Iterable[str]], Iterable[Sequence[text_type]], int) -> HttpResponse + all_public_streams = _default_all_public_streams(user_profile, all_public_streams) + narrow = _default_narrow(user_profile, narrow) + + ret = do_events_register(user_profile, request.client, apply_markdown, + event_types, queue_lifespan_secs, all_public_streams, + narrow=narrow) + return json_success(ret) + diff --git a/zproject/urls.py b/zproject/urls.py index 52f0ce3a63..2fc5a48c15 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -225,7 +225,7 @@ v1_api_and_json_patterns = [ # used to register for an event queue in tornado url(r'^register$', 'zerver.lib.rest.rest_dispatch', - {'POST': 'zerver.views.api_events_register'}), + {'POST': 'zerver.views.events_register.api_events_register'}), # events -> zerver.tornadoviews url(r'^events$', 'zerver.lib.rest.rest_dispatch',