-
Notifications
You must be signed in to change notification settings - Fork 1
/
signals.py
31 lines (26 loc) · 1.26 KB
/
signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from habit_tracker.models import Habit
@receiver(post_save, sender=User)
def assign_habits_to_new_user(sender, instance, created, **kwargs):
if created:
default_habits = [
{"name": "Read Books", "description": "Read at least 20 pages daily"},
{"name": "Exercise", "description": "30 minutes of exercise"},
{"name": "Meditation", "description": "Meditate for 10 minutes"},
{"name": "Read Books Again", "description": "Read at least 20 pages daily"},
{"name": "Exercise Again", "description": "30 minutes of exercise"},
{"name": "Meditation Again", "description": "Meditate for 10 minutes"}
]
for habit_data in default_habits:
Habit.objects.get_or_create(**habit_data)
@receiver(post_save, sender=User)
def create_second_superuser(sender, instance, created, **kwargs):
if created and instance.username == 'admin':
if not User.objects.filter(username='admin2').exists():
User.objects.create_superuser(
username='admin2',
email='admin2@example.com',
password='admin'
)