use first() where appropriate

pull/94/head
eric 2020-08-17 11:20:00 -04:00
parent 5972b8dfeb
commit 3061ca1695
3 changed files with 15 additions and 38 deletions

View File

@ -709,10 +709,7 @@ class Campaign(models.Model):
else:
transactions = self.transaction_set.filter(Q(user=user) & (Q(status=TRANSACTION_STATUS_ERROR) | Q(status=TRANSACTION_STATUS_FAILED)))
# assumption --that the first failed/errored transaction has the amount we need to recharge
if transactions.exists():
return transactions[0]
else:
return None
return transactions.first()
else:
return None
@ -1191,23 +1188,19 @@ class UserProfile(models.Model):
@property
def pledge_count(self):
return self.user.transaction_set.exclude(status='NONE').exclude(status='Canceled', reason=None).exclude(anonymous=True).count()
return self.user.transaction_set.exclude(status='NONE').exclude(
status='Canceled', reason=None).exclude(anonymous=True).count()
@property
def account(self):
# there should be only one active account per user
accounts = self.user.account_set.filter(date_deactivated__isnull=True)
if accounts.exists():
return accounts[0]
return None
return self.user.account_set.filter(date_deactivated__isnull=True).first()
@property
def old_account(self):
accounts = self.user.account_set.filter(date_deactivated__isnull=False).order_by('-date_deactivated')
if accounts.exists():
return None
else:
return accounts[0]
return self.user.account_set.filter(
date_deactivated__isnull=False
).order_by('-date_deactivated').first()
@property
def pledges(self):
@ -1216,10 +1209,7 @@ class UserProfile(models.Model):
@property
def last_transaction(self):
from regluit.payment.models import Transaction
try:
return Transaction.objects.filter(user=self.user).order_by('-date_modified')[0]
except IndexError:
return None
return Transaction.objects.filter(user=self.user).order_by('-date_modified').first()
@property
def ack_name(self):
@ -1236,7 +1226,6 @@ class UserProfile(models.Model):
last = self.last_transaction
if last:
return last.anonymous
else:
return None
@property

View File

@ -659,19 +659,11 @@ class Work(models.Model):
@property
def borrowed(self):
loans = self.acqs.filter(license=BORROWED, expires__gt=now())
if not loans.exists():
return None
else:
return loans[0]
return self.acqs.filter(license=BORROWED, expires__gt=now()).first()
@property
def purchased(self):
purchases = self.acqs.filter(license=INDIVIDUAL, expires__isnull=True)
if not purchases.exists():
return None
else:
return purchases[0]
return self.acqs.filter(license=INDIVIDUAL, expires__isnull=True).first()
@property
def lib_acqs(self):
@ -680,11 +672,8 @@ class Work(models.Model):
@property
def next_acq(self):
""" This is the next available copy in the user's libraries"""
loans = self.acqs.filter(license=LIBRARY, refreshes__gt=now()).order_by('refreshes')
if not loans.exists():
return None
else:
return loans[0]
return self.acqs.filter(license=LIBRARY,
refreshes__gt=now()).order_by('refreshes').first()
@property
def borrowable(self):
@ -696,8 +685,7 @@ class Work(models.Model):
@property
def borrowable_acq(self):
for acq in self.acqs.filter(license=LIBRARY, refreshes__lt=now()):
return acq
return self.acqs.filter(license=LIBRARY, refreshes__lt=now()).first()
@property
def is_duplicate(self):

View File

@ -23,7 +23,7 @@ def purchased(context):
context['in_library'] = None
holds = user.holds.filter(work=work)
if holds.exists():
context['on_hold'] = holds[0]
context['on_hold'] = holds.first()
if user_license:
context['purchased'] = user_license.purchased
context['borrowed'] = user_license.borrowed