Browse Source

added models for hardware provisioning

Damien Nicolas 9 years ago
parent
commit
5440e20a82

+ 0 - 0
hardware_provisioning/__init__.py


+ 3 - 0
hardware_provisioning/admin.py

@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.

+ 39 - 0
hardware_provisioning/fields.py

@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import unicode_literals
+import re
+
+from django.utils.translation import ugettext_lazy as _
+from django.forms import fields
+from django.db import models
+
+MAC_RE = r'^([0-9a-fA-F]{2}([:-]?|$)){6}$'
+mac_re = re.compile(MAC_RE)
+
+
+class MACAddressFormField(fields.RegexField):
+    default_error_messages = {
+        'invalid': _(u'Enter a valid MAC address.'),
+    }
+
+    def __init__(self, *args, **kwargs):
+        super(MACAddressFormField, self).__init__(mac_re, *args, **kwargs)
+
+
+class MACAddressField(models.Field):
+    empty_strings_allowed = False
+
+    def __init__(self, *args, **kwargs):
+        kwargs['max_length'] = 17
+        super(MACAddressField, self).__init__(*args, **kwargs)
+
+    def get_internal_type(self):
+        return "CharField"
+
+    def formfield(self, **kwargs):
+        defaults = {'form_class': MACAddressFormField}
+        defaults.update(kwargs)
+        return super(MACAddressField, self).formfield(**defaults)
+
+    # def get_db_prep_value(self, value, *args, **kwargs):
+    #     return filter(lambda ch: ch not in ':-', value).upper()

+ 70 - 0
hardware_provisioning/migrations/0001_initial.py

@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+from django.conf import settings
+import hardware_provisioning.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Item',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('designation', models.CharField(max_length=100, verbose_name='d\xe9signation')),
+                ('mac_address', hardware_provisioning.fields.MACAddressField(max_length=17, null=True, verbose_name='addresse MAC', blank=True)),
+                ('buy_date', models.DateTimeField(verbose_name='date d\u2019achat')),
+                ('comment', models.TextField(null=True, verbose_name='commentaire', blank=True)),
+            ],
+            options={
+                'verbose_name': 'objet',
+            },
+            bases=(models.Model,),
+        ),
+        migrations.CreateModel(
+            name='ItemType',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('name', models.CharField(max_length=100, verbose_name='nom')),
+            ],
+            options={
+                'verbose_name': 'type d\u2019objet',
+                'verbose_name_plural': 'types d\u2019objet',
+            },
+            bases=(models.Model,),
+        ),
+        migrations.CreateModel(
+            name='Loan',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('loan_date', models.DateTimeField(verbose_name='date de pr\xeat')),
+                ('loan_date_end', models.DateTimeField(null=True, verbose_name='date de fin de pr\xeat', blank=True)),
+                ('location', models.CharField(max_length=100, null=True, verbose_name='emplacement', blank=True)),
+                ('item', models.ForeignKey(related_name='loans', verbose_name='objet', to='hardware_provisioning.Item')),
+                ('user', models.ForeignKey(related_name='loans', verbose_name='membre', to=settings.AUTH_USER_MODEL)),
+            ],
+            options={
+                'verbose_name': 'pr\xeat d\u2019objet',
+                'verbose_name_plural': 'pr\xeats d\u2019objets',
+            },
+            bases=(models.Model,),
+        ),
+        migrations.AddField(
+            model_name='item',
+            name='type',
+            field=models.ForeignKey(related_name='items', verbose_name='type de mat\xe9riel', to='hardware_provisioning.ItemType'),
+            preserve_default=True,
+        ),
+        migrations.AddField(
+            model_name='item',
+            name='user_in_charge',
+            field=models.ForeignKey(related_name='items', verbose_name='membre responsable', to=settings.AUTH_USER_MODEL),
+            preserve_default=True,
+        ),
+    ]

+ 0 - 0
hardware_provisioning/migrations/__init__.py


+ 74 - 0
hardware_provisioning/models.py

@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import unicode_literals
+from django.db import models
+from django.conf import settings
+from .fields import MACAddressField
+
+
+class ItemType(models.Model):
+    name = models.CharField(max_length=100, verbose_name='nom')
+
+    def __unicode__(self):
+        return self.name
+
+    class Meta:
+        verbose_name = 'type d’objet'
+        verbose_name_plural = 'types d’objet'
+
+
+class Item(models.Model):
+    type = models.ForeignKey(ItemType, verbose_name='type de matériel',
+                             related_name='items')
+    designation = models.CharField(max_length=100, verbose_name='désignation')
+    mac_address = MACAddressField(verbose_name='addresse MAC', blank=True,
+                                  null=True)
+    buy_date = models.DateTimeField(verbose_name='date d’achat')
+    user_in_charge = models.ForeignKey(settings.AUTH_USER_MODEL,
+                                       verbose_name='membre responsable',
+                                       related_name='items')
+    comment = models.TextField(verbose_name='commentaire', blank=True,
+                               null=True)
+
+    def __unicode__(self):
+        return self.designation
+
+    def get_current_loan(self):
+        """
+        Returns the current Loan for this Item, if exists, or None.
+        """
+        try:
+            return self.loans.get(loan_date_end__isnull=True)
+        except Loan.DoesNotExist:
+            return None
+
+    def is_available(self):
+        """
+        Returns the status of the Item. If a Loan without an end date exists,
+        returns False (else True).
+        """
+        if self.loans.filter(loan_date_end__isnull=True).count():
+            return False
+        return True
+
+    class Meta:
+        verbose_name = 'objet'
+
+
+class Loan(models.Model):
+    item = models.ForeignKey(Item, verbose_name='objet', related_name='loans')
+    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre',
+                             related_name='loans')
+    loan_date = models.DateTimeField(verbose_name='date de prêt')
+    loan_date_end = models.DateTimeField(verbose_name='date de fin de prêt',
+                                         null=True, blank=True)
+    location = models.CharField(max_length=100, verbose_name='emplacement',
+                                null=True, blank=True)
+
+    def __unicode__(self):
+        return 'prêt de {item} à {user}'.format(item=self.item,
+                                                user=self.user)
+
+    class Meta:
+        verbose_name = 'prêt d’objet'
+        verbose_name_plural = 'prêts d’objets'

+ 3 - 0
hardware_provisioning/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 3 - 0
hardware_provisioning/views.py

@@ -0,0 +1,3 @@
+from django.shortcuts import render
+
+# Create your views here.