Python django.utils.six.iteritems() Examples
The following are 30
code examples of django.utils.six.iteritems().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.utils.six
, or try the search function
.
Example #1
Source File: query.py From python with Apache License 2.0 | 6 votes |
def set_group_by(self): """ Expands the GROUP BY clause required by the query. This will usually be the set of all non-aggregate fields in the return data. If the database backend supports grouping by the primary key, and the query would be equivalent, the optimization will be made automatically. """ self.group_by = [] for col in self.select: self.group_by.append(col) if self.annotation_select: for alias, annotation in six.iteritems(self.annotation_select): for col in annotation.get_group_by_cols(): self.group_by.append(col)
Example #2
Source File: query.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def update_or_create(self, defaults=None, **kwargs): """ Looks up an object with the given kwargs, updating one with defaults if it exists, otherwise creates a new one. Returns a tuple (object, created), where created is a boolean specifying whether an object was created. """ defaults = defaults or {} lookup, params = self._extract_model_params(defaults, **kwargs) self._for_write = True try: obj = self.get(**lookup) except self.model.DoesNotExist: obj, created = self._create_object_from_params(lookup, params) if created: return obj, created for k, v in six.iteritems(defaults): setattr(obj, k, v) with transaction.atomic(using=self.db, savepoint=False): obj.save(using=self.db) return obj, False
Example #3
Source File: subqueries.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def get_related_updates(self): """ Returns a list of query objects: one for each update required to an ancestor model. Each query will have the same filtering conditions as the current query but will only update a single table. """ if not self.related_updates: return [] result = [] for model, values in six.iteritems(self.related_updates): query = UpdateQuery(model) query.values = values if self.related_ids is not None: query.add_filter(('pk__in', self.related_ids)) result.append(query) return result
Example #4
Source File: query.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def set_group_by(self): """ Expands the GROUP BY clause required by the query. This will usually be the set of all non-aggregate fields in the return data. If the database backend supports grouping by the primary key, and the query would be equivalent, the optimization will be made automatically. """ self.group_by = [] for col in self.select: self.group_by.append(col) if self._annotations: for alias, annotation in six.iteritems(self.annotations): for col in annotation.get_group_by_cols(): self.group_by.append(col)
Example #5
Source File: dateparse.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def parse_time(value): """Parses a string and return a datetime.time. This function doesn't support time zone offsets. Raises ValueError if the input is well formatted but not a valid time. Returns None if the input isn't well formatted, in particular if it contains an offset. """ match = time_re.match(value) if match: kw = match.groupdict() if kw['microsecond']: kw['microsecond'] = kw['microsecond'].ljust(6, '0') kw = {k: int(v) for k, v in six.iteritems(kw) if v is not None} return datetime.time(**kw)
Example #6
Source File: dateparse.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def parse_duration(value): """Parses a duration string and returns a datetime.timedelta. The preferred format for durations in Django is '%d %H:%M:%S.%f'. Also supports ISO 8601 representation. """ match = standard_duration_re.match(value) if not match: match = iso8601_duration_re.match(value) if match: kw = match.groupdict() if kw.get('microseconds'): kw['microseconds'] = kw['microseconds'].ljust(6, '0') kw = {k: float(v) for k, v in six.iteritems(kw) if v is not None} return datetime.timedelta(**kw)
Example #7
Source File: subqueries.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def add_update_values(self, values): """ Convert a dictionary of field name to value mappings into an update query. This is the entry point for the public update() method on querysets. """ values_seq = [] for name, val in six.iteritems(values): field = self.get_meta().get_field(name) direct = not (field.auto_created and not field.concrete) or not field.concrete model = field.model._meta.concrete_model if not direct or (field.is_relation and field.many_to_many): raise FieldError( 'Cannot update model field %r (only non-relations and ' 'foreign keys permitted).' % field ) if model is not self.get_meta().model: self.add_related_update(model, field, val) continue values_seq.append((field, model, val)) return self.add_update_fields(values_seq)
Example #8
Source File: loader_tags.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def render(self, context): try: template = self.template.resolve(context) # Does this quack like a Template? if not callable(getattr(template, 'render', None)): # If not, we'll try get_template template = context.template.engine.get_template(template) values = { name: var.resolve(context) for name, var in six.iteritems(self.extra_context) } if self.isolated_context: return template.render(context.new(values)) with context.push(**values): return template.render(context) except Exception: if context.template.engine.debug: raise return ''
Example #9
Source File: models.py From django-actions-logger with MIT License | 6 votes |
def changes_str(self, colon=': ', arrow=smart_text(' \u2192 '), separator='; '): """ Return the changes recorded in this log entry as a string. The formatting of the string can be customized by setting alternate values for colon, arrow and separator. If the formatting is still not satisfying, please use :py:func:`LogAction.changes_dict` and format the string yourself. :param colon: The string to place between the field name and the values. :param arrow: The string to place between each old and new value. :param separator: The string to place between each field. :return: A readable string of the changes in this log entry. """ substrings = [] for field, values in iteritems(self.changes_dict): substring = smart_text('{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}').format( field_name=field, colon=colon, old=values[0], arrow=arrow, new=values[1], ) substrings.append(substring) return separator.join(substrings)
Example #10
Source File: renderers.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def _to_xml(self, xml, data): if isinstance(data, (list, tuple)): for item in data: xml.startElement(self.element_node, {}) self._to_xml(xml, item) xml.endElement(self.element_node) elif isinstance(data, dict): for key, value in six.iteritems(data): xml.startElement(key, {}) self._to_xml(xml, value) xml.endElement(key) elif data is None: # Don't output any value pass else: xml.characters(smart_text(data))
Example #11
Source File: backend.py From django-warrant with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_user_obj(self,username=None,attribute_list=[],metadata={},attr_map={}): user_attrs = cognito_to_dict(attribute_list,CognitoUser.COGNITO_ATTR_MAPPING) django_fields = [f.name for f in CognitoUser.user_class._meta.get_fields()] extra_attrs = {} for k, v in user_attrs.items(): if k not in django_fields: extra_attrs.update({k: user_attrs.pop(k, None)}) if getattr(settings, 'COGNITO_CREATE_UNKNOWN_USERS', True): user, created = CognitoUser.user_class.objects.update_or_create( username=username, defaults=user_attrs) else: try: user = CognitoUser.user_class.objects.get(username=username) for k, v in iteritems(user_attrs): setattr(user, k, v) user.save() except CognitoUser.user_class.DoesNotExist: user = None if user: for k, v in extra_attrs.items(): setattr(user, k, v) return user
Example #12
Source File: parse.py From scale with Apache License 2.0 | 6 votes |
def parse_duration(value): """Parses a duration string and returns a datetime.timedelta. Unlike the standard Django 1.8 function, this only accepts ISO 8601 durations. """ match = iso8601_duration_re.match(value) if match: kw = match.groupdict() kw = {k: float(v) for k, v in six.iteritems(kw) if v is not None} return datetime.timedelta(**kw) # Hack to fix ISO8601 for datetime filters. # This should be taken care of by a future Django fix. And might even be handled # by a newer version of django-rest-framework. Unfortunately, both of these solutions # will accept datetimes without timezone information which we do not want to allow # see https://code.djangoproject.com/tickets/23448 # Solution modified from http://akinfold.blogspot.com/2012/12/datetimefield-doesnt-accept-iso-8601.html
Example #13
Source File: viewsets.py From dynamic-rest with MIT License | 6 votes |
def _patch_all_loop(self, queryset, data): # update by transaction loop updated = 0 try: with transaction.atomic(): for record in queryset: for k, v in six.iteritems(data): setattr(record, k, v) record.save() updated += 1 return updated except IntegrityError as e: raise ValidationError( 'Failed to update records:\n' '%s\n' 'Data: %s' % ( str(e), str(data) ) )
Example #14
Source File: filters.py From dynamic-rest with MIT License | 6 votes |
def _get_implicit_requirements( self, fields, requirements ): """Extract internal prefetch requirements from serializer fields.""" for name, field in six.iteritems(fields): source = field.source # Requires may be manually set on the field -- if not, # assume the field requires only its source. requires = getattr(field, 'requires', None) or [source] for require in requires: if not require: # ignore fields with empty source continue requirement = require.split('.') if requirement[-1] == '': # Change 'a.b.' -> 'a.b.*', # supporting 'a.b.' for backwards compatibility. requirement[-1] = '*' requirements.insert(requirement, TreeMap(), update=True)
Example #15
Source File: serializers.py From dynamic-rest with MIT License | 6 votes |
def _link_fields(self): """Construct dict of name:field for linkable fields.""" query_params = self.get_request_attribute('query_params', {}) if 'exclude_links' in query_params: return {} else: all_fields = self.get_all_fields() return { name: field for name, field in six.iteritems(all_fields) if isinstance(field, DynamicRelationField) and getattr(field, 'link', True) and not ( # Skip sideloaded fields name in self.fields and self.is_field_sideloaded(name) ) and not ( # Skip included single relations # TODO: Use links, when we can generate canonical URLs name in self.fields and not getattr(field, 'many', False) ) }
Example #16
Source File: datastructures.py From dynamic-rest with MIT License | 6 votes |
def get_paths(self): """Get all paths from the root to the leaves. For example, given a chain like `{'a':{'b':{'c':None}}}`, this method would return `[['a', 'b', 'c']]`. Returns: A list of lists of paths. """ paths = [] for key, child in six.iteritems(self): if isinstance(child, TreeMap) and child: # current child is an intermediate node for path in child.get_paths(): path.insert(0, key) paths.append(path) else: # current child is an endpoint paths.append([key]) return paths
Example #17
Source File: fields.py From dynamic-rest with MIT License | 6 votes |
def get_serializer(self, *args, **kwargs): """Get an instance of the child serializer.""" init_args = { k: v for k, v in six.iteritems(self.kwargs) if k in self.SERIALIZER_KWARGS } kwargs = self._inherit_parent_kwargs(kwargs) init_args.update(kwargs) if self.embed and self._is_dynamic: init_args['embed'] = True serializer = self._get_cached_serializer(args, init_args) serializer.parent = self return serializer
Example #18
Source File: renderers.py From drf-json-api with MIT License | 6 votes |
def prepend_links_with_name(self, links, name): changed_links = links.copy() for link_name, link_obj in six.iteritems(links): prepended_name = "%s.%s" % (name, link_name) link_template = "{%s}" % link_name prepended_template = "{%s}" % prepended_name updated_obj = changed_links[link_name] if "href" in link_obj: updated_obj["href"] = link_obj["href"].replace( link_template, prepended_template) changed_links[prepended_name] = changed_links[link_name] del changed_links[link_name] return changed_links
Example #19
Source File: compat.py From drf-cached-instances with Mozilla Public License 2.0 | 6 votes |
def parse_duration(value): """Parse a duration string and returns a datetime.timedelta. The preferred format for durations in Django is '%d %H:%M:%S.%f'. Also supports ISO 8601 representation. """ match = standard_duration_re.match(value) if not match: match = iso8601_duration_re.match(value) if match: kw = match.groupdict() if kw.get('microseconds'): kw['microseconds'] = kw['microseconds'].ljust(6, '0') kw = dict([ (k, float(v)) for k, v in six.iteritems(kw) if v is not None]) return timedelta(**kw)
Example #20
Source File: abstract.py From django-leonardo with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _generate_thumbnails(self, required_thumbnails): _thumbnails = {} for name, opts in six.iteritems(required_thumbnails): try: opts.update({'subject_location': self.subject_location}) thumb = self.file.get_thumbnail(opts) _thumbnails[name] = thumb.url except Exception as e: # catch exception and manage it. We can re-raise it for debugging # purposes and/or just logging it, provided user configured # proper logging configuration if filer_settings.FILER_ENABLE_LOGGING: logger.error('Error while generating thumbnail: %s', e) if filer_settings.FILER_DEBUG: raise return _thumbnails
Example #21
Source File: widgets.py From django-leonardo with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_all_widget_classes(): """returns collected Leonardo Widgets if not declared in settings is used __subclasses__ which not supports widget subclassing """ from leonardo.module.web.models import Widget _widgets = getattr(settings, 'WIDGETS', Widget.__subclasses__()) widgets = [] if isinstance(_widgets, dict): for group, widget_cls in six.iteritems(_widgets): widgets.extend(widget_cls) elif isinstance(_widgets, list): widgets = _widgets return load_widget_classes(widgets)
Example #22
Source File: versions.py From django-leonardo with BSD 3-Clause "New" or "Revised" License | 6 votes |
def varmap(func, var, context=None, name=None): """ Executes ``func(key_name, value)`` on all values recurisively discovering dict and list scoped values. """ if context is None: context = {} objid = id(var) if objid in context: return func(name, '<...>') context[objid] = 1 if isinstance(var, dict): ret = dict((k, varmap(func, v, context, k)) for k, v in six.iteritems(var)) elif isinstance(var, (list, tuple)): ret = [varmap(func, f, context, name) for f in var] else: ret = func(name, var) del context[objid] return ret # We store a cache of module_name->version string to avoid # continuous imports and lookups of modules
Example #23
Source File: i18n.py From python with Apache License 2.0 | 6 votes |
def get_catalog(self): pdict = {} maxcnts = {} catalog = {} trans_cat = self.translation._catalog trans_fallback_cat = self.translation._fallback._catalog if self.translation._fallback else {} for key, value in itertools.chain(six.iteritems(trans_cat), six.iteritems(trans_fallback_cat)): if key == '' or key in catalog: continue if isinstance(key, six.string_types): catalog[key] = value elif isinstance(key, tuple): msgid = key[0] cnt = key[1] maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0)) pdict.setdefault(msgid, {})[cnt] = value else: raise TypeError(key) for k, v in pdict.items(): catalog[k] = [v.get(i, '') for i in range(maxcnts[k] + 1)] return catalog
Example #24
Source File: dateparse.py From python with Apache License 2.0 | 6 votes |
def parse_time(value): """Parses a string and return a datetime.time. This function doesn't support time zone offsets. Raises ValueError if the input is well formatted but not a valid time. Returns None if the input isn't well formatted, in particular if it contains an offset. """ match = time_re.match(value) if match: kw = match.groupdict() if kw['microsecond']: kw['microsecond'] = kw['microsecond'].ljust(6, '0') kw = {k: int(v) for k, v in six.iteritems(kw) if v is not None} return datetime.time(**kw)
Example #25
Source File: dateparse.py From python with Apache License 2.0 | 6 votes |
def parse_duration(value): """Parses a duration string and returns a datetime.timedelta. The preferred format for durations in Django is '%d %H:%M:%S.%f'. Also supports ISO 8601 representation. """ match = standard_duration_re.match(value) if not match: match = iso8601_duration_re.match(value) if match: kw = match.groupdict() sign = -1 if kw.pop('sign', '+') == '-' else 1 if kw.get('microseconds'): kw['microseconds'] = kw['microseconds'].ljust(6, '0') if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'): kw['microseconds'] = '-' + kw['microseconds'] kw = {k: float(v) for k, v in six.iteritems(kw) if v is not None} return sign * datetime.timedelta(**kw)
Example #26
Source File: subqueries.py From python with Apache License 2.0 | 6 votes |
def add_update_values(self, values): """ Convert a dictionary of field name to value mappings into an update query. This is the entry point for the public update() method on querysets. """ values_seq = [] for name, val in six.iteritems(values): field = self.get_meta().get_field(name) direct = not (field.auto_created and not field.concrete) or not field.concrete model = field.model._meta.concrete_model if not direct or (field.is_relation and field.many_to_many): raise FieldError( 'Cannot update model field %r (only non-relations and ' 'foreign keys permitted).' % field ) if model is not self.get_meta().model: self.add_related_update(model, field, val) continue values_seq.append((field, model, val)) return self.add_update_fields(values_seq)
Example #27
Source File: subqueries.py From python with Apache License 2.0 | 6 votes |
def get_related_updates(self): """ Returns a list of query objects: one for each update required to an ancestor model. Each query will have the same filtering conditions as the current query but will only update a single table. """ if not self.related_updates: return [] result = [] for model, values in six.iteritems(self.related_updates): query = UpdateQuery(model) query.values = values if self.related_ids is not None: query.add_filter(('pk__in', self.related_ids)) result.append(query) return result
Example #28
Source File: query.py From python with Apache License 2.0 | 6 votes |
def _execute_query(self): connection = connections[self.using] # Adapt parameters to the database, as much as possible considering # that the target type isn't known. See #17755. params_type = self.params_type adapter = connection.ops.adapt_unknown_value if params_type is tuple: params = tuple(adapter(val) for val in self.params) elif params_type is dict: params = dict((key, adapter(val)) for key, val in six.iteritems(self.params)) else: raise RuntimeError("Unexpected params type: %s" % params_type) self.cursor = connection.cursor() self.cursor.execute(self.sql, params)
Example #29
Source File: viewsets.py From dynamic-rest with MIT License | 6 votes |
def _validate_patch_all(self, data): if not isinstance(data, dict): raise ValidationError( 'Patch-all data must be in object form' ) serializer = self.get_serializer() fields = serializer.get_all_fields() validated = {} for name, value in six.iteritems(data): field = fields.get(name, None) if field is None: raise ValidationError( 'Unknown field: "%s"' % name ) source = field.source or name if source == '*' or field.read_only: raise ValidationError( 'Cannot update field: "%s"' % name ) validated[source] = value return validated
Example #30
Source File: settings.py From django-leonardo with BSD 3-Clause "New" or "Revised" License | 5 votes |
def extract_conf_from(mod, conf=ModuleConfig(CONF_SPEC), depth=0, max_depth=2): """recursively extract keys from module or object by passed config scheme """ # extract config keys from module or object for key, default_value in six.iteritems(conf): conf[key] = _get_key_from_module(mod, key, default_value) # support for recursive dependecies try: filtered_apps = [app for app in conf['apps'] if app not in BLACKLIST] except TypeError: pass except Exception as e: warnings.warn('Error %s during loading %s' % (e, conf['apps'])) for app in filtered_apps: try: app_module = import_module(app) if app_module != mod: app_module = _get_correct_module(app_module) if depth < max_depth: mod_conf = extract_conf_from(app_module, depth=depth+1) for k, v in six.iteritems(mod_conf): # prevent config duplicity # skip config merge if k == 'config': continue if isinstance(v, dict): conf[k].update(v) elif isinstance(v, (list, tuple)): conf[k] = merge(conf[k], v) except Exception as e: pass # swallow, but maybe log for info what happens return conf