/icenine/ice9/views.py

https://github.com/dennisjlee/icenine · Python · 76 lines · 57 code · 13 blank · 6 comment · 9 complexity · 87d6d3c23323720dc8d92ab984aac698 MD5 · raw file

  1. import os
  2. import sys
  3. from django.shortcuts import render_to_response
  4. from django.http import HttpResponse
  5. from icenine.ice9.models import *
  6. def index(request):
  7. return render_to_response('index.djhtml')
  8. type2path = { 'movie': 'movies',
  9. 'tv': 'tv',
  10. 'software': 'software' }
  11. type2prefix = { 'movie': 'movies',
  12. 'tv': 'files',
  13. 'software': 'files' }
  14. def render(request, path, type):
  15. directory = Directory.objects.filter(type=type).get(name='/')
  16. paths_zipped = []
  17. cur_path = '/%s/' % type2path[type]
  18. paths_zipped.append((type2path[type], cur_path))
  19. if len(path) > 1:
  20. for path_component in path[1:].split('/'):
  21. directory = directory.subdirs.get(name=path_component)
  22. cur_path += path_component + '/'
  23. paths_zipped.append((path_component, cur_path))
  24. template = '%s_render_%s.djhtml' % (
  25. type2prefix[type], (request.REQUEST.has_key('hi') and 'hi' or 'low')
  26. )
  27. return render_to_response(template,
  28. {'directory': directory,
  29. 'paths_zipped': paths_zipped,
  30. 'subdirs': directory.subdirs.filter(found=True).order_by('name'),
  31. 'files': directory.files.filter(found=True).order_by('name')})
  32. def recent(request):
  33. files = File.objects.order_by('-addition_date')[:100]
  34. return render_to_response('recent_files.djhtml', {'files': files})
  35. def download(request, path, file, type):
  36. # TODO(djlee): real download page for external users ..
  37. # This is just the internal one that launches the file ..
  38. if path == "":
  39. relative_path = file
  40. else:
  41. if path[0] == '/':
  42. path = path[1:]
  43. relative_path = path + '/' + file
  44. file = File.objects.filter(type=type).get(relative_path__iexact=relative_path)
  45. try:
  46. #os.system('"c:\Program Files\Zoom Player\zplayer.exe" "%s"' % file.path)
  47. p = os.path.normpath(file.path)
  48. sys.stderr.write("Launching file, %s\n" % p)
  49. sys.stderr.flush()
  50. os.startfile(p)
  51. except Exception, e:
  52. sys.stderr.write("Exception when launching local file, %s\n" % e)
  53. sys.stderr.flush()
  54. # NOTE(djlee): if file associations get messed up, uncomment the following
  55. # line to get a zplayer instance launched as the admin user - can fix the
  56. # associations in there.
  57. os.system('"c:\Program Files\Zoom Player\zplayer.exe"')
  58. html = """
  59. <html>
  60. <body onload="window.close()"></body>
  61. </html>
  62. """
  63. return HttpResponse(html)