Skip to content

Instantly share code, notes, and snippets.

@jun66j5
Created April 3, 2011 03:55
Show Gist options
  • Save jun66j5/900161 to your computer and use it in GitHub Desktop.
Save jun66j5/900161 to your computer and use it in GitHub Desktop.
img.avatar {
max-width: 40px;
max-height: 40px;
vertical-align: bottom;
}
jQuery(document).ready(function($) {
var usernames = [];
var elements = $('.report').find("td.owner, td.reporter, td.担当者, td.報告者");
var uniq_username = {};
var data = {username: [], __FORM_TOKEN: form_token};
elements.each(function() {
var text = $(this).text();
usernames.push(text);
if (!text || text == 'anonymous' || text == 'somebody' || text == 'trac')
return;
if (!(text in uniq_username)) {
uniq_username[text] = true;
data.username.push(text);
}
});
if (data.username.length == 0)
return;
$.ajax({
url: avatar_request_path,
data: data,
traditional: true,
dataType: 'json',
type: 'POST',
success: function(json) {
var images = {};
$.each(json, function(username, href) {
var image = new Image();
image.className = 'avatar';
image.src = href;
images[username] = image;
});
var curr = 0;
var length = elements.length;
function step() {
var n = Math.min(length - curr, 25);
for (var i = 0; i < n; i++) {
var index = curr + i;
var username = usernames[index];
if (username in images)
$(elements[index]).prepend(images[username].cloneNode(false));
};
curr += n;
if (curr < length)
setTimeout(step, 50);
}
step();
}
});
});
# -*- coding: utf-8 -*-
from genshi.builder import tag
from trac.core import Component, implements
from trac.web.chrome import Chrome, add_stylesheet, add_script, \
add_script_data, ITemplateProvider
from trac.web.main import IRequestHandler, IRequestFilter
from trac.util.compat import partial
from trac.util.presentation import to_json
class TracAvatarModule(Component):
implements(IRequestHandler, IRequestFilter, ITemplateProvider)
# IRequestFilter methods
def pre_process_request(self, req, handler):
return handler
def post_process_request(self, req, template, data, content_type):
if data:
req.callbacks['avatars'] = self._get_avatars
if template == 'ticket.html':
if data['owner_link']:
ticket = data['ticket']
data['owner_link'] = tag(
self._avatar_image(req, ticket['owner']),
data['owner_link'])
if data['reporter_link']:
data['reporter_link'] = tag(
self._avatar_image(req, ticket['reporter']),
data['reporter_link'])
data['authorinfo'] = partial(self.authorinfo, req)
data['format_author'] = partial(self.format_author, req)
add_script_data(req, {'avatar_request_path': req.href.tracavatar(),
'form_token': req.form_token})
add_stylesheet(req, 'tracavatar/css/avatar.css')
add_script(req, 'tracavatar/js/avatar.js')
return (template, data, content_type)
def _get_avatars(self, req):
db = self.env.get_read_db()
cursor = db.cursor()
cursor.execute("SELECT sid, value FROM session_attribute"
" WHERE authenticated=1 AND name='picture_href'")
avatars = dict((sid, value) for sid, value in cursor)
# default avatar
avatars[None] = req.href.chrome('tracusermanager/img/no_picture.png')
return avatars
def _avatar_image(self, req, author):
src = req.avatars.get(author) or req.avatars[None]
return tag.img(src=src, class_='avatar')
def authorinfo(self, req, author, email_map=None):
return tag(self._avatar_image(req, author),
Chrome(self.env).authorinfo(req, author,
email_map=email_map))
def format_author(self, req, author):
return tag(self._avatar_image(req, author),
Chrome(self.env).format_author(req, author))
# IRequestHandler methods
def match_request(self, req):
if req.path_info in ('/login/tracavatar', '/tracavatar'):
self.log.debug("%s matches %s" % (req.path_info, True))
return True
return False
def process_request(self, req):
sids = req.args.getlist('username')
avatars = {}
if sids:
default = req.href.chrome('tracusermanager/img/no_picture.png')
avatars = dict((sid, default) for sid in sids)
query = "SELECT sid, value FROM session_attribute" + \
" WHERE sid IN(" + ','.join(['%s'] * len(sids)) + ")" + \
" AND authenticated=1 AND name='picture_href'"
db = self.env.get_read_db()
cursor = db.cursor()
cursor.execute(query, sids)
for sid, value in cursor:
avatars[sid] = value
response = to_json(avatars)
if isinstance(response, unicode):
response = response.encode('utf-8')
req.send(response, 'application/json')
# ITemplateProvider methods
def get_templates_dirs(self):
return []
def get_htdocs_dirs(self):
from pkg_resources import resource_filename
return [('tracavatar', resource_filename(__name__, 'htdocs'))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment