From 177fc81071cb3cab31ebc335d4408ff3711c4e27 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Mon, 27 Jan 2014 15:11:54 -0500 Subject: [PATCH] Natural keys for user stuff #264 --- karmaworld/apps/users/models.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/karmaworld/apps/users/models.py b/karmaworld/apps/users/models.py index a2e549f..d6a9657 100644 --- a/karmaworld/apps/users/models.py +++ b/karmaworld/apps/users/models.py @@ -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() -- 2.25.1