apps.py 13 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
from django.db import connection
Patrick's avatar
Patrick committed
10
from django.db.models import F
Patrick's avatar
Patrick committed
11
from django.utils import translation
Patrick's avatar
Patrick committed
12 13
from django.utils.translation import ugettext_lazy as _

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

Patrick's avatar
Patrick committed
50

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

    def ready(self):
Patrick's avatar
Patrick committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        # 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)
Patrick's avatar
WIP  
Patrick committed
71
        from models import Configuration, LUT_DepartmentForCustomer, Staff, Purchase, CustomerInvoice
Patrick's avatar
Patrick committed
72
        from const import DECIMAL_ONE, PERMANENCE_NAME_PERMANENCE, CURRENCY_EUR, ORDER_GROUP, \
Patrick's avatar
Patrick committed
73
            INVOICE_GROUP, CONTRIBUTOR_GROUP, COORDINATION_GROUP, WEBMASTER_GROUP
Patrick's avatar
Patrick committed
74
        try:
75
            # Create if needed and load RepanierSettings var when performing config.save()
Patrick's avatar
Patrick committed
76
            translation.activate(settings.LANGUAGE_CODE)
Patrick's avatar
Patrick committed
77 78 79 80 81 82 83 84 85 86 87 88
            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
89
                    currency=CURRENCY_EUR
Patrick's avatar
Patrick committed
90 91
                )
            config.save()
Patrick's avatar
Patrick committed
92 93 94
            # Purchase.objects.filter(customer_charged__isnull=True).update(
            #     customer_charged=F('customer_invoice__customer_charged')
            # )
Patrick's avatar
WIP  
Patrick committed
95 96 97 98
            # for purchase in Purchase.objects.filter(
            #         customer_charged__isnull=True).select_related("customer_invoice").order_by('?'):
            #     purchase.customer_charged = purchase.customer_invoice.customer_charged
            #     purchase.save(update_fields=["customer_charged",])
99
            Staff.objects.rebuild()
100
            # Create groups with correct rights
Patrick's avatar
Patrick committed
101
            order_group = Group.objects.filter(name=ORDER_GROUP).only('id').order_by('?').first()
102 103
            if order_group is None:
                order_group = Group.objects.create(name=ORDER_GROUP)
Patrick's avatar
Patrick committed
104
            invoice_group = Group.objects.filter(name=INVOICE_GROUP).only('id').order_by('?').first()
105 106
            if invoice_group is None:
                invoice_group = Group.objects.create(name=INVOICE_GROUP)
Patrick's avatar
Patrick committed
107 108 109 110
            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()
111 112 113 114 115
            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
116
                    # 'aldryn_bootstrap3',
117
                    'auth',
Patrick's avatar
Patrick committed
118
                    'cascade_dummy',
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
                    '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
135
            ).only('id').order_by('?')
136 137
            permissions = Permission.objects.filter(
                content_type__in=content_types
Patrick's avatar
Patrick committed
138 139 140 141 142 143 144 145 146
            ).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)
147 148
            content_types = ContentType.objects.exclude(
                app_label__in=[
Patrick's avatar
Patrick committed
149
                    'repanier',
150 151 152 153 154 155 156 157 158
                    'admin',
                    'auth',
                    'contenttypes',
                    'menus',
                    'repanier',
                    'reversion',
                    'sessions',
                    'sites',
                ]
Patrick's avatar
Patrick committed
159
            ).only('id').order_by('?')
160 161
            permissions = Permission.objects.filter(
                content_type__in=content_types
Patrick's avatar
Patrick committed
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 215 216 217 218 219 220 221 222
            ).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"))
223

Patrick's avatar
Patrick committed
224 225
        except Exception as error_str:
            print("##################################")
Patrick's avatar
Patrick committed
226
            print(error_str)
Patrick's avatar
Patrick committed
227 228
            print("##################################")
            other = _("Other qty")