Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django

172 lines
4.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
  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. from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
  16. from django.template import RequestContext
  17. from django.http import HttpResponseRedirect
  18. from django.core.urlresolvers import reverse
  19. from django.contrib.auth.decorators import login_required
  20. from models import Mumble, MumbleUser
  21. from forms import *
  22. from mmobjects import mmServer, mmChannel
  23. # Handler class for all Server specific views
  24. class Storage( object ):
  25. s = list();
  26. r = None;
  27. def mumbles( request ):
  28. "Displays a list of all configured Mumble servers."
  29. return render_to_response(
  30. 'mumble/list.htm',
  31. { 'MumbleObjects': get_list_or_404( Mumble ),
  32. 'MumbleActive': True,
  33. },
  34. context_instance = RequestContext(request)
  35. );
  36. def show( request, server ):
  37. "Displays the channel list for the given Server ID."
  38. srv, o = createChannelList( server );
  39. isAdmin = srv.isUserAdmin( request.user );
  40. # The tab to start on.
  41. displayTab = 0;
  42. if isAdmin:
  43. if request.method == 'POST' and "mode" in request.POST and request.POST['mode'] == 'admin':
  44. adminform = MumbleForm( request.POST, instance=srv );
  45. # In case we redisplay the page, it was displayed with errors on the admin form, so tell
  46. # Ext to show the admin form tab first.
  47. displayTab = 1;
  48. if adminform.is_valid():
  49. adminform.save();
  50. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  51. else:
  52. adminform = MumbleForm( instance=srv );
  53. else:
  54. adminform = None;
  55. registered = False;
  56. if request.user.is_authenticated():
  57. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'reg':
  58. try:
  59. user = MumbleUser.objects.get( server=srv, owner=request.user );
  60. except MumbleUser.DoesNotExist:
  61. regform = MumbleUserForm( request.POST );
  62. if regform.is_valid():
  63. model = regform.save( commit=False );
  64. model.isAdmin = False;
  65. model.server = srv;
  66. model.owner = request.user;
  67. model.save();
  68. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  69. else:
  70. regform = MumbleUserForm( request.POST, instance=user );
  71. if regform.is_valid():
  72. regform.save();
  73. return HttpResponseRedirect( '/mumble/%d' % int(server) );
  74. else:
  75. try:
  76. user = MumbleUser.objects.get( server=srv, owner=request.user );
  77. except MumbleUser.DoesNotExist:
  78. regform = MumbleUserForm();
  79. else:
  80. regform = MumbleUserForm( instance=user );
  81. registered = True;
  82. else:
  83. regform = None;
  84. return render_to_response(
  85. 'mumble/mumble.htm',
  86. {
  87. 'DBaseObject': srv,
  88. 'ServerObject': o,
  89. 'ChannelTable': Storage.s,
  90. "CurrentUserIsAdmin": isAdmin,
  91. "AdminForm": adminform,
  92. "RegForm": regform,
  93. "Registered": registered,
  94. "DisplayTab": displayTab,
  95. 'MumbleActive': True,
  96. },
  97. context_instance = RequestContext(request)
  98. );
  99. def showContent( server, user = None ):
  100. "Renders and returns the channel list for the given Server ID."
  101. from django.template import Context, loader
  102. srv, o = createChannelList( server );
  103. mumbleAcc = None;
  104. if user.is_authenticated():
  105. mmUsers = MumbleUser.objects.filter( owner = user );
  106. if mmUsers:
  107. mumbleAcc = mmUsers[0];
  108. t_content = loader.get_template( 'mumble/content.htm' );
  109. c_content = Context( {
  110. 'DBaseObject': srv,
  111. 'ServerObject': o,
  112. 'ChannelTable': Storage.s,
  113. 'user': user,
  114. 'mumbleAccount': mumbleAcc,
  115. "CurrentUserIsAdmin": srv.isUserAdmin( request.user ),
  116. 'MumbleActive': True,
  117. } );
  118. r_content = t_content.render( c_content );
  119. return r_content;
  120. def createChannelList( server ):
  121. "Renders the channel list."
  122. srv = get_object_or_404( Mumble, id=server );
  123. o = srv.getServerObject();
  124. Storage.s = list();
  125. Storage.r = o.channels[0];
  126. o.channels[0].visit( renderListItem, 0 );
  127. return srv, o;
  128. def renderListItem( item, level ):
  129. "Stores a line in the channel list."
  130. if item == Storage.r:
  131. return;
  132. # Filter channels that don't have players in them and are not a subchannel of root
  133. if level > 1 and item.playerCount == 0:
  134. # I used to test if item is an instance of mmChannel here. For some reason, that doesn't work. Dunno why.
  135. return;
  136. if isinstance( item, mmChannel ):
  137. Storage.s.append( ( level, item, item.parentChannels() ) );
  138. else:
  139. Storage.s.append( ( level, item ) );