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.

145 lines
4.7 KiB

16 years ago
16 years ago
  1. """ This file is part of the mumble-django application.
  2. Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. - Neither the name of the Mumble Developers nor the names of its
  13. contributors may be used to endorse or promote products derived from this
  14. software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. """
  27. from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
  28. from django.template import RequestContext
  29. from django.http import HttpResponseRedirect
  30. from django.core.urlresolvers import reverse
  31. from django.contrib.auth.decorators import login_required
  32. from models import Mumble, MumbleUser
  33. from mmobjects import mmServer, mmChannel
  34. class Storage( object ):
  35. s = list();
  36. r = None;
  37. def show( request, server ):
  38. "Displays the channel list for the given Server ID."
  39. srv, o = createChannelList( server );
  40. return render_to_response(
  41. 'mumble/mumble.htm',
  42. { 'DBaseObject': srv, 'ServerObject': o, 'ChannelTable': Storage.s },
  43. context_instance = RequestContext(request)
  44. );
  45. def showContent( server, user = None ):
  46. "Renders and returns the channel list for the given Server ID."
  47. from django.template import Context, loader
  48. srv, o = createChannelList( server );
  49. mumbleAcc = None;
  50. if user.is_authenticated():
  51. mmUsers = MumbleUser.objects.filter( owner = user );
  52. if mmUsers:
  53. mumbleAcc = mmUsers[0];
  54. t_content = loader.get_template( 'mumble/content.htm' );
  55. c_content = Context( { 'DBaseObject': srv, 'ServerObject': o, 'ChannelTable': Storage.s, 'user': user, 'mumbleAccount': mumbleAcc } );
  56. r_content = t_content.render( c_content );
  57. return r_content;
  58. def createChannelList( server ):
  59. "Renders the channel list."
  60. srv = get_object_or_404( Mumble, id=server );
  61. o = srv.getServerObject();
  62. Storage.s = list();
  63. Storage.r = o.channels[0];
  64. o.channels[0].visit( renderListItem, 0 );
  65. return srv, o;
  66. def renderListItem( item, level ):
  67. "Stores a line in the channel list."
  68. if item == Storage.r:
  69. return;
  70. # Filter channels that don't have players in them and are not a subchannel of root
  71. if level > 1 and item.playerCount == 0:
  72. # I used to test if item is an instance of mmChannel here. For some reason, that doesn't work. Dunno why.
  73. return;
  74. if isinstance( item, mmChannel ):
  75. Storage.s.append( ( level, item, item.parentChannels() ) );
  76. else:
  77. Storage.s.append( ( level, item ) );
  78. @login_required
  79. def register( request, server ):
  80. # Register the current user with this mumble server, or give them a form to change their registration data.
  81. srv = Mumble.objects.get( id=server );
  82. if request.user.is_authenticated():
  83. reg = MumbleUser.objects.get( server=srv, owner=request.user );
  84. else:
  85. reg = None;
  86. return render_to_response(
  87. 'mumble/reg.htm',
  88. { 'Mumble': srv, 'Reg': reg },
  89. context_instance = RequestContext(request)
  90. );
  91. @login_required
  92. def savereg( request ):
  93. #if not request.user.is_authenticated():
  94. # raise Exception, "You need to be logged in to register yourself with Mumble.";
  95. srv = Mumble.objects.get( id=request.POST['id'] );
  96. reg = MumbleUser.objects.get( server=srv, owner=request.user );
  97. if reg is None:
  98. reg = MumbleUser( name=request.POST['username'], password=request.POST['password'], server=srv, owner=request.user );
  99. else:
  100. reg.name = request.POST['username'];
  101. reg.password = request.POST['password'];
  102. reg.save();
  103. return HttpResponseRedirect( "/mumble/%d" % srv.id );