__init__.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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(name, width=0, height=0, crop=False, pre_cache=False, increment_count=False):
  12. try:
  13. size = PhotoSize.objects.get(name=name)
  14. exists = True
  15. except PhotoSize.DoesNotExist:
  16. size = PhotoSize(name=name)
  17. exists = False
  18. if exists:
  19. msg = 'A "%s" photo size already exists. Do you want to replace it? (yes, no):' % name
  20. if not get_response(msg, lambda inp: inp == 'yes', False):
  21. return
  22. print '\nWe will now define the "%s" photo size:\n' % size
  23. w = get_response('Width (in pixels):', lambda inp: int(inp), width)
  24. h = get_response('Height (in pixels):', lambda inp: int(inp), height)
  25. c = get_response('Crop to fit? (yes, no):', lambda inp: inp == 'yes', crop)
  26. p = get_response('Pre-cache? (yes, no):', lambda inp: inp == 'yes', pre_cache)
  27. i = get_response('Increment count? (yes, no):', lambda inp: inp == 'yes', increment_count)
  28. size.width = w
  29. size.height = h
  30. size.crop = c
  31. size.pre_cache = p
  32. size.increment_count = i
  33. size.save()
  34. print '\nA "%s" photo size has been created.\n' % name
  35. return size