apps.py 12.4 KB
Newer Older
Patrick's avatar
Patrick committed
1 2
# -*- coding: utf-8
from __future__ import unicode_literals
Patrick's avatar
Patrick committed
3 4 5

import time

Patrick's avatar
Patrick committed
6 7
from django.apps import AppConfig
from django.conf import settings
Patrick's avatar
Patrick committed
8 9
from django.db import connection
from django.utils import translation
Patrick's avatar
Patrick committed
10 11
from django.utils.translation import ugettext_lazy as _

12 13 14 15 16 17 18 19 20
REPANIER_SETTINGS_CONFIG = None
REPANIER_SETTINGS_TEST_MODE = None
REPANIER_SETTINGS_GROUP_NAME = None
REPANIER_SETTINGS_PERMANENCE_NAME = None
REPANIER_SETTINGS_PERMANENCES_NAME = None
REPANIER_SETTINGS_PERMANENCE_ON_NAME = None
REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION = None
REPANIER_SETTINGS_SEND_OPENING_MAIL_TO_CUSTOMER = None
REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_CUSTOMER = None
Patrick's avatar
Patrick committed
21
REPANIER_SETTINGS_SEND_CANCEL_ORDER_MAIL_TO_CUSTOMER = None
22 23 24 25 26 27
REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_CUSTOMER = None
REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_PRODUCER = None
REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_PRODUCER = None
REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD = None
REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_CUSTOMER = None
REPANIER_SETTINGS_SEND_INVOICE_MAIL_TO_PRODUCER = None
Patrick's avatar
Patrick committed
28 29
REPANIER_SETTINGS_INVOICE = None
REPANIER_SETTINGS_CLOSE_WO_SENDING = None
30 31 32 33 34
REPANIER_SETTINGS_DISPLAY_ANONYMOUS_ORDER_FORM = None
REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM = None
REPANIER_SETTINGS_BANK_ACCOUNT = None
REPANIER_SETTINGS_VAT_ID = None
REPANIER_SETTINGS_PAGE_BREAK_ON_CUSTOMER_CHECK = None
Patrick's avatar
Patrick committed
35 36 37 38 39 40 41 42 43 44 45
REPANIER_SETTINGS_SMS_GATEWAY_MAIL = None
REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS = None
REPANIER_SETTINGS_MEMBERSHIP_FEE = None
REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION = None
REPANIER_SETTINGS_CURRENCY_DISPLAY = None
REPANIER_SETTINGS_AFTER_AMOUNT = None
REPANIER_SETTINGS_CURRENCY_XLSX = None
REPANIER_SETTINGS_HOME_SITE = None
REPANIER_SETTINGS_TRANSPORT = None
REPANIER_SETTINGS_MIN_TRANSPORT = None

Patrick's avatar
Patrick committed
46

Patrick Colmant's avatar
Patrick Colmant committed
47
class RepanierSettings(AppConfig):
Patrick's avatar
Patrick committed
48 49 50 51
    name = 'repanier'
    verbose_name = "Repanier"

    def ready(self):
Patrick's avatar
Patrick committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        # Imports are inside the function because its point is to avoid importing
        # the models when django.contrib."MODELS" isn't installed.
        from django.contrib.auth.models import Group, Permission
        from django.contrib.contenttypes.models import ContentType
        from django.contrib.sites.models import Site
        # If PostgreSQL service is not started the const may not be set
        # Django doesn't complain
        # This happens when the server starts at power up
        # first launching uwsgi before PostgreSQL
        db_started = False
        while not db_started:
            try:
                db_started = connection.cursor() is not None
            except:
                time.sleep(1)
        from models import Configuration, LUT_DepartmentForCustomer
        from const import DECIMAL_ONE, PERMANENCE_NAME_PERMANENCE, EMPTY_STRING, CURRENCY_EUR, ORDER_GROUP, \
            INVOICE_GROUP, CONTRIBUTOR_GROUP, COORDINATION_GROUP, WEBMASTER_GROUP
Patrick's avatar
Patrick committed
70
        try:
71
            # Create if needed and load RepanierSettings var when performing config.save()
Patrick's avatar
Patrick committed
72
            translation.activate(settings.LANGUAGE_CODE)
Patrick's avatar
Patrick committed
73 74 75 76 77 78 79 80 81 82 83 84 85
            config = Configuration.objects.filter(id=DECIMAL_ONE).first()
            if config is None:
                group_name = settings.ALLOWED_HOSTS[0]
                site = Site.objects.get_current()
                if site is not None:
                    site.name = group_name
                    site.domain = group_name
                    site.save()
                config = Configuration.objects.create(
                    group_name=group_name,
                    name=PERMANENCE_NAME_PERMANENCE,
                    bank_account="BE99 9999 9999 9999",
                    vat_id=EMPTY_STRING,
Patrick's avatar
Patrick committed
86 87
                    sms_gateway_mail=EMPTY_STRING,
                    currency=CURRENCY_EUR
Patrick's avatar
Patrick committed
88 89
                )
            config.save()
90
            # Create groups with correct rights
Patrick's avatar
Patrick committed
91
            order_group = Group.objects.filter(name=ORDER_GROUP).only('id').order_by('?').first()
92 93
            if order_group is None:
                order_group = Group.objects.create(name=ORDER_GROUP)
Patrick's avatar
Patrick committed
94
            invoice_group = Group.objects.filter(name=INVOICE_GROUP).only('id').order_by('?').first()
95 96
            if invoice_group is None:
                invoice_group = Group.objects.create(name=INVOICE_GROUP)
Patrick's avatar
Patrick committed
97 98 99 100
            contributor_group = Group.objects.filter(name=CONTRIBUTOR_GROUP).only('id').order_by('?').first()
            if contributor_group is None:
                contributor_group = Group.objects.create(name=CONTRIBUTOR_GROUP)
            coordination_group = Group.objects.filter(name=COORDINATION_GROUP).only('id').order_by('?').first()
101 102 103 104 105
            if coordination_group is None:
                coordination_group = Group.objects.create(name=COORDINATION_GROUP)
            content_types = ContentType.objects.exclude(
                app_label__in=[
                    'admin',
Patrick's avatar
Patrick committed
106
                    # 'aldryn_bootstrap3',
107
                    'auth',
Patrick's avatar
Patrick committed
108
                    'cascade_dummy',
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                    'cms',
                    'cmsplugin_cascade',
                    'cmsplugin_filer_file',
                    'cmsplugin_filer_folder',
                    'cmsplugin_filer_image',
                    'cmsplugin_filer_link',
                    'cmsplugin_filer_video',
                    'contenttypes',
                    'djangocms_text_ckeditor',
                    'easy_thumbnails',
                    'filer'
                    'menus',
                    'reversion',
                    'sessions',
                    'sites',
                ]
Patrick's avatar
Patrick committed
125
            ).only('id').order_by('?')
126 127
            permissions = Permission.objects.filter(
                content_type__in=content_types
Patrick's avatar
Patrick committed
128 129 130 131 132 133 134 135 136
            ).only('id').order_by('?')
            order_group.permissions.set(permissions)
            invoice_group.permissions.set(permissions)
            coordination_group.permissions.set(permissions)
            contributor_group.permissions.set(permissions)
            # WEBMASTER
            webmaster_group = Group.objects.filter(name=WEBMASTER_GROUP).only('id').order_by('?').first()
            if webmaster_group is None:
                webmaster_group = Group.objects.create(name=WEBMASTER_GROUP)
137 138
            content_types = ContentType.objects.exclude(
                app_label__in=[
Patrick's avatar
Patrick committed
139
                    'repanier',
140 141 142 143 144 145 146 147 148
                    'admin',
                    'auth',
                    'contenttypes',
                    'menus',
                    'repanier',
                    'reversion',
                    'sessions',
                    'sites',
                ]
Patrick's avatar
Patrick committed
149
            ).only('id').order_by('?')
150 151
            permissions = Permission.objects.filter(
                content_type__in=content_types
Patrick's avatar
Patrick committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            ).only('id').order_by('?')
            webmaster_group.permissions.set(permissions)
            if LUT_DepartmentForCustomer.objects.count() == 0:
                # Generate a template of LUT_DepartmentForCustomer
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Vegetables"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Basket of vegetables"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Salads"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Tomatoes"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Potatoes"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Greens"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Cabbage"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Fruits"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Basket of fruits"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Apples"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Pears"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Plums"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Bakery"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Flour"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Bread"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Pastry"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Butchery"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Delicatessen"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Chicken"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Pork"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Beef"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Beef and pork"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Veal"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Lamb"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Grocery"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Takeaway"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Pasta"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Chocolates"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Oils"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Eggs"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Jams"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Cookies"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Creamery"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Dairy"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Cow cheese"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Goat cheese"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Sheep cheese"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Mixed cheese"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Icecream"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Cup of icecream"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Icecream per liter"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Icecream in frisco"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Icecream cake"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Sorbet"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Cup of sorbet"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Sorbet per liter"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Drink"))
                LUT_DepartmentForCustomer.objects.create(short_name=_("Juices"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Coffees"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Teas"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Herbal teas"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Wines"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Aperitifs"), parent=parent)
                LUT_DepartmentForCustomer.objects.create(short_name=_("Liqueurs"), parent=parent)
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Hygiene"))
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Deposit"))
                parent = LUT_DepartmentForCustomer.objects.create(short_name=_("Subscription"))
213

Patrick's avatar
Patrick committed
214 215
        except Exception as error_str:
            print("##################################")
Patrick's avatar
Patrick committed
216
            print(error_str)
Patrick's avatar
Patrick committed
217 218
            print("##################################")
            other = _("Other qty")