def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, SearchForm)] except NameError: # We are defining SearchForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(SearchFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = caching_media_property(new_class) opts = new_class._meta = SearchFormOptions(getattr(new_class, 'Meta', None)) # TODO remove this? Currently BaseSearchForm.__init__ throws an error if # no model is defined. if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields if opts.sort_field: fields['sort_by'] = make_sortfield(fields, ordering=opts.model._meta.ordering) new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, DocumentForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(DocumentFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class opts = new_class._meta = DocumentFormOptions( getattr(new_class, 'Meta', None)) if opts.document: # If a model is defined, extract form fields from it. fields = fields_for_document(opts.document, opts.fields, opts.exclude, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): try: parents = [b for b in bases if issubclass(b, PrefForm)] except NameError: # We are defining PrefForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(PrefFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = PrefFormOptions(getattr(new_class, 'Meta', None)) if opts.app: # If an app is defined, extract form fields from it. fields = fields_for_app(opts.app, opts.fields, opts.exclude, opts.owner_class) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, DocumentForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(DocumentFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class opts = new_class._meta = DocumentFormOptions( getattr(new_class, 'Meta', None) ) if opts.document: # If a model is defined, extract form fields from it. fields = fields_for_document(opts.document, opts.fields, opts.exclude, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): try: parents = [b for b in bases if issubclass(b, DocumentForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(DocumentFormMetaClass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = DocumentFormOptions( getattr(new_class, 'Meta', None)) if opts.document: # If a document is defined, extract form fields from it. fields = fields_for_document(opts.document, opts.properties, opts.exclude) # Override default docuemnt fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop("formfield_callback", None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(CachedModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if "media" not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = CachedModelFormOptions(getattr(new_class, "Meta", None)) if opts.objects: formfield_callback = make_formfield_callback(formfield_callback, opts.objects) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in list(fields.items()) if not v] missing_fields = set(none_model_fields) - set(declared_fields.keys()) if missing_fields: message = "Unknown field(s) (%s) specified for %s" message = message % (", ".join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): try: parents = [b for b in bases if issubclass(b, DocumentForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(DocumentFormMetaClass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = DocumentFormOptions(getattr(new_class, 'Meta', None)) if opts.document: # If a document is defined, extract form fields from it. fields = fields_for_document(opts.document, opts.properties, opts.exclude) # Override default docuemnt fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): # We store attrs as ModelFormMetaclass.__new__ clears all fields from it attrs_copy = attrs.copy() new_class = super(ParentsIncludedModelFormMetaclass, cls).__new__(cls, name, bases, attrs) # All declared fields + model fields from parent classes fields_without_current_model = forms.get_declared_fields(bases, attrs_copy, True) new_class.base_fields.update(fields_without_current_model) return new_class
def __new__(cls, name, bases, attrs): # We store attrs as ModelFormMetaclass.__new__ clears all fields from it attrs_copy = attrs.copy() new_class = super(ParentsIncludedModelFormMetaclass, cls).__new__(cls, name, bases, attrs) # All declared fields + model fields from parent classes fields_without_current_model = get_declared_fields(bases, attrs_copy, True) new_class.base_fields.update(fields_without_current_model) return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, cls) \ .__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. if opts.fields is None and opts.exclude is None: # This should be some kind of assertion error once deprecation # cycle is complete. warnings.warn( "Creating a ModelForm without either the 'fields' attribute " "or the 'exclude' attribute is deprecated - form %s " "needs updating" % name, DeprecationWarning, stacklevel=2) ''' if opts.fields == ALL_FIELDS: # sentinel for fields_for_model to indicate "get the list of # fields from the model" opts.fields = None ''' fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback, opts.labels, opts.help_texts, opts.error_messages) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in fields.items() if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): # let django do all the work of finding declared/inherited fields tmp_fields = get_declared_fields(bases, attrs, with_base_fields=False) declared_fields = {} declared_subforms = {} declared_subform_labels = {} # sort declared fields into sub-form overrides and regular fields for fname, f in six.iteritems(tmp_fields): if isinstance(f, SubformField): # FIXME: pass can_delete, can_delete from subformfield to formset? declared_subforms[fname] = f.formclass # if a declared subform fields has a label specified, store it if hasattr(f, 'form_label') and f.form_label is not None: declared_subform_labels[fname] = f.form_label # if a subformclass has a label, use that elif hasattr(f.formclass, 'form_label') and \ f.formclass.form_label is not None: declared_subform_labels[fname] = f.formclass.form_label else: declared_fields[fname] = f new_class = super(XmlObjectFormType, cls).__new__(cls, name, bases, attrs) # use django's default model form options for fields, exclude, widgets, etc. opts = new_class._meta = SubformAwareModelFormOptions( getattr(new_class, 'Meta', None)) if opts.model: # if a model is defined, get xml fields and any subform classes fields, subforms, formsets, subform_labels = formfields_for_xmlobject( opts.model, options=opts, declared_subforms=declared_subforms) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) # store all of the dynamically generated xmlobjectforms for nodefields new_class.subforms = subforms # and for listfields new_class.formsets = formsets # labels for subforms that couldn't be set by formfields_for_xmlobject # declared subform labels should supercede verbose xmlobject field names subform_labels.update(declared_subform_labels) new_class.subform_labels = subform_labels else: fields = declared_fields new_class.subforms = {} new_class.formsets = {} new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, cls) \ .__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. if opts.fields is None and opts.exclude is None: # This should be some kind of assertion error once deprecation # cycle is complete. warnings.warn("Creating a ModelForm without either the 'fields' attribute " "or the 'exclude' attribute is deprecated - form %s " "needs updating" % name, DeprecationWarning, stacklevel=2) ''' if opts.fields == ALL_FIELDS: # sentinel for fields_for_model to indicate "get the list of # fields from the model" opts.fields = None ''' fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback, opts.labels, opts.help_texts, opts.error_messages) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in fields.items() if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): # let django do all the work of finding declared/inherited fields tmp_fields = get_declared_fields(bases, attrs, with_base_fields=False) declared_fields = {} declared_subforms = {} declared_subform_labels = {} # sort declared fields into sub-form overrides and regular fields for fname, f in six.iteritems(tmp_fields): if isinstance(f, SubformField): # FIXME: pass can_delete, can_delete from subformfield to formset? declared_subforms[fname] = f.formclass # if a declared subform fields has a label specified, store it if hasattr(f, 'form_label') and f.form_label is not None: declared_subform_labels[fname] = f.form_label # if a subformclass has a label, use that elif hasattr(f.formclass, 'form_label') and \ f.formclass.form_label is not None: declared_subform_labels[fname] = f.formclass.form_label else: declared_fields[fname] = f new_class = super(XmlObjectFormType, cls).__new__(cls, name, bases, attrs) # use django's default model form options for fields, exclude, widgets, etc. opts = new_class._meta = SubformAwareModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # if a model is defined, get xml fields and any subform classes fields, subforms, formsets, subform_labels = formfields_for_xmlobject(opts.model, options=opts, declared_subforms=declared_subforms) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) # store all of the dynamically generated xmlobjectforms for nodefields new_class.subforms = subforms # and for listfields new_class.formsets = formsets # labels for subforms that couldn't be set by formfields_for_xmlobject # declared subform labels should supercede verbose xmlobject field names subform_labels.update(declared_subform_labels) new_class.subform_labels = subform_labels else: fields = declared_fields new_class.subforms = {} new_class.formsets = {} new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None)) # We check if a string was passed to `fields` or `exclude`, # which is likely to be a mistake where the user typed ('foo') instead # of ('foo',) for opt in ['fields', 'exclude']: value = getattr(opts, opt) if isinstance(value, six.string_types): msg = ("%(model)s.Meta.%(opt)s cannot be a string. " "Did you mean to type: ('%(value)s',)?" % { 'model': new_class.__name__, 'opt': opt, 'value': value, }) raise TypeError(msg) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in six.iteritems(fields) if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) # We check if a string was passed to `fields` or `exclude`, # which is likely to be a mistake where the user typed ('foo') instead # of ('foo',) for opt in ['fields', 'exclude']: value = getattr(opts, opt) if isinstance(value, six.string_types): msg = ("%(model)s.Meta.%(opt)s cannot be a string. " "Did you mean to type: ('%(value)s',)?" % { 'model': new_class.__name__, 'opt': opt, 'value': value, }) raise TypeError(msg) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in six.iteritems(fields) if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [ b for b in bases if issubclass(b, DocumentForm) or issubclass(b, EmbeddedDocumentForm) ] except NameError: # We are defining DocumentForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(DocumentFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None) ) if opts.document: formfield_generator = getattr(opts, 'formfield_generator', _fieldgenerator) # If a model is defined, extract form fields from it. fields = fields_for_document(opts.document, opts.fields, opts.exclude, opts.widgets, formfield_callback, formfield_generator) # make sure opts.fields doesn't specify an invalid field none_document_fields = [k for k, v in fields.items() if not v] missing_fields = (set(none_document_fields) - set(declared_fields.keys())) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): super_new = super(TranslateableModelFormMetaclass, cls).__new__ formfield_callback = attrs.pop("formfield_callback", None) declared_fields = get_declared_fields(bases, attrs, False) new_class = super_new(cls, name, bases, attrs) if "media" not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, "Meta", attrs.get("Meta", None))) if opts.model: if not issubclass(opts.model, TranslateableModel): raise Exception("Only TranslateableModel subclasses may use TranslateableModelForm") mopts = opts.model._meta shared_fields = mopts.get_all_field_names() sfieldnames = [field for field in opts.fields or [] if field in shared_fields] tfieldnames = [field for field in opts.fields or [] if field not in shared_fields] sexclude = [field for field in opts.exclude or [] if field in shared_fields] texclude = [field for field in opts.exclude or [] if field not in shared_fields] if not sfieldnames: sfieldnames = None if not tfieldnames: tfieldnames = None # If a model is defined, extract form fields from it. sfields = fields_for_model(opts.model, sfieldnames, sexclude, opts.widgets, formfield_callback) tfields = fields_for_model( mopts.translations_model, tfieldnames, texclude, opts.widgets, formfield_callback ) fields = sfields fields.update(tfields) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in fields.iteritems() if not v] missing_fields = set(none_model_fields) - set(declared_fields.keys()) if missing_fields: message = "Unknown field(s) (%s) specified for %s" message = message % (", ".join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(mcs, name, bases, attrs): declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs) if bases == (BaseEGModelForm, ): return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. if opts.fields is None and opts.exclude is None: raise ImproperlyConfigured( "Creating a ModelForm without either the 'fields' attribute " "or the 'exclude' attribute is prohibited; form %s " "needs updating." % name) if opts.fields == ALL_FIELDS: # Sentinel for fields_for_model to indicate "get the list of # fields from the model" opts.fields = None fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, opts.localized_fields, opts.labels, opts.help_texts, opts.error_messages) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in six.iteritems(fields) if not v] missing_fields = (set(none_model_fields) - set(declared_fields.keys())) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.base_fields = fields new_class.declared_fields = declared_fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [ b for b in bases if issubclass(b, DocumentForm) or issubclass(b, EmbeddedDocumentForm) ] except NameError: # We are defining DocumentForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(DocumentFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None)) if opts.document: formfield_generator = getattr(opts, 'formfield_generator', _fieldgenerator) # If a model is defined, extract form fields from it. fields = fields_for_document(opts.document, opts.fields, opts.exclude, opts.widgets, formfield_callback, formfield_generator) # make sure opts.fields doesn't specify an invalid field none_document_fields = [k for k, v in fields.items() if not v] missing_fields = (set(none_document_fields) - set(declared_fields.keys())) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(mcs, name, bases, attrs): declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs) if bases == (BaseEGModelForm,): return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. if opts.fields is None and opts.exclude is None: raise ImproperlyConfigured( "Creating a ModelForm without either the 'fields' attribute " "or the 'exclude' attribute is prohibited; form %s " "needs updating." % name ) if opts.fields == ALL_FIELDS: # Sentinel for fields_for_model to indicate "get the list of # fields from the model" opts.fields = None fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, opts.localized_fields, opts.labels, opts.help_texts, opts.error_messages) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in six.iteritems(fields) if not v] missing_fields = (set(none_model_fields) - set(declared_fields.keys())) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.base_fields = fields new_class.declared_fields = declared_fields return new_class
def __new__(cls, name, bases, attrs): fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] fields.sort( lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # If this class is subclassing another Form, add that Form's fields. # Note that we loop over the bases in *reverse*. This is necessary in # order to preserve the correct order of fields. for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields attrs['base_fields'] = SortedDictFromList(fields) # rest of this method is taken from actual django # source for ModelFormMetaclass class formfield_callback = attrs.pop('formfield_callback', lambda f: f.formfield()) try: parents = [b for b in bases if issubclass(b, BaseModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(SmartModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: # HACK: Had to replace baseclass # TODO: Report that this can not overrode, there should be something else. parents = [b for b in bases if issubclass(b, MultilingualModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(MultilingualModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) found_fields = [k for k, v in fields.items() if v is not None] #HACK: This is update of original method. I should be able to change it to overrides translation_model = getattr(opts.model._meta, 'translation_model', None) if translation_model: if opts.exclude is None: exclude = ['id', 'language_code', 'master'] + found_fields else: exclude = list(opts.exclude) + ['id', 'language_code', 'master'] + found_fields translated_fields = fields_for_model( translation_model, opts.fields, exclude, opts.widgets, formfield_callback ) fields.update(translated_fields) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): try: parents = [b for b in bases if issubclass(b, DocumentForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(DocumentFormMetaClass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if "media" not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = DocumentFormOptions(getattr(new_class, "Meta", None)) if opts.schema: fields = fields_for_document( opts.schema, opts.properties, opts.exclude, formfield_callback=opts.formfield_callback ) # Override default docuemnt fields with any custom declared ones # (plus, include all the other declared fields). new_class.serialized_fields = fields.keys() fields.update(declared_fields) elif opts.document: # TODO this should no longer be necessary # If a document is defined, extract form fields from it. fields = fields_for_document( opts.document, opts.properties, opts.exclude, formfield_callback=opts.formfield_callback, dotpath=opts.dotpath, ) # Override default docuemnt fields with any custom declared ones # (plus, include all the other declared fields). new_class.serialized_fields = fields.keys() fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # If this class is subclassing another Form, add that Form's fields. # Note that we loop over the bases in *reverse*. This is necessary in # order to preserve the correct order of fields. for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields attrs['base_fields'] = SortedDictFromList(fields) # rest of this method is taken from actual django # source for ModelFormMetaclass class formfield_callback = attrs.pop('formfield_callback', lambda f: f.formfield()) try: parents = [b for b in bases if issubclass(b, BaseModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(SmartModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): try: parents = [b for b in bases if issubclass(b, DocumentForm) or issubclass(b, EmbeddedDocumentForm)] except NameError: # We are defining DocumentForm itself. parents = None new_class = super(DocumentFormMetaClass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = DocumentFormOptions(getattr(new_class, 'Meta', None)) declared_fields = get_declared_fields(bases, attrs, False) if opts.document: # If a document is defined, extract form fields from it. fields = fields_for_document(opts.document, opts.fields, opts.exclude, opts.widgets, opts.formfield_generator) # make sure fields doesn't specify an invalid field none_document_fields = [k for k, v in fields.iteritems() if not v] missing_fields = set(none_document_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.document.__name__) raise FieldError(message) # Override default document fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) # filter fields not supported by ``formfield_generator`` and not # replaced by ``declared_fields`` for n, f in fields.items(): if not f: del fields[n] else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(mcs, name, bases, attrs): formfield_callback = attrs.pop( 'formfield_callback', lambda f, **kwargs: f.formfield(**kwargs)) try: parents = [b for b in bases if issubclass(b, MapModelForm)] except NameError: # We are defining MapModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(MapModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = forms.widgets.media_property(new_class) opts = new_class._meta = MapModelFormOptions( getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. fields = forms.models.fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields # Transform base fields by extracting types mentioned in 'maps' initial_data_keymap = apply_maps_to_modelform_fields( fields, opts.maps, default_field_class=opts.default_field_class, default_template=opts.template) new_class.initial_data_keymap = initial_data_keymap new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(mcs, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', lambda f, **kwargs: f.formfield(**kwargs)) try: parents = [b for b in bases if issubclass(b, MapModelForm)] except NameError: # We are defining MapModelForm itself. parents = None #declared_fields = forms.models.get_declared_fields(bases, attrs, False) #Above line fails in django1.7 declared_fields = get_declared_fields(bases, attrs, False) new_class = super(MapModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = forms.widgets.media_property(new_class) opts = new_class._meta = MapModelFormOptions( getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. fields = forms.models.fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields # Transform base fields by extracting types mentioned in 'maps' initial_data_keymap = apply_maps_to_modelform_fields( fields, opts.maps, default_field_class=opts.default_field_class, default_template=opts.template) new_class.initial_data_keymap = initial_data_keymap new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # defining ModelForm itself parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # if model is defined then extract the fields from the associated # FOM Object fields = fields_for_model(opts.model, opts.fields, opts.exclude, formfield_callback) # override default FOM Object's fields with any custom declared ones fields.update(declared_fields) new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, HTML5ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(HTML5ModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = original.ModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in fields.iteritems() if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, BaseModelForm)] # for Django<=1.6, fix bug in forms.models: ^^^^^^^^^^^^^ except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(PatchedModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None)) # We check if a string was passed to `fields` or `exclude`, # which is likely to be a mistake where the user typed ('foo') instead # of ('foo',) for opt in ['fields', 'exclude', 'localized_fields']: value = getattr(opts, opt) if isinstance(value, six.string_types) and value != ALL_FIELDS: msg = ("%(model)s.Meta.%(opt)s cannot be a string. " "Did you mean to type: ('%(value)s',)?" % { 'model': new_class.__name__, 'opt': opt, 'value': value, }) raise TypeError(msg) if opts.model: # If a model is defined, extract form fields from it. if opts.fields is None and opts.exclude is None: # This should be some kind of assertion error once deprecation # cycle is complete. warnings.warn( "Creating a ModelForm without either the 'fields' attribute " "or the 'exclude' attribute is deprecated - form %s " "needs updating" % name, PendingDeprecationWarning, stacklevel=2) if opts.fields == ALL_FIELDS: # sentinel for fields_for_model to indicate "get the list of # fields from the model" opts.fields = None fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback, opts.localized_fields, opts.labels, opts.help_texts, opts.error_messages) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in six.iteritems(fields) if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None)) # We check if a string was passed to `fields` or `exclude`, # which is likely to be a mistake where the user typed ('foo') instead # of ('foo',) for opt in ['fields', 'exclude', 'localized_fields']: value = getattr(opts, opt) if isinstance(value, six.string_types) and value != ALL_FIELDS: msg = ("%(model)s.Meta.%(opt)s cannot be a string. " "Did you mean to type: ('%(value)s',)?" % { 'model': new_class.__name__, 'opt': opt, 'value': value, }) raise TypeError(msg) if opts.model: # If a model is defined, extract form fields from it. if opts.fields is None and opts.exclude is None: # This should be some kind of assertion error once deprecation # cycle is complete. warnings.warn("Creating a ModelForm without either the 'fields' attribute " "or the 'exclude' attribute is deprecated - form %s " "needs updating" % name, PendingDeprecationWarning, stacklevel=2) if opts.fields == ALL_FIELDS: # sentinel for fields_for_model to indicate "get the list of # fields from the model" opts.fields = None fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback, opts.localized_fields) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in six.iteritems(fields) if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): """ Django 1.3 fix, that removes all Meta.fields and Meta.exclude fieldnames that are in the translatable model. This ensures that the superclass' init method doesnt throw a validation error """ fields = [] exclude = [] fieldsets = [] if "Meta" in attrs: meta = attrs["Meta"] if getattr(meta, "fieldsets", False): fieldsets = meta.fieldsets meta.fieldsets = [] if getattr(meta, "fields", False): fields = meta.fields meta.fields = [] if getattr(meta, "exclude", False): exclude = meta.exclude meta.exclude = [] # End 1.3 fix super_new = super(TranslatableModelFormMetaclass, cls).__new__ formfield_callback = attrs.pop('formfield_callback', None) declared_fields = get_declared_fields(bases, attrs, False) new_class = super_new(cls, name, bases, attrs) # Start 1.3 fix if fields: new_class.Meta.fields = fields if exclude: new_class.Meta.exclude = exclude if fieldsets: new_class.Meta.fieldsets = fieldsets # End 1.3 fix if not getattr(new_class, "Meta", None): class Meta: exclude = ['language_code'] new_class.Meta = Meta elif not getattr(new_class.Meta, 'exclude', None): new_class.Meta.exclude = ['language_code'] elif getattr(new_class.Meta, 'exclude', False): if 'language_code' not in new_class.Meta.exclude: new_class.Meta.exclude.append("language_code") if 'Media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', attrs.get('Meta', None))) if opts.model: # bail out if a wrong model uses this form class if not issubclass(opts.model, TranslatableModel): raise TypeError( "Only TranslatableModel subclasses may use TranslatableModelForm" ) mopts = opts.model._meta shared_fields = mopts.get_all_field_names() # split exclude and include fieldnames into shared and translated sfieldnames = [field for field in opts.fields or [] if field in shared_fields] tfieldnames = [field for field in opts.fields or [] if field not in shared_fields] sexclude = [field for field in opts.exclude or [] if field in shared_fields] texclude = [field for field in opts.exclude or [] if field not in shared_fields] # required by fields_for_model if not sfieldnames : sfieldnames = None if not fields else [] if not tfieldnames: tfieldnames = None if not fields else [] # If a model is defined, extract form fields from it. sfields = fields_for_model(opts.model, sfieldnames, sexclude, opts.widgets, formfield_callback) tfields = fields_for_model(mopts.translations_model, tfieldnames, texclude, opts.widgets, formfield_callback) fields = sfields fields.update(tfields) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in fields.iteritems() if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) if new_class._meta.exclude: new_class._meta.exclude = list(new_class._meta.exclude) else: new_class._meta.exclude = [] for field in (mopts.translations_accessor, 'master'): if not field in new_class._meta.exclude: new_class._meta.exclude.append(field) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields # always exclude the FKs return new_class
def __new__(cls, name, bases, attrs): """ Django 1.3 fix, that removes all Meta.fields and Meta.exclude fieldnames that are in the translatable model. This ensures that the superclass' init method doesnt throw a validation error """ fields = [] exclude = [] if "Meta" in attrs: meta = attrs["Meta"] if getattr(meta, "fields", False): fields = meta.fields meta.fields = [] if getattr(meta, "exclude", False): exclude = meta.exclude meta.exclude = [] # End 1.3 fix super_new = super(TranslatableModelFormMetaclass, cls).__new__ formfield_callback = attrs.pop('formfield_callback', None) declared_fields = get_declared_fields(bases, attrs, False) new_class = super_new(cls, name, bases, attrs) # Start 1.3 fix if fields: new_class.Meta.fields = fields if exclude: new_class.Meta.exclude = exclude # End 1.3 fix if 'Media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', attrs.get('Meta', None))) if opts.model: # bail out if a wrong model uses this form class if not issubclass(opts.model, TranslatableModel): raise TypeError( "Only TranslatableModel subclasses may use TranslatableModelForm" ) mopts = opts.model._meta shared_fields = mopts.get_all_field_names() # split exclude and include fieldnames into shared and translated sfieldnames = [field for field in opts.fields or [] if field in shared_fields] tfieldnames = [field for field in opts.fields or [] if field not in shared_fields] sexclude = [field for field in opts.exclude or [] if field in shared_fields] texclude = [field for field in opts.exclude or [] if field not in shared_fields] # required by fields_for_model if not sfieldnames: sfieldnames = None if not tfieldnames: tfieldnames = None # If a model is defined, extract form fields from it. sfields = fields_for_model(opts.model, sfieldnames, sexclude, opts.widgets, formfield_callback) tfields = fields_for_model(mopts.translations_model, tfieldnames, texclude, opts.widgets, formfield_callback) fields = sfields fields.update(tfields) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in fields.iteritems() if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) if new_class._meta.exclude: new_class._meta.exclude = list(new_class._meta.exclude) else: new_class._meta.exclude = [] for field in (mopts.translations_accessor, 'master'): if not field in new_class._meta.exclude: new_class._meta.exclude.append(field) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields # always exclude the FKs return new_class