bgneal@1: from photologue.models import PhotoSize bgneal@1: bgneal@1: def get_response(msg, func=int, default=None): bgneal@1: while True: bgneal@1: resp = raw_input(msg) bgneal@1: if not resp and default is not None: bgneal@1: return default bgneal@1: try: bgneal@1: return func(resp) bgneal@1: except: bgneal@1: print 'Invalid input.' bgneal@1: bgneal@1: def create_photosize(name, width=0, height=0, crop=False, pre_cache=False, increment_count=False): bgneal@1: try: bgneal@1: size = PhotoSize.objects.get(name=name) bgneal@1: exists = True bgneal@1: except PhotoSize.DoesNotExist: bgneal@1: size = PhotoSize(name=name) bgneal@1: exists = False bgneal@1: if exists: bgneal@1: msg = 'A "%s" photo size already exists. Do you want to replace it? (yes, no):' % name bgneal@1: if not get_response(msg, lambda inp: inp == 'yes', False): bgneal@1: return bgneal@1: print '\nWe will now define the "%s" photo size:\n' % size bgneal@1: w = get_response('Width (in pixels):', lambda inp: int(inp), width) bgneal@1: h = get_response('Height (in pixels):', lambda inp: int(inp), height) bgneal@1: c = get_response('Crop to fit? (yes, no):', lambda inp: inp == 'yes', crop) bgneal@1: p = get_response('Pre-cache? (yes, no):', lambda inp: inp == 'yes', pre_cache) bgneal@1: i = get_response('Increment count? (yes, no):', lambda inp: inp == 'yes', increment_count) bgneal@1: size.width = w bgneal@1: size.height = h bgneal@1: size.crop = c bgneal@1: size.pre_cache = p bgneal@1: size.increment_count = i bgneal@1: size.save() bgneal@1: print '\nA "%s" photo size has been created.\n' % name bgneal@1: return size