changeset 123:3ae999b0c53b

Forums: added a jquery ui dialog of extra smileys.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Nov 2009 21:15:31 +0000
parents b28d30848c57
children 9c18250972d5
files gpp/forums/forms.py gpp/forums/views.py gpp/smiley/admin.py gpp/smiley/models.py gpp/smiley/urls.py gpp/smiley/views.py gpp/templates/forums/edit_post.html gpp/templates/forums/new_post.html gpp/templates/forums/new_topic.html gpp/templates/forums/topic.html gpp/templates/smiley/smiley_farm.html media/icons/emoticon_smile.png media/js/forums.js media/js/shoutbox.js
diffstat 14 files changed, 96 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/forms.py	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/forums/forms.py	Sun Nov 08 21:15:31 2009 +0000
@@ -17,10 +17,12 @@
 
     class Media:
         css = {
-            'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
+            'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
+                settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
         }
-        js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
-            ('js/forums.js', )
+        js = (settings.GPP_THIRD_PARTY_JS['markitup'] + 
+                settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
+                ('js/forums.js', ))
 
     def clean_topic_id(self):
         id = self.cleaned_data['topic_id']
@@ -55,10 +57,12 @@
 
     class Media:
         css = {
-            'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
+            'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
+                settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
         }
-        js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
-            ('js/forums.js', )
+        js = (settings.GPP_THIRD_PARTY_JS['markitup'] + 
+                settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
+                ('js/forums.js', ))
 
     def __init__(self, user, forum, *args, **kwargs):
         super(NewTopicForm, self).__init__(*args, **kwargs)
@@ -102,10 +106,12 @@
 
     class Media:
         css = {
-            'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
+            'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
+                settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
         }
-        js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
-            ('js/forums.js', )
+        js = (settings.GPP_THIRD_PARTY_JS['markitup'] + 
+                settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
+                ('js/forums.js', ))
 
 
 class MoveTopicForm(forms.Form):
--- a/gpp/forums/views.py	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/forums/views.py	Sun Nov 08 21:15:31 2009 +0000
@@ -292,6 +292,8 @@
     else:
         form = PostForm(instance=post)
 
+    post.user_profile = request.user.get_profile()
+
     return render_to_response('forums/edit_post.html', {
         'forum': post.topic.forum,
         'topic': post.topic,
--- a/gpp/smiley/admin.py	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/smiley/admin.py	Sun Nov 08 21:15:31 2009 +0000
@@ -6,6 +6,6 @@
 from smiley.models import Smiley
 
 class SmileyAdmin(admin.ModelAdmin):
-    list_display = ('title', 'code', 'html')
+    list_display = ('title', 'code', 'html', 'is_extra')
 
 admin.site.register(Smiley, SmileyAdmin)
--- a/gpp/smiley/models.py	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/smiley/models.py	Sun Nov 08 21:15:31 2009 +0000
@@ -2,34 +2,38 @@
 Models for the smiley application.
 """
 from django.db import models
+from django.core.cache import cache
+
+CACHE_TIMEOUT = 60 * 5      # seconds
 
 
 class SmileyManager(models.Manager):
-    smiley_map = None
-    smilies = None
 
     def get_smiley_map(self):
-        if self.smiley_map is None:
-            smilies = self.all()
-            self.smiley_map = {}
-            for s in smilies:
-                self.smiley_map[s.code] = s.html()
-        return self.smiley_map
+        map = cache.get('smiley_map')
+        if map:
+            return map
 
-    def get_smilies(self):
-        if self.smilies is None:
-            self.smilies = self.all()
-        return self.smilies
+        map = dict((s.code, s.html()) for s in self.all())
+        cache.set('smiley_map', map, CACHE_TIMEOUT)
+        return map
 
-    def clear_cache(self):
-        self.smiley_map = None
-        self.smilies = None
+    def get_smilies(self, extra=False):
+        key = 'smileys' if not extra else 'smileys_extra'
+        smilies = cache.get(key)
+        if smilies:
+            return smilies
+
+        smilies = self.filter(is_extra=extra)
+        cache.set(key, smilies, CACHE_TIMEOUT)
+        return smilies
 
 
 class Smiley(models.Model):
     image = models.ImageField(upload_to='smiley/images/')
     title = models.CharField(max_length=32)
     code = models.CharField(max_length=32)
+    is_extra = models.BooleanField()
 
     objects = SmileyManager()
 
@@ -45,8 +49,8 @@
 
     def html(self):
         if self.image:
-            return u'<img src="%s" alt="%s" title="%s" />' % \
-                    (self.get_absolute_url(), self.title, self.title)
+            return (u'<img src="%s" alt="%s" title="%s" />' %
+                    (self.get_absolute_url(), self.title, self.title))
         return u''
     html.allow_tags = True
 
--- a/gpp/smiley/urls.py	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/smiley/urls.py	Sun Nov 08 21:15:31 2009 +0000
@@ -6,4 +6,5 @@
 
 urlpatterns = patterns('smiley.views',
     url(r'^farm/$', 'farm', name='smiley-farm'),
+    url(r'^farm/extra/$', 'farm', kwargs={'extra': True}, name='smiley-farm_extra'),
 )
--- a/gpp/smiley/views.py	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/smiley/views.py	Sun Nov 08 21:15:31 2009 +0000
@@ -10,10 +10,9 @@
 
 @login_required
 @require_GET
-def farm(request):
+def farm(request, extra=False):
     return render_to_response('smiley/smiley_farm.html', {
-        'smilies': Smiley.objects.get_smilies(),
+        'smilies': Smiley.objects.get_smilies(extra),
         },
         context_instance = RequestContext(request))
 
-# vim: ts=4 sw=4
--- a/gpp/templates/forums/edit_post.html	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/templates/forums/edit_post.html	Sun Nov 08 21:15:31 2009 +0000
@@ -20,8 +20,13 @@
 <fieldset>
 <legend>Edit Post</legend>
 {{ form.as_p }}
+<a href="#" id="more_smileys">
+<img src="{{ MEDIA_URL }}icons/emoticon_smile.png" alt="More smileys" title="More smileys" /></a>
 <input type="submit" value="Update Post" id="forums-edit-post" />
 </fieldset>
 </form>
 </div>
+<div id="smileys_dialog" title="More Smileys">
+<img src="{{ MEDIA_URL }}icons/ajax_busy.gif" alt="Loading" id="smiley_busy" />
+</div>
 {% endblock %}
--- a/gpp/templates/forums/new_post.html	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/templates/forums/new_post.html	Sun Nov 08 21:15:31 2009 +0000
@@ -17,10 +17,15 @@
 <fieldset>
 <legend>New Post</legend>
 {{ form.as_p }}
+<a href="#" id="more_smileys">
+<img src="{{ MEDIA_URL }}icons/emoticon_smile.png" alt="More smileys" title="More smileys" /></a>
 <input type="submit" value="Submit Post" id="forums-new-post" />
 </fieldset>
 </form>
 </div>
+<div id="smileys_dialog" title="More Smileys">
+<img src="{{ MEDIA_URL }}icons/ajax_busy.gif" alt="Loading" id="smiley_busy" />
+</div>
 {% else %}
    {% if topic.locked %}
    <p>This topic is locked.</p>
--- a/gpp/templates/forums/new_topic.html	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/templates/forums/new_topic.html	Sun Nov 08 21:15:31 2009 +0000
@@ -11,7 +11,13 @@
 
 <form action="." method="post">
 {{ form.as_p }}
+<a href="#" id="more_smileys">
+<img src="{{ MEDIA_URL }}icons/emoticon_smile.png" alt="More smileys" title="More smileys" /></a>
 <input type="submit" name="post" value="Submit" />
 </form>
 
+<div id="smileys_dialog" title="More Smileys">
+<img src="{{ MEDIA_URL }}icons/ajax_busy.gif" alt="Loading" id="smiley_busy" />
+</div>
+
 {% endblock %}
--- a/gpp/templates/forums/topic.html	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/templates/forums/topic.html	Sun Nov 08 21:15:31 2009 +0000
@@ -58,9 +58,14 @@
 <fieldset>
 <legend>Reply to &quot;{{ topic.name }}&quot;</legend>
 {{ form.as_p }}
+<a href="#" id="more_smileys">
+<img src="{{ MEDIA_URL }}icons/emoticon_smile.png" alt="More smileys" title="More smileys" /></a>
 <input type="submit" value="Submit Reply" id="forums-reply-post" />
 </fieldset>
 </form>
+<div id="smileys_dialog" title="More Smileys">
+<img src="{{ MEDIA_URL }}icons/ajax_busy.gif" alt="Loading" id="smiley_busy" />
+</div>
 {% endif %}
 </div>
 {% endblock %}
--- a/gpp/templates/smiley/smiley_farm.html	Tue Oct 27 02:56:16 2009 +0000
+++ b/gpp/templates/smiley/smiley_farm.html	Sun Nov 08 21:15:31 2009 +0000
@@ -1,6 +1,5 @@
 <div class="smiley_farm">
 {% for s in smilies %}
-    <img src="{{ s.image.url }}" alt="{{ s.code }}" title="{{ s.title }} {{ s.code }}"
-        onclick="sb_smiley_click(' {{ s.code }} ');" />
+<img src="{{ s.image.url }}" alt="{{ s.code }}" title="{{ s.title }} {{ s.code }}" />
 {% endfor %}
 </div>
Binary file media/icons/emoticon_smile.png has changed
--- a/media/js/forums.js	Tue Oct 27 02:56:16 2009 +0000
+++ b/media/js/forums.js	Sun Nov 08 21:15:31 2009 +0000
@@ -83,4 +83,31 @@
             'WARNING: all posts will be lost.');
    });
    $('#id_body').markItUp(mySettings);
+   $('#smileys_dialog').dialog({autoOpen:false});
+   var firstTime = true;
+   $('#more_smileys').click(function () {
+         $('#smileys_dialog').dialog('open');
+         var postBox = $('#id_body')[0];
+         if (firstTime) {
+            $.ajax({
+               url: '/smiley/farm/extra/',
+               type: 'GET',
+               dataType: 'html',
+               success: function(data, textStatus) {
+                  var img = $('#smiley_busy');
+                  img.hide();
+                  img.after(data);
+                  $('#smileys_dialog .smiley_farm img').click(function() {
+                     postBox.value += ' ' + this.alt + ' ';
+                     postBox.focus();
+                  });
+                  firstTime = false;
+               },
+               error: function (xhr, textStatus, ex) {
+                  alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + xhr.responseText);
+               }
+            });
+         }
+         return false;
+      });
 });
--- a/media/js/shoutbox.js	Tue Oct 27 02:56:16 2009 +0000
+++ b/media/js/shoutbox.js	Sun Nov 08 21:15:31 2009 +0000
@@ -28,15 +28,13 @@
       if (!smilies_loaded) {
          smiley_frame.load('/smiley/farm/', function () {
             $('#shoutbox-busy-icon').hide();
+            var txt = $("#shoutbox-smiley-input")[0];
+            $('#shoutbox-smiley-frame img').click(function() {
+               txt.value += ' ' + this.alt + ' ';
+               txt.focus();
+            });
             smilies_loaded = true;
          });
       }
    });
 });
-
-function sb_smiley_click(code)
-{
-    var txt = document.getElementById("shoutbox-smiley-input");
-    txt.value += code;
-    txt.focus();
-}