Factor out the determination of which messages to send immediately

(imported from commit 31c688986e4d8f923913083f36dea9c79a4c36ce)
This commit is contained in:
Keegan McAllister 2012-09-28 16:25:31 -04:00
parent a8ec5c533f
commit c65a387c1e
1 changed files with 24 additions and 14 deletions

View File

@ -146,15 +146,27 @@ def format_updates_response(messages, mit_sync_bot=False, apply_markdown=False):
messages = [m for m in messages if not mit_sync_table.get(m.id)]
return {'zephyrs': [message.to_dict(apply_markdown) for message in messages]}
def get_updates_backend(request, handler, last_received=None, **kwargs):
user_profile = UserProfile.objects.get(user=request.user)
def return_messages_immediately(request, handler, user_profile, **kwargs):
try:
first = int(request.POST['first'])
last = int(request.POST['last'])
except TypeError:
return False
except ValueError:
return False
if last_received:
new_messages = (Zephyr.objects.filter(usermessage__user_profile = user_profile,
id__gt = last_received)
messages = (Zephyr.objects.filter(usermessage__user_profile = user_profile,
id__gt = last)
.order_by('id')[:400])
if new_messages:
handler.finish(format_updates_response(new_messages, **kwargs))
if messages:
handler.finish(format_updates_response(messages, **kwargs))
return True
return False
def get_updates_backend(request, handler, **kwargs):
user_profile = UserProfile.objects.get(user=request.user)
if return_messages_immediately(request, handler, user_profile, **kwargs):
return
def on_receive(zephyrs):
@ -171,11 +183,9 @@ def get_updates_backend(request, handler, last_received=None, **kwargs):
@asynchronous
@require_post
def get_updates(request, handler):
last_received = request.POST.get('last')
if not last_received:
return json_error("Missing last received")
return get_updates_backend(request, handler, last_received=last_received,
apply_markdown=True)
if not ('last' in request.POST and 'first' in request.POST):
return json_error("Missing message range")
return get_updates_backend(request, handler, apply_markdown=True)
@login_required
@asynchronous