Use minecraft avatars
This commit is contained in:
0
minecraft/templatetags/__init__.py
Normal file
0
minecraft/templatetags/__init__.py
Normal file
15
minecraft/templatetags/minecraft.py
Normal file
15
minecraft/templatetags/minecraft.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from django.core.urlresolvers import reverse
|
||||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@register.tag
|
||||
def avatar(parser, token):
|
||||
return AvatarNode(token.split_contents()[1])
|
||||
|
||||
class AvatarNode(template.Node):
|
||||
def __init__(self, uservar):
|
||||
self.__user = template.Variable(uservar)
|
||||
|
||||
def render(self, context):
|
||||
return '<img src="%s"/>'%(reverse('minecraft.views.avatar', kwargs={'username': self.__user.resolve(context)}),)
|
5
minecraft/urls.py
Normal file
5
minecraft/urls.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.conf.urls.defaults import patterns, include, url
|
||||
|
||||
urlpatterns = patterns('minecraft',
|
||||
url(r'^avatar/(?P<username>.*).png$', 'views.avatar'),
|
||||
)
|
19
minecraft/views.py
Normal file
19
minecraft/views.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from PIL import Image
|
||||
from django.http import HttpResponse
|
||||
from urllib2 import urlopen
|
||||
from cStringIO import StringIO
|
||||
from django.core.cache import cache
|
||||
from django.views.decorators.cache import cache_control
|
||||
|
||||
def avatar(request, username):
|
||||
avatar = cache.get('minecraft-avatar-%s'%(username))
|
||||
if avatar is None:
|
||||
imgStream = StringIO(urlopen("http://minecraft.net/skin/%s.png"%(username)).read())
|
||||
img = Image.open(imgStream)
|
||||
img = img.crop((8, 8, 16,16))
|
||||
img = img.resize((64, 64), Image.NEAREST)
|
||||
buf = StringIO()
|
||||
img.save(buf, "PNG")
|
||||
avatar = buf.getvalue()
|
||||
cache.set('minecraft-avatar-%s', avatar, 86400)
|
||||
return HttpResponse(avatar, content_type="image/png")
|
@@ -1,7 +1,7 @@
|
||||
{%load static %}
|
||||
{% load cache %}
|
||||
{% load flatpages %}
|
||||
{% load avatar_tags %}
|
||||
{% load minecraft %}
|
||||
{% get_static_prefix as STATIC_PREFIX %}
|
||||
<html>
|
||||
<head>
|
||||
@@ -34,7 +34,7 @@
|
||||
<h2 class="title">{{user.username}}</h2>
|
||||
<div class="userbox">
|
||||
<div class="avatar">
|
||||
{% avatar user %}
|
||||
{% avatar user.get_profile.mc_username %}
|
||||
</div>
|
||||
<div class="balance">
|
||||
<span id="balance-display">{{ user.get_profile.currencyaccount.balance|floatformat:2 }}</span> grist
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{% load markup %}
|
||||
{% load avatar_tags %}
|
||||
{% load minecraft %}
|
||||
|
||||
<a name="reply-{{post.id}}"></a>
|
||||
<div class="forum-post-user">
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{% extends 'base_simple.html' %}
|
||||
{% load avatar_tags %}
|
||||
{% load minecraft %}
|
||||
|
||||
{% block title %}Users{% endblock %}
|
||||
{% block sectiontitle %}Users{% endblock %}
|
||||
|
1
urls.py
1
urls.py
@@ -9,6 +9,7 @@ urlpatterns = patterns('',
|
||||
url(r'^petitions/', include('petition.urls')),
|
||||
url(r'^comments/', include('django.contrib.comments.urls')),
|
||||
url(r'^news/', include('news.urls')),
|
||||
url(r'^minecraft/', include('minecraft.urls')),
|
||||
url(r'^profiles/', include('profiles.urls')),
|
||||
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
|
Reference in New Issue
Block a user