views: Move presence views to presence.py.

This commit is contained in:
Tim Abbott 2016-07-26 14:26:39 -07:00
parent 62affa53c9
commit 6dc5681171
4 changed files with 55 additions and 39 deletions

View File

@ -30,7 +30,7 @@ from zerver.models import Message, UserProfile, Stream, Subscription, Huddle, \
from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \
do_activate_user, do_create_user, do_create_realm, set_default_streams, \
internal_send_message, update_user_presence, do_events_register, \
get_status_dict, do_change_enable_offline_email_notifications, \
do_change_enable_offline_email_notifications, \
do_change_enable_digest_emails, \
get_default_subs, user_email_is_unique, do_invite_users, do_refer_friend, \
compute_mit_user_fullname, do_set_muted_topics, clear_followup_emails_queue, \
@ -1155,42 +1155,6 @@ def api_fetch_google_client_id(request):
return json_error(_("GOOGLE_CLIENT_ID is not configured"), status=400)
return json_success({"google_client_id": settings.GOOGLE_CLIENT_ID})
def get_status_list(requesting_user_profile):
# type: (UserProfile) -> Dict[str, Any]
return {'presences': get_status_dict(requesting_user_profile),
'server_timestamp': time.time()}
@has_request_variables
def update_active_status_backend(request, user_profile, status=REQ(),
new_user_input=REQ(validator=check_bool, default=False)):
# type: (HttpRequest, UserProfile, str, bool) -> HttpResponse
status_val = UserPresence.status_from_string(status)
if status_val is None:
raise JsonableError(_("Invalid presence status: %s") % (status,))
else:
update_user_presence(user_profile, request.client, now(), status_val,
new_user_input)
ret = get_status_list(user_profile)
if user_profile.realm.domain == "mit.edu":
try:
activity = UserActivity.objects.get(user_profile = user_profile,
query="get_events_backend",
client__name="zephyr_mirror")
ret['zephyr_mirror_active'] = \
(activity.last_visit.replace(tzinfo=None) >
datetime.datetime.utcnow() - datetime.timedelta(minutes=5))
except UserActivity.DoesNotExist:
ret['zephyr_mirror_active'] = False
return json_success(ret)
@authenticated_json_post_view
def json_get_active_statuses(request, user_profile):
# type: (HttpRequest, UserProfile) -> HttpResponse
return json_success(get_status_list(user_profile))
# Does not need to be authenticated because it's called from rest_dispatch
@has_request_variables
def api_events_register(request, user_profile,

52
zerver/views/presence.py Normal file
View File

@ -0,0 +1,52 @@
from __future__ import absolute_import
import datetime
import time
from typing import Any
from django.http import HttpRequest, HttpResponse
from django.utils.timezone import now
from django.utils.translation import ugettext as _
from zerver.decorator import authenticated_json_post_view
from zerver.lib.actions import get_status_dict, update_user_presence
from zerver.lib.request import has_request_variables, REQ, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.validator import check_bool
from zerver.models import UserActivity, UserPresence, UserProfile
def get_status_list(requesting_user_profile):
# type: (UserProfile) -> Dict[str, Any]
return {'presences': get_status_dict(requesting_user_profile),
'server_timestamp': time.time()}
@has_request_variables
def update_active_status_backend(request, user_profile, status=REQ(),
new_user_input=REQ(validator=check_bool, default=False)):
# type: (HttpRequest, UserProfile, str, bool) -> HttpResponse
status_val = UserPresence.status_from_string(status)
if status_val is None:
raise JsonableError(_("Invalid presence status: %s") % (status,))
else:
update_user_presence(user_profile, request.client, now(), status_val,
new_user_input)
ret = get_status_list(user_profile)
if user_profile.realm.domain == "mit.edu":
try:
activity = UserActivity.objects.get(user_profile = user_profile,
query="get_events_backend",
client__name="zephyr_mirror")
ret['zephyr_mirror_active'] = \
(activity.last_visit.replace(tzinfo=None) >
datetime.datetime.utcnow() - datetime.timedelta(minutes=5))
except UserActivity.DoesNotExist:
ret['zephyr_mirror_active'] = False
return json_success(ret)
@authenticated_json_post_view
def json_get_active_statuses(request, user_profile):
# type: (HttpRequest, UserProfile) -> HttpResponse
return json_success(get_status_list(user_profile))

View File

@ -17,7 +17,7 @@ legacy_urls = [
url(r'^json/subscriptions/property$', 'zerver.views.streams.json_subscription_property'),
url(r'^json/get_subscribers$', 'zerver.views.streams.json_get_subscribers'),
url(r'^json/fetch_api_key$', 'zerver.views.json_fetch_api_key'),
url(r'^json/get_active_statuses$', 'zerver.views.json_get_active_statuses'),
url(r'^json/get_active_statuses$', 'zerver.views.presence.json_get_active_statuses'),
url(r'^json/tutorial_send_message$', 'zerver.views.tutorial.json_tutorial_send_message'),
url(r'^json/tutorial_status$', 'zerver.views.tutorial.json_tutorial_status'),
url(r'^json/report_error$', 'zerver.views.report.json_report_error'),

View File

@ -173,7 +173,7 @@ v1_api_and_json_patterns = [
{'GET': 'zerver.views.get_pointer_backend',
'PUT': 'zerver.views.update_pointer_backend'}),
url(r'^users/me/presence$', 'zerver.lib.rest.rest_dispatch',
{'POST': 'zerver.views.update_active_status_backend'}),
{'POST': 'zerver.views.presence.update_active_status_backend'}),
# Endpoint used by mobile devices to register their push
# notification credentials
url(r'^users/me/apns_device_token$', 'zerver.lib.rest.rest_dispatch',