def render(self, name, value, attrs=None): initial = value or '' final_attrs = self.build_attrs(self.attrs) final_attrs.update(attrs or {}) self.html_id = final_attrs.pop('id', name) final_attrs.pop('required', None) lookup = registry.get(self.channel) if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'current_repr': initial, 'current_id': initial, 'help_text': help_text, 'html_id': self.html_id, 'name': name, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), } context.update( make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ('ajax_select/autocomplete_%s.html' % self.channel, 'ajax_select/autocomplete.html') return mark_safe(render_to_string(templates, context))
def render(self, name, value, attrs=None): initial = value or "" final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop("id", name) lookup = registry.get(self.channel) if self.show_help_text: help_text = self.help_text else: help_text = "" context = { "current_repr": initial, "current_id": initial, "help_text": help_text, "html_id": self.html_id, "name": name, "extra_attrs": mark_safe(flatatt(final_attrs)), "func_slug": self.html_id.replace("-", ""), } context.update(make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ("ajax_select/autocomplete_%s.html" % self.channel, "ajax_select/autocomplete.html") return mark_safe(render_to_string(templates, context))
def clean(self, value): if value: lookup = registry.get(self.channel) objs = lookup.get_objects([value]) if len(objs) != 1: # someone else might have deleted it while you were editing # or your channel is faulty # out of the scope of this field to do anything more than tell you it doesn't exist raise forms.ValidationError("%s cannot find object: %s" % (lookup, value)) return objs[0] else: if self.required: raise forms.ValidationError(self.error_messages['required']) return None
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(self.attrs) final_attrs.update(attrs or {}) final_attrs.pop('required', None) self.html_id = final_attrs.pop('id', name) lookup = registry.get(self.channel) values = list(value) if all([isinstance(v, Model) for v in values]): objects = values else: objects = lookup.get_objects(values) current_ids = pack_ids([obj.pk for obj in objects]) # text repr of currently selected items initial = [ [lookup.format_item_display(obj), obj.pk] for obj in objects ] if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id': self.html_id, 'current': value, 'current_ids': current_ids, 'current_reprs': mark_safe( json.dumps(initial, cls=DjangoJSONEncoder) ), 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), 'add_link': self.add_link, } context.update( make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self.channel, 'ajax_select/autocompleteselectmultiple.html') out = render_to_string(templates, context) return mark_safe(out)
def get_initial_data(self, field_name, parent): lookup = registry.get(self.channel_name) initial = None qs = None if hasattr(parent, "initial_data"): pks = parent.initial_data.getlist(field_name) if pks: qs = self.queryset.filter(pk__in=pks) elif hasattr(parent, "instance") and parent.instance: qs = getattr(parent.instance, field_name).all() initial = qs and [[lookup.format_item_display(obj), obj.pk] for obj in qs] or [] return initial
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(self.attrs) final_attrs.update(attrs or {}) final_attrs.pop('required', None) self.html_id = final_attrs.pop('id', name) lookup = registry.get(self.channel) values = list(value) if all([isinstance(v, Model) for v in values]): objects = values else: objects = lookup.get_objects(values) current_ids = pack_ids([obj.pk for obj in objects]) # text repr of currently selected items initial = [[lookup.format_item_display(obj), obj.pk] for obj in objects] if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id': self.html_id, 'current': value, 'current_ids': current_ids, 'current_reprs': mark_safe(json.dumps(initial, cls=DjangoJSONEncoder)), 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), 'add_link': self.add_link, } context.update( make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self.channel, 'ajax_select/autocompleteselectmultiple.html') out = render_to_string(templates, context) return mark_safe(out)
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = registry.get(self.channel) # eg. value = [3002L, 1194L] if value: # |pk|pk| of current current_ids = "|" + "|".join(str(pk) for pk in value) + "|" else: current_ids = "|" objects = lookup.get_objects(value) # text repr of currently selected items initial = [] for obj in objects: display = lookup.format_item_display(obj) initial.append([display, obj.pk]) if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id': self.html_id, 'current': value, 'current_ids': current_ids, 'current_reprs': mark_safe(json.dumps(initial)), 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), 'add_link': self.add_link, } context.update( plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ('ajax_select/autocompleteselectmultiple_%s.html' % self.channel, 'ajax_select/autocompleteselectmultiple.html') out = render_to_string(templates, context) return mark_safe(out)
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop("id", name) lookup = registry.get(self.channel) # eg. value = [3002L, 1194L] if value: # |pk|pk| of current current_ids = "|" + "|".join(str(pk) for pk in value) + "|" else: current_ids = "|" objects = lookup.get_objects(value) # text repr of currently selected items initial = [] for obj in objects: display = lookup.format_item_display(obj) initial.append([display, obj.pk]) if self.show_help_text: help_text = self.help_text else: help_text = "" context = { "name": name, "html_id": self.html_id, "current": value, "current_ids": current_ids, "current_reprs": mark_safe(json.dumps(initial)), "help_text": help_text, "extra_attrs": mark_safe(flatatt(final_attrs)), "func_slug": self.html_id.replace("-", ""), "add_link": self.add_link, } context.update(plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ( "ajax_select/autocompleteselectmultiple_%s.html" % self.channel, "ajax_select/autocompleteselectmultiple.html", ) out = render_to_string(templates, context) return mark_safe(out)
def get_initial_data(self, field_name, parent): lookup = registry.get(self.channel_name) initial = None obj = None if hasattr(parent, "initial_data"): pk = parent.initial_data.get(field_name) if pk: try: obj = self.related_model.objects.get(pk=pk) except self.related_model.DoesNotExist: pass elif hasattr(parent, "instance") and parent.instance: obj = getattr(parent.instance, field_name) initial = obj and [[lookup.format_item_display(obj), obj.pk]] or None return initial
def autoselect_fields_check_can_add(form, model, user): """ Adapted from ajax_select.fields from the django-ajax-selects package. Modified to catch non-database fields and use the lookup to determine if the user has the add permission for that lookup's model. """ for name, form_field in form.declared_fields.items(): if isinstance( form_field, (AutoCompleteSelectMultipleField, AutoCompleteSelectField)): try: db_field = model._meta.get_field(name) if hasattr(db_field, "remote_field"): form_field.check_can_add(user, db_field.remote_field.model) else: form_field.check_can_add(user, db_field.rel.to) except FieldDoesNotExist: lookup = registry.get(form_field.channel) form_field.check_can_add(user, lookup.model)
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop("id", name) lookup = registry.get(self.channel) if isinstance(value, QuerySet): objects = value else: objects = lookup.get_objects(value) current_ids = pack_ids([obj.pk for obj in objects]) # text repr of currently selected items initial = [[lookup.format_item_display(obj), obj.pk] for obj in objects] if self.show_help_text: help_text = self.help_text else: help_text = "" context = { "name": name, "html_id": self.html_id, "current": value, "current_ids": current_ids, "current_reprs": mark_safe(json.dumps(initial)), "help_text": help_text, "extra_attrs": mark_safe(flatatt(final_attrs)), "func_slug": self.html_id.replace("-", ""), "add_link": self.add_link, } context.update(make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ( "ajax_select/autocompleteselectmultiple_%s.html" % self.channel, "ajax_select/autocompleteselectmultiple.html", ) out = render_to_string(templates, context) return mark_safe(out)
def render(self, name, value, attrs=None): value = value or '' final_attrs = self.build_attrs(self.attrs) final_attrs.update(attrs or {}) final_attrs.pop('required', None) self.html_id = final_attrs.pop('id', name) current_repr = '' initial = None lookup = registry.get(self.channel) if value: objs = lookup.get_objects([value]) try: obj = objs[0] except IndexError: raise Exception("%s cannot find object:%s" % (lookup, value)) current_repr = lookup.format_item_display(obj) initial = [current_repr, obj.pk] if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id': self.html_id, 'current_id': value, 'current_repr': current_repr, 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), 'add_link': self.add_link, } context.update( make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ('ajax_select/autocompleteselect_%s.html' % self.channel, 'ajax_select/autocompleteselect.html') out = render_to_string(templates, context) return mark_safe(out)
def render(self, name, value, attrs=None): value = value or '' final_attrs = self.build_attrs(self.attrs) final_attrs.update(attrs or {}) final_attrs.pop('required', None) self.html_id = final_attrs.pop('id', name) current_repr = '' initial = None lookup = registry.get(self.channel) if value: objs = lookup.get_objects([value]) try: obj = objs[0] except IndexError: raise Exception("%s cannot find object:%s" % (lookup, value)) current_repr = lookup.format_item_display(obj) initial = [current_repr, obj.pk] if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id': self.html_id, 'current_id': value, 'current_repr': current_repr, 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), 'add_link': self.add_link, } context.update( make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ( 'ajax_select/autocompleteselect_%s.html' % self.channel, 'ajax_select/autocompleteselect.html') out = render_to_string(templates, context) return mark_safe(out)
def _check_can_add(self, user, related_model): """ Check if the User can create a related_model. If the LookupChannel implements check_can_add() then use this. Else uses Django's default permission system. If it can add, then enable the widget to show the green + link """ lookup = registry.get(self.channel) if hasattr(lookup, "can_add"): can_add = lookup.can_add(user, related_model) else: ctype = ContentType.objects.get_for_model(related_model) can_add = user.has_perm("%s.add_%s" % (ctype.app_label, ctype.model)) if can_add: app_label = related_model._meta.app_label model = related_model._meta.object_name.lower() self.widget.add_link = reverse("admin:%s_%s_add" % (app_label, model)) + "?_popup=1"
def _check_can_add(self, user, related_model): """ Check if the User can create a related_model. If the LookupChannel implements check_can_add() then use this. Else uses Django's default permission system. If it can add, then enable the widget to show the green + link """ lookup = registry.get(self.channel) if hasattr(lookup, 'can_add'): can_add = lookup.can_add(user, related_model) else: ctype = ContentType.objects.get_for_model(related_model) can_add = user.has_perm("%s.add_%s" % (ctype.app_label, ctype.model)) if can_add: app_label = related_model._meta.app_label model = related_model._meta.object_name.lower() self.widget.add_link = reverse('admin:%s_%s_add' % (app_label, model)) + '?_popup=1'
def render(self, name, value, attrs=None): value = value or "" final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop("id", name) current_repr = "" initial = None lookup = registry.get(self.channel) if value: objs = lookup.get_objects([value]) try: obj = objs[0] except IndexError: raise Exception("%s cannot find object:%s" % (lookup, value)) current_repr = lookup.format_item_display(obj) initial = [current_repr, obj.pk] if self.show_help_text: help_text = self.help_text else: help_text = "" context = { "name": name, "html_id": self.html_id, "current_id": value, "current_repr": current_repr, "help_text": help_text, "extra_attrs": mark_safe(flatatt(final_attrs)), "func_slug": self.html_id.replace("-", ""), "add_link": self.add_link, } context.update(make_plugin_options(lookup, self.channel, self.plugin_options, initial)) templates = ("ajax_select/autocompleteselect_%s.html" % self.channel, "ajax_select/autocompleteselect.html") out = render_to_string(templates, context) return mark_safe(out)
def render(self, name, value, attrs=None, renderer=None): if value is None: value = [] final_attrs = self.build_attrs(self.attrs) final_attrs.update(attrs or {}) final_attrs.pop('required', None) html_id = final_attrs.pop('id', name) deck_id = "%s_%s_on_deck" % (html_id, get_uid()) lookup = registry.get(self.channel) if value: values = self.multiple and list(value) or [value] if all([isinstance(v, Model) for v in values]): objects = values else: objects = lookup.get_objects(values) initial = [[lookup.format_item_display(obj), obj.pk] for obj in objects] else: initial = [] context = {"name": name, 'html_id': html_id, 'deck_id': deck_id} # self.widget_plugin_options context.update( make_plugin_options(lookup, self.channel, name, {}, initial, deck_id, self.multiple)) templates = ('ast_ajax_select/input_select_%s.html' % self.channel, 'ast_ajax_select/input_select.html') out = render_to_string(templates, context) return mark_safe(out)