views:

245

answers:

2

Hello,

I am writing a django app that keeps track of which email addresses are allowed to post content to a user's account. The user can whitelist and blacklist addresses as they like.

Any addresses that aren't specified can either be handled per message or just default to whitelist or blacklist (again user specified).

Here are the django models I wrote... do you think is a good way to do it? or should I add a whitelist and blacklist field to each user's profile model?

class knownEmail(models.Model):
    # The user who set this address' permission, NOT
    # the user who the address belongs to...
    relatedUser = models.ManyToManyField(User)
    email = models.EmailField()

class whiteList(knownEmail):
    pass

class blackList(knownEmail):
    pass

Then I could do something like:

def checkPermission(user, emailAddress):
    "Check if 'emailAddress' is allowed to post content to 'user's profile"
    if whiteList.objects.filter(relatedUser=user, email=emailAddress):
        return True
    elif blackList.objects.filter(relatedUser=user, email=emailAddress):
        return False
    else:
        return None

Is there a better way?

+3  A: 

[Please start All Class Names With Upper Case Letters.]

Your code doesn't make use of your class distinction very well.

Specifically, your classes don't have any different behavior. Since both classes have all the same methods, it isn't clear why these are two different classes in the first place. If they have different methods, then your solution is good.

If, however, they don't have different methods, you might want to look at providing a customized manager for each of the two subsets of KnownEmail

class WhiteList( models.Manager ):
    def get_query_set( self ):
        return super( WhiteList, self ).get_query_set().filter( status='W' )

class BlackList( models.Manager )
    def get_query_set( self ):
        return super( WhiteList, self ).get_query_set().filter( status='B' )

class KnownEmail( models.Model ):
    relatedUser = models.ForeignKey(User)
    email = models.EmailField()
    status = models.CharField( max_length=1, choices=LIST_CHOICES )
    objects = models.Manager() # default manager shows all lists
    whiteList= WhiteList() # KnownEmail.whiteList.all() is whitelist subset
    blackList= BlackList() # KnownEmail.blackList.all() is blackList subset
S.Lott
+4  A: 

I would restructure it so both lists were contained in one model.

class PermissionList(models.Model):
    setter = models.ManyToManyField(User)
    email = models.EmailField(unique=True) #don't want conflicting results
    permission = models.BooleanField()

Then, your lists would just be:

# whitelist
PermissionList.objects.filter(permission=True)
# blacklist
PermissionList.objects.filter(permission=False)

To check a particular user, you just add a couple functions to the model:

class PermissionList(...):
    ...
    @classmethod
    def is_on_whitelist(email):
        return PermissionList.objects.filter(email=email, permission=True).count() > 0

    @classmethod
    def is_on_blacklist(email):
        return PermissionList.objects.filter(email=email, permission=False).count() > 0

    @classmethod
    def has_permission(email):
        if PermissionList.is_on_whitelist(email):
            return True
        if PermissionList.is_on_blacklist(email):
            return False
        return None

Having everything in one place is a lot simpler, and you can make more interesting queries with less work.

tghw
thanks, I think your second class method should be is_on_blacklist though ;)
Jiaaro