class UpdateTagUIOrder(TemplateView): """ display the widget for the master_ui_edit_tag_order field as updated based on the value of the master_ui_edit_tags field on MasterUIMappingsOrganizationView edit form. """ def __init__(self, **kwargs): self.widget = SetupTagUISortedCheckboxSelectMultiple() def choice_iterator(self): for uimap in self.queryset: yield (uimap.pk, uimap.tag.__unicode__()) def post(self, request, *args, **kwargs): """ add or remove checkbox items to the order field based on contents of the master_ui_edit_tags field. UIMappings may not exist corresponding to the chosen tag. UIMappings will never exist for a newly created MasterUI. """ tag_vals = [Tag.objects.filter(pk=t)[0] for t in request.POST.getlist( 'tags[]')] mui = request.POST.getlist('mui')[0] if mui: self.queryset = UIMapping.objects.select_related('tag' ).filter(master_ui__pk=mui, tag__in=tag_vals).order_by('index') filtered = self.queryset.filter(default=True) else: self.queryset = UIMapping.objects.none() filtered = self.queryset # if tag id in request is not present in queryset, then we need to # create a line in the widget for a new empty uimapping. Store the # tag id in a hidden field on the widget tags_seen = [map.tag for map in self.queryset] tags_unseen = [t for t in tag_vals if t not in tags_seen] html = self.widget.render('master_ui_edit-ui_mappings_tag_order', value=[uimap.pk for uimap in filtered], attrs={ 'id': 'id_master_ui_edit-ui_mappings_tag_order'}, choices=self.choice_iterator(), new_maps=tags_unseen,) return HttpResponse(mark_safe(html))
def __init__(self, **kwargs): self.widget = SetupTagUISortedCheckboxSelectMultiple()