def post(self, request, **kwargs): component = self.get_component() name = request.POST.get("name") addon = ADDONS.get(name) installed = {x.addon.name for x in self.get_queryset()} if (not name or addon is None or not addon.can_install(component, request.user) or (name in installed and not addon.multiple)): return self.redirect_list(_("Invalid addon name specified!")) form = None if addon.settings_form is None: addon.create(component) return self.redirect_list() if "form" in request.POST: form = addon.get_add_form(component, data=request.POST) if form.is_valid(): form.save() return self.redirect_list() else: form = addon.get_add_form(component) addon.pre_install(component, request) return self.response_class( request=self.request, template=["addons/addon_detail.html"], context={ "addon": addon, "form": form, "object": self.kwargs["component_obj"], }, )
def post(self, request, **kwargs): component = self.get_component() name = request.POST.get('name') addon = ADDONS.get(name) installed = set([x.addon.name for x in self.get_queryset()]) if (not name or addon is None or not addon.can_install(component, request.user) or (name in installed and not addon.multiple)): return self.redirect_list(_('Invalid addon name specified!')) form = None if addon.settings_form is None: addon.create(component) return self.redirect_list() elif 'form' in request.POST: form = addon.get_add_form(component, data=request.POST) if form.is_valid(): form.save() return self.redirect_list() else: form = addon.get_add_form(component) return self.response_class( request=self.request, template=['addons/addon_detail.html'], context={ 'addon': addon, 'form': form, 'object': self.kwargs['component_obj'], }, )
def handle(self, *args, **options): """List installed addons.""" for _unused, obj in sorted(ADDONS.items()): self.stdout.write('.. _addon-{}:'.format(obj.name)) self.stdout.write('\n') self.stdout.write(obj.verbose) self.stdout.write('-' * len(obj.verbose)) self.stdout.write('\n') self.stdout.write('\n'.join(wrap(obj.description, 79))) self.stdout.write('\n')
def handle(self, *args, **options): """Create default set of languages, optionally updating them to match current shipped definitions. """ for dummy, obj in sorted(ADDONS.items()): self.stdout.write(obj.verbose) self.stdout.write('-' * len(obj.verbose)) self.stdout.write('\n') self.stdout.write('\n'.join(wrap(obj.description, 79))) self.stdout.write('\n')
def handle(self, *args, **options): """List installed addons.""" for _unused, obj in sorted(ADDONS.items()): self.stdout.write(f".. _addon-{obj.name}:") self.stdout.write("\n") self.stdout.write(obj.verbose) self.stdout.write("-" * len(obj.verbose)) self.stdout.write("\n") self.stdout.write("\n".join(wrap(obj.description, 79))) self.stdout.write("\n")
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 handle(self, *args, **options): """Create default set of languages, optionally updating them to match current shipped definitions. """ for dummy, obj in sorted(ADDONS.items()): self.stdout.write('.. _addon-{}:'.format(obj.name)) self.stdout.write('\n') self.stdout.write(obj.verbose) self.stdout.write('-' * len(obj.verbose)) self.stdout.write('\n') self.stdout.write('\n'.join(wrap(obj.description, 79))) self.stdout.write('\n')
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
def handle(self, *args, **options): """List installed add-ons.""" fake_addon = Addon(component=Component(project=Project(pk=-1), pk=-1)) for _unused, obj in sorted(ADDONS.items()): self.stdout.write(f".. _addon-{obj.name}:") self.stdout.write("\n") self.stdout.write(obj.verbose) self.stdout.write("-" * len(obj.verbose)) self.stdout.write("\n") self.stdout.write(f":Add-on ID: ``{obj.name}``") if obj.settings_form: form = obj(fake_addon).get_settings_form(None) table = [(f"``{name}``", str(field.label), self.get_help_text(field, name)) for name, field in form.fields.items()] prefix = ":Configuration: " name_width = max( len(name) for name, _label, _help_text in table) label_width = max( len(label) for _name, label, _help_text in table) help_text_width = max( max(len(line) for line in help_text) if help_text else 0 for _name, _label, help_text in table) name_row = "-" * (name_width + 2) label_row = "-" * (label_width + 2) help_text_row = "-" * (help_text_width + 2) for name, label, help_text in table: if not prefix.isspace(): self.stdout.write( f"{prefix}+{name_row}+{label_row}+{help_text_row}+" ) prefix = " " if not help_text: line = "" self.stdout.write( f"{prefix}| {name:<{name_width}s} | {label:<{label_width}s} | {line:<{help_text_width}s} |" ) for pos, line in enumerate(help_text): if pos > 0: name = label = "" self.stdout.write( f"{prefix}| {name:<{name_width}s} | {label:<{label_width}s} | {line:<{help_text_width}s} |" ) self.stdout.write( f"{prefix}+{name_row}+{label_row}+{help_text_row}+") else: self.stdout.write( ":Configuration: `This add-on has no configuration.`") events = ", ".join(EVENT_NAMES[event] for event in obj.events) self.stdout.write(f":Triggers: {events}") self.stdout.write("\n") self.stdout.write("\n".join(wrap(obj.description, 79))) self.stdout.write("\n")
def handle(self, *args, **options): """List installed add-ons.""" fake_addon = Addon(component=Component(project=Project())) for _unused, obj in sorted(ADDONS.items()): self.stdout.write(f".. _addon-{obj.name}:") self.stdout.write("\n") self.stdout.write(obj.verbose) self.stdout.write("-" * len(obj.verbose)) self.stdout.write("\n") if obj.settings_form: form = obj(fake_addon).get_settings_form(None) params = ", ".join(f"``{key}``" for key in form.fields.keys()) else: params = "`This add-on has no configuration.`" self.stdout.write(PARAMS_TABLE.format(obj.name, params)) self.stdout.write("\n") self.stdout.write("\n".join(wrap(obj.description, 79))) self.stdout.write("\n")
def handle(self, *args, **options): """List installed add-ons.""" fake_addon = Addon(component=Component(project=Project())) for _unused, obj in sorted(ADDONS.items()): self.stdout.write(f".. _addon-{obj.name}:") self.stdout.write("\n") self.stdout.write(obj.verbose) self.stdout.write("-" * len(obj.verbose)) self.stdout.write("\n") self.stdout.write(f":Add-on ID: ``{obj.name}``") if obj.settings_form: form = obj(fake_addon).get_settings_form(None) table = [ (f"``{name}``", str(field.label), str(field.help_text)) for name, field in form.fields.items() ] prefix = ":Configuration: " name_width = max(len(row[0]) for row in table) label_width = max(len(row[1]) for row in table) help_text_width = max(len(row[2]) for row in table) name_row = "-" * (name_width + 2) label_row = "-" * (label_width + 2) help_text_row = "-" * (help_text_width + 2) for name, label, help_text in table: if not prefix.isspace(): self.stdout.write( f"{prefix}+{name_row}+{label_row}+{help_text_row}+" ) prefix = " " self.stdout.write( f"{prefix}| {name:<{name_width}s} | {label:<{label_width}s} | {help_text:<{help_text_width}s} |" ) self.stdout.write( f"{prefix}+{name_row}+{label_row}+{help_text_row}+" ) else: self.stdout.write(":Configuration: `This add-on has no configuration.`") self.stdout.write("\n") self.stdout.write("\n".join(wrap(obj.description, 79))) self.stdout.write("\n")