Skip to content

Commit

Permalink
cache test on chat
Browse files Browse the repository at this point in the history
  • Loading branch information
josven committed Mar 21, 2012
1 parent e3e275f commit 70117c3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
9 changes: 5 additions & 4 deletions apps/chat/static/js/chat.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var updateChat = function() {

$.ajax({
url: window.location.pathname,
url: window.location.pathname + "get/",
data: {
's': $("li:[data-id]").first().data('id')
},
statusCode: {
200: function(data) {
$('#chatlist .loading-message').fadeOut().remove();
var amount = $('#chatlist li.entry').length;
$(data).hide().prependTo("#chatlist").slideDown("slow");
$('#chatlist li.entry:gt(49)').fadeOut().remove();

formatJsReplyButton();
formatImageDialogs();
hilightUserName();
Expand Down Expand Up @@ -51,13 +51,14 @@ var hilightUserName = function() {
};

jQuery(document).ready(function() {

// tinychat
$('.tinychat_embed').css({
'height': '85%',
'margin-top': '3%'
});


updateChat();
bindReplyButton();
hilightUserName();

Expand Down
12 changes: 10 additions & 2 deletions apps/chat/templates/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
{% endif %}

<ul id="chatlist" class="flat-list entrylist">
{% for entry in posts %}
{% comment %}
{% for entry in posts %}
<li data-id="{{ entry.id }}" class="entry ui-widget ui-widget-content">
{% render_entry entry request %}
</li>
{% endfor %}
{% endfor %}
{% endcomment %}

<li class="loading-message entry ui-widget ui-widget-content">
<h4 class="ui-widget-header entry-head" >Laddar chat...</h4>
</li>


</ul>

{% endblock content %}
7 changes: 5 additions & 2 deletions apps/chat/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
urlpatterns = patterns('',
url(r'^$', 'apps.chat.views.chat', name='chat'),

# Get chat, ajax api
url(r'^get/$', 'apps.chat.views.get_chat', name='get_chat'),

# Post
url(r'^post/$', 'apps.chat.views.chat', name='chat_post'),

# Return a single post
url(r'^(?P<id>\d*)\.(?P<type>\w*)$', 'apps.chat.views.chatpost', name='chatpost'),
#url(r'^(?P<id>\d*)\.(?P<type>\w*)$', 'apps.chat.views.chatpost', name='chatpost'),

# Return a range posts
url(r'^(?P<id1>\d*)-(?P<id2>\d*)\.(?P<type>\w*)$', 'apps.chat.views.chatposts', name='chatposts'),
#url(r'^(?P<id1>\d*)-(?P<id2>\d*)\.(?P<type>\w*)$', 'apps.chat.views.chatposts', name='chatposts'),

#Tinychat
url(r'^camchat/$', 'apps.chat.views.camchat', name='camchat'),
Expand Down
43 changes: 28 additions & 15 deletions apps/chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.http import Http404, HttpResponse
from django.conf import settings

import site_settings

from apps.core.utils import render

"""
Expand All @@ -19,11 +21,6 @@
"""

"""
Constants
"""
LIST_ITEM_LIMIT = 30

@never_cache
@login_required(login_url='/auth/login/')
def camchat(request):
Expand All @@ -33,7 +30,7 @@ def camchat(request):
messages.add_message(request, messages.INFO, 'Lösenord är:fisk')
return render(request,'tinychat.html', vars)

@never_cache

@login_required(login_url='/auth/login/')
def chat(request):
vars = {
Expand All @@ -48,9 +45,22 @@ def chat(request):
form.save()
return HttpResponse(status=200) # OK!
return HttpResponse(status=404) #CHANGE THIS


vars['form'] = PostForm()
vars['posts'] = Post.active.order_by('-date_created')[:site_settings.CHAT_LIST_ITEM_LIMIT]

return render(request,'chat.html', vars)

@never_cache
@login_required(login_url='/auth/login/')
def get_chat(request):

vars = {}

if request.is_ajax():
if request.method == 'GET':
posts = Post.active.order_by('-date_created')[:LIST_ITEM_LIMIT]

posts = Post.active.order_by('-date_created')[:site_settings.CHAT_LIST_ITEM_LIMIT]
client_post = int(float( request.GET.get('s', '0') ) )
current_post = int(float( posts[0].id ) )
number_of_posts = current_post - client_post
Expand All @@ -59,13 +69,15 @@ def chat(request):
vars['posts'] = posts[0:number_of_posts]
return render(request,'_chat.html', vars)

return HttpResponse(status=302) # Not changed
return HttpResponse(status=302) # Not changed

vars['form'] = PostForm()
vars['posts'] = Post.active.order_by('-date_created')[:LIST_ITEM_LIMIT]

return render(request,'chat.html', vars)
# Only allow ajax get
return HttpResponse(status=404)


'''
OLD
@never_cache
@login_required(login_url='/auth/login/')
def chatpost(request,id,type):
Expand Down Expand Up @@ -133,7 +145,7 @@ def chatposts(request,id1,id2,type):
vars['isFirstPage'] = True
else:
# Trim query to match orginal request
posts = posts[1:LIST_ITEM_LIMIT+1]
posts = posts[1:site_settings.CHAT_LIST_ITEM_LIMIT+1]
except:
# "no match"
raise Http404
Expand All @@ -149,4 +161,5 @@ def chatposts(request,id1,id2,type):
raise Http404
else:
# "wrong method"
raise Http404
raise Http404
'''

0 comments on commit 70117c3

Please sign in to comment.