def test_path(self): """Tag should accept a path as its argument.""" storage_path = self.create_image('100x100.png') helper = AdjustmentHelper(storage_path, width=50, height=50, adjustment='fit') t = Template("{% load daguerre %}{% adjust image width=50 height=50 adjustment='fit' %}") c = Context({'image': storage_path}) self.assertEqual(t.render(c), helper.info_dict()['url'])
def test_file(self): """Tag should accept an :class:`ImageFieldFile` as its argument.""" storage_path = self.create_image('100x100.png') adjusted = AdjustedImage() adjusted.adjusted = storage_path helper = AdjustmentHelper(storage_path, width=50, height=50, adjustment='fit') t = Template("{% load daguerre %}{% adjust image width=50 height=50 adjustment='fit' %}") c = Context({'image': adjusted.adjusted}) self.assertEqual(t.render(c), helper.info_dict()['url'])
def test_nonexistant(self): """ A 404 should be raised if the original image doesn't exist. """ factory = RequestFactory() storage_path = 'nonexistant.png' helper = AdjustmentHelper(storage_path, width=10, height=10) self.view.kwargs = {'storage_path': storage_path} self.view.request = factory.get('/', helper.to_querydict(secure=True)) self.assertRaises(Http404, self.view.get, self.view.request)
def render(self, context): # storage_path might be an ImageFile. storage_path = self.storage_path.resolve(context) kwargs = dict((k, v.resolve(context)) for k, v in self.kwargs.iteritems()) helper = AdjustmentHelper(storage_path, **kwargs) info_dict = helper.info_dict() if self.asvar is not None: context[self.asvar] = info_dict return '' return info_dict
def test_nonexistant(self): """ A 404 should be raised if the original image doesn't exist. """ factory = RequestFactory() storage_path = 'nonexistant.png' helper = AdjustmentHelper(storage_path, width=10, height=5) self.view.kwargs = {'storage_path': storage_path} get_params = helper.to_querydict() self.view.request = factory.get('/', get_params, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertRaises(Http404, self.view.get, self.view.request)
def test_check_security(self): """A 404 should be raised if the security hash is missing or incorrect.""" storage_path = 'path/to/thing.jpg' helper = AdjustmentHelper(storage_path, width=10, height=5, crop='face') factory = RequestFactory() self.view.kwargs = {'storage_path': storage_path} get_params = {} self.view.request = factory.get('/', get_params) self.assertRaises(Http404, self.view.get_helper) get_params = {AdjustmentHelper.param_map['security']: 'fake!'} self.view.request = factory.get('/', get_params) self.assertRaises(Http404, self.view.get_helper) get_params = helper.to_querydict(secure=True) self.view.request = factory.get('/', get_params)
def get_helper(self): try: return AdjustmentHelper.from_querydict(self.kwargs['storage_path'], self.request.GET, secure=self.secure) except ValueError, e: raise Http404(e.message)
def handle_noargs(self, **options): if not hasattr(settings, 'DAGUERRE_PREADJUSTMENTS'): raise CommandError(NO_ADJUSTMENTS) adjustments = settings.DAGUERRE_PREADJUSTMENTS args = [] for (model_or_queryset, lookup), kwargs_list in adjustments.iteritems(): if isinstance(model_or_queryset, basestring): app_label, model_name = model_or_queryset.split('.') model_or_queryset = get_model(app_label, model_name) if issubclass(model_or_queryset, Model): queryset = model_or_queryset.objects.all() elif isinstance(model_or_queryset, QuerySet): queryset = model_or_queryset._clone() else: raise CommandError("Invalid model or queryset: {0}".format(model_or_queryset)) for kwargs in kwargs_list: args.append((queryset, lookup, kwargs)) skipped_count = 0 remaining = [] remaining_count = 0 for queryset, lookup, kwargs in args: bulk_helper = BulkAdjustmentHelper(queryset, lookup, **kwargs) query_kwargs = bulk_helper.get_query_kwargs() adjusted_images = AdjustedImage.objects.filter(**query_kwargs) for adjusted_image in adjusted_images: try: del bulk_helper.remaining[adjusted_image.storage_path] except KeyError: pass else: skipped_count += 1 remaining_count += len(bulk_helper.remaining) remaining.append((bulk_helper.remaining.keys(), kwargs)) self.stdout.write("Skipped {0} path{1} which have already been adjusted.\n".format(skipped_count, pluralize(skipped_count))) if remaining_count == 0: self.stdout.write("No paths remaining to adjust.\n") else: self.stdout.write("Adjusting {0} path{1}... ".format(remaining_count, pluralize(remaining_count))) self.stdout.flush() failed_count = 0 for storage_paths, kwargs in remaining: for storage_path in storage_paths: helper = AdjustmentHelper(storage_path, **kwargs) try: helper.adjust() except IOError: failed_count += 1 self.stdout.write("Done.\n") if failed_count: self.stdout.write("{0} path{1} failed due to I/O errors.".format(failed_count, pluralize(failed_count))) if options['remove']: queryset = AdjustedImage.objects.all() for qs, lookup, kwargs in args: bulk_helper = BulkAdjustmentHelper(qs, lookup, **kwargs) query_kwargs = bulk_helper.get_query_kwargs() queryset = queryset.exclude(**query_kwargs) count = queryset.count() if count == 0: self.stdout.write("No adjusted images found to remove.\n") else: self.stdout.write("Removing {0} adjusted image{1}... ".format(count, pluralize(count))) self.stdout.flush() queryset.delete() self.stdout.write("Done.\n") # For pre-1.5: add an extra newline. self.stdout.write("\n")