/laudio/src/javascript.py

https://github.com/jgehring/Laudio · Python · 94 lines · 47 code · 13 blank · 34 comment · 12 complexity · de46423454ddb5030ff10807909db6ab MD5 · raw file

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. """
  4. Laudio - A webbased musicplayer
  5. Copyright (C) 2010 Bernhard Posselt, bernhard.posselt@gmx.at
  6. Laudio is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. Laudio 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. You should have received a copy of the GNU General Public License
  15. along with Foobar. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. from django.template import Context, Template
  18. from django.conf import settings
  19. from laudio.models import Settings
  20. import os
  21. class JavaScript(object):
  22. """This class is for enabling django template vars and template syntax
  23. in javascript files and manipulating the js for different views"""
  24. def __init__(self, view, request):
  25. """First we set the functions and files we have to include for
  26. the view we serve
  27. Keyword arguments:
  28. view -- can be: "library", "settings" or "playlist"; sets javascript
  29. according to those views
  30. """
  31. self.view = view
  32. files = []
  33. """check settings values"""
  34. try:
  35. config = Settings.objects.get(pk=1)
  36. if request.user.is_authenticated():
  37. if request.user.get_profile().showLib and self.view == "library":
  38. files.append("func/autoload.js")
  39. else:
  40. if config.showLib and self.view == "library":
  41. files.append("func/autoload.js")
  42. except Settings.DoesNotExist, AttributeError:
  43. pass
  44. """Depending on the view, different js files are being included.
  45. We specify the ones we want to load with a files tuple, path
  46. starting from src/javascript/"""
  47. if self.view == "library":
  48. files.append("inc/includes.js")
  49. files.append("ui/collection.js")
  50. files.append("ui/controls.js")
  51. files.append("ui/tablesorting.js")
  52. files.append("ui/playlist.js")
  53. files.append("ui/nav.js")
  54. files.append("func/player.js")
  55. files.append("func/search.js")
  56. elif self.view == "settings":
  57. files.append("inc/includes.js",)
  58. files.append("ui/settings.js",)
  59. else:
  60. pass
  61. content = ""
  62. # loop over files and build the content
  63. for f in files:
  64. # get the javascript from the file
  65. fh = os.path.join(settings.INSTALL_DIR, "src/javascript/%s" % f )
  66. file = open(fh, 'r')
  67. content += file.read()
  68. file.close()
  69. # create template and parse context
  70. tpl = Template(content)
  71. context = Context( {} )
  72. self.javascript = tpl.render(context)
  73. def __str__(self):
  74. return self.javascript