def resource_policies(self): policies = [] append = policies.append for name, value in self.data.iteritems(): if not value: continue uplimit = value if name.endswith('_uplimit'): subs = name.split('_uplimit') prefix, suffix = subs try: resource = Resource.objects.get(name=prefix) except Resource.DoesNotExist: raise forms.ValidationError("Resource %s does not exist" % resource.name) # keep only resource limits for selected resource groups if self.data.get('is_selected_%s' % resource.group, "0") == "1": if not resource.ui_visible: raise forms.ValidationError("Invalid resource %s" % resource.name) d = model_to_dict(resource) try: uplimit = long(uplimit) except ValueError: m = "Limit should be an integer" raise forms.ValidationError(m) display = units.show(uplimit, resource.unit) d.update(dict(resource=prefix, uplimit=uplimit, display_uplimit=display)) append(d) ordered_keys = presentation.RESOURCES['resources_order'] def resource_order(r): if r['str_repr'] in ordered_keys: return ordered_keys.index(r['str_repr']) else: return -1 policies = sorted(policies, key=resource_order) return policies
def resource_policies(self): policies = [] append = policies.append for name, value in self.data.iteritems(): if not value: continue uplimit = value if name.endswith('_uplimit'): subs = name.split('_uplimit') prefix, suffix = subs try: resource = Resource.objects.get(name=prefix) except Resource.DoesNotExist: raise forms.ValidationError("Resource %s does not exist" % resource.name) # keep only resource limits for selected resource groups if self.data.get( 'is_selected_%s' % resource.group, "0" ) == "1": if not resource.allow_in_projects: raise forms.ValidationError("Invalid resource %s" % resource.name) d = model_to_dict(resource) if uplimit: d.update(dict(resource=prefix, uplimit=uplimit)) else: d.update(dict(resource=prefix, uplimit=None)) append(d) ordered_keys = presentation.RESOURCES['resources_order'] def resource_order(r): if r['str_repr'] in ordered_keys: return ordered_keys.index(r['str_repr']) else: return -1 policies = sorted(policies, key=resource_order) return policies
def resource_policies(self): policies = [] append = policies.append for name, value in self.data.iteritems(): if not value: continue uplimit = value if name.endswith('_uplimit'): subs = name.split('_uplimit') prefix, suffix = subs try: resource = Resource.objects.get(name=prefix) except Resource.DoesNotExist: raise forms.ValidationError("Resource %s does not exist" % resource.name) # keep only resource limits for selected resource groups if self.data.get('is_selected_%s' % resource.group, "0") == "1": if not resource.allow_in_projects: raise forms.ValidationError("Invalid resource %s" % resource.name) d = model_to_dict(resource) if uplimit: d.update(dict(resource=prefix, uplimit=uplimit)) else: d.update(dict(resource=prefix, uplimit=None)) append(d) ordered_keys = presentation.RESOURCES['resources_order'] def resource_order(r): if r['str_repr'] in ordered_keys: return ordered_keys.index(r['str_repr']) else: return -1 policies = sorted(policies, key=resource_order) return policies
def _resources_catalog(): """ `resource_catalog` contains a list of tuples. Each tuple contains the group key the resource is assigned to and resources list of dicts that contain resource information. `resource_groups` contains information about the groups """ # presentation data resources_meta = presentation.RESOURCES resource_groups = resources_meta.get('groups', {}) resource_catalog = () resource_keys = [] # resources in database resource_details = map(lambda obj: model_to_dict(obj, exclude=[]), Resource.objects.all()) # initialize resource_catalog to contain all group/resource information for r in resource_details: if not r.get('group') in resource_groups: resource_groups[r.get('group')] = {'icon': 'unknown'} resource_keys = [r.get('str_repr') for r in resource_details] resource_catalog = [[g, filter(lambda r: r.get('group', '') == g, resource_details)] for g in resource_groups] # order groups, also include unknown groups groups_order = resources_meta.get('groups_order') for g in resource_groups.keys(): if not g in groups_order: groups_order.append(g) # order resources, also include unknown resources resources_order = resources_meta.get('resources_order') for r in resource_keys: if not r in resources_order: resources_order.append(r) # sort catalog groups resource_catalog = sorted(resource_catalog, key=lambda g: groups_order.index(g[0])) # sort groups def groupindex(g): return groups_order.index(g[0]) resource_groups_list = sorted([(k, v) for k, v in resource_groups.items()], key=groupindex) resource_groups = OrderedDict(resource_groups_list) # sort resources def resourceindex(r): return resources_order.index(r['str_repr']) for index, group in enumerate(resource_catalog): resource_catalog[index][1] = sorted(resource_catalog[index][1], key=resourceindex) if len(resource_catalog[index][1]) == 0: resource_catalog.pop(index) for gindex, g in enumerate(resource_groups): if g[0] == group[0]: resource_groups.pop(gindex) # filter out resources which user cannot request in a project application for group, resources in list(resource_catalog): for resource in resources: if not resource.get('ui_visible'): resources.remove(resource) # cleanup empty groups resource_catalog_new = [] for group, resources in list(resource_catalog): if len(resources) == 0: resource_groups.pop(group) else: resource_catalog_new.append((group, resources)) return resource_catalog_new, resource_groups
def _resources_catalog(as_dict=False): """ `resource_catalog` contains a list of tuples. Each tuple contains the group key the resource is assigned to and resources list of dicts that contain resource information. `resource_groups` contains information about the groups """ # presentation data resources_meta = presentation.RESOURCES resource_groups = resources_meta.get('groups', {}) resource_catalog = () resource_keys = [] # resources in database resource_details = map(lambda obj: model_to_dict(obj, exclude=[]), Resource.objects.all()) # initialize resource_catalog to contain all group/resource information for r in resource_details: if not r.get('group') in resource_groups: resource_groups[r.get('group')] = {'icon': 'unknown'} resource_keys = [r.get('str_repr') for r in resource_details] resource_catalog = [[ g, filter(lambda r: r.get('group', '') == g, resource_details) ] for g in resource_groups] # order groups, also include unknown groups groups_order = resources_meta.get('groups_order') for g in resource_groups.keys(): if not g in groups_order: groups_order.append(g) # order resources, also include unknown resources resources_order = resources_meta.get('resources_order') for r in resource_keys: if not r in resources_order: resources_order.append(r) # sort catalog groups resource_catalog = sorted(resource_catalog, key=lambda g: groups_order.index(g[0])) # sort groups def groupindex(g): return groups_order.index(g[0]) resource_groups_list = sorted([(k, v) for k, v in resource_groups.items()], key=groupindex) resource_groups = OrderedDict(resource_groups_list) # sort resources def resourceindex(r): return resources_order.index(r['str_repr']) for index, group in enumerate(resource_catalog): resource_catalog[index][1] = sorted(resource_catalog[index][1], key=resourceindex) if len(resource_catalog[index][1]) == 0: resource_catalog.pop(index) for gindex, g in enumerate(resource_groups): if g[0] == group[0]: resource_groups.pop(gindex) # filter out resources which user cannot request in a project application for group, resources in list(resource_catalog): for resource in resources: if not resource.get('ui_visible'): resources.remove(resource) # cleanup empty groups resource_catalog_new = [] for group, resources in list(resource_catalog): if len(resources) == 0: resource_groups.pop(group) else: resource_catalog_new.append((group, resources)) if as_dict: resource_catalog_new = OrderedDict(resource_catalog_new) for name, resources in resource_catalog_new.iteritems(): _rs = OrderedDict() for resource in resources: _rs[resource.get('name')] = resource resource_catalog_new[name] = _rs resource_groups = OrderedDict(resource_groups) return resource_catalog_new, resource_groups
def resource_policies(self): policies = [] append = policies.append resource_indexes = {} include_diffs = False is_new = self.instance and self.instance.id is None existing_policies = [] existing_data = {} # normalize to single values dict data = dict() for key, value in self.data.iteritems(): data[key] = value if not is_new: # User may have emptied some fields. Empty values are not handled # below. Fill data as if user typed "0" in field, but only # for resources which exist in application project and have # non-zero capacity (either for member or project). include_diffs = True existing_policies = self.instance.resource_set append_groups = set() for policy in existing_policies: cap_set = max(policy.project_capacity, policy.member_capacity) if not policy.resource.ui_visible: continue rname = policy.resource.name group = policy.resource.group existing_data["%s_p_uplimit" % rname] = "0" existing_data["%s_m_uplimit" % rname] = "0" append_groups.add(group) for key, value in existing_data.iteritems(): if not key in data or data.get(key, '') == '': data[key] = value for group in append_groups: data["is_selected_%s" % group] = "1" for name, value in data.iteritems(): if not value: continue if name.endswith('_uplimit'): is_project_limit = name.endswith('_p_uplimit') suffix = '_p_uplimit' if is_project_limit else '_m_uplimit' if value == 'inf' or value == 'Unlimited': value = units.PRACTICALLY_INFINITE uplimit = value prefix, _suffix = name.split(suffix) try: resource = Resource.objects.get(name=prefix) except Resource.DoesNotExist: raise forms.ValidationError("Resource %s does not exist" % resource.name) if is_project_limit: member_limit = data.get(prefix + '_m_uplimit') try: pvalue = int(value) mvalue = int(member_limit) except: raise forms.ValidationError("Invalid format") else: project_limit = data.get(prefix + '_p_uplimit') try: mvalue = int(value) pvalue = int(project_limit) except: raise forms.ValidationError("Invalid format") if mvalue > pvalue: msg = "%s per member limit exceeds total limit" raise forms.ValidationError(msg % resource.name) # keep only resource limits for selected resource groups if data.get('is_selected_%s' % \ resource.group, "0") == "1": if not resource.ui_visible: raise forms.ValidationError("Invalid resource %s" % resource.name) d = model_to_dict(resource) try: uplimit = long(uplimit) except ValueError: m = "Limit should be an integer" raise forms.ValidationError(m) display = units.show(uplimit, resource.unit) if display == "inf": display = "Unlimited" handled = resource_indexes.get(prefix) diff_data = None if include_diffs: try: policy = existing_policies.get(resource=resource) if is_project_limit: pval = policy.project_capacity else: pval = policy.member_capacity if pval != uplimit: diff = pval - uplimit diff_display = units.show(abs(diff), resource.unit, inf="Unlimited") diff_is_inf = False prev_is_inf = False if uplimit == units.PRACTICALLY_INFINITE: diff_display = "Unlimited" diff_is_inf = True if pval == units.PRACTICALLY_INFINITE: diff_display = "Unlimited" prev_is_inf = True prev_display = units.show(pval, resource.unit, inf="Unlimited") diff_data = { 'prev': pval, 'prev_display': prev_display, 'diff': diff, 'diff_display': diff_display, 'increased': diff < 0, 'diff_is_inf': diff_is_inf, 'prev_is_inf': prev_is_inf, 'operator': '+' if diff < 0 else '-' } except: pass if is_project_limit: d.update(dict(resource=prefix, p_uplimit=uplimit, display_p_uplimit=display)) if diff_data: d.update(dict(resource=prefix, p_diff=diff_data)) if not handled: d.update(dict(resource=prefix, m_uplimit=0, display_m_uplimit=units.show(0, resource.unit))) else: d.update(dict(resource=prefix, m_uplimit=uplimit, display_m_uplimit=display)) if diff_data: d.update(dict(resource=prefix, m_diff=diff_data)) if not handled: d.update(dict(resource=prefix, p_uplimit=0, display_p_uplimit=units.show(0, resource.unit))) if resource_indexes.get(prefix, None) is not None: # already included in policies handled.update(d) else: # keep track of resource dicts append(d) resource_indexes[prefix] = d ordered_keys = presentation.RESOURCES['resources_order'] def resource_order(r): if r['str_repr'] in ordered_keys: return ordered_keys.index(r['str_repr']) else: return -1 policies = sorted(policies, key=resource_order) return policies