cms_menus.py 6.68 KB
Newer Older
Patrick's avatar
Patrick committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# -*- coding: utf-8
from __future__ import unicode_literals

import datetime

from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from menus.base import Menu, NavigationNode
from menus.menu_pool import menu_pool

from const import *
from models import Permanence
from models import PermanenceBoard


class PermanenceMenu(Menu):
    def get_nodes(self, request):
19
        from apps import REPANIER_SETTINGS_PERMANENCES_NAME, REPANIER_SETTINGS_INVOICE, REPANIER_SETTINGS_DISPLAY_WHO_IS_WHO
Patrick's avatar
Patrick committed
20
        user = request.user
21
        if user.is_anonymous or user.is_staff:
Patrick's avatar
Patrick committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
            is_anonymous = True
        else:
            is_anonymous = False
        nodes = []
        master_id = 2
        node = NavigationNode(
            "%s" % REPANIER_SETTINGS_PERMANENCES_NAME,
            "/",
            id=master_id,
            visible=True
        )
        nodes.append(node)
        submenu_id = master_id

        separator = False
Patrick's avatar
Patrick committed
37
        permanence_board_set = PermanenceBoard.objects.filter(permanence__status__lte=PERMANENCE_WAIT_FOR_INVOICED).only(
Patrick's avatar
Patrick committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            "id").order_by('?')
        if permanence_board_set.exists():
            submenu_id += 1
            node = NavigationNode(
                _('Calendar of activities'),
                reverse('permanence_view'),
                id=submenu_id, parent_id=master_id,
                visible=True
            )
            nodes.append(node)
            separator = True

        displayed_permanence_counter = 0

        first_pass = True
        for permanence in Permanence.objects.filter(status=PERMANENCE_OPENED) \
                .only("id", "permanence_date", "with_delivery_point") \
                .order_by('permanence_date'):
            displayed_permanence_counter += 1
            if first_pass and separator:
                submenu_id = self.append_separator(nodes, master_id, submenu_id)
            first_pass = False
            separator = True
            submenu_id = self.append_permanence(is_anonymous, permanence, nodes, master_id, submenu_id)

        first_pass = True
        closed_separator = separator
Patrick's avatar
Patrick committed
65 66 67 68
        for permanence in Permanence.objects.filter(
                status__in=[PERMANENCE_CLOSED, PERMANENCE_SEND],
                master_permanence__isnull=True
        ).only("id", "permanence_date").order_by('-permanence_date'):
Patrick's avatar
Patrick committed
69 70 71 72
            displayed_permanence_counter += 1
            if first_pass and closed_separator:
                submenu_id = self.append_separator(nodes, master_id, submenu_id)
            first_pass = False
Patrick's avatar
Patrick committed
73
            # separator = True
Patrick's avatar
Patrick committed
74 75 76 77
            closed_separator = False
            submenu_id = self.append_permanence(is_anonymous, permanence, nodes, master_id, submenu_id)
            if displayed_permanence_counter > 4:
                break
Patrick's avatar
WIP  
Patrick committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        # if displayed_permanence_counter < 4:
        #     max_counter = 4 - displayed_permanence_counter
        #     for permanence in Permanence.objects.filter(status__in=[PERMANENCE_INVOICED, PERMANENCE_ARCHIVED]) \
        #             .only("id", "permanence_date") \
        #             .order_by('-permanence_date'):
        #         if permanence.permanence_date >= (
        #             timezone.now() - datetime.timedelta(weeks=LIMIT_DISPLAYED_PERMANENCE)).date():
        #             if first_pass and closed_separator:
        #                 submenu_id = self.append_separator(nodes, master_id, submenu_id)
        #             first_pass = False
        #             separator = True
        #             submenu_id = self.append_permanence(is_anonymous, permanence, nodes, master_id, submenu_id)
        #         max_counter -= 1
        #         if max_counter <= 0:
        #             break
Patrick's avatar
Patrick committed
93

94 95 96 97 98 99 100 101 102 103 104
        # if REPANIER_SETTINGS_INVOICE and not request.user.is_staff:
        #     if separator:
        #         submenu_id = self.append_separator(nodes, master_id, submenu_id)
        #     submenu_id += 1
        #     node = NavigationNode(
        #         '<span id="my_balance">%s</span>' % _('My balance'),
        #         reverse('customer_invoice_view', args=(0,)),
        #         id=submenu_id, parent_id=master_id,
        #         visible=True
        #     )
        #     nodes.append(node)
Patrick's avatar
Patrick committed
105 106 107 108 109 110

            # for node in nodes:
            #     logging.debug('Node before : %s' % node.get_menu_title())
            #     for attr in (x for x in dir(node) if not x.startswith('__')):
            #         logging.debug('%s => %s' % (attr, getattr(node, attr)))

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        # master_id = 3
        # node = NavigationNode(
        #     "%s" % _('Group'),
        #     "/",
        #     id=master_id,
        #     visible=True
        # )
        # nodes.append(node)
        # submenu_id = master_id + 1
        # node = NavigationNode(
        #     _('Send mail to coordinators'),
        #     reverse('send_mail_to_coordinators_view'),
        #     id=submenu_id, parent_id=master_id,
        #     visible=True
        # )
        # nodes.append(node)
        # submenu_id += 1
        # if REPANIER_SETTINGS_DISPLAY_WHO_IS_WHO:
        #     node = NavigationNode(
        #         _('Send mail to all members'),
        #         reverse('send_mail_to_all_members_view'),
        #         id=submenu_id, parent_id=master_id,
        #         visible=True
        #     )
        #     nodes.append(node)
        #     submenu_id += 1
        #     node = NavigationNode(
        #         _('Who is who'),
        #         reverse('who_is_who_view'),
        #         id=submenu_id, parent_id=master_id,
        #         visible=True
        #     )
        #     nodes.append(node)
        #     submenu_id += 1
        # node = NavigationNode(
        #     _('Me'),
        #     reverse('me_view'),
        #     id=submenu_id, parent_id=master_id,
        #     visible=True
        # )
        # nodes.append(node)
Patrick's avatar
Patrick committed
152 153 154 155

        return nodes

    def append_permanence(self, is_anonymous, permanence, nodes, master_id, submenu_id):
156
        path = reverse('order_view', args=(permanence.id,))
Patrick's avatar
Patrick committed
157
        if not is_anonymous and permanence.status > PERMANENCE_OPENED:
158
            path = path + "?is_basket=yes"
Patrick's avatar
Patrick committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        submenu_id += 1
        node = NavigationNode(
            permanence.get_permanence_customer_display(),
            path,
            id=submenu_id, parent_id=master_id,
            visible=True
        )
        nodes.append(node)
        return submenu_id

    def append_separator(self, nodes, master_id, submenu_id):
        submenu_id += 1
        node = NavigationNode(
            '------',
            "/",
            id=submenu_id, parent_id=master_id,
            visible=True
        )
        nodes.append(node)
        return submenu_id

menu_pool.register_menu(PermanenceMenu)