Annotate zilencer/views.py.

This commit is contained in:
pxhanus 2016-06-05 02:10:13 +00:00 committed by Tim Abbott
parent 2bcf313a85
commit e247d9783f
1 changed files with 13 additions and 2 deletions

View File

@ -1,11 +1,13 @@
from __future__ import absolute_import from __future__ import absolute_import
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.http import HttpResponse from django.http import HttpResponse, HttpRequest
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.views import login as django_login_page from django.contrib.auth.views import login as django_login_page
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from zilencer.models import Deployment
from zerver.decorator import has_request_variables, REQ from zerver.decorator import has_request_variables, REQ
from zerver.lib.actions import internal_send_message from zerver.lib.actions import internal_send_message
from zerver.lib.redis_utils import get_redis_client from zerver.lib.redis_utils import get_redis_client
@ -13,16 +15,19 @@ from zerver.lib.response import json_success, json_error, json_response, json_me
from zerver.lib.rest import rest_dispatch as _rest_dispatch from zerver.lib.rest import rest_dispatch as _rest_dispatch
from zerver.lib.validator import check_dict from zerver.lib.validator import check_dict
from zerver.models import get_realm, get_user_profile_by_email, resolve_email_to_domain, \ from zerver.models import get_realm, get_user_profile_by_email, resolve_email_to_domain, \
UserProfile UserProfile, Realm
from .error_notify import notify_server_error, notify_browser_error from .error_notify import notify_server_error, notify_browser_error
from django.conf import settings from django.conf import settings
import time import time
from typing import Dict, Optional, Any
rest_dispatch = csrf_exempt((lambda request, *args, **kwargs: _rest_dispatch(request, globals(), *args, **kwargs))) rest_dispatch = csrf_exempt((lambda request, *args, **kwargs: _rest_dispatch(request, globals(), *args, **kwargs)))
client = get_redis_client() client = get_redis_client()
def has_enough_time_expired_since_last_message(sender_email, min_delay): def has_enough_time_expired_since_last_message(sender_email, min_delay):
# type: (str, float) -> bool
# This function returns a boolean, but it also has the side effect # This function returns a boolean, but it also has the side effect
# of noting that a new message was received. # of noting that a new message was received.
key = 'zilencer:feedback:%s' % (sender_email,) key = 'zilencer:feedback:%s' % (sender_email,)
@ -34,6 +39,7 @@ def has_enough_time_expired_since_last_message(sender_email, min_delay):
return delay > min_delay return delay > min_delay
def get_ticket_number(): def get_ticket_number():
# type: () -> int
fn = '/var/tmp/.feedback-bot-ticket-number' fn = '/var/tmp/.feedback-bot-ticket-number'
try: try:
ticket_number = int(open(fn).read()) + 1 ticket_number = int(open(fn).read()) + 1
@ -44,6 +50,7 @@ def get_ticket_number():
@has_request_variables @has_request_variables
def submit_feedback(request, deployment, message=REQ(validator=check_dict([]))): def submit_feedback(request, deployment, message=REQ(validator=check_dict([]))):
# type: (HttpRequest, Deployment, Dict[str, str]) -> HttpResponse
domainish = message["sender_domain"] domainish = message["sender_domain"]
if get_realm("zulip.com") not in deployment.realms.all(): if get_realm("zulip.com") not in deployment.realms.all():
domainish += " via " + deployment.name domainish += " via " + deployment.name
@ -79,6 +86,7 @@ def submit_feedback(request, deployment, message=REQ(validator=check_dict([]))):
@has_request_variables @has_request_variables
def report_error(request, deployment, type=REQ(), report=REQ(validator=check_dict([]))): def report_error(request, deployment, type=REQ(), report=REQ(validator=check_dict([]))):
# type: (HttpRequest, Deployment, str, Dict[str, Optional[str]]) -> HttpResponse
report['deployment'] = deployment.name report['deployment'] = deployment.name
if type == 'browser': if type == 'browser':
notify_browser_error(report) notify_browser_error(report)
@ -89,6 +97,7 @@ def report_error(request, deployment, type=REQ(), report=REQ(validator=check_dic
return json_success() return json_success()
def realm_for_email(email): def realm_for_email(email):
# type: (str) -> Optional[Realm]
try: try:
user = get_user_profile_by_email(email) user = get_user_profile_by_email(email)
return user.realm return user.realm
@ -101,12 +110,14 @@ def realm_for_email(email):
@csrf_exempt @csrf_exempt
@has_request_variables @has_request_variables
def lookup_endpoints_for_user(request, email=REQ()): def lookup_endpoints_for_user(request, email=REQ()):
# type: (HttpRequest, str) -> HttpResponse
try: try:
return json_response(realm_for_email(email).deployment.endpoints) return json_response(realm_for_email(email).deployment.endpoints)
except AttributeError: except AttributeError:
return json_error(_("Cannot determine endpoint for user."), status=404) return json_error(_("Cannot determine endpoint for user."), status=404)
def account_deployment_dispatch(request, **kwargs): def account_deployment_dispatch(request, **kwargs):
# type: (HttpRequest, **Any) -> HttpResponse
sso_unknown_email = False sso_unknown_email = False
if request.method == 'POST': if request.method == 'POST':
email = request.POST['username'] email = request.POST['username']