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.

159 lines
5.0 KiB

16 years ago
16 years ago
16 years ago
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 mumbles( request ):
  38. "Displays a list of all configured Mumble servers."
  39. return render_to_response(
  40. 'mumble/list.htm',
  41. { 'MumbleObjects': get_list_or_404( Mumble ) },
  42. context_instance = RequestContext(request)
  43. );
  44. def show( request, server ):
  45. "Displays the channel list for the given Server ID."
  46. srv, o = createChannelList( server );
  47. return render_to_response(
  48. 'mumble/mumble.htm',
  49. { 'DBaseObject': srv, 'ServerObject': o, 'ChannelTable': Storage.s },
  50. context_instance = RequestContext(request)
  51. );
  52. def showContent( server, user = None ):
  53. "Renders and returns the channel list for the given Server ID."
  54. from django.template import Context, loader
  55. srv, o = createChannelList( server );
  56. mumbleAcc = None;
  57. if user.is_authenticated():
  58. mmUsers = MumbleUser.objects.filter( owner = user );
  59. if mmUsers:
  60. mumbleAcc = mmUsers[0];
  61. t_content = loader.get_template( 'mumble/content.htm' );
  62. c_content = Context( { 'DBaseObject': srv, 'ServerObject': o, 'ChannelTable': Storage.s, 'user': user, 'mumbleAccount': mumbleAcc } );
  63. r_content = t_content.render( c_content );
  64. return r_content;
  65. def createChannelList( server ):
  66. "Renders the channel list."
  67. srv = get_object_or_404( Mumble, id=server );
  68. o = srv.getServerObject();
  69. Storage.s = list();
  70. Storage.r = o.channels[0];
  71. o.channels[0].visit( renderListItem, 0 );
  72. return srv, o;
  73. def renderListItem( item, level ):
  74. "Stores a line in the channel list."
  75. if item == Storage.r:
  76. return;
  77. # Filter channels that don't have players in them and are not a subchannel of root
  78. if level > 1 and item.playerCount == 0:
  79. # I used to test if item is an instance of mmChannel here. For some reason, that doesn't work. Dunno why.
  80. return;
  81. if isinstance( item, mmChannel ):
  82. Storage.s.append( ( level, item, item.parentChannels() ) );
  83. else:
  84. Storage.s.append( ( level, item ) );
  85. @login_required
  86. def register( request, server ):
  87. # Register the current user with this mumble server, or give them a form to change their registration data.
  88. srv = Mumble.objects.get( id=server );
  89. if request.user.is_authenticated():
  90. try:
  91. reg = MumbleUser.objects.get( server=srv, owner=request.user );
  92. except MumbleUser.DoesNotExist:
  93. reg = None;
  94. else:
  95. reg = None;
  96. return render_to_response(
  97. 'mumble/reg.htm',
  98. { 'Mumble': srv, 'Reg': reg },
  99. context_instance = RequestContext(request)
  100. );
  101. @login_required
  102. def savereg( request ):
  103. #if not request.user.is_authenticated():
  104. # raise Exception, "You need to be logged in to register yourself with Mumble.";
  105. srv = Mumble.objects.get( id=request.POST['id'] );
  106. try:
  107. reg = MumbleUser.objects.get( server=srv, owner=request.user );
  108. except MumbleUser.DoesNotExist:
  109. reg = None;
  110. if reg is None:
  111. reg = MumbleUser( name=request.POST['username'], password=request.POST['password'], server=srv, owner=request.user );
  112. else:
  113. reg.name = request.POST['username'];
  114. reg.password = request.POST['password'];
  115. reg.save();
  116. return HttpResponseRedirect( "/mumble/%d" % srv.id );