Natural keys for user stuff #264
authorCharles Connell <charles@connells.org>
Mon, 27 Jan 2014 20:11:54 +0000 (15:11 -0500)
committerCharles Connell <charles@connells.org>
Mon, 27 Jan 2014 20:11:54 +0000 (15:11 -0500)
karmaworld/apps/users/models.py

index a2e549fbab950646f4814b0956940897b08cbe11..d6a9657b1cc33ee4d152bc5d49b24f6af45e2057 100644 (file)
@@ -15,11 +15,20 @@ from karmaworld.apps.courses.models import School
 logger = logging.getLogger(__name__)
 
 
+class UserProfileManager(models.Manager):
+    """ Handle restoring data. """
+    def get_by_natural_key(self, user):
+        return self.get(user=user)
+
+
 class UserProfile(models.Model):
     user      = models.OneToOneField(User)
 
     school    = models.ForeignKey(School, blank=True, null=True)
 
+    def natural_key(self):
+        return (self.user)
+
     def get_points(self):
         sum = 0
         for cls in ALL_KARMA_EVENT_CLASSES:
@@ -81,6 +90,12 @@ def give_email_confirm_karma(sender, **kwargs):
     GenericKarmaEvent.create_event(kwargs['email_address'].user, kwargs['email_address'].email, GenericKarmaEvent.EMAIL_CONFIRMED)
 
 
+class BaseKarmaEventManager(models.Manager):
+    """ Handle restoring data. """
+    def get_by_natural_key(self, points, user, timestamp):
+        return self.get(points=points, user=user, timestamp=timestamp)
+
+
 class BaseKarmaEvent(models.Model):
     points    = models.IntegerField()
     user      = models.ForeignKey(User)
@@ -88,6 +103,10 @@ class BaseKarmaEvent(models.Model):
 
     class Meta:
         abstract = True
+        unique_together = ('points', 'user', 'timestamp')
+
+    def natural_key(self):
+        return (self.points, self.user, self.timestamp)
 
     def get_message(self):
         raise NotImplemented()