text
stringlengths
0
828
The spec can affect how individual fields will be serialized by
implementing ``serialize()`` for the fields needing customization.
:returns: dictionary
""""""
def should_we_insert(value, field_spec):
return value not in self.missing or field_spec.required
errors = {}
ret = {}
for field_name, field_spec in self.spec.fields.items():
value = self._get_value_for_serialization(entity, field_name, field_spec)
func = self._get_serialize_func(field_name, self.spec)
try:
# perform serialization
value = func(value, entity, request)
if should_we_insert(value, field_spec):
ret[field_name] = value
except ValidationError, e:
if hasattr(e, 'message_dict'):
# prefix error keys with top level field name
errors.update(dict(zip(
[field_name + '.' + key for key in e.message_dict.keys()],
e.message_dict.values())))
else:
errors[field_name] = e.messages
if errors:
raise ValidationError(errors)
return None if ret == {} else ret"
3949,"def _create_value(self, data, name, spec):
"""""" Create the value for a field.
:param data: the whole data for the entity (all fields).
:param name: name of the initialized field.
:param spec: spec for the whole entity.
""""""
field = getattr(self, 'create_' + name, None)
if field:
# this factory has a special creator function for this field
return field(data, name, spec)
value = data.get(name)
return spec.fields[name].clean(value)"
3950,"def _get_serialize_func(self, name, spec):
"""""" Return the function that is used for serialization. """"""
func = getattr(self, 'serialize_' + name, None)
if func:
# this factory has a special serializer function for this field
return func
func = getattr(spec.fields[name], 'serialize', None)
if func:
return func
return lambda value, entity, request: value"
3951,"def _get_value_for_serialization(self, data, name, spec):
"""""" Return the value of the field in entity (or ``None``). """"""
name = self.property_name_map[name]
return getattr(data, name, None)"
3952,"def _create_mappings(self, spec):
"""""" Create property name map based on aliases. """"""
ret = dict(zip(set(spec.fields), set(spec.fields)))
ret.update(dict([(n, s.alias) for n, s in spec.fields.items() if s.alias]))
return ret"
3953,"def all_substrings(s):
''' yields all substrings of a string '''
join = ''.join
for i in range(1, len(s) + 1):
for sub in window(s, i):
yield join(sub)"
3954,"def equivalent_release_for_product(self, product):
""""""
Returns the release for a specified product with the same
channel and major version with the highest minor version,
or None if no such releases exist
""""""
releases = self._default_manager.filter(
version__startswith=self.major_version() + '.',
channel=self.channel, product=product).order_by('-version')
if not getattr(settings, 'DEV', False):
releases = releases.filter(is_public=True)
if releases:
return sorted(
sorted(releases, reverse=True,
key=lambda r: len(r.version.split('.'))),
reverse=True, key=lambda r: r.version.split('.')[1])[0]"
3955,"def notes(self, public_only=False):
""""""
Retrieve a list of Note instances that should be shown for this
release, grouped as either new features or known issues, and sorted
first by sort_num highest to lowest and then by created date,
which is applied to both groups,
and then for new features we also sort by tag in the order specified
by Note.TAGS, with untagged notes coming first, then finally moving
any note with the fixed tag that starts with the release version to
the top, for what we call ""dot fixes"".
""""""