Completed
Push — master ( 46baa4...8c7969 )
by Bart
01:12
created

Bottle.__str__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
1
from whisky.models import Whisky
2
from collection.models import Collection
3
from userprofile.models import UserProfile
4
from base.settings import APP_DIR, STATIC_URL
5
6
from django.db import models
7
8
import os.path 
9
from datetime import datetime
10
11
12
class Bottle(models.Model):
13
    whisky = models.ForeignKey(Whisky)
14
    collection = models.ForeignKey(Collection, null=True, blank=True)
15
    volume = models.FloatField('volume', default=700.0)
16
    volumeConsumedInitial = models.FloatField('initially consumed', default=0.0)
17
    empty = models.BooleanField(default=False)
18
    buyer = models.ForeignKey(UserProfile, default=1)
19
    price = models.FloatField('price', default=0.0)
20
    donation = models.BooleanField(default=False)
21
    date = models.DateTimeField(default=datetime.now, editable=True, blank=True)
22
23
    def get_actual_volume(self):
24
        from glass.models import Glass
25
        drinks = Glass.objects.filter(bottle__id=self.id)
26
        volume_drunk = 0.0
27
        for drink in drinks:
28
            volume_drunk += drink.volume
29
        actual_volume = self.volume - self.volumeConsumedInitial - volume_drunk
30
        return actual_volume
31
32
    @staticmethod
33
    def get_actual_volume_all():
34
        bottles = Bottle.objects.order_by("volume")
35
        total_instock_liter = 0.0
36
        for bottle in bottles:
37
            if not bottle.empty:
38
                total_instock_liter += bottle.get_actual_volume() / 1000.0
39
        return total_instock_liter
40
41
    @staticmethod
42
    def get_actual_value_all():
43
        bottles = Bottle.objects.order_by("volume")
44
        total_value_stock = 0.0
45
        for bottle in bottles:
46
            if not bottle.empty:
47
                total_value_stock += bottle.get_actual_volume() / bottle.volume * bottle.price
48
        return total_value_stock
49
50
    @staticmethod
51
    def get_average_percentage_not_empty():
52
        bottles = Bottle.objects.filter(empty=False)
53
        add_bottles_info(bottles)
54
        average_percentage_left = 0.0
55
        for bottle in bottles:
56
            average_percentage_left += bottle.percentageLeft_int
57
        if len(bottles) != 0:
58
            average_percentage_left /= float(len(bottles))
59
        return average_percentage_left
60
61
    def __str__(self):
62
        name = str(self.whisky)
63
        return name
64
65
    class Meta:
66
        ordering = ['whisky']
67
68
69
def add_bottle_info(bottle):
70
    bottle.distillery = bottle.whisky.distillery
71
    bottle.volume_liters = '%.1f' % (bottle.volume / 1000.0)
72
    bottle.volumeActual = bottle.get_actual_volume()
73
    bottle.age_int = int(bottle.whisky.age)
74
    bottle.alcoholPercentage_int = int(bottle.whisky.alcoholPercentage)
75
76
    percentage_left = 0.0
77
    percentage_gone = 100.0
78
    status_meter_width = 0.0
79
80
    if bottle.volume != 0.0:
81
        percentage_left = bottle.volumeActual/bottle.volume * 100.0
82
        percentage_gone = 100 - (bottle.volumeActual/bottle.volume * 100.0)
83
        status_meter_width = bottle.volumeActual/bottle.volume * 75.0 + 4
84
85
    if percentage_left < 0.0:
86
        status_meter_width = 0.0
87
88
    bottle.percentageLeft_int = percentage_left
89
    bottle.percentageLeft = '%.0f' % percentage_left
90
    bottle.percentageGone = '%.0f' % percentage_gone
91
    bottle.statusMeterWidth = '%.0f' % status_meter_width
92
    imagename = APP_DIR + 'static/images/bottles/' + str(bottle.whisky.distillery) + str(bottle.age_int) + '.jpg'
93
    if os.path.isfile(imagename):
94
        bottle.imagename = STATIC_URL + 'images/bottles/' + str(bottle.whisky.distillery) + str(bottle.age_int) + '.jpg'
95
    else:
96
        bottle.imagename = STATIC_URL + 'images/bottles/unknown.jpg'
97
    return bottle
98
99
100
def add_bottles_info(bottles):
101
    for bottle in bottles:
102
        add_bottle_info(bottle)
103
    return bottles
104