def _update_search_text(self):
     search_fields = filter(bool, [
         self.first_name, self.last_name, self.line1, self.line2,
         self.line3, self.line4, self.state, self.postcode,
         self.country.name
     ])
     self.search_text = ' '.join(search_fields)
Example #2
0
 def _allocate_to_hkvms(self, vm, hkvms):
     # Check some relevant errors
     iter_hkvm = iter(hkvms)
     try:
         not_empty_hkvm_group = itertools.chain([next(iter_hkvm)],
                                                iter_hkvm)
     except StopIteration:
         raise ValueError('Empty group or selection')
     try:
         hkvm_ok = filter(lambda hkvm: hkvm.last_status == 'OK',
                          not_empty_hkvm_group)
         hkvm_status_ok = itertools.chain([next(hkvm_ok)], hkvm_ok)
     except StopIteration:
         raise ValueError('All HKVM in the selection are failing')
     iter_weight = self._iter_hkvm_weight(hkvm_status_ok, vm)
     hkvm, weight = max(iter_weight, key=operator.itemgetter(1))
     if weight <= 0:
         # All weight are below zero: no hkvm can fit
         raise ValueError('No HKVM can fit the size of the VM')
     else:
         hkvm.memory -= vm.memory
         hkvm.disk -= vm.disk
         vm.hkvm = hkvm
         vm.status = 'TO_CREATE'
         self.logger.info('\'{}\' goes to \'{}\''.format(vm, hkvm))
         vm.save()
    def __init__(old_init, self, *args, **kwargs):
        if isinstance(self, ModelAdmin):
            model, admin_site = (args + (None, None))[0:2]
            if not model:
                model = kwargs.get('model')
        else:
            model = self.model

        generic_fk_fields = get_generic_fk_file_fields_for_model(model)

        if len(generic_fk_fields):
            # ModelAdmin.inlines is defined as a mutable on that
            # class, so we need to copy it before we append.
            # (otherwise we'll modify the `inlines` attribute for
            # all ModelAdmins).
            try:
                self.inlines = list(self.inlines)
            except:
                self.inlines = []

            # Prevent duplicate inlines being added
            existing_inline_fields = filter(None, [getattr(i, 'field', None) for i in self.inlines])
            generic_fk_fields_to_add = set(generic_fk_fields) ^ set(existing_inline_fields)

            for field in generic_fk_fields_to_add:
                self.inlines.append(field.get_inline_admin_formset())

        old_init(self, *args, **kwargs)
Example #4
0
 def _update_search_text(self):
     search_fields = filter(bool, [
         self.full_name, self.first_name, self.last_name, self.line1,
         self.line2, self.line3, self.line4, self.full_address,
         self.sub_districts, self.city, self.state, self.postcode,
         self.country.name
     ])
     self.search_text = ' '.join(search_fields)
    def get_file_list(self, **kwargs):
        from staticpreprocessor.conf import settings

        file_list = get_files(self.storage, location=settings.STATIC_PREPROCESSOR_ROOT)
        if self.extensions is not None:
            file_list = filter(lambda f: os.path.splitext(f)[1] in self.extensions, file_list)
        if self.exclude_match:
            file_list = filter(lambda f: not fnmatch.fnmatch(f, self.exclude_match), file_list)
        if self.exclude_regex:
            exclude_regex = re.compile(self.exclude_regex)
            file_list = filter(lambda f: not bool(exclude_regex.search(f)), file_list)
        if self.include_match:
            file_list = filter(lambda f: fnmatch.fnmatch(f, self.include_match), file_list)
        if self.include_regex:
            include_regex = re.compile(self.include_regex)
            file_list = filter(lambda f: bool(include_regex.search(f)), file_list)
        return file_list
Example #6
0
 def _parse_vgdisplay_stdout(self, vgdisplay_stdout):
     for line in vgdisplay_stdout:
         tokens = list(filter(None, line.split(' ')))
         if not tokens[:4] == [u'Free', u'PE', u'/', u'Size']:
             continue
         else:
             return int(float(tokens[6]))
     else:
         raise ValueError('Vgdisplay Free Space Token not Found')
Example #7
0
def iterload(modname, verbose=False, failfast=False):
    """
    Loads all modules with name 'modname' from all installed apps and returns
    and iterator of those modules.

    If verbose is True, debug information will be printed to stdout.

    If failfast is True, import errors will not be surpressed.
    """
    return filter(None, (get_module(app, modname, verbose, failfast)
                         for app in installed_apps()))
Example #8
0
 def get_translatable_content(self):
     """
     Returns {field_name: field_contents} for translatable fields, where
     field_contents > ''
     """
     fields = (f for f in self._meta.fields
               if isinstance(f, (models.CharField, models.TextField)) and
                  f.editable and not f.choices and
                  f.name not in self.translatable_content_excluded_fields)
     return dict(filter(itemgetter(1),
                        ((f.name, getattr(self, f.name)) for f in fields)))
Example #9
0
 def get_translatable_content(self):
     """
     Returns {field_name: field_contents} for translatable fields, where
     field_contents > ''
     """
     fields = (f for f in self._meta.fields
               if isinstance(f, (models.CharField, models.TextField)) and
                  f.editable and not f.choices and
                  f.name not in self.translatable_content_excluded_fields)
     return dict(filter(itemgetter(1),
                        ((f.name, getattr(self, f.name)) for f in fields)))
Example #10
0
def iterload(modname, verbose=False, failfast=False):
    """
    Loads all modules with name 'modname' from all installed apps and returns
    and iterator of those modules.

    If verbose is True, debug information will be printed to stdout.

    If failfast is True, import errors will not be surpressed.
    """
    return filter(None, (get_module(app, modname, verbose, failfast)
                         for app in installed_apps()))
Example #11
0
 def thumbs(self):
     thumb_ids = filter(None, self.request.GET.get('thumbs', '').split(','))
     try:
         thumb_ids = map(int, thumb_ids)
     except TypeError:
         thumbs = Thumb.objects.none()
     else:
         thumbs = Thumb.objects.filter(pk__in=thumb_ids)
     thumb_dict = dict([(t.name, t) for t in thumbs])
     ordered_thumbs = [
         thumb_dict.get(s.name, Thumb(name=s.name)) for s in self.sizes if not s.is_alias]
     return FakeQuerySet(ordered_thumbs, thumbs)
Example #12
0
 def join_fields(self, fields, separator=u", "):
     """
     Join a sequence of fields using the specified separator
     """
     field_values = []
     for field in fields:
         # Title is special case
         if field == 'title':
             value = self.get_title_display()
         else:
             value = getattr(self, field)
         field_values.append(value)
     return separator.join(filter(bool, field_values))
 def join_fields(self, fields, separator=u", "):
     """
     Join a sequence of fields using the specified separator
     """
     field_values = []
     for field in fields:
         # Title is special case
         if field == 'title':
             value = self.get_title_display()
         else:
             value = getattr(self, field)
         field_values.append(value)
     return separator.join(filter(bool, field_values))
 def get_file_list(self, **kwargs):
     from staticpreprocessor.conf import settings
     file_list = get_files(self.storage,
                           location=settings.STATIC_PREPROCESSOR_ROOT)
     if self.extensions is not None:
         file_list = filter(
             lambda f: os.path.splitext(f)[1] in self.extensions, file_list)
     if self.exclude_match:
         file_list = filter(
             lambda f: not fnmatch.fnmatch(f, self.exclude_match),
             file_list)
     if self.exclude_regex:
         exclude_regex = re.compile(self.exclude_regex)
         file_list = filter(lambda f: not bool(exclude_regex.search(f)),
                            file_list)
     if self.include_match:
         file_list = filter(
             lambda f: fnmatch.fnmatch(f, self.include_match), file_list)
     if self.include_regex:
         include_regex = re.compile(self.include_regex)
         file_list = filter(lambda f: bool(include_regex.search(f)),
                            file_list)
     return file_list
Example #15
0
 def thumbs(self):
     thumb_ids = filter(None, self.request.GET.get('thumbs', '').split(','))
     try:
         thumb_ids = map(int, thumb_ids)
     except TypeError:
         thumbs = Thumb.objects.none()
     else:
         thumbs = Thumb.objects.filter(pk__in=thumb_ids)
     thumb_dict = dict([(t.name, t) for t in thumbs])
     ordered_thumbs = [
         thumb_dict.get(s.name, Thumb(name=s.name)) for s in self.sizes
         if not s.is_alias
     ]
     return FakeQuerySet(ordered_thumbs, thumbs)
Example #16
0
    def serialize_redirect_uris_field(self, obj, **kwargs):
        """Serialize the ``redirect_uris`` field to a list.

        Args:
            obj (reviewboard.oauth.models.Application):
                The application being serialized.

            **kwargs (dict):
                Ignored keyword arguments

        Returns:
            list of unicode:
            The list of allowable redirect URIs.
        """
        return list(filter(len, obj.redirect_uris.split()))
Example #17
0
    def serialize_redirect_uris_field(self, obj, **kwargs):
        """Serialize the ``redirect_uris`` field to a list.

        Args:
            obj (reviewboard.oauth.models.Application):
                The application being serialized.

            **kwargs (dict):
                Ignored keyword arguments

        Returns:
            list of unicode:
            The list of allowable redirect URIs.
        """
        return list(filter(len, obj.redirect_uris.split()))
Example #18
0
def build_plugin_tree(plugins):
    """
    Accepts an iterable of plugins and assigns tuples, sorted by position, of
    children plugins to their respective parents.
    Returns a sorted list of root plugins.
    """
    cache = dict((p.pk, p) for p in plugins)
    by_parent_id = attrgetter('parent_id')
    nonroots = sorted(filter(by_parent_id, cache.values()),
                      key=attrgetter('parent_id', 'position'))
    families = ((cache[parent_id], tuple(children))
                for parent_id, children in groupby(nonroots, by_parent_id))
    for parent, children in families:
        parent.child_plugin_instances = children
    return sorted(filterfalse(by_parent_id, cache.values()),
                  key=attrgetter('position'))
Example #19
0
    def render(self, name, value, attrs=None):
        """Render the widget.

        Args:
            name (unicode):
                The field name.

            value (unicode):
                The field value.

            attrs (dict, optional):
                Additional attributes.

        Returns:
            django.utils.safestring.SafeText:
            The rendered widget.
        """
        attrs = self.build_attrs(attrs)
        id_ = attrs.pop('id')

        if 'class' in attrs:
            attrs['class'] += ' list-edit-item'
        else:
            attrs['class'] = 'list-edit-item'

        value = value or ''
        value_list = list(
            filter(len, (item.strip() for item in value.split(self._sep))))

        return render_to_string(
            self.template_name, {
                'name':
                name,
                'value':
                value,
                'attrs':
                format_html_join('', ' {0}="{1}"', sorted(
                    six.iteritems(attrs))),
                'id':
                id_,
                'remove_text':
                _('Remove this item.'),
                'sep':
                self._sep,
                'value_list':
                value_list,
            })
Example #20
0
def json_default(obj):
    if six.callable(getattr(obj, '__serialize__', None)):
        dct = obj.__serialize__()
        module = obj.__module__
        if module == '__builtin__':
            module = None
        if isinstance(obj, type):
            name = obj.__name__
        else:
            name = obj.__class__.__name__
        type_name = u'.'.join(filter(None, [module, name]))
        if type_name == 'cropduster.resizing.Size':
            type_name = 'Size'
        dct.update({'__type__': type_name})
        return dct
    raise TypeError("object of type %s is not JSON serializable" %
                    type(obj).__name__)
Example #21
0
def build_plugin_tree(plugins):
    """
    Accepts an iterable of plugins and assigns tuples, sorted by position, of
    children plugins to their respective parents.
    Returns a sorted list of root plugins.
    """
    cache = dict((p.pk, p) for p in plugins)
    by_parent_id = attrgetter('parent_id')
    nonroots = sorted(filter(by_parent_id, cache.values()),
                      key=attrgetter('parent_id', 'position'))
    families = ((cache[parent_id], tuple(children))
                for parent_id, children
                in groupby(nonroots, by_parent_id))
    for parent, children in families:
        parent.child_plugin_instances = children
    return sorted(filterfalse(by_parent_id, cache.values()),
                  key=attrgetter('position'))
Example #22
0
 def _parse_virsh_list_stdout(self, virsh_stdout):
     res = dict(started_vm={}, stopped_vm={})
     for line in virsh_stdout:
         tokens = (t.strip() for t in line.strip().split(' ', 2))
         tokens = list(filter(None, tokens))
         if tokens == ['Id', 'Name', 'State']:
             continue
         elif len(tokens) < 3:
             continue
         else:
             id_, vm_name, status = tokens
             if status == 'running':
                 res['started_vm'][vm_name] = {}
             elif status == 'shut off':
                 res['stopped_vm'][vm_name] = {}
             else:
                 raise ValueError('Unknown VM Status in virsh output:'
                                  ' \'{}\''.format(status))
     return res
Example #23
0
def create_default_plugins(request, placeholders, template, lang):
    """
    Create all default plugins for the given ``placeholders`` if they have
    a "default_plugins" configuration value in settings.
    return all plugins, children, grandchildren (etc.) created
    """
    from cms.api import add_plugin

    def _create_default_plugins(placeholder, confs, parent=None):
        """
        Auxillary function that builds all of a placeholder's default plugins
        at the current level and drives the recursion down the tree.
        Returns the plugins at the current level along with all descendants.
        """
        plugins, descendants = [], []
        addable_confs = (
            conf for conf in confs
            if has_plugin_permission(request.user, conf['plugin_type'], 'add'))
        for conf in addable_confs:
            plugin = add_plugin(placeholder,
                                conf['plugin_type'],
                                lang,
                                target=parent,
                                **conf['values'])
            if 'children' in conf:
                args = placeholder, conf['children'], plugin
                descendants += _create_default_plugins(*args)
            plugin.notify_on_autoadd(request, conf)
            plugins.append(plugin)
        if parent:
            parent.notify_on_autoadd_children(request, conf, plugins)
        return plugins + descendants

    unfiltered_confs = ((ph,
                         get_placeholder_conf('default_plugins', ph.slot,
                                              template))
                        for ph in placeholders)
    # Empty confs must be filtered before filtering on add permission
    mutable_confs = (
        (ph, default_plugin_confs)
        for ph, default_plugin_confs in filter(itemgetter(1), unfiltered_confs)
        if ph.has_add_permission(request))
    return sum(starmap(_create_default_plugins, mutable_confs), [])
Example #24
0
    def render(self, name, value, attrs=None):
        """Render the widget.

        Args:
            name (unicode):
                The field name.

            value (unicode):
                The field value.

            attrs (dict, optional):
                Additional attributes.

        Returns:
            django.utils.safestring.SafeText:
            The rendered widget.
        """
        attrs = self.build_attrs(attrs)
        id_ = attrs.pop('id')

        if 'class' in attrs:
            attrs['class'] += ' list-edit-item'
        else:
            attrs['class'] = 'list-edit-item'

        value = value or ''
        value_list = list(
            filter(len, (item.strip() for item in value.split(self._sep)))
        )

        return render_to_string(
            self.template_name,
            {
                'name': name,
                'value': value,
                'attrs': format_html_join('', ' {0}="{1}"',
                                          sorted(six.iteritems(attrs))),
                'id': id_,
                'remove_text': _('Remove this item.'),
                'sep': self._sep,
                'value_list': value_list,
            })
Example #25
0
    def __init__(self, name, label=None, w=None, h=None, retina=False, auto=None, min_w=None, min_h=None,
            max_w=None, max_h=None, required=True):

        self.min_w = max(w or 1, min_w or 1) or 1
        self.min_h = max(h or 1, min_h or 1) or 1
        self.max_w = max_w
        self.max_h = max_h

        if auto is not None:
            try:
                if not all([isinstance(sz, Size) for sz in auto]):
                    raise TypeError()
            except TypeError:
                raise Exception("kwarg `auto` must be a list of Size objects")
            else:
                for auto_size in auto:
                    auto_size.parent = self
                    if auto_size.auto:
                        raise ImproperlyConfigured("The `auto` kwarg cannot be used recursively")
        self.name = name
        self.auto = auto
        self.retina = retina
        self.width = w
        self.height = h
        self.label = label or u' '.join(filter(None, re.split(r'[_\-]', name))).title()
        self.required = required

        self.min_aspect = (self.w / self.h) if (self.w and self.h) else 0
        self.max_aspect = self.min_aspect or INFINITY

        if self.w and self.min_h > 1:
            self.max_aspect = min(self.max_aspect, self.w / self.min_h)

        if self.w and self.max_h:
            self.min_aspect = max(self.min_aspect, self.w / self.max_h)

        if self.h and self.min_w > 1:
            self.min_aspect = max(self.min_aspect, self.min_w / self.h)

        if self.h and self.max_w:
            self.max_aspect = min(self.max_aspect, self.max_w / self.h)
Example #26
0
    def __init__(self, name, label=None, w=None, h=None, retina=False, auto=None, min_w=None, min_h=None,
            max_w=None, max_h=None, required=True):

        self.min_w = max(w or 1, min_w or 1) or 1
        self.min_h = max(h or 1, min_h or 1) or 1
        self.max_w = max_w
        self.max_h = max_h

        if auto is not None:
            try:
                if not all([isinstance(sz, Size) for sz in auto]):
                    raise TypeError()
            except TypeError:
                raise Exception("kwarg `auto` must be a list of Size objects")
            else:
                for auto_size in auto:
                    auto_size.parent = self
                    if auto_size.auto:
                        raise ImproperlyConfigured("The `auto` kwarg cannot be used recursively")
        self.name = name
        self.auto = auto
        self.retina = retina
        self.width = w
        self.height = h
        self.label = label or u' '.join(filter(None, re.split(r'[_\-]', name))).title()
        self.required = required

        self.min_aspect = (self.w / self.h) if (self.w and self.h) else 0
        self.max_aspect = self.min_aspect or INFINITY

        if self.w and self.min_h > 1:
            self.max_aspect = min(self.max_aspect, self.w / self.min_h)

        if self.w and self.max_h:
            self.min_aspect = max(self.min_aspect, self.w / self.max_h)

        if self.h and self.min_w > 1:
            self.min_aspect = max(self.min_aspect, self.min_w / self.h)

        if self.h and self.max_w:
            self.max_aspect = min(self.max_aspect, self.max_w / self.h)
Example #27
0
def create_default_plugins(request, placeholders, template, lang):
    """
    Create all default plugins for the given ``placeholders`` if they have
    a "default_plugins" configuration value in settings.
    return all plugins, children, grandchildren (etc.) created
    """
    from cms.api import add_plugin

    def _create_default_plugins(placeholder, confs, parent=None):
        """
        Auxillary function that builds all of a placeholder's default plugins
        at the current level and drives the recursion down the tree.
        Returns the plugins at the current level along with all descendants.
        """
        plugins, descendants = [], []
        addable_confs = (conf for conf in confs
                         if has_plugin_permission(request.user,
                                                  conf['plugin_type'], 'add'))
        for conf in addable_confs:
            plugin = add_plugin(placeholder, conf['plugin_type'], lang,
                                target=parent, **conf['values'])
            if 'children' in conf:
                args = placeholder, conf['children'], plugin
                descendants += _create_default_plugins(*args)
            plugin.notify_on_autoadd(request, conf)
            plugins.append(plugin)
        if parent:
            parent.notify_on_autoadd_children(request, conf, plugins)
        return plugins + descendants


    unfiltered_confs = ((ph, get_placeholder_conf('default_plugins',
                                                  ph.slot, template))
                        for ph in placeholders)
    # Empty confs must be filtered before filtering on add permission
    mutable_confs = ((ph, default_plugin_confs)
                     for ph, default_plugin_confs
                     in filter(itemgetter(1), unfiltered_confs)
                     if ph.has_add_permission(request))
    return sum(starmap(_create_default_plugins, mutable_confs), [])
Example #28
0
def crop(request):
    if request.method == "GET":
        return json_error(request,
                          'crop',
                          action="cropping image",
                          errors=["Form submission invalid"])

    crop_form = CropForm(request.POST, request.FILES, prefix='crop')
    if not crop_form.is_valid():
        return json_error(request,
                          'crop',
                          action='submitting form',
                          forms=[crop_form],
                          log=True,
                          exc_info=full_exc_info())

    crop_data = copy.deepcopy(crop_form.cleaned_data)

    if crop_data.get('image_id'):
        db_image = Image.objects.get(pk=crop_data['image_id'])
    else:
        db_image = Image(image=crop_data['orig_image'])

    try:
        with db_image.image as f:
            f.open()
            pil_image = PIL.Image.open(BytesIO(f.read()))
            pil_image.filename = f.name
    except IOError:
        pil_image = None

    FormSet = modelformset_factory(Thumb, form=ThumbForm, formset=ThumbFormSet)
    thumb_formset = FormSet(request.POST, request.FILES, prefix='thumbs')

    if not thumb_formset.is_valid():
        return json_error(request,
                          'crop',
                          action='submitting form',
                          formsets=[thumb_formset],
                          log=True,
                          exc_info=full_exc_info())

    cropped_thumbs = thumb_formset.save(commit=False)

    non_model_fields = set(ThumbForm.declared_fields) - set(
        [f.name for f in Thumb._meta.fields])

    # The fields we will pull from when populating the ThumbForm initial data
    json_thumb_fields = ['id', 'name', 'width', 'height']

    thumbs_with_crops = [t for t in cropped_thumbs if t.crop_w and t.crop_h]
    thumbs_data = [f.cleaned_data for f in thumb_formset]

    standalone_mode = crop_data['standalone']

    # Address a standalone mode issue where, because the thumbs don't have a pk value,
    # Django no longer returns them in Formset.save() if they are in initial_forms
    if standalone_mode and not cropped_thumbs and len(
            thumb_formset.initial_forms):
        thumb_form = thumb_formset.initial_forms[0]
        obj = thumb_form.instance
        cropped_thumbs = [
            thumb_formset.save_existing(thumb_form, obj, commit=False)
        ]

    for i, (thumb, thumb_form) in enumerate(zip(cropped_thumbs,
                                                thumb_formset)):
        changed_fields = set(thumb_form.changed_data) - non_model_fields
        thumb_form._changed_data = list(changed_fields)
        thumb_data = thumbs_data[i]
        size = thumb_data['size']

        if changed_fields & set(['crop_x', 'crop_y', 'crop_w', 'crop_h']):
            # Clear existing primary key to force new thumb creation
            thumb.pk = None

            thumb.width = min(filter(None, [thumb.width, thumb.crop_w]))
            thumb.height = min(filter(None, [thumb.height, thumb.crop_h]))

            try:
                new_thumbs = db_image.save_size(size,
                                                thumb,
                                                tmp=True,
                                                standalone=standalone_mode)
            except CropDusterResizeException as e:
                return json_error(request,
                                  'crop',
                                  action="saving size",
                                  errors=[force_text(e)])

            if not new_thumbs:
                continue

            if standalone_mode:
                thumb = new_thumbs
                new_thumbs = {thumb.name: thumb}

            cropped_thumbs[i] = thumb = new_thumbs.get(thumb.name, thumb)

            update_props = [
                'crop_x', 'crop_y', 'crop_w', 'crop_h', 'width', 'height',
                'id', 'name'
            ]
            for prop in update_props:
                thumbs_data[i][prop] = getattr(thumb, prop)

            thumbs_data[i].update({
                'changed': True,
                'url': db_image.get_image_url(thumb.name),
            })

            for name, new_thumb in six.iteritems(new_thumbs):
                thumb_data = dict([(k, getattr(new_thumb, k))
                                   for k in json_thumb_fields])
                thumb_data['url'] = db_image.get_image_url(
                    name, tmp=not (new_thumb.image_id))
                crop_data['thumbs'].update({name: thumb_data})
                if new_thumb.reference_thumb_id:
                    continue
                thumbs_data[i]['thumbs'].update({name: thumb_data})
        elif thumb.pk and thumb.name and thumb.crop_w and thumb.crop_h:
            thumb_path = db_image.get_image_path(thumb.name, tmp=False)
            tmp_thumb_path = db_image.get_image_path(thumb.name, tmp=True)

            if default_storage.exists(thumb_path):
                if not thumb_form.cleaned_data.get(
                        'changed') or not default_storage.exists(
                            tmp_thumb_path):
                    with default_storage.open(thumb_path) as f:
                        with default_storage.open(tmp_thumb_path,
                                                  'wb') as tmp_file:
                            tmp_file.write(f.read())

        if not thumb.pk and not thumb.crop_w and not thumb.crop_h:
            if not len(thumbs_with_crops):
                continue
            best_fit = thumb_form.cleaned_data['size'].fit_to_crop(
                thumbs_with_crops[0], original_image=pil_image)
            if best_fit:
                thumbs_data[i].update({
                    'crop_x': best_fit.box.x1,
                    'crop_y': best_fit.box.y1,
                    'crop_w': best_fit.box.w,
                    'crop_h': best_fit.box.h,
                    'changed': True,
                    'id': None,
                })

    for thumb_data in thumbs_data:
        if isinstance(thumb_data['id'], Thumb):
            thumb_data['id'] = thumb_data['id'].pk

    preview_url = db_image.get_image_url('_preview')
    preview_w = PREVIEW_WIDTH
    preview_h = PREVIEW_HEIGHT
    orig_width, orig_height = crop_data['orig_w'], crop_data['orig_h']
    if (orig_width and orig_height):
        resize_ratio = min(PREVIEW_WIDTH / float(orig_width),
                           PREVIEW_HEIGHT / float(orig_height))
        if resize_ratio < 1:
            preview_w = int(round(orig_width * resize_ratio))
            preview_h = int(round(orig_height * resize_ratio))

    return HttpResponse(json.dumps({
        'crop': crop_data,
        'thumbs': thumbs_data,
        'initial': True,
        'preview_url': preview_url,
        'preview_w': preview_w,
        'preview_h': preview_h
    }),
                        content_type='application/json')
 def _update_search_text(self):
     search_fields = filter(
         bool, [self.first_name, self.last_name,
                self.line1, self.line4.city_name])
     self.search_text = ' '.join(search_fields)
 def join_fields(self, fields, separator=u", "):
     """
     Join a sequence of fields using the specified separator
     """
     field_values = self.get_field_values(fields)
     return separator.join(filter(bool, field_values))
 def _update_search_text(self):
     search_fields = filter(
         bool, [self.first_name, self.last_name,
                self.line1, self.line2, self.line3, self.line4,
                self.state, self.postcode, self.country.name])
     self.search_text = ' '.join(search_fields)
Example #32
0
def user_name(user):
    if user.first_name or user.last_name:
        names = list(filter(bool, [user.first_name, user.last_name]))
        return u' '.join(names)
    return user.username
Example #33
0
def crop(request):
    if request.method == "GET":
        return json_error(request, 'crop', action="cropping image",
                errors=["Form submission invalid"])

    crop_form = CropForm(request.POST, request.FILES, prefix='crop')
    if not crop_form.is_valid():
        return json_error(request, 'crop', action='submitting form', forms=[crop_form],
                log=True, exc_info=full_exc_info())

    crop_data = copy.deepcopy(crop_form.cleaned_data)
    db_image = Image(image=crop_data['orig_image'])
    try:
        pil_image = PIL.Image.open(db_image.image.path)
    except IOError:
        pil_image = None

    FormSet = modelformset_factory(Thumb, form=ThumbForm, formset=ThumbFormSet)
    thumb_formset = FormSet(request.POST, request.FILES, prefix='thumbs')

    if not thumb_formset.is_valid():
        return json_error(request, 'crop', action='submitting form', formsets=[thumb_formset],
                log=True, exc_info=full_exc_info())

    cropped_thumbs = thumb_formset.save(commit=False)

    non_model_fields = set(ThumbForm.declared_fields) - set([f.name for f in Thumb._meta.fields])

    # The fields we will pull from when populating the ThumbForm initial data
    json_thumb_fields = ['id', 'name', 'width', 'height']

    thumbs_with_crops = [t for t in cropped_thumbs if t.crop_w and t.crop_h]
    thumbs_data = [f.cleaned_data for f in thumb_formset]

    standalone_mode = crop_data['standalone']

    # Address a standalone mode issue where, because the thumbs don't have a pk value,
    # Django no longer returns them in Formset.save() if they are in initial_forms
    if standalone_mode and not cropped_thumbs and len(thumb_formset.initial_forms):
        thumb_form = thumb_formset.initial_forms[0]
        obj = thumb_form.instance
        cropped_thumbs = [thumb_formset.save_existing(thumb_form, obj, commit=False)]

    for i, (thumb, thumb_form) in enumerate(zip(cropped_thumbs, thumb_formset)):
        changed_fields = set(thumb_form.changed_data) - non_model_fields
        thumb_form._changed_data = list(changed_fields)
        thumb_data = thumbs_data[i]
        size = thumb_data['size']

        if changed_fields & set(['crop_x', 'crop_y', 'crop_w', 'crop_h']):
            # Clear existing primary key to force new thumb creation
            thumb.pk = None

            thumb.width = min(filter(None, [thumb.width, thumb.crop_w]))
            thumb.height = min(filter(None, [thumb.height, thumb.crop_h]))

            try:
                new_thumbs = db_image.save_size(size, thumb, tmp=True, standalone=standalone_mode)
            except CropDusterResizeException as e:
                return json_error(request, 'crop',
                                  action="saving size", errors=[force_text(e)])

            if not new_thumbs:
                continue

            if standalone_mode:
                thumb = new_thumbs
                new_thumbs = {thumb.name: thumb}

            cropped_thumbs[i] = thumb = new_thumbs.get(thumb.name, thumb)

            update_props = ['crop_x', 'crop_y', 'crop_w', 'crop_h', 'width', 'height', 'id', 'name']
            for prop in update_props:
                thumbs_data[i][prop] = getattr(thumb, prop)

            thumbs_data[i].update({
                'changed': True,
                'url': db_image.get_image_url(thumb.name),
            })

            for name, new_thumb in six.iteritems(new_thumbs):
                thumb_data = dict([(k, getattr(new_thumb, k)) for k in json_thumb_fields])
                crop_data['thumbs'].update({name: thumb_data})
                if new_thumb.reference_thumb_id:
                    continue
                thumbs_data[i]['thumbs'].update({name: thumb_data})
        elif thumb.pk and thumb.name and thumb.crop_w and thumb.crop_h:
            thumb_path = db_image.get_image_path(thumb.name, tmp=False)
            tmp_thumb_path = db_image.get_image_path(thumb.name, tmp=True)
            if os.path.exists(thumb_path):
                if not thumb_form.cleaned_data.get('changed') or not os.path.exists(tmp_thumb_path):
                    shutil.copy(thumb_path, tmp_thumb_path)

        if not thumb.pk and not thumb.crop_w and not thumb.crop_h:
            if not len(thumbs_with_crops):
                continue
            best_fit = thumb_form.cleaned_data['size'].fit_to_crop(
                    thumbs_with_crops[0], original_image=pil_image)
            if best_fit:
                thumbs_data[i].update({
                    'crop_x': best_fit.box.x1,
                    'crop_y': best_fit.box.y1,
                    'crop_w': best_fit.box.w,
                    'crop_h': best_fit.box.h,
                    'changed': True,
                    'id': None,
                })

    for thumb_data in thumbs_data:
        if isinstance(thumb_data['id'], Thumb):
            thumb_data['id'] = thumb_data['id'].pk

    return HttpResponse(json.dumps({
        'crop': crop_data,
        'thumbs': thumbs_data,
        'initial': True,
    }), content_type='application/json')
Example #34
0
 def name(self):
     if self.user.first_name or self.user.last_name:
         names = list(filter(
             bool, [self.user.first_name, self.user.last_name]))
         return ' '.join(names)
     return self.user.username
Example #35
0
 def format_value(self, value):
     if value:
         return ','.join(map(six.text_type, filter(bool, value)))
     else:
         return ''
Example #36
0
 def value_from_datadict(self, data, files, name):
     value = data.get(name, None)
     if value is None:
         return []
     else:
         return list(filter(bool, value.split(',')))
Example #37
0
 def format_value(self, value):
     if value:
         return ','.join(map(six.text_type, filter(bool, value)))
     else:
         return ''
Example #38
0
def user_name(user):
    if user.first_name or user.last_name:
        names = list(filter(bool, [user.first_name, user.last_name]))
        return u' '.join(names)
    return user.username
Example #39
0
 def value_from_datadict(self, data, files, name):
     value = data.get(name, None)
     if value is None:
         return []
     else:
         return list(filter(bool, value.split(',')))
 def join_fields(self, fields, separator=u", "):
     """
     Join a sequence of fields using the specified separator
     """
     field_values = self.get_field_values(fields)
     return separator.join(filter(bool, field_values))