Move all zephyr HTML generation to the client

(imported from commit 3afec768a5cd220c14ce615e03c29ee22754fdf2)
This commit is contained in:
Keegan McAllister 2012-08-30 13:56:15 -04:00
parent ddacce81b7
commit d57d19e562
4 changed files with 24 additions and 33 deletions

View File

@ -8,29 +8,15 @@
<script type="text/javascript" src="/static/jquery/jquery.form.js"></script>
<script type="text/javascript" src="/static/js/zephyr.js"></script>
<script type="text/javascript">
var initial_zephyr_json = {{ zephyr_json }};
</script>
<div class="row-fluid">
<div id="main_div" class="span12">
<table id="table">
{% for zephyr in zephyrs %}
<tr id={{ zephyr.id }}>
<td class="pointer">{% if user_profile.pointer == zephyr.id %}<p id="selected">&#x25b6;{% else %}<p>{% endif %}</p></td>
<td class="zephyr">
<p>
{% if zephyr.recipient.type == 'class' %}
<span onclick="narrow('{{ zephyr.display_recipient }}', '{{ zephyr.id }}')" class="label zephyr_label_clickable zephyr_class">{{ zephyr.display_recipient }}</span>
<span onclick="narrow_instance('{{ zephyr.display_recipient }}', '{{ zephyr.instance }}', '{{ zephyr.id }}')" class="label zephyr_label_clickable zephyr_instance">{{ zephyr.instance }}</span>
{% else %}
<span onclick="narrow_personals('{{ zephyr.id }}')" class="label zephyr_label_clickable zephyr_personal_recipient">{{ zephyr.display_recipient }}</span>
&larr;
{% endif %}
<span onclick="prepare_personal('{{ zephyr.sender.user.username }}')" class="label zephyr_label_clickable zephyr_sender">{{ zephyr.sender.user.username }}</span><br />
{{ zephyr.content|linebreaksbr }}
</p></td>
</tr>
{% endfor %}
<tr><td class="pointer"><p id="selected">&#x25b6;</p></td><td>dummy row, FIXME</td></tr>
{# Zephyrs go here #}
</table>
</div>
</div>

View File

@ -76,6 +76,14 @@ class Zephyr(models.Model):
display_recipient = get_display_recipient(self.recipient)
return "<Zephyr: %s / %s / %r>" % (display_recipient, self.instance, self.sender)
def to_dict(self):
return {'id' : self.id,
'sender' : self.sender.user.username,
'type' : self.recipient.type,
'display_recipient': get_display_recipient(self.recipient),
'instance' : self.instance,
'content' : self.content }
def send_zephyr(**kwargs):
zephyr = kwargs["instance"]
if zephyr.recipient.type == "personal":

View File

@ -257,6 +257,10 @@ function add_message(index, zephyr) {
$("#table tr:last").after(new_str);
}
$(function() {
$(initial_zephyr_json).each(add_message);
});
function get_updates_longpoll(data) {
if (data && data.zephyrs) {
$.each(data.zephyrs, add_message);

View File

@ -8,7 +8,8 @@ from django.shortcuts import render
from django.utils.timezone import utc
from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, get_display_recipient, filter_by_subscriptions
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \
Recipient, filter_by_subscriptions
from zephyr.forms import RegistrationForm
import tornado.web
@ -44,15 +45,16 @@ def home(request):
return HttpResponseRedirect('accounts/home/')
zephyrs = filter_by_subscriptions(Zephyr.objects.all(), request.user)
for zephyr in zephyrs:
zephyr.display_recipient = get_display_recipient(zephyr.recipient)
user = request.user
user_profile = UserProfile.objects.get(user=user)
if user_profile.pointer == -1 and zephyrs:
user_profile.pointer = min([zephyr.id for zephyr in zephyrs])
user_profile.save()
return render_to_response('zephyr/index.html', {'zephyrs': zephyrs, 'user_profile': user_profile},
zephyr_json = simplejson.dumps([zephyr.to_dict() for zephyr in zephyrs])
return render_to_response('zephyr/index.html',
{'zephyr_json' : zephyr_json,
'user_profile': user_profile },
context_instance=RequestContext(request))
def update(request):
@ -83,17 +85,8 @@ def get_updates_longpoll(request, handler):
def on_receive(zephyrs):
if handler.request.connection.stream.closed():
return
new_zephyr_list = []
for zephyr in zephyrs:
new_zephyr_list.append({"id": zephyr.id,
"sender": zephyr.sender.user.username,
"display_recipient": get_display_recipient(zephyr.recipient),
"type": zephyr.recipient.type,
"instance": zephyr.instance,
"content": zephyr.content
})
try:
handler.finish({'zephyrs': new_zephyr_list})
handler.finish({'zephyrs': [zephyr.to_dict() for zephyr in zephyrs]})
except socket.error, e:
pass