def get_context_data(self, **kwargs): result = super(AddonList, self).get_context_data(**kwargs) component = self.kwargs['component'] result['object'] = component installed = set([x.addon.name for x in result['object_list']]) result['available'] = [ x for x in ADDONS.values() if x.is_compatible(component) and x.name not in installed ] return result
def validate(self, attrs): instance = self.instance try: name = attrs["name"] except KeyError: if self.partial and self.instance: name = self.instance.name else: raise serializers.ValidationError( {"name": "Can not change add-on name"}) if instance: # Update component = instance.component else: # Create component = self._context["component"] # This could probably work, but it safer not to allow it if instance and instance.name != name: raise serializers.ValidationError( {"name": "Can not change add-on name"}) try: addon_class = ADDONS[name] except KeyError: raise serializers.ValidationError( {"name": f"Add-on not found: {name}"}) # Don't allow duplicate add-ons if not instance: installed = set( Addon.objects.filter_component(component).values_list( "name", flat=True)) available = { x.name for x in ADDONS.values() if x.multiple or x.name not in installed } if name not in available: raise serializers.ValidationError( {"name": f"Add-on already installed: {name}"}) addon = addon_class() if not addon.can_install(component, None): raise serializers.ValidationError( {"name": f"could not enable add-on {name}, not compatible"}) if addon.has_settings() and "configuration" in attrs: form = addon.get_add_form(None, component, data=attrs["configuration"]) form.is_valid() if not form.is_valid(): raise serializers.ValidationError( {"configuration": list(get_form_errors(form))}) return attrs
def get_context_data(self, **kwargs): result = super(AddonList, self).get_context_data(**kwargs) component = self.kwargs['component_obj'] result['object'] = component installed = set([x.addon.name for x in result['object_list']]) result['available'] = [ x for x in ADDONS.values() if x.can_install(component, self.request.user) and (x.multiple or x.name not in installed) ] return result
def get_context_data(self, **kwargs): result = super(AddonList, self).get_context_data(**kwargs) component = self.kwargs['component_obj'] result['object'] = component installed = set([x.addon.name for x in result['object_list']]) result['available'] = [ x for x in ADDONS.values() if x.can_install(component, self.request.user) and ( x.multiple or x.name not in installed) ] return result
def get_context_data(self, **kwargs): result = super().get_context_data(**kwargs) component = self.kwargs["component_obj"] result["object"] = component installed = {x.addon.name for x in result["object_list"]} result["available"] = sorted( (x for x in ADDONS.values() if x.can_install(component, self.request.user) and ( x.multiple or x.name not in installed)), key=lambda x: x.name, ) return result