Browse Source

Add admin views to models

Jocelyn Delande 9 years ago
parent
commit
0d9a50669e
4 changed files with 65 additions and 30 deletions
  1. 26 1
      costs/admin.py
  2. 14 4
      costs/models.py
  3. 22 4
      costs/templates/costs/resources_list.html
  4. 3 21
      costs/templates/costs/services_list.html

+ 26 - 1
costs/admin.py

@@ -1,3 +1,28 @@
 from django.contrib import admin
 
-# Register your models here.
+from .models import Cost, Good, Service, CostUse, GoodUse
+
+
+class GoodUseInline(admin.TabularInline):
+    model = GoodUse
+    extra = 1
+
+
+class CostUseInline(admin.TabularInline):
+    model = CostUse
+    extra = 1
+
+
+@admin.register(Cost)
+class CostAdmin(admin.ModelAdmin):
+    list_display = ('name', 'price')
+
+
+@admin.register(Good)
+class GoodAdmin(admin.ModelAdmin):
+    list_display = ('name', 'price', 'provisioning_duration')
+
+
+@admin.register(Service)
+class ServiceAdmin(admin.ModelAdmin):
+    inlines = (CostUseInline, GoodUseInline)

+ 14 - 4
costs/models.py

@@ -9,6 +9,9 @@ class AbstractItem(models.Model):
     name = models.CharField(max_length=130)
     description = models.TextField(blank=True)
 
+    def __str__(self):
+        return self.name
+
     class Meta:
         abstract = True
 
@@ -16,7 +19,10 @@ class AbstractItem(models.Model):
 class Cost(AbstractItem):
     """ A monthtly cost we have to pay
     """
-    price = models.PositiveIntegerField()
+    price = models.PositiveIntegerField(help_text="Coût mensuel")
+
+    class Meta:
+        verbose_name = 'Coût'
 
     def used(self):
         sharing_costs = CostUse.objects.filter(resource=self)
@@ -33,6 +39,9 @@ class Good(AbstractItem):
     provisioning_duration = models.DurationField(
         choices=settings.PROVISIONING_DURATIONS)
 
+    class Meta:
+        verbose_name = 'Bien'
+
 
 class AbstractUse(models.Model):
     share = models.FloatField(validators=[less_than_one])
@@ -42,9 +51,10 @@ class AbstractUse(models.Model):
         abstract = True
 
     def clean(self):
-        if (self.resource.used() + self.share) > 1:
-            raise ValidationError(
-                "Cannot use more than 100% of {})".format(self.resource))
+        if hasattr(self, 'resource'):
+            if (self.resource.used() + self.share) > 1:
+                raise ValidationError(
+                    "Cannot use more than 100% of {})".format(self.resource))
 
 
 class CostUse(AbstractUse):

+ 22 - 4
costs/templates/costs/resources_list.html

@@ -1,13 +1,31 @@
-{% if services.count > 0 %}
+<h1>Coûts récurents & matériel amorti</h1>
 
+{% if costs.count > 0 %}
+
+<h2>Coûts récurents</h2>
+<ul>
+  {% for i in costs %}
+  <li>{{ i.name }}</li>
+  {% endfor %}
+</ul>
+
+{% else %}
+
+<p>Pas de coûts récurents définis pour l'instant</p>
+
+{% endif %}
+
+{% if goods.count > 0 %}
+
+<h2>Matériel amorti</h2>
 <ul>
-  {% for i in services %}
+  {% for i in goods %}
   <li>{{ i.name }}</li>
   {% endfor %}
 </ul>
 
 {% else %}
 
-<p>Pas de services définis pour l'instant</p>
+<p>Pas de biens définis pour l'instant</p>
 
-{% endif %}
+{% endif %}

+ 3 - 21
costs/templates/costs/services_list.html

@@ -1,31 +1,13 @@
-<h1>Coûts récurents & matériel amorti</h1>
+{% if services.count > 0 %}
 
-{% if costs.count > 0 %}
-
-<h2>Coûts récurents</h2>
-<ul>
-  {% for i in costs %}
-  <li>{{ i.name }}</li>
-  {% endfor %}
-</ul>
-
-{% else %}
-
-<p>Pas de coûts récurents définis pour l'instant</p>
-
-{% endif %}
-
-{% if goods.count > 0 %}
-
-<h2>Matériel amorti</h2>
 <ul>
-  {% for i in goods %}
+  {% for i in services %}
   <li>{{ i.name }}</li>
   {% endfor %}
 </ul>
 
 {% else %}
 
-<p>Pas de biens définis pour l'instant</p>
+<p>Pas de services définis pour l'instant</p>
 
 {% endif %}