12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- from datetime import date
- from django.contrib import admin
- from django.contrib.auth import get_user_model
- from .models import ItemType, Item, Loan, Storage
- User = get_user_model()
- admin.site.register(ItemType)
- class OwnerFilter(admin.SimpleListFilter):
- title = "Propriétaire"
- parameter_name = 'owner'
- def lookups(self, request, model_admin):
- owners = [
- (i.pk, i) for i in User.objects.filter(items__isnull=False)]
- return [(None, "L'association")] + owners
- def queryset(self, request, queryset):
- if self.value():
- return queryset.filter(owner__pk=self.value())
- else:
- return queryset
- @admin.register(Item)
- class ItemAdmin(admin.ModelAdmin):
- list_display = (
- 'designation', 'type', 'mac_address', 'serial', 'owner',
- 'buy_date', 'is_available')
- list_filter = ('type__name', 'storage', 'buy_date', OwnerFilter)
- search_fields = (
- 'designation', 'mac_address', 'serial',
- 'owner__email', 'owner__nickname',
- 'owner__first_name', 'owner__last_name')
- actions = ['give_back']
- def give_back(self, request, queryset):
- for item in queryset.filter(loans__loan_date_end=None):
- item.give_back()
- give_back.short_description = 'Rendre le matériel'
- @admin.register(Loan)
- class LoanAdmin(admin.ModelAdmin):
- list_display = ('item', 'user', 'loan_date', 'loan_date_end', 'location')
- list_filter = ('item__designation', 'user__username')
- search_fields = ('item', 'user')
- actions = ['end_loan']
- def end_loan(self, request, queryset):
- queryset.filter(loan_date_end=None).update(
- loan_date_end=date.today())
- end_loan.short_description = 'Mettre fin au prêt'
- @admin.register(Storage)
- class StorageAdmin(admin.ModelAdmin):
- list_display = ('name', 'truncated_notes')
- def truncated_notes(self, obj):
- if len(obj.notes) > 50:
- return '{}…'.format(obj.notes[:50])
- else:
- return obj.note
- truncated_notes.short_description = 'notes'
|