from django.db import models

# Create your models here.

@@ -9,49 +9,1903 @@
 ngo.
-db import models%0A%0A# Create your models here.
+contrib.auth.models import Permission%0Afrom django.db.models.signals import post_save, pre_delete%0Afrom django.dispatch import receiver%0Afrom cardbox.card_model import Card%0Afrom cardbox.deck_model import Deck%0Afrom guardian.shortcuts import assign_perm, get_users_with_perms%0Afrom guardian.models    import UserObjectPermission%0Afrom memorize.models import Practice%0Afrom django.contrib.auth.models import User%0A%0A@receiver(post_save, sender=UserObjectPermission)%0Adef create_practice_objects_for_new_viewers(sender, **kwargs):%0A    if kwargs%5B'instance'%5D.permission_id == Permission.objects.get(codename=%22view_deck%22).id:%0A        for card in Card.objects.filter(deck=kwargs%5B'instance'%5D.object_pk):%0A            Practice(item=card, user=User.objects.get(id = kwargs%5B'instance'%5D.user_id)).save()%0A%0A@receiver(pre_delete, sender=UserObjectPermission)%0Adef delete_practice_objects_for_removed_viewers(sender, **kwargs):%0A    if kwargs%5B'instance'%5D.permission_id == Permission.objects.get(codename=%22view_deck%22).id:%0A        for card in Card.objects.filter(deck=kwargs%5B'instance'%5D.object_pk):%0A            Practice.objects.get(object_id=card.ID, user=User.objects.get(id = kwargs%5B'instance'%5D.user_id)).delete()%0A%0A@receiver(post_save, sender=Card)%0Adef create_practice_objects_for_new_card(sender,update_fields, **kwargs):%0A    %22%22%22Creates practice objects for all users with permission to view the card.%0A%0A    %22%22%22%0A    perm_users = get_users_with_perms(kwargs%5B'instance'%5D.deck)%0A    for user in perm_users:%0A        practice = Practice(item = kwargs%5B'instance'%5D, user = user)%0A        if Practice.objects.filter(object_id = kwargs%5B'instance'%5D.ID, user=user).count() == 0:%0A            practice.save()%0A%0A%0A%0A@receiver(pre_delete, sender=Card)%0Adef delete_practice_objects(sender, **kwargs):%0A    %22%22%22Deletes all practice objects for a card once it is deleted.%0A%0A    %22%22%22%0A    Practice.objects.filter(object_id = kwargs%5B'instance'%5D.ID).delete()%0A
 %0A

Add signal hooks to create practice objects