utils.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import datetime
  2. import json
  3. from collections import OrderedDict
  4. from django.core.serializers import serialize
  5. from django.db.models import Count, OuterRef, Subquery
  6. from django.http import QueryDict
  7. from jinja2 import Environment
  8. from dcim.choices import CableLengthUnitChoices
  9. from extras.utils import is_taggable
  10. def csv_format(data):
  11. """
  12. Encapsulate any data which contains a comma within double quotes.
  13. """
  14. csv = []
  15. for value in data:
  16. # Represent None or False with empty string
  17. if value is None or value is False:
  18. csv.append('')
  19. continue
  20. # Convert dates to ISO format
  21. if isinstance(value, (datetime.date, datetime.datetime)):
  22. value = value.isoformat()
  23. # Force conversion to string first so we can check for any commas
  24. if not isinstance(value, str):
  25. value = '{}'.format(value)
  26. # Double-quote the value if it contains a comma or line break
  27. if ',' in value or '\n' in value:
  28. value = value.replace('"', '""') # Escape double-quotes
  29. csv.append('"{}"'.format(value))
  30. else:
  31. csv.append('{}'.format(value))
  32. return ','.join(csv)
  33. def foreground_color(bg_color):
  34. """
  35. Return the ideal foreground color (black or white) for a given background color in hexadecimal RGB format.
  36. """
  37. bg_color = bg_color.strip('#')
  38. r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
  39. if r * 0.299 + g * 0.587 + b * 0.114 > 186:
  40. return '000000'
  41. else:
  42. return 'ffffff'
  43. def dynamic_import(name):
  44. """
  45. Dynamically import a class from an absolute path string
  46. """
  47. components = name.split('.')
  48. mod = __import__(components[0])
  49. for comp in components[1:]:
  50. mod = getattr(mod, comp)
  51. return mod
  52. def get_subquery(model, field):
  53. """
  54. Return a Subquery suitable for annotating a child object count.
  55. """
  56. subquery = Subquery(
  57. model.objects.filter(
  58. **{field: OuterRef('pk')}
  59. ).order_by().values(
  60. field
  61. ).annotate(
  62. c=Count('*')
  63. ).values('c')
  64. )
  65. return subquery
  66. def serialize_object(obj, extra=None, exclude=None):
  67. """
  68. Return a generic JSON representation of an object using Django's built-in serializer. (This is used for things like
  69. change logging, not the REST API.) Optionally include a dictionary to supplement the object data. A list of keys
  70. can be provided to exclude them from the returned dictionary. Private fields (prefaced with an underscore) are
  71. implicitly excluded.
  72. """
  73. json_str = serialize('json', [obj])
  74. data = json.loads(json_str)[0]['fields']
  75. # Include any custom fields
  76. if hasattr(obj, 'get_custom_fields'):
  77. data['custom_fields'] = {
  78. field: str(value) for field, value in obj.cf.items()
  79. }
  80. # Include any tags
  81. if is_taggable(obj):
  82. data['tags'] = [tag.name for tag in obj.tags.all()]
  83. # Append any extra data
  84. if extra is not None:
  85. data.update(extra)
  86. # Copy keys to list to avoid 'dictionary changed size during iteration' exception
  87. for key in list(data):
  88. # Private fields shouldn't be logged in the object change
  89. if isinstance(key, str) and key.startswith('_'):
  90. data.pop(key)
  91. # Explicitly excluded keys
  92. if isinstance(exclude, (list, tuple)) and key in exclude:
  93. data.pop(key)
  94. return data
  95. def dict_to_filter_params(d, prefix=''):
  96. """
  97. Translate a dictionary of attributes to a nested set of parameters suitable for QuerySet filtering. For example:
  98. {
  99. "name": "Foo",
  100. "rack": {
  101. "facility_id": "R101"
  102. }
  103. }
  104. Becomes:
  105. {
  106. "name": "Foo",
  107. "rack__facility_id": "R101"
  108. }
  109. And can be employed as filter parameters:
  110. Device.objects.filter(**dict_to_filter(attrs_dict))
  111. """
  112. params = {}
  113. for key, val in d.items():
  114. k = prefix + key
  115. if isinstance(val, dict):
  116. params.update(dict_to_filter_params(val, k + '__'))
  117. else:
  118. params[k] = val
  119. return params
  120. def deepmerge(original, new):
  121. """
  122. Deep merge two dictionaries (new into original) and return a new dict
  123. """
  124. merged = OrderedDict(original)
  125. for key, val in new.items():
  126. if key in original and isinstance(original[key], dict) and isinstance(val, dict):
  127. merged[key] = deepmerge(original[key], val)
  128. else:
  129. merged[key] = val
  130. return merged
  131. def to_meters(length, unit):
  132. """
  133. Convert the given length to meters.
  134. """
  135. length = int(length)
  136. if length < 0:
  137. raise ValueError("Length must be a positive integer")
  138. valid_units = CableLengthUnitChoices.values()
  139. if unit not in valid_units:
  140. raise ValueError(
  141. "Unknown unit {}. Must be one of the following: {}".format(unit, ', '.join(valid_units))
  142. )
  143. if unit == CableLengthUnitChoices.UNIT_METER:
  144. return length
  145. if unit == CableLengthUnitChoices.UNIT_CENTIMETER:
  146. return length / 100
  147. if unit == CableLengthUnitChoices.UNIT_FOOT:
  148. return length * 0.3048
  149. if unit == CableLengthUnitChoices.UNIT_INCH:
  150. return length * 0.3048 * 12
  151. raise ValueError("Unknown unit {}. Must be 'm', 'cm', 'ft', or 'in'.".format(unit))
  152. def render_jinja2(template_code, context):
  153. """
  154. Render a Jinja2 template with the provided context. Return the rendered content.
  155. """
  156. return Environment().from_string(source=template_code).render(**context)
  157. def prepare_cloned_fields(instance):
  158. """
  159. Compile an object's `clone_fields` list into a string of URL query parameters. Tags are automatically cloned where
  160. applicable.
  161. """
  162. params = {}
  163. for field_name in getattr(instance, 'clone_fields', []):
  164. field = instance._meta.get_field(field_name)
  165. field_value = field.value_from_object(instance)
  166. # Swap out False with URL-friendly value
  167. if field_value is False:
  168. field_value = ''
  169. # Omit empty values
  170. if field_value not in (None, ''):
  171. params[field_name] = field_value
  172. # Copy tags
  173. if is_taggable(instance):
  174. params['tags'] = ','.join([t.name for t in instance.tags.all()])
  175. # Concatenate parameters into a URL query string
  176. param_string = '&'.join(
  177. ['{}={}'.format(k, v) for k, v in params.items()]
  178. )
  179. return param_string
  180. def querydict_to_dict(querydict):
  181. """
  182. Convert a django.http.QueryDict object to a regular Python dictionary, preserving lists of multiple values.
  183. (QueryDict.dict() will return only the last value in a list for each key.)
  184. """
  185. assert isinstance(querydict, QueryDict)
  186. return {
  187. key: querydict.get(key) if len(value) == 1 and key != 'pk' else querydict.getlist(key)
  188. for key, value in querydict.lists()
  189. }
  190. def shallow_compare_dict(source_dict, destination_dict, exclude=None):
  191. """
  192. Return a new dictionary of the different keys. The values of `destination_dict` are returned. Only the equality of
  193. the first layer of keys/values is checked. `exclude` is a list or tuple of keys to be ignored.
  194. """
  195. difference = {}
  196. for key in destination_dict:
  197. if source_dict.get(key) != destination_dict[key]:
  198. if isinstance(exclude, (list, tuple)) and key in exclude:
  199. continue
  200. difference[key] = destination_dict[key]
  201. return difference