apps.py 5.89 KB
Newer Older
Patrick's avatar
Patrick committed
1 2 3 4
# -*- coding: utf-8
from __future__ import unicode_literals
from django.apps import AppConfig
from django.conf import settings
5
from django.contrib.auth.models import Group, Permission
Patrick's avatar
Patrick committed
6 7 8
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from const import *
9
from django.contrib.contenttypes.models import ContentType
Patrick's avatar
Patrick committed
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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
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
REPANIER_SETTINGS_INVOICE= None
REPANIER_SETTINGS_DISPLAY_ANONYMOUS_ORDER_FORM = None
REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM = None
REPANIER_SETTINGS_BANK_ACCOUNT = None
REPANIER_SETTINGS_DELIVERY_POINT = None
REPANIER_SETTINGS_DISPLAY_VAT = None
REPANIER_SETTINGS_VAT_ID = None
REPANIER_SETTINGS_PAGE_BREAK_ON_CUSTOMER_CHECK = None
Patrick's avatar
Patrick committed
34

Patrick Colmant's avatar
Patrick Colmant committed
35
class RepanierSettings(AppConfig):
Patrick's avatar
Patrick committed
36 37 38 39 40 41
    name = 'repanier'
    verbose_name = "Repanier"

    def ready(self):
        from models import Configuration
        try:
42
            # Create if needed and load RepanierSettings var when performing config.save()
Patrick's avatar
Patrick committed
43 44 45 46 47 48 49 50 51 52
            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,
Patrick's avatar
Patrick committed
53
                    test_mode=True,
Patrick's avatar
Patrick committed
54
                    name=PERMANENCE_NAME_PERMANENCE,
Patrick's avatar
Patrick committed
55 56 57 58 59
                    max_week_wo_participation=Decimal('99'),
                    send_opening_mail_to_customer=False,
                    send_order_mail_to_customer=False,
                    send_order_mail_to_producer=False,
                    send_invoice_mail_to_customer=False,
60
                    send_abstract_order_mail_to_customer=False,
Patrick's avatar
Patrick committed
61
                    send_invoice_mail_to_producer=False,
62
                    send_abstract_order_mail_to_producer=False,
Patrick's avatar
Patrick committed
63
                    send_order_mail_to_board=False,
Patrick's avatar
Patrick committed
64 65
                    invoice=True,
                    display_anonymous_order_form=False,
Patrick's avatar
Patrick committed
66
                    display_producer_on_order_form=False,
Patrick's avatar
Patrick committed
67
                    bank_account="BE99 9999 9999 9999",
Patrick's avatar
Patrick committed
68 69
                    delivery_point=False,
                    display_vat=False,
Patrick's avatar
Patrick committed
70
                    vat_id=EMPTY_STRING,
Patrick's avatar
Patrick committed
71
                    page_break_on_customer_check=False
Patrick's avatar
Patrick committed
72 73
                )
            config.save()
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            # Create groups with correct rights
            order_group = Group.objects.filter(name=ORDER_GROUP).only('id').order_by().first()
            if order_group is None:
                order_group = Group.objects.create(name=ORDER_GROUP)
            invoice_group = Group.objects.filter(name=INVOICE_GROUP).only('id').order_by().first()
            if invoice_group is None:
                invoice_group = Group.objects.create(name=INVOICE_GROUP)
            coordination_group = Group.objects.filter(name=COORDINATION_GROUP).only('id').order_by().first()
            if coordination_group is None:
                coordination_group = Group.objects.create(name=COORDINATION_GROUP)
            content_types = ContentType.objects.exclude(
                app_label__in=[
                    'admin',
                    'aldryn_bootstrap3',
                    'auth',
                    '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',
                ]
            ).only('id').order_by()
            permissions = Permission.objects.filter(
                content_type__in=content_types
            ).only('id').order_by()
            order_group.permissions = permissions
            invoice_group.permissions = permissions
            coordination_group.permissions = permissions
            content_types = ContentType.objects.exclude(
                app_label__in=[
                    'admin',
                    'auth',
                    'contenttypes',
                    'filer'
                    'menus',
                    'repanier',
                    'reversion',
                    'sessions',
                    'sites',
                ]
            ).only('id').order_by()
            permissions = Permission.objects.filter(
                content_type__in=content_types
            ).only('id').order_by()
            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)
            webmaster_group.permissions = permissions

Patrick's avatar
Patrick committed
133 134 135 136 137
        except Exception as error_str:
            print("##################################")
            print error_str
            print("##################################")
            other = _("Other qty")