Total Complexity | 3 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |