def sizes(self): return json.loads(self.request.GET.get('sizes', '[]'))
def test_post_request(self): img_file = open(os.path.join(self.TEST_IMG_DIR, 'img.jpg'), 'rb') data = { u'image': img_file, u'upload_to': [u'test'], u'image_element_id': u'mt_image', u'md5': u'', u'preview_height': u'500', u'preview_width': u'800', u'sizes': u''' [{ "auto": [{ "max_w": null, "retina": 0, "min_h": 1, "name": "lead", "w": 570, "h": null, "min_w": 570, "__type__": "Size", "max_h": null, "label": "Lead" }, { "max_w": null, "retina": 0, "min_h": 110, "name": "featured_small", "w": 170, "h": 110, "min_w": 170, "__type__": "Size", "max_h": null, "label": "Featured Small" }, { "max_w": null, "retina": 0, "min_h": 250, "name": "featured_large", "w": 386, "h": 250, "min_w": 386, "__type__": "Size", "max_h": null, "label": "Featured Large" }], "retina": 0, "name": "lead_large", "h": null, "min_w": 615, "__type__": "Size", "max_h": null, "label": "Lead Large", "max_w": null, "min_h": 250, "w": 615 }]''', } request = self.factory.post(reverse('cropduster-upload'), data) request.user = self.user response = views.upload(request) data = json.loads(response.content) uploaded_img_path = get_media_path(data['url']) self.assertEqual(response.status_code, 200) self.assertTrue(os.path.exists(uploaded_img_path))
def clean(self, ns_dict): for ns, values in six.iteritems(ns_dict): for k, v, opts in values: current = self bits = k.split('/') for bit in bits[:-1]: bit = bit.rpartition(':')[-1] m = re.search(r'^(.*)\[(\d+)\](?=\/|\Z)', bit) if not m: current = current.setdefault(bit, {}) else: bit = m.group(1) index = int(m.group(2)) - 1 if isinstance(current.get(bit), list): if len(current[bit]) < (index + 1): current[bit] += [{}] * (1 + index - len(current[bit])) current = current[bit][index] else: current[bit] = [{}] * (index + 1) current = current[bit][index] if opts.get('VALUE_IS_ARRAY') and not v: v = [] elif opts.get('VALUE_IS_STRUCT') and not v: v = {} ns_prefix, sep, k = bits[-1].rpartition(':') if ns_prefix == 'stDim' and k in ('w, h'): try: v = int(v) except (TypeError, ValueError): v = None elif ns_prefix == 'stArea' and k in ('w', 'h', 'x', 'y'): try: v = float(v) except (TypeError, ValueError): v = None elif k == 'DerivedFrom' and isinstance(v, six.string_types): v = re.sub(r'^xmp\.did:', '', v).lower() elif ns_prefix == 'crop' and k == 'json': v = json.loads(v) m = re.search(r'^(.*)\[(\d+)\](?=\/|\Z)', k) if m: current = current.setdefault(m.group(1), [{}]) k = int(m.group(2)) - 1 if isinstance(current, list) and isinstance(k, six.integer_types): if len(current) <= k: current.append(*([{}] * (1 + k - len(current)))) # Now assign value to current position try: current[k] = v except TypeError: # Special-case if current isn't a dict. current = {k: v} except IndexError: if k != 0 or not isinstance(current, list): raise current = [v]
def clean_size(self): try: return json.loads(self.cleaned_data.get('size', 'null')) except: return None
def clean_thumbs(self): try: return json.loads(self.cleaned_data.get('thumbs', '{}')) except: return {}
def clean_sizes(self): sizes = self.cleaned_data.get('sizes') try: return json.loads(sizes) except: return []
def clean_sizes(self): try: json.loads(self.cleaned_data.get('sizes', '[]')) except: return []
def clean(self, ns_dict): for ns, values in six.iteritems(ns_dict): for k, v, opts in values: current = self bits = k.split('/') for bit in bits[:-1]: bit = bit.rpartition(':')[-1] m = re.search(r'^(.*)\[(\d+)\](?=\/|\Z)', bit) if not m: current = current.setdefault(bit, {}) else: bit = m.group(1) index = int(m.group(2)) - 1 if isinstance(current.get(bit), list): if len(current[bit]) < (index + 1): current[bit] += [{}] * (1 + index - len(current[bit])) current = current[bit][index] else: current[bit] = [{}] * (index + 1) current = current[bit][index] if opts.get('VALUE_IS_ARRAY') and not v: v = [] elif opts.get('VALUE_IS_STRUCT') and not v: v = {} ns_prefix, sep, k = bits[-1].rpartition(':') if ns_prefix == 'stDim' and k in ('w, h'): try: v = int(v) except (TypeError, ValueError): v = None elif ns_prefix == 'stArea' and k in ('w', 'h', 'x', 'y'): try: v = float(v) except (TypeError, ValueError): v = None elif k == 'DerivedFrom' and isinstance(v, six.string_types): v = re.sub(r'^xmp\.did:', '', v).lower() elif ns_prefix == 'crop' and k == 'json': v = json.loads(v) elif ns_prefix == 'crop' and k == 'md5': v = v.lower() m = re.search(r'^(.*)\[(\d+)\](?=\/|\Z)', k) if m: current = current.setdefault(m.group(1), [{}]) k = int(m.group(2)) - 1 if isinstance(current, list) and isinstance( k, six.integer_types): if len(current) <= k: current.append(*([{}] * (1 + k - len(current)))) # Now assign value to current position try: current[k] = v except TypeError: # Special-case if current isn't a dict. current = {k: v} except IndexError: if k != 0 or not isinstance(current, list): raise current = [v]
def test_post_request(self): img_file = open(os.path.join(self.TEST_IMG_DIR, 'img.jpg'), 'rb') data = { u'image': img_file, u'upload_to': [u'test'], u'image_element_id': u'mt_image', u'md5': u'', u'preview_height': u'500', u'preview_width': u'800', u'sizes': u''' [{ "auto": [{ "max_w": null, "retina": 0, "min_h": 1, "name": "lead", "w": 570, "h": null, "min_w": 570, "__type__": "Size", "max_h": null, "label": "Lead" }, { "max_w": null, "retina": 0, "min_h": 110, "name": "featured_small", "w": 170, "h": 110, "min_w": 170, "__type__": "Size", "max_h": null, "label": "Featured Small" }, { "max_w": null, "retina": 0, "min_h": 250, "name": "featured_large", "w": 386, "h": 250, "min_w": 386, "__type__": "Size", "max_h": null, "label": "Featured Large" }], "retina": 0, "name": "lead_large", "h": null, "min_w": 615, "__type__": "Size", "max_h": null, "label": "Lead Large", "max_w": null, "min_h": 250, "w": 615 }]''', } request = self.factory.post(reverse('cropduster-upload'), data) request.user = self.user response = views.upload(request) self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertTrue(default_storage.exists(data['orig_image']))