Django Model OWNership - a reusable app for checking ownership of objects (you define "ownership").
Python
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
mown
.gitignore
README

README

django-mown (Model OWNership)

Summary
-------

This app provides a class for models to inherit from if they have the notion
of ownership (e.g. a Book belongs to a User).

The path of ownership between user and object is entirely up to you; simply
override is_owned_by() in your model to define "ownership" however you want.

Installation
------------

1) Put mown in your PYTHONPATH.

2) Subclass mown.models.HasOwner in any model that has the notion of an owner.

3) Implement is_owned_by(self, user) for all models that inherit from HasOwner.

   Example, in which users own libraries and all books in those libraries:

        from django.db import models
        from django.contrib.auth.models import User
        from mown.models import HasOwner

        class Library(HasOwner, models.Model)
            librarians = models.ManyToManyField(User, related_name="libraries")
            name = models.CharField(max_length=80)

            def is_owned_by(self, user):
                if user.libraries.filter(pk=self.pk).count():
                   return True
                return False

        class Book(HasOwner, models.Model):
            library = models.ForeignKey(Library, related_name="books")
            title = models.CharField(max_length=200, db_index=True)

            def is_owned_by(self, user):
                return self.library.is_owned_by(user)

Checking Ownership
------------------

MOWN provides two facilities for checking model ownership:

Class Method:
  A class method get_mine() is provided to models inheriting from HasOwner.
  This method is intended to replicate the behavior of the standard model
  manager get() method, with two differences:
   * if the object does not exist, Http404 is raised
   * if the specified user does not own the object, PermissionDenied is raised

View Decorator:
  Also provided is a view decorator that has the same behavior as get_mine()
  but takes a model class as a parameter. This is useful for ensuring that an
  object is owned by the current user before a view is even run.

Please see the source code docstrings for details on using these facilities.