PrayTeam / scriptured-prayer

GNU Affero General Public License v3.0
1 stars 0 forks source link

UserCard Creation #108

Closed kenancasey closed 3 weeks ago

kenancasey commented 2 months ago

How/when are user cards created? Bryce and I were looking at this yesterday and it seemed like we need to create user card objects that are a copy of all of the cards whenever a new user is created for all of the built-in decks. Am I thinking about that correctly? @Soyokaze-42

kenancasey commented 2 months ago

Thoughts on this? @Soyokaze-42

Soyokaze-42 commented 2 months ago

I'm just getting back from a vacation to see the total solar eclipse. That was amazing!

So, Django has a really useful feature for events-based programming called signals. I used this in prayerapp/models.py (currently line 232 for Cards and line 245 for Users) to do exactly what you are looking into:

Signals

@receiver(post_save, sender=Card) def create_usercards_for_card(instance, created, **kwargs): """When a new card is created, create a UserCard for each user.""" if created: for user in User.objects.all(): usercard = UserCard.objects.create( user=user, card=instance, created_by=instance.created_by, ) usercard.save()

@receiver(post_save, sender=settings.AUTH_USER_MODEL) def create_related_objects_for_user(instance, created, **kwargs): """When a new user is created, create a UserCard for each non-private card.""" if created: for card in Card.objects.filter(private=False): usercard = UserCard.objects.create(user=instance, card=card) usercard.save() for category in Category.objects.all(): option = UserCategoryOptions.objects.create(user=instance, category=category) option.save() userprofile = UserProfile.objects.create(user=instance) userprofile.save()