Browse files

Initial commit

  • Loading branch information...
1 parent b24f374 commit 2ddb9baaa411d6555a4759cb2e0a8c71a88df42e Thomas Bartelmess committed Dec 8, 2012
Showing with 66 additions and 0 deletions.
  1. +40 −0 modelmixins/__init__.py
  2. +26 −0 setup.py
View
40 modelmixins/__init__.py
@@ -0,0 +1,40 @@
+"""
+Mixin support for django models
+"""
+from django.db import models
+from django.db.models.signals import class_prepared
+from django.dispatch import receiver
+from django.db.models.fields import Field
+
+
+class ModelMixin(object):
+ """
+ Base class for model mixins
+ """
+
+ @classmethod
+ def model_mixin(cls):
+ """
+ Adds the fields of the class to the all classes that mix it in
+ """
+ fields = {}
+
+ for (name, attr) in cls.__dict__.items():
+ if isinstance(attr, models.Field):
+ fields[name] = attr
+
+ for fieldname in fields.keys():
+ delattr(cls, fieldname)
+
+ for subclass in cls.__subclasses__():
+ assert issubclass(subclass, models.Model)
+ for (fieldname, field) in fields.items():
+ field.contribute_to_class(subclass, fieldname)
+
+
+
+@receiver(class_prepared)
+def mixin(sender, **kwargs):
+ for base in sender.__bases__:
+ if issubclass(base, ModelMixin):
+ base.model_mixin()
View
26 setup.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+Setup script
+"""
+
+from distutils.core import setup
+
+setup(name='django-model-mixins',
+ version='1.0',
+ description='Simple helper to extend django models using mixins',
+ author='Thomas Bartelmess',
+ author_email='thomas.bartelmess@me.com',
+ url='https://github.com/tbartelmess/django-model-mixins',
+ packages=['modelmixins'],
+ classifiers=[
+ 'Development Status :: 3 - Alpha',
+ 'Environment :: Web Environment',
+ 'Framework :: Django',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Utilities'
+ ],
+ )

0 comments on commit 2ddb9ba

Please sign in to comment.