filer.utils.files: 30 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/filer/utils/files.py

Stats: 0 executed, 26 missed, 4 excluded, 24 ignored

  1. #-*- coding: utf-8 -*-
  2. import os
  3. from django.utils.text import get_valid_filename as get_valid_filename_django
  4. from django.template.defaultfilters import slugify
  5. from django.core.files.uploadedfile import SimpleUploadedFile
  6. class UploadException(Exception):
  7. pass
  8. def handle_upload(request):
  9. if not request.method == "POST":
  10. raise UploadException("AJAX request not valid: must be POST")
  11. if request.is_ajax():
  12. # the file is stored raw in the request
  13. is_raw = True
  14. filename = request.GET.get('qqfile', False) or request.GET.get('filename', False) or ''
  15. if hasattr(request, 'body'):
  16. # raw_post_data was depreciated in django 1.4:
  17. # https://docs.djangoproject.com/en/dev/releases/1.4/#httprequest-raw-post-data-renamed-to-httprequest-body
  18. data = request.body
  19. else:
  20. # fallback for django 1.3
  21. data = request.raw_post_data
  22. upload = SimpleUploadedFile(name=filename, content=data)
  23. else:
  24. if len(request.FILES) == 1:
  25. # FILES is a dictionary in Django but Ajax Upload gives the uploaded file an
  26. # ID based on a random number, so it cannot be guessed here in the code.
  27. # Rather than editing Ajax Upload to pass the ID in the querystring, note that
  28. # each upload is a separate request so FILES should only have one entry.
  29. # Thus, we can just grab the first (and only) value in the dict.
  30. is_raw = False
  31. upload = request.FILES.values()[0]
  32. filename = upload.name
  33. else:
  34. raise UploadException("AJAX request not valid: Bad Upload")
  35. return upload, filename, is_raw
  36. def get_valid_filename(s):
  37. """
  38. like the regular get_valid_filename, but also slugifies away
  39. umlauts and stuff.
  40. """
  41. s = get_valid_filename_django(s)
  42. filename, ext = os.path.splitext(s)
  43. filename = slugify(filename)
  44. ext = slugify(ext)
  45. if ext:
  46. return u"%s.%s" % (filename, ext)
  47. else:
  48. return u"%s" % (filename,)