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.

246 lines
7.4 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
16 years ago
16 years ago
16 years ago
  1. # -*- coding: utf-8 -*-
  2. """ This file is part of the mumble-django application.
  3. Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. - Neither the name of the Mumble Developers nor the names of its
  14. contributors may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. """
  28. from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
  29. from django.template import RequestContext
  30. from django.http import HttpResponseRedirect
  31. from django.core.urlresolvers import reverse
  32. from django.contrib.auth.decorators import login_required
  33. from models import Mumble, MumbleUser
  34. from forms import *
  35. from mmobjects import mmServer, mmChannel
  36. # Handler class for all Server specific views
  37. class Storage( object ):
  38. s = list();
  39. r = None;
  40. def mumbles( request ):
  41. "Displays a list of all configured Mumble servers."
  42. return render_to_response(
  43. 'mumble/list.htm',
  44. { 'MumbleObjects': get_list_or_404( Mumble ) },
  45. context_instance = RequestContext(request)
  46. );
  47. def show( request, server ):
  48. "Displays the channel list for the given Server ID."
  49. srv, o = createChannelList( server );
  50. isAdmin = srv.isUserAdmin( request.user );
  51. if isAdmin:
  52. if request.method == 'POST' and "mode" in request.POST and request.POST['mode'] == 'admin':
  53. adminform = MumbleForm( request.POST, instance=srv );
  54. if adminform.is_valid():
  55. adminform.save();
  56. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  57. else:
  58. adminform = MumbleForm( instance=srv );
  59. else:
  60. adminform = None;
  61. registered = False;
  62. if request.user.is_authenticated():
  63. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'reg':
  64. try:
  65. user = MumbleUser.objects.get( server=srv, owner=request.user );
  66. except MumbleUser.DoesNotExist:
  67. regform = MumbleUserForm( request.POST );
  68. if regform.is_valid():
  69. model = regform.save( commit=False );
  70. model.isAdmin = False;
  71. model.server = srv;
  72. model.owner = request.user;
  73. model.save();
  74. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  75. else:
  76. regform = MumbleUserForm( request.POST, instance=user );
  77. if regform.is_valid():
  78. regform.save();
  79. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  80. else:
  81. try:
  82. user = MumbleUser.objects.get( server=srv, owner=request.user );
  83. except MumbleUser.DoesNotExist:
  84. regform = MumbleUserForm();
  85. else:
  86. regform = MumbleUserForm( instance=user );
  87. registered = True;
  88. else:
  89. regform = None;
  90. return render_to_response(
  91. 'mumble/mumble.htm',
  92. {
  93. 'DBaseObject': srv,
  94. 'ServerObject': o,
  95. 'ChannelTable': Storage.s,
  96. "CurrentUserIsAdmin": isAdmin,
  97. "AdminForm": adminform,
  98. "RegForm": regform,
  99. "Registered": registered,
  100. },
  101. context_instance = RequestContext(request)
  102. );
  103. def showContent( server, user = None ):
  104. "Renders and returns the channel list for the given Server ID."
  105. from django.template import Context, loader
  106. srv, o = createChannelList( server );
  107. mumbleAcc = None;
  108. if user.is_authenticated():
  109. mmUsers = MumbleUser.objects.filter( owner = user );
  110. if mmUsers:
  111. mumbleAcc = mmUsers[0];
  112. t_content = loader.get_template( 'mumble/content.htm' );
  113. c_content = Context( {
  114. 'DBaseObject': srv,
  115. 'ServerObject': o,
  116. 'ChannelTable': Storage.s,
  117. 'user': user,
  118. 'mumbleAccount': mumbleAcc,
  119. "CurrentUserIsAdmin": srv.isUserAdmin( request.user ),
  120. } );
  121. r_content = t_content.render( c_content );
  122. return r_content;
  123. def createChannelList( server ):
  124. "Renders the channel list."
  125. srv = get_object_or_404( Mumble, id=server );
  126. o = srv.getServerObject();
  127. Storage.s = list();
  128. Storage.r = o.channels[0];
  129. o.channels[0].visit( renderListItem, 0 );
  130. return srv, o;
  131. def renderListItem( item, level ):
  132. "Stores a line in the channel list."
  133. if item == Storage.r:
  134. return;
  135. # Filter channels that don't have players in them and are not a subchannel of root
  136. if level > 1 and item.playerCount == 0:
  137. # I used to test if item is an instance of mmChannel here. For some reason, that doesn't work. Dunno why.
  138. return;
  139. if isinstance( item, mmChannel ):
  140. Storage.s.append( ( level, item, item.parentChannels() ) );
  141. else:
  142. Storage.s.append( ( level, item ) );
  143. @login_required
  144. def register( request, server ):
  145. # Register the current user with this mumble server, or give them a form to change their registration data.
  146. srv = Mumble.objects.get( id=server );
  147. if request.user.is_authenticated():
  148. try:
  149. reg = MumbleUser.objects.get( server=srv, owner=request.user );
  150. except MumbleUser.DoesNotExist:
  151. reg = None;
  152. else:
  153. reg = None;
  154. return render_to_response(
  155. 'mumble/reg.htm',
  156. { 'Mumble': srv, 'Reg': reg },
  157. context_instance = RequestContext(request)
  158. );
  159. @login_required
  160. def savereg( request ):
  161. srv = Mumble.objects.get( id=request.POST['server'] );
  162. if request.method == 'POST':
  163. try:
  164. user = MumbleUser.objects.get( server=srv, owner=request.user );
  165. except MumbleUser.DoesNotExist:
  166. form = MumbleUserForm( request.POST );
  167. if form.is_valid():
  168. model = form.save( commit=False );
  169. model.isAdmin = False;
  170. model.server = srv;
  171. model.save();
  172. else:
  173. form = MumbleForm( request.POST, instance=user );
  174. if form.is_valid():
  175. model.save();
  176. return HttpResponseRedirect( '/mumble/%d' % int(serverid) );
  177. else:
  178. form = MumbleForm( instance=murmur );
  179. return render_to_response(
  180. 'mumble/admin.htm',
  181. { "Mumble": murmur, "Adminform": form, "CurrentUserIsAdmin": murmur.isUserAdmin( request.user ) },
  182. context_instance = RequestContext(request)
  183. );
  184. @login_required
  185. def admin( request, serverid ):
  186. murmur = get_object_or_404( Mumble, id=serverid );
  187. if request.method == 'POST':
  188. form = MumbleForm( request.POST, instance=murmur );
  189. if form.is_valid():
  190. form.save();
  191. return HttpResponseRedirect( '/mumble/%d' % int(serverid) );
  192. else:
  193. form = MumbleForm( instance=murmur );
  194. return render_to_response(
  195. 'mumble/admin.htm',
  196. { "Mumble": murmur, "Adminform": form, "CurrentUserIsAdmin": murmur.isUserAdmin( request.user ) },
  197. context_instance = RequestContext(request)
  198. );