File diff 2423d806af36 → 2755f34f36d7
conntrackt/models.py
Show inline comments
 
@@ -3,6 +3,25 @@ from django.core.exceptions import Valid
 
from django.db import models
 

	
 

	
 
class ViewPermissionModelMetaClass(models.base.ModelBase):
 
    """
 
    Implements a meta class that can be used for automatically adding the view
 
    permission for a model.
 

	
 
    Limitation is that syncdb must be called with --all if using South.
 

	
 
    Original source found at:
 

	
 
    http://stackoverflow.com/questions/725913/dynamic-meta-attributes-for-django-models
 
    """
 

	
 
    def __new__(cls, name, bases, attrs):
 
        cl = super(ViewPermissionModelMetaClass, cls).__new__(cls, name, bases, attrs)
 
        cl._meta.permissions.append(('view_' + cl._meta.module_name, u'Can view %s' % cl._meta.verbose_name))
 

	
 
        return cl
 

	
 

	
 
class Project(models.Model):
 
    """
 
    Implements a model with information about a project. A project has some
 
@@ -18,6 +37,8 @@ class Project(models.Model):
 
    name = models.CharField(max_length=100)
 
    description = models.TextField(blank=True)
 

	
 
    __metaclass__ = ViewPermissionModelMetaClass
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
@@ -54,6 +75,8 @@ class Location(models.Model):
 
    name = models.CharField(max_length=100)
 
    description = models.TextField(blank=True)
 

	
 
    __metaclass__ = ViewPermissionModelMetaClass
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
@@ -88,6 +111,8 @@ class Entity(models.Model):
 
    project = models.ForeignKey(Project)
 
    location = models.ForeignKey(Location)
 

	
 
    __metaclass__ = ViewPermissionModelMetaClass
 

	
 
    class Meta:
 
        """
 
        Overrides some of the default parameters used by Django for this model.