Commit 2e62bc71 authored by ljf's avatar ljf

Merge branch 'jd-enhance-inventory-admin' into 'master'

Améliore l'interface d'admin des objets

Closes #189

See merge request !6
parents 06a285b1 4da67e01
......@@ -166,9 +166,8 @@ INSTALLED_APPS = (
# Uncomment the next line to enable admin documentation:
#'django.contrib.admindocs',
'polymorphic',
# 'south',
'autocomplete_light', #Automagic autocomplete foreingkey form component
'activelink', #Detect if a link match actual page
'autocomplete_light', # Automagic autocomplete foreingkey form component
'activelink', # Detect if a link match actual page
'compat',
'hijack',
'django_extensions',
......
This diff is collapsed.
# -*- coding: utf-8 -*-
import autocomplete_light
from models import Item
# This will generate a MemberAutocomplete class
autocomplete_light.register(Item,
# Just like in ModelAdmin.search_fields
search_fields=[
'designation', 'mac_address', 'serial'],
attrs={
# This will set the input placeholder attribute:
'placeholder': "Nom / adresse MAC / n° de série de l'objet",
'data-autocomplete-minimum-characters': 3,
},
)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q
from django.conf import settings
......@@ -74,6 +75,14 @@ class Item(models.Model):
objects = ItemQuerySet().as_manager()
def __unicode__(self):
if self.serial and self.mac_address:
return '{} ({} / {})'.format(
self.designation, self.mac_address, self.serial
)
elif self.serial or self.mac_address:
return '{} ({})'.format(
self.designation, self.mac_address or self.serial
)
return self.designation
def save(self, *args, **kwargs):
......@@ -102,6 +111,14 @@ class Item(models.Model):
is_available.boolean = True
is_available.short_description = 'disponible'
def current_borrower(self):
current_loan = self.loans.running().first()
if current_loan:
return current_loan.user
else:
return None
current_borrower.short_description = 'Prêté à'
def get_mac_and_serial(self):
mac = self.mac_address
serial = self.serial
......@@ -109,6 +126,7 @@ class Item(models.Model):
return "{} / {}".format(mac, serial)
else:
return mac or serial or ''
get_mac_and_serial.short_description = 'Adresse MAC / n° de série'
class Meta:
verbose_name = 'objet'
......@@ -141,7 +159,9 @@ class Loan(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, verbose_name='membre',
related_name='loans', null=True, on_delete=models.SET_NULL)
loan_date = models.DateTimeField(verbose_name='date de prêt')
loan_date = models.DateTimeField(
verbose_name='date de prêt',
default=timezone.now)
loan_date_end = models.DateTimeField(verbose_name='date de fin de prêt',
null=True, blank=True)
notes = models.TextField(null=True, blank=True)
......@@ -152,9 +172,19 @@ class Loan(models.Model):
def get_mac_and_serial(self):
return self.item.get_mac_and_serial()
get_mac_and_serial.short_description = "Adresse MAC / n° de série"
def short_date(self):
return '{:%d/%m/%y}'.format(self.loan_date)
short_date.short_description = "Emprunté le…"
def short_date_end(self):
if self.loan_date_end:
return '{:%d/%m/%y}'.format(self.loan_date_end)
else:
return None
short_date_end.short_description = "Rendu le…"
def user_can_close(self, user):
return (not self.item.is_available()) and (self.user == user)
......@@ -163,6 +193,18 @@ class Loan(models.Model):
is_running.boolean = True
is_running.short_description = 'En cours ?'
def clean(self):
current_loan = self.item.get_current_loan()
if (
self.is_running()
and
current_loan
and
self.item.get_current_loan() != self
):
raise ValidationError(
"Il y a déjà un emprunt en cours sur cet objet")
class Meta:
verbose_name = 'prêt d’objet'
verbose_name_plural = 'prêts d’objets'
......
......@@ -22,24 +22,20 @@
<table id="member_loans" class="full-width">
<thead>
<tr>
<th>Type de matériel</th>
<th>Matériel prêté</th>
<th>Type de matériel</th>
<th>Date de prêt</th>
{% if view == 'old' %}<th>Date retour</th>{% endif %}
<th>Addr. MAC</th>
<th>Num. de série</th>
{% if view != 'old' %}<th>Actions</th>{% endif %}
</tr>
</thead>
<tbody>
{% for loan in loans %}
<tr>
<td>{{ loan.item.type }}</td>
<td>{{ loan.item }}</a></td>
<td>{{ loan.loan_date }}</td>
{% if view == 'old' %}<td>{{ loan.loan_date_end }}</td>{% endif %}
<td>{{ loan.item.mac_address|default:"n/a" }}</td>
<td>{{ loan.item.serial|default:"n/a" }}</td>
<td>{{ loan.item.type }}</td>
<td>{{ loan.loan_date|date:'j N Y' }}</td>
{% if view == 'old' %}<td>{{ loan.loan_date_end|date:'j N Y' }}</td>{% endif %}
{% if view != 'old' %}
<td class="actions">
<div class="button-group">
......
......@@ -95,9 +95,12 @@ class MaillingListAdmin(admin.ModelAdmin):
admin.site.register(MaillingList, MaillingListAdmin)
# Enrich the MemberAdmin with maillists-related information
_MemberAdmin = admin.site._registry[coin.members.admin.Member].__class__
class MemberAdmin(coin.members.admin.MemberAdmin):
inlines = coin.members.admin.MemberAdmin.inlines + [
class MemberAdmin(_MemberAdmin):
inlines = _MemberAdmin.inlines + [
MaillingListSubscriptionInline,
AddMaillingListSubscriptionInline,
]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment