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.

189 lines
5.9 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
  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. 'MumbleActive': True,
  46. },
  47. context_instance = RequestContext(request)
  48. );
  49. def show( request, server ):
  50. "Displays the channel list for the given Server ID."
  51. srv, o = createChannelList( 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. # In case we redisplay the page, it was displayed with errors on the admin form, so tell
  59. # Ext to show the admin form tab first.
  60. displayTab = 1;
  61. if adminform.is_valid():
  62. adminform.save();
  63. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  64. else:
  65. adminform = MumbleForm( instance=srv );
  66. else:
  67. adminform = None;
  68. registered = False;
  69. if request.user.is_authenticated():
  70. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'reg':
  71. try:
  72. user = MumbleUser.objects.get( server=srv, owner=request.user );
  73. except MumbleUser.DoesNotExist:
  74. regform = MumbleUserForm( request.POST );
  75. if regform.is_valid():
  76. model = regform.save( commit=False );
  77. model.isAdmin = False;
  78. model.server = srv;
  79. model.owner = request.user;
  80. model.save();
  81. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  82. else:
  83. regform = MumbleUserForm( request.POST, instance=user );
  84. if regform.is_valid():
  85. regform.save();
  86. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  87. else:
  88. try:
  89. user = MumbleUser.objects.get( server=srv, owner=request.user );
  90. except MumbleUser.DoesNotExist:
  91. regform = MumbleUserForm();
  92. else:
  93. regform = MumbleUserForm( instance=user );
  94. registered = True;
  95. else:
  96. regform = None;
  97. return render_to_response(
  98. 'mumble/mumble.htm',
  99. {
  100. 'DBaseObject': srv,
  101. 'ServerObject': o,
  102. 'ChannelTable': Storage.s,
  103. "CurrentUserIsAdmin": isAdmin,
  104. "AdminForm": adminform,
  105. "RegForm": regform,
  106. "Registered": registered,
  107. "DisplayTab": displayTab,
  108. 'MumbleActive': True,
  109. },
  110. context_instance = RequestContext(request)
  111. );
  112. def showContent( server, user = None ):
  113. "Renders and returns the channel list for the given Server ID."
  114. from django.template import Context, loader
  115. srv, o = createChannelList( server );
  116. mumbleAcc = None;
  117. if user.is_authenticated():
  118. mmUsers = MumbleUser.objects.filter( owner = user );
  119. if mmUsers:
  120. mumbleAcc = mmUsers[0];
  121. t_content = loader.get_template( 'mumble/content.htm' );
  122. c_content = Context( {
  123. 'DBaseObject': srv,
  124. 'ServerObject': o,
  125. 'ChannelTable': Storage.s,
  126. 'user': user,
  127. 'mumbleAccount': mumbleAcc,
  128. "CurrentUserIsAdmin": srv.isUserAdmin( request.user ),
  129. 'MumbleActive': True,
  130. } );
  131. r_content = t_content.render( c_content );
  132. return r_content;
  133. def createChannelList( server ):
  134. "Renders the channel list."
  135. srv = get_object_or_404( Mumble, id=server );
  136. o = srv.getServerObject();
  137. Storage.s = list();
  138. Storage.r = o.channels[0];
  139. o.channels[0].visit( renderListItem, 0 );
  140. return srv, o;
  141. def renderListItem( item, level ):
  142. "Stores a line in the channel list."
  143. if item == Storage.r:
  144. return;
  145. # Filter channels that don't have players in them and are not a subchannel of root
  146. if level > 1 and item.playerCount == 0:
  147. # I used to test if item is an instance of mmChannel here. For some reason, that doesn't work. Dunno why.
  148. return;
  149. if isinstance( item, mmChannel ):
  150. Storage.s.append( ( level, item, item.parentChannels() ) );
  151. else:
  152. Storage.s.append( ( level, item ) );