Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.6 KiB

16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. *
  5. * Mumble-Django is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This package is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. """
  15. import simplejson
  16. from StringIO import StringIO
  17. from os.path import join
  18. from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
  19. from django.template import RequestContext
  20. from django.http import Http404, HttpResponse, HttpResponseRedirect
  21. from django.contrib.auth.decorators import login_required
  22. from django.contrib.auth.models import User
  23. from django.conf import settings
  24. from django.core.urlresolvers import reverse
  25. from django.contrib.auth import views as auth_views
  26. from models import Mumble, MumbleUser
  27. from forms import *
  28. from mmobjects import *
  29. def redir( request ):
  30. """ Redirect to the servers list. """
  31. return HttpResponseRedirect( reverse( mumbles ) );
  32. def mumbles( request ):
  33. """ Display a list of all configured Mumble servers, or redirect if only one configured. """
  34. mumbles = get_list_or_404( Mumble );
  35. if len(mumbles) == 1:
  36. return HttpResponseRedirect( reverse( show, kwargs={ 'server': mumbles[0].id, } ) );
  37. return render_to_response(
  38. 'mumble/list.htm',
  39. { 'MumbleObjects': mumbles,
  40. 'MumbleActive': True,
  41. 'MEDIA_URL': settings.MEDIA_URL,
  42. },
  43. context_instance = RequestContext(request)
  44. );
  45. def show( request, server ):
  46. """Display the channel list for the given Server ID.
  47. This includes not only the channel list itself, but indeed the user registration,
  48. server admin and user texture form as well. The template then uses JavaScript
  49. to display these forms integrated into the Channel viewer.
  50. """
  51. srv = get_object_or_404( Mumble, id=server );
  52. isAdmin = srv.isUserAdmin( request.user );
  53. # The tab to start on.
  54. displayTab = 0;
  55. if isAdmin:
  56. if request.method == 'POST' and "mode" in request.POST and request.POST['mode'] == 'admin':
  57. adminform = MumbleForm( request.POST, instance=srv );
  58. if adminform.is_valid():
  59. adminform.save();
  60. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  61. else:
  62. displayTab = 2;
  63. else:
  64. adminform = MumbleForm( instance=srv );
  65. else:
  66. adminform = None;
  67. registered = False;
  68. user = None;
  69. if request.user.is_authenticated():
  70. # Unregistered users may or may not need a password to register.
  71. if settings.PROTECTED_MODE and srv.passwd:
  72. unregged_user_form = MumbleUserPasswordForm;
  73. else:
  74. unregged_user_form = MumbleUserForm;
  75. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'reg':
  76. try:
  77. user = MumbleUser.objects.get( server=srv, owner=request.user );
  78. except MumbleUser.DoesNotExist:
  79. regform = unregged_user_form( request.POST );
  80. regform.server = srv;
  81. if regform.is_valid():
  82. model = regform.save( commit=False );
  83. model.isAdmin = False;
  84. model.server = srv;
  85. model.owner = request.user;
  86. model.save();
  87. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  88. else:
  89. displayTab = 1;
  90. else:
  91. regform = MumbleUserForm( request.POST, instance=user );
  92. if regform.is_valid():
  93. regform.save();
  94. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  95. else:
  96. displayTab = 1;
  97. else:
  98. try:
  99. user = MumbleUser.objects.get( server=srv, owner=request.user );
  100. except MumbleUser.DoesNotExist:
  101. regform = unregged_user_form();
  102. else:
  103. regform = MumbleUserForm( instance=user );
  104. registered = True;
  105. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'texture' and registered:
  106. textureform = MumbleTextureForm( request.POST, request.FILES );
  107. if textureform.is_valid():
  108. user.setTexture( request.FILES['texturefile'] );
  109. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  110. else:
  111. textureform = MumbleTextureForm();
  112. else:
  113. regform = None;
  114. textureform = None;
  115. # ChannelTable is a somewhat misleading name, as it actually contains channels and players.
  116. channelTable = [];
  117. for id in srv.channels:
  118. if id != 0 and srv.channels[id].show:
  119. channelTable.append( srv.channels[id] );
  120. for id in srv.players:
  121. channelTable.append( srv.players[id] );
  122. show_url = reverse( show, kwargs={ 'server': srv.id } );
  123. login_url = reverse( auth_views.login );
  124. return render_to_response(
  125. 'mumble/mumble.htm',
  126. {
  127. 'MEDIA_URL': settings.MEDIA_URL,
  128. 'login_url': "%s?next=%s" % ( login_url, show_url ),
  129. 'DBaseObject': srv,
  130. 'ChannelTable': channelTable,
  131. 'CurrentUserIsAdmin': isAdmin,
  132. 'AdminForm': adminform,
  133. 'RegForm': regform,
  134. 'TextureForm': textureform,
  135. 'Registered': registered,
  136. 'DisplayTab': displayTab,
  137. 'MumbleActive': True,
  138. 'MumbleAccount':user,
  139. },
  140. context_instance = RequestContext(request)
  141. );
  142. def showTexture( request, server, userid = None ):
  143. """ Pack the given user's texture into an HttpResponse.
  144. If userid is none, use the currently logged in User.
  145. """
  146. srv = get_object_or_404( Mumble, id=int(server) );
  147. if userid is None:
  148. if request.user.is_authenticated():
  149. user = get_object_or_404( MumbleUser, server=srv, owner=request.user );
  150. else:
  151. raise Http404();
  152. else:
  153. user = get_object_or_404( MumbleUser, server=srv, id=int(userid) );
  154. try:
  155. img = user.getTexture();
  156. except ValueError:
  157. raise Http404();
  158. else:
  159. buffer = StringIO();
  160. img.save( buffer, "PNG" );
  161. return HttpResponse( buffer.getvalue(), "image/png" );
  162. @login_required
  163. def users( request, server ):
  164. """ Create a list of MumbleUsers for a given server serialized as a JSON object.
  165. If the request has a "data" field, evaluate that and update the user records.
  166. """
  167. srv = get_object_or_404( Mumble, id=int(server) );
  168. if not srv.isUserAdmin( request.user ):
  169. return HttpResponse(
  170. simplejson.dumps({ 'success': False, 'objects': [], 'errormsg': 'Access denied' }),
  171. mimetype='text/javascript'
  172. );
  173. if request.method == 'POST':
  174. data = simplejson.loads( request.POST['data'] );
  175. for record in data:
  176. if record['id'] == -1:
  177. if record['delete']:
  178. continue;
  179. mu = MumbleUser( server=srv );
  180. else:
  181. mu = MumbleUser.objects.get( id=record['id'] );
  182. if record['delete']:
  183. mu.delete();
  184. continue;
  185. mu.name = record['name'];
  186. mu.password = record['password'];
  187. mu.isAdmin = record['admin'];
  188. if record['owner']:
  189. mu.owner = User.objects.get( id=int(record['owner']) );
  190. mu.save();
  191. users = [];
  192. for mu in srv.mumbleuser_set.all():
  193. owner = None;
  194. if mu.owner is not None:
  195. owner = mu.owner.id
  196. users.append( {
  197. 'id': mu.id,
  198. 'name': mu.name,
  199. 'password': None,
  200. 'owner': owner,
  201. 'admin': mu.getAdmin(),
  202. } );
  203. return HttpResponse(
  204. simplejson.dumps( { 'success': True, 'objects': users } ),
  205. mimetype='text/javascript'
  206. );
  207. @login_required
  208. def djangousers( request ):
  209. """ Return a list of all Django users' names and IDs. """
  210. users = [ { 'uid': '', 'uname': '------' } ];
  211. for du in User.objects.all().order_by( 'username' ):
  212. users.append( {
  213. 'uid': du.id,
  214. 'uname': unicode( du ),
  215. } );
  216. return HttpResponse(
  217. simplejson.dumps( { 'success': True, 'objects': users } ),
  218. mimetype='text/javascript'
  219. );