ore.models.user.create_user_profile()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nop 4
1
import django.contrib.auth.models as auth
2
3
import django.db.models as models
4
from django.db.models.signals import post_save
5
from django.dispatch import receiver
6
7
8
class UserProfile(models.Model):
9
10
    """
11
    Class: UserProfile
12
13
    Extends: models.Model
14
15
    A user profile that enriches the standard django user with additional information, such as e.g. if he or she wants
16
    to receive newsletters.
17
18
    Fields:
19
     {User} - the wrapped django user
20
     {bool} - flag indicating whether a newsletter is desired
21
    """
22
    user = models.OneToOneField(auth.User, related_name='profile')
23
    newsletter = models.BooleanField(default=False)
24
25
    class Meta:
26
        app_label = 'ore'
27
28
# handler that automatically creates a new user profile when also a new django user is created
29
# This breaks on fresh database creation, since the signal is triggered when no UserProfile
30
# table was created so far. For this reason, we silently ignore all errors
31
# here.
32
33
34
@receiver(post_save, sender=auth.User)
35
def create_user_profile(sender, instance, created, **kwargs):
36
    if created:
37
        try:
38
            UserProfile.objects.get_or_create(user=instance)
39
        except Exception:
40
            pass
41
42
43
# ensures that only the UserProfile is visible when importing this module
44
__all__ = ['UserProfile']
45