rebelstuff

Keep track of your stuff
git clone https://git.ce9e.org/rebelstuff.git

commit
9106c9a62448909c773b66a28e7564d6a95a5087
parent
85eb082ebabf2d8a9205a8833f3c9ce72fa037b2
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-08-25 14:50
add price

Diffstat

M rebelstuff/admin.py 7 ++++++-
M rebelstuff/fixtures/sample_data.json 15 ++++++++++-----
A rebelstuff/migrations/0003_stuff_price.py 19 +++++++++++++++++++
M rebelstuff/models.py 14 ++++++++++++++
M settings/dev.py 2 ++

5 files changed, 51 insertions, 6 deletions


diff --git a/rebelstuff/admin.py b/rebelstuff/admin.py

@@ -22,14 +22,19 @@ class StuffAdmin(admin.ModelAdmin):
   22    22 class BookingItemInline(admin.TabularInline):
   23    23     model = models.BookingItem
   24    24     autocomplete_fields = ['stuff']
   -1    25     readonly_fields = ['price']
   -1    26 
   -1    27     def price(self, obj):
   -1    28         return obj.stuff.price
   25    29 
   26    30 
   27    31 class BookingAdmin(admin.ModelAdmin):
   28    -1     list_display = ['name', 'start', 'end', 'status']
   -1    32     list_display = ['name', 'start', 'end', 'status', 'price']
   29    33     date_hierarchy = 'start'
   30    34     search_fields = ['name']
   31    35     list_filter = ['status']
   32    36     inlines = [BookingItemInline]
   -1    37     readonly_fields = ['price']
   33    38 
   34    39 
   35    40 site = Site()

diff --git a/rebelstuff/fixtures/sample_data.json b/rebelstuff/fixtures/sample_data.json

@@ -5,7 +5,8 @@
    5     5     "fields": {
    6     6         "name": "Micros",
    7     7         "description": "",
    8    -1         "amount": 10
   -1     8         "amount": 10,
   -1     9         "price": 2
    9    10     }
   10    11 },
   11    12 {
@@ -14,7 +15,8 @@
   14    15     "fields": {
   15    16         "name": "Microst\u00e4nder",
   16    17         "description": "",
   17    -1         "amount": 8
   -1    18         "amount": 8,
   -1    19         "price": 1
   18    20     }
   19    21 },
   20    22 {
@@ -23,7 +25,8 @@
   23    25     "fields": {
   24    26         "name": "Boxen",
   25    27         "description": "",
   26    -1         "amount": 5
   -1    28         "amount": 5,
   -1    29         "price": 20
   27    30     }
   28    31 },
   29    32 {
@@ -32,7 +35,8 @@
   32    35     "fields": {
   33    36         "name": "Steckdosen",
   34    37         "description": "",
   35    -1         "amount": 10
   -1    38         "amount": 10,
   -1    39         "price": 1
   36    40     }
   37    41 },
   38    42 {
@@ -41,7 +45,8 @@
   41    45     "fields": {
   42    46         "name": "Mischpult",
   43    47         "description": "",
   44    -1         "amount": 1
   -1    48         "amount": 1,
   -1    49         "price": 25
   45    50     }
   46    51 },
   47    52 {

diff --git a/rebelstuff/migrations/0003_stuff_price.py b/rebelstuff/migrations/0003_stuff_price.py

@@ -0,0 +1,19 @@
   -1     1 # Generated by Django 2.2.4 on 2019-08-25 14:36
   -1     2 
   -1     3 from django.db import migrations, models
   -1     4 
   -1     5 
   -1     6 class Migration(migrations.Migration):
   -1     7 
   -1     8     dependencies = [
   -1     9         ('rebelstuff', '0002_booking_status'),
   -1    10     ]
   -1    11 
   -1    12     operations = [
   -1    13         migrations.AddField(
   -1    14             model_name='stuff',
   -1    15             name='price',
   -1    16             field=models.PositiveIntegerField(default=0, verbose_name='Price'),
   -1    17             preserve_default=False,
   -1    18         ),
   -1    19     ]

diff --git a/rebelstuff/models.py b/rebelstuff/models.py

@@ -1,7 +1,9 @@
    1     1 import datetime
    2     2 
   -1     3 from django.conf import settings
    3     4 from django.core.exceptions import ValidationError
    4     5 from django.db import models
   -1     6 from django.utils.functional import cached_property
    5     7 from django.utils.translation import gettext_lazy as _
    6     8 
    7     9 
@@ -9,6 +11,7 @@ class Stuff(models.Model):
    9    11     name = models.CharField(_('Name'), max_length=64, unique=True)
   10    12     description = models.TextField(_('Description'), blank=True)
   11    13     amount = models.PositiveIntegerField(_('Amount'))
   -1    14     price = models.PositiveIntegerField(_('Price'))
   12    15 
   13    16     def __str__(self):
   14    17         return self.name
@@ -65,6 +68,17 @@ class Booking(models.Model):
   65    68                 'end': _('Must be after start.'),
   66    69             })
   67    70 
   -1    71     @cached_property
   -1    72     def price(self):
   -1    73         if not self.id:
   -1    74             return None
   -1    75 
   -1    76         agg = self.bookingitem_set.aggregate(daily=models.Sum(
   -1    77             models.F('amount') * models.F('stuff__price')
   -1    78         ))
   -1    79         duration = self.end - self.start
   -1    80         return settings.PRICE_BASE + agg['daily'] * duration.days
   -1    81 
   68    82     def iter_days(self):
   69    83         day = self.start
   70    84         while day <= self.end:

diff --git a/settings/dev.py b/settings/dev.py

@@ -23,3 +23,5 @@ DATABASES = {
   23    23 }
   24    24 
   25    25 FEED_TOKEN = 'e7XATeGqQodsoyFbYKAEUZvUjsLMV7gE'
   -1    26 
   -1    27 PRICE_BASE = 10