mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 09:27:43 +00:00
Refactor colorizing requests into a subscription properties model.
(imported from commit da4bfee4a12e941d4ec4c9f140973bb138c82c93)
This commit is contained in:
@@ -46,8 +46,7 @@ urlpatterns = patterns('',
|
|||||||
url(r'^json/subscriptions/remove$', 'zephyr.views.json_remove_subscriptions'),
|
url(r'^json/subscriptions/remove$', 'zephyr.views.json_remove_subscriptions'),
|
||||||
url(r'^json/subscriptions/add$', 'zephyr.views.json_add_subscriptions'),
|
url(r'^json/subscriptions/add$', 'zephyr.views.json_add_subscriptions'),
|
||||||
url(r'^json/subscriptions/exists$', 'zephyr.views.json_stream_exists'),
|
url(r'^json/subscriptions/exists$', 'zephyr.views.json_stream_exists'),
|
||||||
url(r'^json/subscriptions/colors$', 'zephyr.views.json_stream_colors'),
|
url(r'^json/subscriptions/property$', 'zephyr.views.json_subscription_property'),
|
||||||
url(r'^json/subscriptions/colorize$', 'zephyr.views.json_stream_colorize'),
|
|
||||||
url(r'^json/fetch_api_key$', 'zephyr.views.json_fetch_api_key'),
|
url(r'^json/fetch_api_key$', 'zephyr.views.json_fetch_api_key'),
|
||||||
|
|
||||||
# These are json format views used by the API. They require an API key.
|
# These are json format views used by the API. They require an API key.
|
||||||
|
|||||||
@@ -60,9 +60,10 @@ function draw_colorpicker(stream_name) {
|
|||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: '/json/subscriptions/colorize',
|
url: '/json/subscriptions/property',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
data: {
|
data: {
|
||||||
|
"property": "stream_colors",
|
||||||
"stream_name": stream_name,
|
"stream_name": stream_name,
|
||||||
"color": hex_color
|
"color": hex_color
|
||||||
},
|
},
|
||||||
@@ -108,9 +109,10 @@ exports.get_color = function (stream_name) {
|
|||||||
|
|
||||||
exports.fetch_colors = function () {
|
exports.fetch_colors = function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'GET',
|
||||||
url: '/json/subscriptions/colors',
|
url: '/json/subscriptions/property',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
|
data: {"property": "stream_colors"},
|
||||||
timeout: 10*1000,
|
timeout: 10*1000,
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
if (data) {
|
if (data) {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ from django.views.decorators.csrf import csrf_exempt
|
|||||||
from zephyr.decorator import asynchronous, require_post, \
|
from zephyr.decorator import asynchronous, require_post, \
|
||||||
authenticated_api_view, authenticated_json_post_view, \
|
authenticated_api_view, authenticated_json_post_view, \
|
||||||
internal_notify_view, RespondAsynchronously, \
|
internal_notify_view, RespondAsynchronously, \
|
||||||
has_request_variables, POST
|
has_request_variables, POST, authenticated_json_view
|
||||||
from zephyr.lib.query import last_n
|
from zephyr.lib.query import last_n
|
||||||
from zephyr.lib.avatar import gravatar_hash
|
from zephyr.lib.avatar import gravatar_hash
|
||||||
from zephyr.lib.response import json_success, json_error
|
from zephyr.lib.response import json_success, json_error
|
||||||
@@ -837,8 +837,27 @@ def json_stream_exists(request, user_profile, stream=POST):
|
|||||||
active=True).exists()
|
active=True).exists()
|
||||||
return json_success(result)
|
return json_success(result)
|
||||||
|
|
||||||
@authenticated_json_post_view
|
class SubscriptionProperties(object):
|
||||||
def json_stream_colors(request, user_profile):
|
"""
|
||||||
|
A class for managing GET and POST requests for subscription properties. The
|
||||||
|
name for a request handler is <request type>_<property name>.
|
||||||
|
|
||||||
|
Requests must have already been authenticated before being processed here.
|
||||||
|
|
||||||
|
Requests that set or change subscription properties should typically log the
|
||||||
|
change through log_event.
|
||||||
|
"""
|
||||||
|
def __call__(self, request, user_profile, property):
|
||||||
|
property_method = getattr(self, "%s_%s" % (request.method.lower(), property), None)
|
||||||
|
if not property_method:
|
||||||
|
return json_error("Unknown property or invalid verb for %s" % (property,))
|
||||||
|
|
||||||
|
return property_method(request, user_profile)
|
||||||
|
|
||||||
|
def request_property(self, request_dict, property):
|
||||||
|
return request_dict.get(property, "").strip()
|
||||||
|
|
||||||
|
def get_stream_colors(self, request, user_profile):
|
||||||
subscriptions = Subscription.objects.filter(user_profile=user_profile, active=True)
|
subscriptions = Subscription.objects.filter(user_profile=user_profile, active=True)
|
||||||
stream_subs = [sub for sub in subscriptions if sub.recipient.type == Recipient.STREAM]
|
stream_subs = [sub for sub in subscriptions if sub.recipient.type == Recipient.STREAM]
|
||||||
stream_colors = [(Stream.objects.get(id=sub.recipient.type_id).name,
|
stream_colors = [(Stream.objects.get(id=sub.recipient.type_id).name,
|
||||||
@@ -846,9 +865,8 @@ def json_stream_colors(request, user_profile):
|
|||||||
|
|
||||||
return json_success({"stream_colors": stream_colors})
|
return json_success({"stream_colors": stream_colors})
|
||||||
|
|
||||||
@authenticated_json_post_view
|
def post_stream_colors(self, request, user_profile):
|
||||||
@has_request_variables
|
stream_name = self.request_property(request.POST, "stream_name")
|
||||||
def json_stream_colorize(request, user_profile, stream_name=POST, color=POST):
|
|
||||||
stream = get_stream(stream_name, user_profile.realm)
|
stream = get_stream(stream_name, user_profile.realm)
|
||||||
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
recipient = Recipient.objects.get(type_id=stream.id, type=Recipient.STREAM)
|
||||||
subscription = Subscription.objects.filter(user_profile=user_profile,
|
subscription = Subscription.objects.filter(user_profile=user_profile,
|
||||||
@@ -858,11 +876,41 @@ def json_stream_colorize(request, user_profile, stream_name=POST, color=POST):
|
|||||||
|
|
||||||
stream_color, _ = StreamColor.objects.get_or_create(subscription=subscription[0])
|
stream_color, _ = StreamColor.objects.get_or_create(subscription=subscription[0])
|
||||||
# TODO: sanitize color.
|
# TODO: sanitize color.
|
||||||
stream_color.color = color
|
stream_color.color = self.request_property(request.POST, "color")
|
||||||
stream_color.save()
|
stream_color.save()
|
||||||
|
|
||||||
return json_success()
|
return json_success()
|
||||||
|
|
||||||
|
subscription_properties = SubscriptionProperties()
|
||||||
|
|
||||||
|
def make_property_call(request, query_dict, user_profile):
|
||||||
|
property = query_dict.get("property").strip()
|
||||||
|
if not property:
|
||||||
|
return json_error("Missing property")
|
||||||
|
|
||||||
|
return subscription_properties(request, user_profile, property.lower())
|
||||||
|
|
||||||
|
def make_get_property_call(request, user_profile):
|
||||||
|
return make_property_call(request, request.GET, user_profile)
|
||||||
|
|
||||||
|
def make_post_property_call(request, user_profile):
|
||||||
|
return make_property_call(request, request.POST, user_profile)
|
||||||
|
|
||||||
|
@authenticated_json_view
|
||||||
|
def json_subscription_property(request, user_profile):
|
||||||
|
"""
|
||||||
|
This is the entry point to accessing or changing subscription
|
||||||
|
properties. Authentication happens here.
|
||||||
|
|
||||||
|
Add a handler for a new subscription property in SubscriptionProperties.
|
||||||
|
"""
|
||||||
|
if request.method == "GET":
|
||||||
|
return make_get_property_call(request, user_profile)
|
||||||
|
elif request.method == "POST":
|
||||||
|
return make_post_property_call(request, user_profile)
|
||||||
|
else:
|
||||||
|
return json_error("Invalid verb")
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
@require_post
|
@require_post
|
||||||
@has_request_variables
|
@has_request_variables
|
||||||
|
|||||||
Reference in New Issue
Block a user