Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[py3] Used six.with_metaclass wherever necessary.

  • Loading branch information...
commit d11d45aad969be313b9e046d0d42b179a3fb6906 1 parent 7fa51a2
Aymeric Augustin authored July 20, 2012
4  django/contrib/admin/options.py
@@ -24,6 +24,7 @@
24 24
 from django.utils.datastructures import SortedDict
25 25
 from django.utils.html import escape, escapejs
26 26
 from django.utils.safestring import mark_safe
  27
+from django.utils import six
27 28
 from django.utils.text import capfirst, get_text_list
28 29
 from django.utils.translation import ugettext as _
29 30
 from django.utils.translation import ungettext
@@ -57,9 +58,8 @@ class IncorrectLookupParameters(Exception):
57 58
 
58 59
 csrf_protect_m = method_decorator(csrf_protect)
59 60
 
60  
-class BaseModelAdmin(object):
  61
+class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
61 62
     """Functionality common to both ModelAdmin and InlineAdmin."""
62  
-    __metaclass__ = forms.MediaDefiningClass
63 63
 
64 64
     raw_id_fields = ()
65 65
     fields = None
18  django/db/models/base.py
@@ -24,6 +24,7 @@
24 24
 from django.utils.translation import ugettext_lazy as _
25 25
 from django.utils.functional import curry
26 26
 from django.utils.encoding import smart_str, force_unicode
  27
+from django.utils import six
27 28
 from django.utils.text import get_text_list, capfirst
28 29
 
29 30
 
@@ -275,8 +276,8 @@ def __init__(self, db=None):
275 276
         # This impacts validation only; it has no effect on the actual save.
276 277
         self.adding = True
277 278
 
278  
-class Model(object):
279  
-    __metaclass__ = ModelBase
  279
+
  280
+class ModelWithoutMeta(object):
280 281
     _deferred = False
281 282
 
282 283
     def __init__(self, *args, **kwargs):
@@ -369,7 +370,7 @@ def __init__(self, *args, **kwargs):
369 370
                     pass
370 371
             if kwargs:
371 372
                 raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
372  
-        super(Model, self).__init__()
  373
+        super(ModelWithoutMeta, self).__init__()
373 374
         signals.post_init.send(sender=self.__class__, instance=self)
374 375
 
375 376
     def __repr__(self):
@@ -401,7 +402,7 @@ def __reduce__(self):
401 402
         only module-level classes can be pickled by the default path.
402 403
         """
403 404
         if not self._deferred:
404  
-            return super(Model, self).__reduce__()
  405
+            return super(ModelWithoutMeta, self).__reduce__()
405 406
         data = self.__dict__
406 407
         defers = []
407 408
         for field in self._meta.fields:
@@ -876,6 +877,15 @@ def clean_fields(self, exclude=None):
876 877
             raise ValidationError(errors)
877 878
 
878 879
 
  880
+# For unknown reasons, six.with_metaclass doesn't work correctly for Model.
  881
+# Fallback to exec'ing the appropriate syntax for each Python version.
  882
+
  883
+if six.PY3:
  884
+    six.exec_("class Model(ModelWithoutMeta, metaclass=ModelBase): pass")
  885
+else:
  886
+    six.exec_("class Model(ModelWithoutMeta): __metaclass__ = ModelBase")
  887
+
  888
+
879 889
 ############################################
880 890
 # HELPER FUNCTIONS (CURRIED MODEL METHODS) #
881 891
 ############################################
4  django/forms/forms.py
@@ -14,6 +14,7 @@
14 14
 from django.utils.html import conditional_escape, format_html
15 15
 from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
16 16
 from django.utils.safestring import mark_safe
  17
+from django.utils import six
17 18
 
18 19
 
19 20
 __all__ = ('BaseForm', 'Form')
@@ -380,14 +381,13 @@ def visible_fields(self):
380 381
         """
381 382
         return [field for field in self if not field.is_hidden]
382 383
 
383  
-class Form(BaseForm):
  384
+class Form(six.with_metaclass(DeclarativeFieldsMetaclass, BaseForm)):
384 385
     "A collection of Fields, plus their associated data."
385 386
     # This is a separate class from BaseForm in order to abstract the way
386 387
     # self.fields is specified. This class (Form) is the one that does the
387 388
     # fancy metaclass stuff purely for the semantic sugar -- it allows one
388 389
     # to define a form using declarative syntax.
389 390
     # BaseForm itself has no way of designating self.fields.
390  
-    __metaclass__ = DeclarativeFieldsMetaclass
391 391
 
392 392
 class BoundField(StrAndUnicode):
393 393
     "A Field plus data"
6  django/forms/models.py
@@ -15,6 +15,7 @@
15 15
     MultipleHiddenInput, media_property)
16 16
 from django.utils.encoding import smart_unicode, force_unicode
17 17
 from django.utils.datastructures import SortedDict
  18
+from django.utils import six
18 19
 from django.utils.text import get_text_list, capfirst
19 20
 from django.utils.translation import ugettext_lazy as _, ugettext
20 21
 
@@ -365,8 +366,8 @@ def save(self, commit=True):
365 366
 
366 367
     save.alters_data = True
367 368
 
368  
-class ModelForm(BaseModelForm):
369  
-    __metaclass__ = ModelFormMetaclass
  369
+class ModelForm(six.with_metaclass(ModelFormMetaclass, BaseModelForm)):
  370
+    pass
370 371
 
371 372
 def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
372 373
                       formfield_callback=None,  widgets=None):
@@ -401,6 +402,7 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
401 402
 
402 403
     form_metaclass = ModelFormMetaclass
403 404
 
  405
+    # TODO: this doesn't work under Python 3.
404 406
     if issubclass(form, BaseModelForm) and hasattr(form, '__metaclass__'):
405 407
         form_metaclass = form.__metaclass__
406 408
 
4  django/forms/widgets.py
@@ -17,6 +17,7 @@
17 17
 from django.utils.encoding import StrAndUnicode, force_unicode
18 18
 from django.utils.safestring import mark_safe
19 19
 from django.utils import datetime_safe, formats
  20
+from django.utils import six
20 21
 
21 22
 __all__ = (
22 23
     'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
@@ -153,8 +154,7 @@ def __unicode__(self):
153 154
             args.append(self.choices)
154 155
         return self.parent_widget.render(*args)
155 156
 
156  
-class Widget(object):
157  
-    __metaclass__ = MediaDefiningClass
  157
+class Widget(six.with_metaclass(MediaDefiningClass)):
158 158
     is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
159 159
     needs_multipart_form = False # Determines does this widget need multipart form
160 160
     is_localized = False

0 notes on commit d11d45a

Please sign in to comment.
Something went wrong with that request. Please try again.