__init__.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from photologue.models import PhotoSize
  2. def get_response(msg, func=int, default=None):
  3. while True:
  4. resp = raw_input(msg)
  5. if not resp and default is not None:
  6. return default
  7. try:
  8. return func(resp)
  9. except:
  10. print "Invalid input."
  11. def create_photosize(
  12. name, width=0, height=0, crop=False, pre_cache=False, increment_count=False
  13. ):
  14. try:
  15. size = PhotoSize.objects.get(name=name)
  16. exists = True
  17. except PhotoSize.DoesNotExist:
  18. size = PhotoSize(name=name)
  19. exists = False
  20. if exists:
  21. msg = (
  22. 'A "%s" photo size already exists. Do you want to replace it? (yes, no):'
  23. % name
  24. )
  25. if not get_response(msg, lambda inp: inp == "yes", False):
  26. return
  27. print '\nWe will now define the "%s" photo size:\n' % size
  28. w = get_response("Width (in pixels):", lambda inp: int(inp), width)
  29. h = get_response("Height (in pixels):", lambda inp: int(inp), height)
  30. c = get_response("Crop to fit? (yes, no):", lambda inp: inp == "yes", crop)
  31. p = get_response("Pre-cache? (yes, no):", lambda inp: inp == "yes", pre_cache)
  32. i = get_response(
  33. "Increment count? (yes, no):", lambda inp: inp == "yes", increment_count
  34. )
  35. size.width = w
  36. size.height = h
  37. size.crop = c
  38. size.pre_cache = p
  39. size.increment_count = i
  40. size.save()
  41. print '\nA "%s" photo size has been created.\n' % name
  42. return size