From bb193cf4f1be34aa56c68e0278c81f709e973082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 11 Mar 2016 12:42:46 +0200 Subject: [PATCH] Profile and Subscription managers use get_or_create to avoid some boilerplate --- hc/accounts/models.py | 7 +------ hc/payments/models.py | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index 72403f44..422a4989 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -16,12 +16,7 @@ from hc.lib import emails class ProfileManager(models.Manager): def for_user(self, user): - try: - profile = self.get(user_id=user.id) - except Profile.DoesNotExist: - profile = Profile(user=user) - profile.save() - + profile, created = Profile.objects.get_or_create(user_id=user.id) return profile diff --git a/hc/payments/models.py b/hc/payments/models.py index 616d7574..6391bc67 100644 --- a/hc/payments/models.py +++ b/hc/payments/models.py @@ -5,12 +5,7 @@ from django.db import models class SubscriptionManager(models.Manager): def for_user(self, user): - try: - sub = self.get(user_id=user.id) - except Subscription.DoesNotExist: - sub = Subscription(user=user) - sub.save() - + sub, created = Subscription.objects.get_or_create(user_id=user.id) return sub