Exemplo n.º 1
0
Arquivo: models.py Projeto: pvl/django
    def __init__(self,
                 queryset,
                 empty_label=u"---------",
                 cache_choices=False,
                 required=True,
                 widget=None,
                 label=None,
                 initial=None,
                 help_text=None,
                 to_field_name=None,
                 *args,
                 **kwargs):
        if required and (initial is not None):
            self.empty_label = None
        else:
            self.empty_label = empty_label
        self.cache_choices = cache_choices

        # Call Field instead of ChoiceField __init__() because we don't need
        # ChoiceField.__init__().
        Field.__init__(self, required, widget, label, initial, help_text,
                       *args, **kwargs)
        self.queryset = queryset
        self.choice_cache = None
        self.to_field_name = to_field_name
Exemplo n.º 2
0
 def __init__(self, field_items, skip_check=False, *args,
              **kwargs):
     self.field_dict = OrderedDict(field_items)
     self.skip_check = skip_check
     # Make sure no subfield is named 'SKIP_CHECK_NAME'. If
     # skip_check is True this field will clash with the addtional
     # subfield added by the DictCharField constructor.  We perform
     # this check even if skip_check=False because having a field named
     # 'skip_check' that isn't used to actually skip the checks would be
     # very confusing.
     if SKIP_CHECK_NAME in self.field_dict.keys():
         raise RuntimeError(
             "'%s' is a reserved name "
             "(it can't be used to name a subfield)." % SKIP_CHECK_NAME)
     # if skip_check: add a BooleanField to the list of fields, this will
     # be used to skip the validation of the fields and accept arbitrary
     # data.
     if skip_check:
         self.field_dict[SKIP_CHECK_NAME] = forms.BooleanField(
             required=False)
     self.names = [name for name in self.field_dict.keys()]
     # Create the DictCharWidget with init values from the list of fields.
     self.fields = self.field_dict.values()
     self.widget = DictCharWidget(
         [field.widget for field in self.fields],
         self.names,
         [field.label for field in self.fields],
         skip_check=skip_check,
         )
     # Upcall to Field and not MultiValueField to avoid setting all the
     # subfields' 'required' attributes to False.
     Field.__init__(self, *args, **kwargs)
Exemplo n.º 3
0
    def __init__(
        self,
        queryset,
        empty_label="---------",
        cache_choices=False,
        required=True,
        widget=None,
        label=None,
        initial=None,
        help_text=None,
        to_field_name=None,
        *args,
        **kwargs
    ):
        if required and (initial is not None):
            self.empty_label = None
        else:
            self.empty_label = empty_label
        self.cache_choices = cache_choices

        # Call Field instead of ChoiceField __init__() because we don't need
        # ChoiceField.__init__().
        Field.__init__(self, required, widget, label, initial, help_text, *args, **kwargs)
        self.queryset = queryset
        self.choice_cache = None
        self.to_field_name = to_field_name
Exemplo n.º 4
0
 def __init__(self, field_items, skip_check=False, *args, **kwargs):
     self.field_dict = OrderedDict(field_items)
     self.skip_check = skip_check
     # Make sure no subfield is named 'SKIP_CHECK_NAME'. If
     # skip_check is True this field will clash with the addtional
     # subfield added by the DictCharField constructor.  We perform
     # this check even if skip_check=False because having a field named
     # 'skip_check' that isn't used to actually skip the checks would be
     # very confusing.
     if SKIP_CHECK_NAME in self.field_dict:
         raise RuntimeError("'%s' is a reserved name "
                            "(it can't be used to name a subfield)." %
                            SKIP_CHECK_NAME)
     # if skip_check: add a BooleanField to the list of fields, this will
     # be used to skip the validation of the fields and accept arbitrary
     # data.
     if skip_check:
         self.field_dict[SKIP_CHECK_NAME] = forms.BooleanField(
             required=False)
     self.names = [name for name in self.field_dict]
     # Create the DictCharWidget with init values from the list of fields.
     self.fields = list(self.field_dict.values())
     self.widget = DictCharWidget(
         [field.widget for field in self.fields],
         self.names,
         [field.initial for field in self.fields],
         [field.label for field in self.fields],
         skip_check=skip_check,
     )
     # Upcall to Field and not MultiValueField to avoid setting all the
     # subfields' 'required' attributes to False.
     Field.__init__(self, *args, **kwargs)
def ModelChoiceField__init__(self,
                             queryset,
                             empty_label=u"---------",
                             cache_choices=False,
                             required=True,
                             widget=None,
                             label=None,
                             initial=None,
                             help_text=None,
                             to_field_name=None,
                             *args,
                             **kwargs):
    if required and (initial is not None):
        self.empty_label = None
    else:
        self.empty_label = empty_label
    self.cache_choices = cache_choices

    # Monkey starts here
    if self.__class__ in (ModelChoiceField, ModelMultipleChoiceField):
        meta = queryset.model._meta
        key = '%s.%s' % (meta.app_label, meta.module_name)
        # Handle both legacy settings SIMPLE_AUTOCOMPLETE_MODELS and new
        # setting SIMPLE_AUTOCOMPLETE.
        models = getattr(settings, 'SIMPLE_AUTOCOMPLETE_MODELS',
                         getattr(settings, 'SIMPLE_AUTOCOMPLETE', {}).keys())
        if key in models:
            pickled = pickle.dumps(
                (queryset.model._meta.app_label,
                 queryset.model._meta.module_name, queryset.query))
            token = hashlib.md5(pickled).hexdigest()
            _simple_autocomplete_queryset_cache[token] = pickled
            if self.__class__ == ModelChoiceField:
                widget = AutoCompleteWidget(token=token, model=queryset.model)
            else:
                widget = AutoCompleteMultipleWidget(token=token,
                                                    model=queryset.model)
    # Monkey ends here

    # Call Field instead of ChoiceField __init__() because we don't need
    # ChoiceField.__init__().
    Field.__init__(self, required, widget, label, initial, help_text, *args,
                   **kwargs)

    self.queryset = queryset
    self.choice_cache = None
    self.to_field_name = to_field_name
Exemplo n.º 6
0
def ModelChoiceField__init__(self, queryset, empty_label=u"---------",
        cache_choices=False, required=True, widget=None, label=None,
        initial=None, help_text=None, to_field_name=None, *args, **kwargs):
    if required and (initial is not None):
        self.empty_label = None
    else:
        self.empty_label = empty_label
    self.cache_choices = cache_choices

    # Monkey starts here
    if self.__class__ in (ModelChoiceField, ModelMultipleChoiceField):
        meta = queryset.model._meta
        key = '%s.%s' % (meta.app_label, meta.model_name)
        # Handle both legacy settings SIMPLE_AUTOCOMPLETE_MODELS and new
        # setting SIMPLE_AUTOCOMPLETE.
        models = getattr(
            settings, 'SIMPLE_AUTOCOMPLETE_MODELS',
            getattr(settings, 'SIMPLE_AUTOCOMPLETE', {}).keys()
        )
        if key in models:
            pickled = pickle.dumps((
                queryset.model._meta.app_label,
                queryset.model._meta.model_name,
                queryset.query
            ))
            token = hashlib.md5(pickled).hexdigest()
            _simple_autocomplete_queryset_cache[token] = pickled
            if self.__class__ == ModelChoiceField:
                widget = AutoCompleteWidget(token=token, model=queryset.model)
            else:
                widget = AutoCompleteMultipleWidget(
                    token=token, model=queryset.model
                )
    # Monkey ends here

    # Call Field instead of ChoiceField __init__() because we don't need
    # ChoiceField.__init__().
    if 'limit_choices_to' in kwargs:
        kwargs.pop('limit_choices_to')
    Field.__init__(self, required, widget, label, initial, help_text,
                   *args, **kwargs)

    self.queryset = queryset
    self.choice_cache = None
    self.to_field_name = to_field_name
Exemplo n.º 7
0
    def __init__(self, model, form=NestedInlineForm, fields=None, exclude=None,
                 formfield_callback=None, widgets=None, localized_fields=None,
                 labels=None, help_texts=None, error_messages=None,
                 instance=None,
                 required=True, widget=None, *args, **kwargs):
        """
        __init__(self, model, form=NestedInlineForm, *args, **kwargs)

        """
        self.model = model
        self.form_name = form
        self.fields = fields
        self.exclude = exclude
        self.formfield_callback = formfield_callback
        self.widgets = widgets
        self.localized_fields = localized_fields
        self.labels = labels
        self.help_texts = help_texts
        self.error_messages = error_messages

        self.instance = instance

        # Skip InlineForeignKeyField, we only need some of it's functionality.
        Field.__init__(self, required=required, widget=widget, *args, **kwargs)
Exemplo n.º 8
0
 def __init__(self, lookup_name, *args, **kwargs):
     kwargs.pop('widget', 1) #dummy True value, to remove widget argument
     Field.__init__(self, widget=AutocompleteSelect(lookup_name, kwargs.pop('widget_attrs', {})), *args, **kwargs) #setting current widget as  an autocomplete text input
     AutocompleteField.__init__(self, lookup_name)
Exemplo n.º 9
0
 def __init__(self, lookup_name, *args, **kwargs):
     kwargs.pop('widget', 1) #dummy True value, to remove widget argument
     Field.__init__(self, widget=AutocompleteSelectMultiple(lookup_name, kwargs.pop('widget_attrs', {})), *args, **kwargs)
     AutocompleteField.__init__(self, lookup_name)
Exemplo n.º 10
0
 def __init__(self, model=None, field=None, *args, **kwargs):
     self.model = model
     self.field = field
     self.widget = AdminImageUploaderWidget(field=field,model=model,url='/admin/image/upload/')
     Field.__init__(self, *args, **kwargs)
Exemplo n.º 11
0
 def __init__(self, url, model=None, using=None, field=None, *args, **kwargs):
     self.model = model
     self.using = using
     self.field = field
     self.widget = AutocompleteWidget(url=url, field=field)
     Field.__init__(self, *args, **kwargs)