From d62cb9f453b1a091ec43998f8d8b00a0894080f2 Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Mon, 18 May 2009 22:37:51 +0200 Subject: [PATCH] implemented setting user textures --- pyweb/mumble/forms.py | 8 +++++++- pyweb/mumble/models.py | 23 ++++++++++++++++++++++- pyweb/mumble/views.py | 14 ++++++++++++-- template/mumble/mumble.htm | 20 ++++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/pyweb/mumble/forms.py b/pyweb/mumble/forms.py index 23c6327..4f7c740 100644 --- a/pyweb/mumble/forms.py +++ b/pyweb/mumble/forms.py @@ -14,7 +14,8 @@ * GNU General Public License for more details. """ -from django.forms import ModelForm +from django import forms +from django.forms import Form, ModelForm from models import * class MumbleForm( ModelForm ): @@ -28,3 +29,8 @@ class MumbleUserForm( ModelForm ): model = MumbleUser; fields = ( 'name', 'password' ); + +class MumbleTextureForm( Form ): + texturefile = forms.ImageField(); + + diff --git a/pyweb/mumble/models.py b/pyweb/mumble/models.py index 420f04f..50e1a36 100644 --- a/pyweb/mumble/models.py +++ b/pyweb/mumble/models.py @@ -14,6 +14,10 @@ * GNU General Public License for more details. """ +from PIL import Image +from struct import pack +from zlib import compress + from django.contrib.auth.models import User from django.db import models @@ -219,7 +223,24 @@ class MumbleUser( models.Model ): bus.setACL( *acl.pack() ); return value; - + def setTexture( self, infile ): + # open image, convert to RGBA, and resize to 600x60 + img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) ); + # iterate over the list and pack everything into a string + bgrastring = ""; + for ent in list( img.getdata() ): + # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs + # to be reordered when passed to pack() + bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] ); + # compress using zlib + compressed = compress( bgrastring ); + # pack the original length in 4 byte big endian, and concat the compressed + # data to it to emulate qCompress(). + texture = pack( ">L", len(bgrastring) ) + compressed; + # finally call murmur and set the texture + murmur = self.server.getDbusObject(); + murmur.setTexture( dbus.Int32( self.mumbleid ), texture ); + @staticmethod def pre_delete_listener( **kwargs ): kwargs['instance'].unregister(); diff --git a/pyweb/mumble/views.py b/pyweb/mumble/views.py index 1ccf99c..357d42a 100644 --- a/pyweb/mumble/views.py +++ b/pyweb/mumble/views.py @@ -66,6 +66,7 @@ def show( request, server ): adminform = None; registered = False; + if request.user.is_authenticated(): if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'reg': try: @@ -92,8 +93,18 @@ def show( request, server ): else: regform = MumbleUserForm( instance=user ); registered = True; + + if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'texture' and registered: + textureform = MumbleTextureForm( request.POST, request.FILES ); + if textureform.is_valid(): + user.setTexture( request.FILES['texturefile'] ); + return HttpResponseRedirect( '/mumble/%d' % int(server) ); + else: + textureform = MumbleTextureForm(); + else: regform = None; + textureform = None; return render_to_response( 'mumble/mumble.htm', @@ -104,6 +115,7 @@ def show( request, server ): "CurrentUserIsAdmin": isAdmin, "AdminForm": adminform, "RegForm": regform, + "TextureForm": textureform, "Registered": registered, "DisplayTab": displayTab, 'MumbleActive': True, @@ -168,5 +180,3 @@ def renderListItem( item, level ): Storage.s.append( ( level, item ) ); - - diff --git a/template/mumble/mumble.htm b/template/mumble/mumble.htm index e33c176..228625a 100644 --- a/template/mumble/mumble.htm +++ b/template/mumble/mumble.htm @@ -37,6 +37,20 @@ {% endif %} + {% if Registered %} +
+

User Texture

+ You can upload an image that you would like to use as your user texture here.
+
+ + {{ TextureForm }} +
+ + +
+
+ {% endif %} + {% if CurrentUserIsAdmin %}

Server administration

@@ -87,6 +101,9 @@