def extra_wizard_fields(self): prefix, field_name = parsed_field_name(self.field_name) prefix = SEPARATED_FIELD.join(prefix) fields = get_fields_from_model(self.model, adaptors=(TextFieldReportField, )) current_field_name = self.field_name.split(SEPARATED_FIELD)[-1] choices = [(f['name'], f['verbose_name']) for f in fields[0] if f['name'] != current_field_name] if not choices: return {} initial = None if self.instance: field_options = self.instance.options.get(self.field_name, None) if field_options: initial = field_options.get('other_fields', None) return { 'other_fields': forms.MultipleChoiceField( label=_('Other fields to filter'), required=False, choices=choices, widget=forms.CheckboxSelectMultiple, initial=initial, help_text= _('Choose other fields, when you filter with this field, you will search in these also' )) }
def get_adaptor(self): app_label = self.cleaned_data.get("app_label") module_name = self.cleaned_data.get("module_name") field_name = self.cleaned_data.get("field_name") ct = ContentType.objects.get(app_label=app_label, model=module_name) model = ct.model_class() prefix, field_name_parsed = parsed_field_name(field_name) field_name, field = get_field_by_name(model, field_name_parsed) return get_adaptor(field)(model, field, field_name, treatment_transmeta=False)
def get_adaptor(self): app_label = self.cleaned_data.get('app_label') module_name = self.cleaned_data.get('module_name') field_name = self.cleaned_data.get('field_name') ct = ContentType.objects.get(app_label=app_label, model=module_name) model = ct.model_class() prefix, field_name_parsed = parsed_field_name(field_name) field_name, field = get_field_by_name(model, field_name_parsed) return get_adaptor(field)(model, field, field_name, treatment_transmeta=False)
def get_field_form(self, opts=None, default=True, fields_form_filter=None, fields_form_display=None): prefix, field_name = parsed_field_name(self.field_name) form = modelform_factory(self.model, form=BaseReportForm, fields=[field_name]) field = self.get_basic_field_form(form, field_name) autoreports_initial = getattr(settings, 'AUTOREPORTS_INITIAL', True) autoreports_subfix = getattr(settings, 'AUTOREPORTS_SUBFIX', True) if not autoreports_initial: field.initial = None if opts: help_text = self.get_help_text_to_opts(opts) display = opts.get('display', None) filters = opts.get('filters', []) label = self.get_label_to_opts(opts) if label: field.label = label if help_text: field.help_text = help_text if display: fields_form_display[self.field_name] = copy(field) for fil in filters: field_copy = copy(field) fil, verbose_fil = self.get_change_filter(fil, opts) field_name_subfix = "%s__%s" % (self.field_name_parsed, fil) if autoreports_subfix: field_label = u"%s (%s)" % (field_copy.label, verbose_fil) field_copy.label = field_label fields_form_filter[field_name_subfix] = self.change_widget( field_copy, opts) else: if default: fil = self.get_filter_default() if fil is None: return (fields_form_filter, fields_form_display) field_name_subfix = "%s__%s" % (self.field_name_parsed, fil) if autoreports_subfix: field_label = u"%s (%s)" % (field.label, dict(self.get_filters())[fil]) field.label = field_label fields_form_filter[field_name_subfix] = self.change_widget( field) else: fields_form_display[self.field_name] = field return (fields_form_filter, fields_form_display)
def reports_ajax_fields_options(request): module_name = request.GET.get('module_name') app_label = request.GET.get('app_label') is_admin = simplejson.loads(request.GET.get('is_admin', True),) ct = ContentType.objects.get(model=module_name, app_label=app_label) model = ct.model_class() field_name = request.GET.get('field') prefix, field_name_parsed = parsed_field_name(field_name) field_name_x, field = get_field_by_name(model, field_name_parsed) adaptor = get_adaptor(field)(model, field, field_name, treatment_transmeta=False) wizard = adaptor.get_form(is_admin) wizard_render = adaptor.render(wizard, model, is_admin) return HttpResponse(wizard_render, mimetype='text/html')
def reports_ajax_fields_options(request): module_name = request.GET.get('module_name') app_label = request.GET.get('app_label') is_admin = simplejson.loads(request.GET.get('is_admin', True), ) ct = ContentType.objects.get(model=module_name, app_label=app_label) model = ct.model_class() field_name = request.GET.get('field') prefix, field_name_parsed = parsed_field_name(field_name) field_name_x, field = get_field_by_name(model, field_name_parsed) adaptor = get_adaptor(field)(model, field, field_name, treatment_transmeta=False) wizard = adaptor.get_form(is_admin) wizard_render = adaptor.render(wizard, model, is_admin) return HttpResponse(wizard_render, mimetype='text/html')
def extra_wizard_fields(self): prefix, field_name = parsed_field_name(self.field_name) prefix = SEPARATED_FIELD.join(prefix) fields = get_fields_from_model(self.model, adaptors=(TextFieldReportField,)) current_field_name = self.field_name.split(SEPARATED_FIELD)[-1] choices = [(f['name'], f['verbose_name']) for f in fields[0] if f['name'] != current_field_name] if not choices: return {} initial = None if self.instance: field_options = self.instance.options.get(self.field_name, None) if field_options: initial = field_options.get('other_fields', None) return {'other_fields': forms.MultipleChoiceField(label=_('Other fields to filter'), required=False, choices=choices, widget=forms.CheckboxSelectMultiple, initial=initial, help_text=_('Choose other fields, when you filter with this field, you will search in these also'))}
def get_field_form(self, opts=None, default=True, fields_form_filter=None, fields_form_display=None): prefix, field_name = parsed_field_name(self.field_name) form = modelform_factory(self.model, form=BaseReportForm, fields=[field_name]) field = self.get_basic_field_form(form, field_name) autoreports_initial = getattr(settings, 'AUTOREPORTS_INITIAL', True) autoreports_subfix = getattr(settings, 'AUTOREPORTS_SUBFIX', True) if not autoreports_initial: field.initial = None if opts: help_text = self.get_help_text_to_opts(opts) display = opts.get('display', None) filters = opts.get('filters', []) label = self.get_label_to_opts(opts) if label: field.label = label if help_text: field.help_text = help_text if display: fields_form_display[self.field_name] = copy(field) for fil in filters: field_copy = copy(field) fil, verbose_fil = self.get_change_filter(fil, opts) field_name_subfix = "%s__%s" % (self.field_name_parsed, fil) if autoreports_subfix: field_label = u"%s (%s)" % (field_copy.label, verbose_fil) field_copy.label = field_label fields_form_filter[field_name_subfix] = self.change_widget(field_copy, opts) else: if default: fil = self.get_filter_default() if fil is None: return (fields_form_filter, fields_form_display) field_name_subfix = "%s__%s" % (self.field_name_parsed, fil) if autoreports_subfix: field_label = u"%s (%s)" % (field.label, dict(self.get_filters())[fil]) field.label = field_label fields_form_filter[field_name_subfix] = self.change_widget(field) else: fields_form_display[self.field_name] = field return (fields_form_filter, fields_form_display)
def get_verbose_name(self): label = getattr(self.field, 'label', '') if label: return label prefix, field_name = parsed_field_name(self.field_name) return field_name