def _get_erred_resources_module(self): """ Returns a list of links to resources which are in ERRED state and linked to a shared service settings. """ result_module = modules.LinkList(title=_('Resources in erred state')) erred_state = structure_models.NewResource.States.ERRED children = [] resource_models = SupportedServices.get_resource_models() resources_in_erred_state_overall = 0 for resource_type, resource_model in resource_models.items(): queryset = resource_model.objects.filter( service_project_link__service__settings__shared=True) erred_amount = queryset.filter(state=erred_state).count() if erred_amount: resources_in_erred_state_overall = resources_in_erred_state_overall + erred_amount link = self._get_erred_resource_link(resource_model, erred_amount, erred_state) children.append(link) if resources_in_erred_state_overall: result_module.title = '%s (%s)' % ( result_module.title, resources_in_erred_state_overall) result_module.children = children else: result_module.pre_content = _('Nothing found.') return result_module
def queryset(self, request, queryset): if self.value(): model = SupportedServices.get_resource_models().get( self.value(), None) if model: return queryset.filter(resource_content_type=ContentType. objects.get_for_model(model)) return queryset
def get_field_type(field): """ Returns field type/possible values. """ if isinstance(field, core_filters.MappedMultipleChoiceFilter): return ' | '.join(['"%s"' % f for f in sorted(field.mapped_to_model)]) if isinstance(field, OrderingFilter) or isinstance(field, ChoiceFilter): return ' | '.join(['"%s"' % f[0] for f in field.extra['choices']]) if isinstance(field, ChoiceField): return ' | '.join(['"%s"' % f for f in sorted(field.choices)]) if isinstance(field, HyperlinkedRelatedField): if field.view_name.endswith('detail'): return 'link to %s' % reverse( field.view_name, kwargs={ '%s' % field.lookup_field: "'%s'" % field.lookup_field }, ) return reverse(field.view_name) if isinstance(field, structure_filters.ServiceTypeFilter): return ' | '.join([ '"%s"' % f for f in SupportedServices.get_filter_mapping().keys() ]) if isinstance(field, ResourceTypeFilter): return ' | '.join([ '"%s"' % f for f in SupportedServices.get_resource_models().keys() ]) if isinstance(field, core_serializers.GenericRelatedField): links = [] for model in field.related_models: detail_view_name = core_utils.get_detail_view_name(model) for f in field.lookup_fields: try: link = reverse(detail_view_name, kwargs={'%s' % f: "'%s'" % f}) except NoReverseMatch: pass else: links.append(link) break path = ', '.join(links) if path: return 'link to any: %s' % path if isinstance(field, core_filters.ContentTypeFilter): return "string in form 'app_label'.'model_name'" if isinstance(field, ModelMultipleChoiceFilter): return get_field_type(field.field) if isinstance(field, ListSerializer): return 'list of [%s]' % get_field_type(field.child) if isinstance(field, ManyRelatedField): return 'list of [%s]' % get_field_type(field.child_relation) if isinstance(field, ModelField): return get_field_type(field.model_field) name = field.__class__.__name__ for w in ('Filter', 'Field', 'Serializer'): name = name.replace(w, '') return FIELDS.get(name, name)
def filter_has_resources(self, queryset, name, value): service_settings_with_resources = [] for model in SupportedServices.get_resource_models().values(): service_settings_with_resources \ += list(model.objects .values_list('service_project_link__service__settings__pk', flat=True)) if value is True: return queryset.filter(pk__in=service_settings_with_resources) return queryset.exclude(Q(pk__in=service_settings_with_resources))
def filter(self, qs, value): if value in EMPTY_VALUES: return qs resource_models = SupportedServices.get_resource_models() try: model = resource_models[value] ct = ContentType.objects.get_for_model(model) return super(ResourceTypeFilter, self).filter(qs, ct) except (ContentType.DoesNotExist, KeyError): return qs.none()
def lookups(self, request, model_admin): resources = [ (model, name) for name, model in SupportedServices.get_resource_models().items() ] others = [(model, model.__name__) for model in models.PriceEstimate.get_estimated_models() if not issubclass(model, structure_models.ResourceMixin)] estimated_models = [(core_utils.serialize_class(model), name) for model, name in resources + others] return sorted(estimated_models, key=lambda x: x[1])
def run(self): date = datetime.now() - timedelta(days=1) start_date = date.replace(hour=0, minute=0, second=0, microsecond=0) end_date = start_date + timedelta(days=1, microseconds=-1) # XXX: it's just a placeholder, it doesn't work properly now nor implemented anyhow # perhaps it should merely use price estimates.. models = SupportedServices.get_resource_models().values() for model in models: resources = model.objects.filter( service_project_link__service__settings__shared=True) for resource in resources: try: data = resource.get_cost(start_date, end_date) except NotImplementedError: continue else: resource.customer.debit_account(data['total_amount'])
def lookups(self, request, model_admin): return [ (name, name) for name, model in SupportedServices.get_resource_models().items() if model in CostTrackingRegister.registered_resources ]
'tags', 'port', ) USER_LOGGERS = ( Saml2AuthEventLogger, SocialEventLogger, AuthEventLogger, FreeIPAEventLogger, ) USER_EVENTS = reduce( operator.add, map(lambda logger: tuple(logger.Meta.event_types), USER_LOGGERS)) RESOURCE_MODELS = SupportedServices.get_resource_models() def get_scopes(event): scopes = [] user_uuid = event.context.get('user_uuid') if user_uuid and event.event_type in USER_EVENTS: try: user = User.objects.get(uuid=user_uuid) scopes.append(user) except ObjectDoesNotExist: pass affected_user_uuid = event.context.get('affected_user_uuid') if affected_user_uuid: try: