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.

113 lines
3.0 KiB

16 years ago
  1. from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
  2. from django.template import RequestContext
  3. from django.http import HttpResponseRedirect
  4. from django.core.urlresolvers import reverse
  5. from django.contrib.auth.decorators import login_required
  6. from models import Mumble, MumbleUser
  7. from mmobjects import mmServer, mmChannel
  8. class Storage( object ):
  9. s = list();
  10. r = None;
  11. def show( request, server ):
  12. "Displays the channel list for the given Server ID."
  13. srv, o = createChannelList( server );
  14. return render_to_response(
  15. 'mumble/mumble.htm',
  16. { 'DBaseObject': srv, 'ServerObject': o, 'ChannelTable': Storage.s },
  17. context_instance = RequestContext(request)
  18. );
  19. def showContent( server, user = None ):
  20. "Renders and returns the channel list for the given Server ID."
  21. from django.template import Context, loader
  22. srv, o = createChannelList( server );
  23. mumbleAcc = None;
  24. if user is not None:
  25. mmUsers = MumbleUser.objects.filter( owner = user );
  26. if mmUsers:
  27. mumbleAcc = mmUsers[0];
  28. t_content = loader.get_template( 'mumble/content.htm' );
  29. c_content = Context( { 'DBaseObject': srv, 'ServerObject': o, 'ChannelTable': Storage.s, 'user': user, 'mumbleAccount': mumbleAcc } );
  30. r_content = t_content.render( c_content );
  31. return r_content;
  32. def createChannelList( server ):
  33. "Renders the channel list."
  34. srv = get_object_or_404( Mumble, id=server );
  35. o = srv.getServerObject();
  36. Storage.s = list();
  37. Storage.r = o.channels[0];
  38. o.channels[0].visit( renderListItem, 0 );
  39. return srv, o;
  40. def renderListItem( item, level ):
  41. "Stores a line in the channel list."
  42. if item == Storage.r:
  43. return;
  44. # Filter channels that don't have players in them and are not a subchannel of root
  45. if level > 1 and item.playerCount == 0:
  46. # I used to test if item is an instance of mmChannel here. For some reason, that doesn't work. Dunno why.
  47. return;
  48. if isinstance( item, mmChannel ):
  49. Storage.s.append( ( level, item, item.parentChannels() ) );
  50. else:
  51. Storage.s.append( ( level, item ) );
  52. @login_required
  53. def register( request, server ):
  54. # Register the current user with this mumble server, or give them a form to change their registration data.
  55. srv = Mumble.objects.get( id=server );
  56. if request.user.is_authenticated():
  57. reg = MumbleUser.objects.get( server=srv, owner=request.user );
  58. else:
  59. reg = None;
  60. return render_to_response(
  61. 'mumble/reg.htm',
  62. { 'Mumble': srv, 'Reg': reg },
  63. context_instance = RequestContext(request)
  64. );
  65. @login_required
  66. def savereg( request ):
  67. #if not request.user.is_authenticated():
  68. # raise Exception, "You need to be logged in to register yourself with Mumble.";
  69. srv = Mumble.objects.get( id=request.POST['id'] );
  70. reg = MumbleUser.objects.get( server=srv, owner=request.user );
  71. if reg is None:
  72. reg = MumbleUser( name=request.POST['username'], password=request.POST['password'], server=srv, owner=request.user );
  73. else:
  74. reg.name = request.POST['username'];
  75. reg.password = request.POST['password'];
  76. reg.save();
  77. return HttpResponseRedirect( "/mumble/%d" % srv.id );