Advertisement
Romeech

refactoring

Dec 6th, 2018
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. def build_display_name(resource):
  2.     display_name = resource['ui__display_name']
  3.     return display_name if display_name else display_name_from_resource_name(resource['name'])
  4.  
  5.  
  6. def build_base_item(resource):
  7.     return {
  8.         'name': resource['name'],
  9.         'type': resource['unit__title'],
  10.         'display_name': build_display_name(resource),
  11.         'description': resource['description__value'],
  12.     }
  13.  
  14.  
  15. def compose_item(resource, item):
  16.     final_item = build_base_item(resource)
  17.     final_item['limit'] = item
  18.     return final_item
  19.  
  20.  
  21. def compose_renewal_item(resource, item):
  22.     limit, from_date, till_date, delta, rn_period = item
  23.     final_item = build_base_item(resource)
  24.     final_item.update({
  25.         'limit': limit,
  26.         'from': from_date,
  27.         'till': till_date,
  28.         'delta': delta,
  29.         'renewal_period': rn_period,
  30.     })
  31.     return final_item
  32.  
  33.  
  34. def perform_limits_composition(connector, item_dict, composer):
  35.     sorted_resources = connector.resources.filter(
  36.         name__in=item_dict.keys()
  37.     ).values(
  38.         'name', 'unit__title', 'ui__display_name', 'description__value',
  39.     ).order_by(
  40.         'created',
  41.     )
  42.  
  43.     return [composer(res, item_dict[res['name']]) for res in sorted_resources]
  44.  
  45.  
  46. def compose_limits(connector, item_dict):
  47.     return perform_limits_composition(connector, item_dict, compose_item)
  48.  
  49.  
  50. def compose_renewed_limits(connector, rn_item_dict):
  51.     return perform_limits_composition(connector, rn_item_dict, compose_renewal_item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement