models.py 582 Bytes
Newer Older
Christophe Siraut's avatar
Christophe Siraut committed
1 2
from django.db import models
from django.contrib.auth.models import User
Christophe Siraut's avatar
Christophe Siraut committed
3
from django.contrib.auth.signals import user_logged_in
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
4
from django.core.exceptions import ObjectDoesNotExist
Christophe Siraut's avatar
Christophe Siraut committed
5 6 7

class UserProfile(models.Model):
    user = models.OneToOneField(User)
8
    email_notifications = models.BooleanField()
Christophe Siraut's avatar
Christophe Siraut committed
9 10

def login_handler(user, **kwargs):
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
11
    """Verify user profile exists"""
Christophe Siraut's avatar
Christophe Siraut committed
12 13
    try:
        profile = UserProfile.objects.get(user=user)
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
14
    except ObjectDoesNotExist:
Christophe Siraut's avatar
Christophe Siraut committed
15 16 17 18 19
        profile = UserProfile(user=user)
        profile.save()
    return

user_logged_in.connect(login_handler)