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.

245 lines
6.8 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
15 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 django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
  18. from django.template import RequestContext
  19. from django.http import Http404, HttpResponse, HttpResponseRedirect
  20. from django.contrib.auth.decorators import login_required
  21. from django.contrib.auth.models import User
  22. from django.conf import settings
  23. from models import Mumble, MumbleUser
  24. from forms import *
  25. from mmobjects import *
  26. def mumbles( request ):
  27. """Display a list of all configured Mumble servers, or redirects if only one configured."""
  28. mumbles = get_list_or_404( Mumble );
  29. if len(mumbles) == 1:
  30. return HttpResponseRedirect( '/mumble/%d' % mumbles[0].id );
  31. return render_to_response(
  32. 'mumble/list.htm',
  33. { 'MumbleObjects': mumbles,
  34. 'MumbleActive': True,
  35. },
  36. context_instance = RequestContext(request)
  37. );
  38. def show( request, server ):
  39. """Display the channel list for the given Server ID.
  40. This includes not only the channel list itself, but indeed the user registration,
  41. server admin and user texture form as well. The template then uses JavaScript
  42. to display these forms integrated into the Channel viewer.
  43. """
  44. srv = get_object_or_404( Mumble, id=server );
  45. isAdmin = srv.isUserAdmin( request.user );
  46. # The tab to start on.
  47. displayTab = 0;
  48. if isAdmin:
  49. if request.method == 'POST' and "mode" in request.POST and request.POST['mode'] == 'admin':
  50. adminform = MumbleForm( request.POST, instance=srv );
  51. if adminform.is_valid():
  52. adminform.save();
  53. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  54. else:
  55. displayTab = 2;
  56. else:
  57. adminform = MumbleForm( instance=srv );
  58. else:
  59. adminform = None;
  60. registered = False;
  61. user = None;
  62. if request.user.is_authenticated():
  63. # Unregistered users may or may not need a password to register.
  64. if settings.PROTECTED_MODE and srv.passwd:
  65. unregged_user_form = MumbleUserPasswordForm;
  66. else:
  67. unregged_user_form = MumbleUserForm;
  68. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'reg':
  69. try:
  70. user = MumbleUser.objects.get( server=srv, owner=request.user );
  71. except MumbleUser.DoesNotExist:
  72. regform = unregged_user_form( request.POST );
  73. regform.server = srv;
  74. if regform.is_valid():
  75. model = regform.save( commit=False );
  76. model.isAdmin = False;
  77. model.server = srv;
  78. model.owner = request.user;
  79. model.save();
  80. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  81. else:
  82. displayTab = 1;
  83. else:
  84. regform = MumbleUserForm( request.POST, instance=user );
  85. if regform.is_valid():
  86. regform.save();
  87. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  88. else:
  89. displayTab = 1;
  90. else:
  91. try:
  92. user = MumbleUser.objects.get( server=srv, owner=request.user );
  93. except MumbleUser.DoesNotExist:
  94. regform = unregged_user_form();
  95. else:
  96. regform = MumbleUserForm( instance=user );
  97. registered = True;
  98. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'texture' and registered:
  99. textureform = MumbleTextureForm( request.POST, request.FILES );
  100. if textureform.is_valid():
  101. user.setTexture( request.FILES['texturefile'] );
  102. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  103. else:
  104. textureform = MumbleTextureForm();
  105. else:
  106. regform = None;
  107. textureform = None;
  108. # ChannelTable is a somewhat misleading name, as it actually contains channels and players.
  109. channelTable = [];
  110. for id in srv.channels:
  111. if id != 0 and srv.channels[id].show:
  112. channelTable.append( srv.channels[id] );
  113. for id in srv.players:
  114. channelTable.append( srv.players[id] );
  115. return render_to_response(
  116. 'mumble/mumble.htm',
  117. {
  118. 'DBaseObject': srv,
  119. 'ChannelTable': channelTable,
  120. 'CurrentUserIsAdmin': isAdmin,
  121. 'AdminForm': adminform,
  122. 'RegForm': regform,
  123. 'TextureForm': textureform,
  124. 'Registered': registered,
  125. 'DisplayTab': displayTab,
  126. 'MumbleActive': True,
  127. 'MumbleAccount':user,
  128. },
  129. context_instance = RequestContext(request)
  130. );
  131. def showTexture( request, server, userid = None ):
  132. """Pack the currently logged in user's texture (if any) into an HttpResponse."""
  133. srv = get_object_or_404( Mumble, id=int(server) );
  134. if userid is None:
  135. if request.user.is_authenticated():
  136. user = get_object_or_404( MumbleUser, server=srv, owner=request.user );
  137. else:
  138. raise Http404();
  139. else:
  140. user = get_object_or_404( MumbleUser, server=srv, id=int(userid) );
  141. try:
  142. img = user.getTexture();
  143. except ValueError:
  144. raise Http404();
  145. else:
  146. buffer = StringIO();
  147. img.save( buffer, "PNG" );
  148. return HttpResponse( buffer.getvalue(), "image/png" );
  149. @login_required
  150. def users( request, server ):
  151. srv = get_object_or_404( Mumble, id=int(server) );
  152. if not srv.isUserAdmin( request.user ):
  153. return HttpResponse(
  154. simplejson.dumps({ 'success': False, 'objects': [], 'errormsg': 'Access denied' }),
  155. mimetype='text/javascript'
  156. );
  157. if request.method == 'POST':
  158. data = simplejson.loads( request.POST['data'] );
  159. for record in data:
  160. if record['id'] == -1:
  161. if record['delete']:
  162. continue;
  163. mu = MumbleUser( server=srv );
  164. else:
  165. mu = MumbleUser.objects.get( id=record['id'] );
  166. if record['delete']:
  167. mu.delete();
  168. continue;
  169. mu.name = record['name'];
  170. mu.password = record['password'];
  171. mu.isAdmin = record['admin'];
  172. if record['owner']:
  173. mu.owner = User.objects.get( id=int(record['owner']) );
  174. mu.save();
  175. users = [];
  176. for mu in srv.mumbleuser_set.all():
  177. owner = None;
  178. if mu.owner is not None:
  179. owner = mu.owner.id
  180. users.append( {
  181. 'id': mu.id,
  182. 'name': mu.name,
  183. 'password': None,
  184. 'owner': owner,
  185. 'admin': mu.getAdmin(),
  186. } );
  187. return HttpResponse(
  188. simplejson.dumps( { 'success': True, 'objects': users } ),
  189. mimetype='text/javascript'
  190. );
  191. @login_required
  192. def djangousers( request ):
  193. "Return a list of all Django users' names and IDs."
  194. users = [ { 'uid': '', 'uname': '------' } ];
  195. for du in User.objects.all().order_by( 'username' ):
  196. users.append( {
  197. 'uid': du.id,
  198. 'uname': unicode( du ),
  199. } );
  200. return HttpResponse(
  201. simplejson.dumps( { 'success': True, 'objects': users } ),
  202. mimetype='text/javascript'
  203. );