Improve columns in level admin

This commit is contained in:
Gabriel Augendre 2019-06-28 15:24:03 +02:00
parent c7ad114c5c
commit 0522bac302
2 changed files with 28 additions and 29 deletions

View file

@ -37,34 +37,34 @@ class LevelAdmin(admin.ModelAdmin):
list_display = [ list_display = [
'name', 'name',
'order', 'order',
'book_count', 'non_acquired_book_count',
'consumable_count', 'non_acquired_book_price',
'consumable_price',
'non_acquired_consumable_count', 'non_acquired_consumable_count',
'non_acquired_consumable_price' 'non_acquired_consumable_price',
'non_acquired_total_price',
] ]
list_editable = ['order'] list_editable = ['order']
list_display_links = ['name'] list_display_links = ['name']
def consumable_count(self, obj: Level): def non_acquired_book_count(self, obj: Level):
return obj.consumable_count return obj.book_set.filter(previously_acquired=False, consumable=False).count()
consumable_count.short_description = 'Nombre de consommable(s)' non_acquired_book_count.short_description = 'Nombre de livres à acheter (hors consommables)'
def book_count(self, obj: Level): def non_acquired_book_price(self, obj: Level):
return obj.book_set.count() return f'{obj.non_acquired_book_price:.2f}'
book_count.short_description = 'Nombre de livre(s)' non_acquired_book_price.short_description = 'Coût des livres à acheter (hors consommables)'
def consumable_price(self, obj: Level):
return f'{obj.consumable_price:.2f}'
consumable_price.short_description = 'Coût des consommables'
def non_acquired_consumable_count(self, obj: Level): def non_acquired_consumable_count(self, obj: Level):
return obj.non_acquired_consumable_count return obj.book_set.filter(consumable=True, previously_acquired=False).count()
non_acquired_consumable_count.short_description = 'Nombre de consommable(s) à acheter cette année' non_acquired_consumable_count.short_description = 'Nombre de consommables à acheter'
def non_acquired_consumable_price(self, obj: Level): def non_acquired_consumable_price(self, obj: Level):
return f'{obj.non_acquired_consumable_price:.2f}' return f'{obj.non_acquired_consumable_price:.2f}'
non_acquired_consumable_price.short_description = 'Coût des consommables à acheter cette année' non_acquired_consumable_price.short_description = 'Coût des consommables à acheter'
def non_acquired_total_price(self, obj: Level):
return f'{obj.non_acquired_total_price:.2f}'
non_acquired_total_price.short_description = 'Coût total à acheter'
class BookResource(resources.ModelResource): class BookResource(resources.ModelResource):

View file

@ -117,23 +117,22 @@ class Level(BaseModel):
return self.name return self.name
@property @property
def consumable_count(self): def non_acquired_consumable_price(self):
return self.book_set.filter(consumable=True).count() price = self.book_set.filter(consumable=True, previously_acquired=False).aggregate(Sum('price')).get('price__sum', 0)
@property
def non_acquired_consumable_count(self):
return self.book_set.filter(consumable=True, previously_acquired=False).count()
@property
def consumable_price(self):
price = self.book_set.filter(consumable=True).aggregate(Sum('price')).get('price__sum', 0)
if price is None: if price is None:
return 0 return 0
return price return price
@property @property
def non_acquired_consumable_price(self): def non_acquired_book_price(self):
price = self.book_set.filter(consumable=True, previously_acquired=False).aggregate(Sum('price')).get('price__sum', 0) price = self.book_set.filter(consumable=False, previously_acquired=False).aggregate(Sum('price')).get('price__sum', 0)
if price is None:
return 0
return price
@property
def non_acquired_total_price(self):
price = self.book_set.filter(previously_acquired=False).aggregate(Sum('price')).get('price__sum', 0)
if price is None: if price is None:
return 0 return 0
return price return price