Home:ALL Converter>email whitelist/blacklist in python/django

email whitelist/blacklist in python/django

Ask Time:2009-04-13T23:46:11         Author:Jiaaro

Json Formatter

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?

Author:Jiaaro,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/744308/email-whitelist-blacklist-in-python-django
tghw :

I would restructure it so both lists were contained in one model.\n\nclass PermissionList(models.Model):\n setter = models.ManyToManyField(User)\n email = models.EmailField(unique=True) #don't want conflicting results\n permission = models.BooleanField()\n\n\nThen, your lists would just be:\n\n# whitelist\nPermissionList.objects.filter(permission=True)\n# blacklist\nPermissionList.objects.filter(permission=False)\n\n\nTo check a particular user, you just add a couple functions to the model:\n\nclass PermissionList(...):\n ...\n @classmethod\n def is_on_whitelist(email):\n return PermissionList.objects.filter(email=email, permission=True).count() > 0\n\n @classmethod\n def is_on_blacklist(email):\n return PermissionList.objects.filter(email=email, permission=False).count() > 0\n\n @classmethod\n def has_permission(email):\n if PermissionList.is_on_whitelist(email):\n return True\n if PermissionList.is_on_blacklist(email):\n return False\n return None\n\n\nHaving everything in one place is a lot simpler, and you can make more interesting queries with less work.",
2009-04-13T22:17:36
S.Lott :

[Please start All Class Names With Upper Case Letters.]\n\nYour code doesn't make use of your class distinction very well. \n\nSpecifically, 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.\n\nIf, 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\n\nclass WhiteList( models.Manager ):\n def get_query_set( self ):\n return super( WhiteList, self ).get_query_set().filter( status='W' )\n\nclass BlackList( models.Manager )\n def get_query_set( self ):\n return super( BlackList, self ).get_query_set().filter( status='B' )\n\nclass KnownEmail( models.Model ):\n relatedUser = models.ForeignKey(User)\n email = models.EmailField()\n status = models.CharField( max_length=1, choices=LIST_CHOICES )\n objects = models.Manager() # default manager shows all lists\n whiteList= WhiteList() # KnownEmail.whiteList.all() is whitelist subset\n blackList= BlackList() # KnownEmail.blackList.all() is blackList subset\n",
2009-04-13T15:54:41
yy