GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0fa676...0b5929 )
by Jason
01:10
created

PriceModifierModule.get_menu_entries()   A

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 11
rs 9.4286
1
# -*- coding: utf-8 -*-
2
import datetime
3
4
import six
5
from django.db.models import Q
6
from django.utils.translation import ugettext_lazy as _
7
8
from reservable_pricing.models import PeriodPriceModifier
9
from reservations.utils import get_start_and_end_from_request
10
from shoop.admin.base import AdminModule, MenuEntry
11
from shoop.core.models import ShopProduct
12
from shoop.core.pricing import PriceInfo, PricingContext, PricingModule
13
14
15
class ReservablePricingContext(PricingContext):
16
    REQUIRED_VALUES = ("start_date", "end_date", "shop")
17
    shop = None
18
19
20
class ReservablePricingModule(PricingModule):
21
    identifier = "reservable_pricing"
22
    name = _("Reservable Pricing")
23
24
    pricing_context_class = ReservablePricingContext
25
26
    def get_context_from_request(self, request):
27
        start_date, end_date = get_start_and_end_from_request(request)
28
29
        return self.pricing_context_class(
30
            shop=request.shop,
31
            start_date=start_date,
32
            end_date=end_date,
33
        )
34
35
    def get_price_info(self, context, product, quantity=1):
36
        shop = context.shop
37
38
        if isinstance(product, six.integer_types):
39
            product_id = product
40
            shop_product = ShopProduct.objects.get(product_id=product_id, shop=shop)
41
        else:
42
            shop_product = product.get_shop_instance(shop)
43
            product_id = product.pk
44
45
        base_price = (shop_product.default_price_value or 0) * quantity
46
        modifiers_price = self.get_modifiers_price(product_id, quantity, context.start_date, context.end_date)
47
        total_price = base_price + modifiers_price
48
49
        return PriceInfo(
50
            price=shop.create_price(total_price),
51
            base_price=shop.create_price(total_price),
52
            quantity=quantity,
53
        )
54
55
    @staticmethod
56
    def get_modifiers_price(product_id, quantity, start_date, end_date):
57
        """Get amount to add to base price from period modifiers."""
58
        modifiers_price = 0
59
        if start_date and end_date:
60
            # Get any period modifiers that could affect this period
61
            date_filters = Q(start_date__lte=start_date, end_date__gte=start_date) | \
62
                           Q(start_date__lte=end_date, end_date__gte=end_date)
63
            modifiers = PeriodPriceModifier.objects.filter(
0 ignored issues
show
Bug introduced by
The Class PeriodPriceModifier does not seem to have a member named objects.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
64
                modifier__gt=0, product=product_id
65
            ).filter(date_filters)
66
            # Add to the total days * modifier value
67
            for modifier in modifiers:
68
                start_from = max(start_date, modifier.start_date)
69
                # We add +1 day to end because end date is always the *next day*, ie the day guest leaves
70
                end_on = min(end_date, modifier.end_date + datetime.timedelta(days=1))
71
                days = min((end_on - start_from).days, quantity)
72
                modifiers_price += days * modifier.modifier
73
        return modifiers_price
74
75
76
class PriceModifierModule(AdminModule):
77
    name = _("Price modifiers")
78
    category = name
79
80
    def get_menu_entries(self, request):
81
        return [
82
            MenuEntry(
83
                text=_("Period"), icon="fa fa-money",
84
                url="reservable_pricing:modifiers.list",
85
                category=self.category
86
            ),
87
            MenuEntry(
88
                text=_("Day of Week"), icon="fa fa-money",
89
                url="reservable_pricing:modifiers.list",
90
                category=self.category
91
            ),
92
        ]
93