def get_context_data(self, **kwargs): ret = super(Catalog, self).get_context_data(**kwargs) try: model_type_id = int(self.kwargs.get('type', '')) except ValueError: model_type_id = None kind = self.kwargs.get('kind') sidebar_items = ( [ MenuHeader('Component groups'), ] + [ MenuItem( label=t.raw.title(), name='component-%d' % t.id, fugue_icon=COMPONENT_ICONS.get(t.id), view_name='catalog', view_args=('component', t.id), ) for t in ComponentType(item=lambda t: t) ] + [ MenuHeader('Device groups'), ] + [ MenuItem( label=t.raw.title(), name='device-%d' % t.id, fugue_icon=DEVICE_ICONS.get(t.id), view_name='catalog', view_args=('device', t.id), ) for t in DeviceType(item=lambda t: t) ] + [ MenuHeader('Tools'), MenuItem( 'History', name='history', fugue_icon='fugue-hourglass', view_name='catalog_history', ), MenuItem( 'Pricing groups', name='pricing', fugue_icon='fugue-shopping-basket', view_name='catalog_pricing', view_args=('pricing',), ), ] ) selected = '%s-%d' % (kind, model_type_id) if model_type_id else '' ret.update({ 'component_model_types': ComponentType(item=lambda a: a), 'details': self.kwargs.get('details', 'info'), 'device_model_types': DeviceType(item=lambda a: a), 'editable': True, 'kind': kind, 'model_type_id': model_type_id, 'sidebar_items': sidebar_items, 'sidebar_selected': selected, 'subsection': model_type_id, }) return ret
def dehydrate(self, bundle): device = bundle.obj details = _get_details(bundle.obj) components = dict() total = 0 for detail in details: model = detail.get('model') price = detail.get('price') or 0 model_type = None model_name = str(model) if hasattr(model, 'type'): try: model_type = ComponentType.from_id(model.type) except ValueError: pass if model_type and model_type == ComponentType.software: model = ComponentType.software.name, model_name = model if not components.get(model_name): components[model_name] = { 'model': model, 'count': 1, 'price': price, 'serial': detail.get('serial'), } else: components[model_name]['count'] += 1 total += price bundle.data['components'] = components.values() bundle.data['total_cost'] = total bundle.data['deprecated'] = device.is_deprecated() splunk_start = bundle.request.GET.get('splunk_start') splunk_end = bundle.request.GET.get('splunk_end') if splunk_start and splunk_end: try: splunk_start = datetime.datetime.strptime( splunk_start, '%Y-%m-%d', ) splunk_end = datetime.datetime.strptime( splunk_end, '%Y-%m-%d', ) except ValueError: splunk_start, splunk_end = None, None splunk = self.splunk_cost(bundle.obj, splunk_start, splunk_end) bundle.data['splunk'] = splunk return bundle
def dehydrate(self, bundle): device = bundle.obj details = _get_details(bundle.obj) components = dict() total = 0 for detail in details: model = detail.get("model") price = detail.get("price") or 0 model_type = None model_name = str(model) if hasattr(model, "type"): try: model_type = ComponentType.from_id(model.type) except ValueError: pass if model_type and model_type == ComponentType.software: model = (ComponentType.software.name,) model_name = model if not components.get(model_name): components[model_name] = {"model": model, "count": 1, "price": price, "serial": detail.get("serial")} else: components[model_name]["count"] += 1 total += price bundle.data["components"] = components.values() bundle.data["total_cost"] = total bundle.data["deprecated"] = device.is_deprecated() splunk_start = bundle.request.GET.get("splunk_start") splunk_end = bundle.request.GET.get("splunk_end") if splunk_start and splunk_end: try: splunk_start = datetime.datetime.strptime(splunk_start, "%Y-%m-%d") splunk_end = datetime.datetime.strptime(splunk_end, "%Y-%m-%d") except ValueError: splunk_start, splunk_end = None, None splunk = self.splunk_cost(bundle.obj, splunk_start, splunk_end) bundle.data["splunk"] = splunk return bundle