apps.py 12.5 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
import sys
Patrick's avatar
Patrick committed
7 8
from django.apps import AppConfig
from django.conf import settings
Patrick's avatar
Patrick committed
9 10
from django.db import connection
from django.utils import translation
Patrick's avatar
Patrick committed
11 12
from django.utils.translation import ugettext_lazy as _

13 14 15
REPANIER_SETTINGS_CONFIG = None
REPANIER_SETTINGS_TEST_MODE = None
REPANIER_SETTINGS_GROUP_NAME = None
Patrick's avatar
Patrick committed
16 17 18
REPANIER_SETTINGS_PERMANENCE_NAME = _("Permanence")
REPANIER_SETTINGS_PERMANENCES_NAME = _("Permanences")
REPANIER_SETTINGS_PERMANENCE_ON_NAME = _("Permanence on ")
19 20 21
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
22
REPANIER_SETTINGS_SEND_CANCEL_ORDER_MAIL_TO_CUSTOMER = None
23 24 25 26 27 28
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
29 30
REPANIER_SETTINGS_INVOICE = None
REPANIER_SETTINGS_CLOSE_WO_SENDING = None
31 32
REPANIER_SETTINGS_DISPLAY_ANONYMOUS_ORDER_FORM = None
REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM = None
33
REPANIER_SETTINGS_DISPLAY_WHO_IS_WHO = None
34 35 36
REPANIER_SETTINGS_BANK_ACCOUNT = None
REPANIER_SETTINGS_VAT_ID = None
REPANIER_SETTINGS_PAGE_BREAK_ON_CUSTOMER_CHECK = None
Patrick's avatar
Patrick committed
37 38 39 40 41 42 43 44 45 46
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
47
DJANGO_IS_MIGRATION_RUNNING = 'makemigrations' in sys.argv or 'migrate' in sys.argv
Patrick's avatar
Patrick committed
48

Patrick's avatar
Patrick committed
49

Patrick Colmant's avatar
Patrick Colmant committed
50
class RepanierSettings(AppConfig):
Patrick's avatar
Patrick committed
51 52 53 54
    name = 'repanier'
    verbose_name = "Repanier"

    def ready(self):
Patrick's avatar
Patrick committed
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)
70
        from models import Configuration, LUT_DepartmentForCustomer, Staff
Patrick's avatar
Patrick committed
71 72
        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
73
        try:
74
            # Create if needed and load RepanierSettings var when performing config.save()
Patrick's avatar
Patrick committed
75
            translation.activate(settings.LANGUAGE_CODE)
Patrick's avatar
Patrick committed
76 77 78 79 80 81 82 83 84 85 86 87
            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",
Patrick's avatar
Patrick committed
88
                    currency=CURRENCY_EUR
Patrick's avatar
Patrick committed
89 90
                )
            config.save()
91
            Staff.objects.rebuild()
92
            # Create groups with correct rights
Patrick's avatar
Patrick committed
93
            order_group = Group.objects.filter(name=ORDER_GROUP).only('id').order_by('?').first()
94 95
            if order_group is None:
                order_group = Group.objects.create(name=ORDER_GROUP)
Patrick's avatar
Patrick committed
96
            invoice_group = Group.objects.filter(name=INVOICE_GROUP).only('id').order_by('?').first()
97 98
            if invoice_group is None:
                invoice_group = Group.objects.create(name=INVOICE_GROUP)
Patrick's avatar
Patrick committed
99 100 101 102
            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()
103 104 105 106 107
            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
108
                    # 'aldryn_bootstrap3',
109
                    'auth',
Patrick's avatar
Patrick committed
110
                    'cascade_dummy',
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                    '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
127
            ).only('id').order_by('?')
128 129
            permissions = Permission.objects.filter(
                content_type__in=content_types
Patrick's avatar
Patrick committed
130 131 132 133 134 135 136 137 138
            ).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)
139 140
            content_types = ContentType.objects.exclude(
                app_label__in=[
Patrick's avatar
Patrick committed
141
                    'repanier',
142 143 144 145 146 147 148 149 150
                    'admin',
                    'auth',
                    'contenttypes',
                    'menus',
                    'repanier',
                    'reversion',
                    'sessions',
                    'sites',
                ]
Patrick's avatar
Patrick committed
151
            ).only('id').order_by('?')
152 153
            permissions = Permission.objects.filter(
                content_type__in=content_types
Patrick's avatar
Patrick committed
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 213 214
            ).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"))
215

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