forked from DGNum/gestioCOF
Add custom Manager for Account model.
- When Account model is queried with Account.objects, it always add .select_related('cofprofile_user', 'negative'). - Eg benefits: history doesn't do anymore one request by account to fill the account filter. Important Using this workaround (systemically append select_related) can be dangerous normally, however a certain number of properties in cofprofile and user are frequently used (as firstname or lastname), and the benefits seems greater.
This commit is contained in:
parent
33cee05884
commit
709d421b2c
3 changed files with 30 additions and 18 deletions
|
@ -46,6 +46,10 @@ class GenericTeamBackend(object):
|
||||||
|
|
||||||
def get_user(self, user_id):
|
def get_user(self, user_id):
|
||||||
try:
|
try:
|
||||||
return User.objects.get(pk=user_id)
|
return (
|
||||||
|
User.objects
|
||||||
|
.select_related('profile__acount_kfet')
|
||||||
|
.get(pk=user_id)
|
||||||
|
)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -27,8 +27,19 @@ def default_promo():
|
||||||
now = date.today()
|
now = date.today()
|
||||||
return now.month <= 8 and now.year-1 or now.year
|
return now.month <= 8 and now.year-1 or now.year
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
|
||||||
|
class AccountManager(models.Manager):
|
||||||
|
"""Manager for Account Model."""
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""Always append related data to this Account."""
|
||||||
|
return super().get_queryset().select_related('cofprofile__user',
|
||||||
|
'negative')
|
||||||
|
|
||||||
|
|
||||||
class Account(models.Model):
|
class Account(models.Model):
|
||||||
|
objects = AccountManager()
|
||||||
|
|
||||||
cofprofile = models.OneToOneField(
|
cofprofile = models.OneToOneField(
|
||||||
CofProfile, on_delete = models.PROTECT,
|
CofProfile, on_delete = models.PROTECT,
|
||||||
related_name = "account_kfet")
|
related_name = "account_kfet")
|
||||||
|
|
|
@ -346,35 +346,32 @@ def account_create_ajax(request, username=None, login_clipper=None,
|
||||||
'user_form' : forms['user_form'],
|
'user_form' : forms['user_form'],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
# Account - Read
|
# Account - Read
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def account_read(request, trigramme):
|
def account_read(request, trigramme):
|
||||||
try:
|
account = get_object_or_404(Account, trigramme=trigramme)
|
||||||
account = (
|
|
||||||
Account.objects
|
|
||||||
.select_related('cofprofile__user', 'negative')
|
|
||||||
.get(trigramme=trigramme)
|
|
||||||
)
|
|
||||||
except Account.DoesNotExist:
|
|
||||||
raise Http404
|
|
||||||
|
|
||||||
# Checking permissions
|
# Checking permissions
|
||||||
if not request.user.has_perm('kfet.is_team') \
|
if not request.user.has_perm('kfet.is_team') \
|
||||||
and request.user != account.user:
|
and request.user != account.user:
|
||||||
raise PermissionDenied
|
raise PermissionDenied
|
||||||
|
|
||||||
addcosts = (OperationGroup.objects
|
addcosts = (
|
||||||
.filter(opes__addcost_for=account,opes__canceled_at=None)
|
OperationGroup.objects
|
||||||
.extra({'date':"date(at)"})
|
.filter(opes__addcost_for=account,
|
||||||
|
opes__canceled_at=None)
|
||||||
|
.extra({'date': "date(at)"})
|
||||||
.values('date')
|
.values('date')
|
||||||
.annotate(sum_addcosts=Sum('opes__addcost_amount'))
|
.annotate(sum_addcosts=Sum('opes__addcost_amount'))
|
||||||
.order_by('-date'))
|
.order_by('-date')
|
||||||
|
)
|
||||||
|
|
||||||
return render(request, "kfet/account_read.html", {
|
return render(request, "kfet/account_read.html", {
|
||||||
'account' : account,
|
'account': account,
|
||||||
'addcosts': addcosts,
|
'addcosts': addcosts,
|
||||||
'settings': { 'subvention_cof': Settings.SUBVENTION_COF() },
|
'settings': {'subvention_cof': Settings.SUBVENTION_COF()},
|
||||||
})
|
})
|
||||||
|
|
||||||
# Account - Update
|
# Account - Update
|
||||||
|
|
Loading…
Reference in a new issue