~unifield-team/unifield-web/uf15

« back to all changes in this revision

Viewing changes to addons/openerp/controllers/database.py

  • Committer: jf
  • Date: 2019-08-09 08:00:03 UTC
  • mfrom: (4967.1.4 US-6032-web)
  • Revision ID: jfb@tempo-consulting.fr-20190809080003-mkvoyi0mu3z7wkde
Tags: uf14.0rc1
US-6032 [IMP] Auto instance creation: new options for scheduled actions

lp:~jfb-tempo-consulting/unifield-web/US-6032

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
from ConfigParser import NoOptionError, NoSectionError
42
42
import threading
43
43
 
 
44
TRUE_LIST = (True, 'True', 'true', 'TRUE', 'Yes', 'YES', 'yes')
44
45
def get_lang_list():
45
46
    langs = [('en_US', 'English (US)')]
46
47
    try:
358
359
            self.msg = {'message': ustr(_('The option \'%s\' from section \'[%s]\' have to be one of those values: %r. (currently it is \'%s\').') % (option, section, possible_values, value)),
359
360
                        'title': ustr(_('Wrong option'))}
360
361
 
 
362
    def check_date_time(self, config, section, option, time_format):
 
363
        if not config.has_option(section, option):
 
364
            return True
 
365
        value = config.get(section, option)
 
366
        if value:
 
367
            try:
 
368
                time.strptime(value, time_format)
 
369
            except:
 
370
                return False
 
371
        return True
 
372
 
 
373
    def check_datetime(self, config, section, option):
 
374
        if not self.check_date_time(config, section, option, '%Y-%m-%d %H:%M'):
 
375
            self.msg = {'message': ustr(_('The option \'%s\' from section \'[%s]\' datetime format expected YYY-MM-DD HH:MM') % (option, section)),
 
376
                        'title': ustr(_('Wrong format'))}
 
377
 
 
378
    def check_time(self, config, section, option):
 
379
        if not self.check_date_time(config, section, option, '%H:%M'):
 
380
            self.msg = {'message': ustr(_('The option \'%s\' from section \'[%s]\' time format expected HH:MM') % (option, section)),
 
381
                        'title': ustr(_('Wrong format'))}
 
382
 
361
383
    def check_config_file(self, file_path):
362
384
        '''
363
385
        perform some basic checks to avoid crashing later
416
438
                ('partner', 'external_account_payable'),
417
439
                ('partner', 'internal_account_receivable'),
418
440
                ('partner', 'internal_account_payable'),
 
441
                ('autosync', 'interval_nb'),
 
442
                ('stockmission', 'interval_nb'),
419
443
            )
420
444
            for section, option in not_empty_int_option_list:
421
445
                self.check_mandatory_int(config, section, option)
427
451
                ('instance', 'instance_level', ('coordo', 'project')),
428
452
                ('instance', 'lang', ('fr_MF', 'es_MF', 'en_MF')),
429
453
                ('backup', 'auto_bck_interval_unit', ('minutes', 'hours', 'work_days', 'days', 'weeks', 'months')),
 
454
                ('autosync', 'interval_unit', ('minutes', 'hours', 'work_days', 'days', 'weeks', 'months')),
 
455
                ('stockmission', 'interval_unit', ('minutes', 'hours', 'work_days', 'days', 'weeks', 'months')),
430
456
                ('reconfigure', 'delivery_process', ('complex', 'simple')),
431
457
            )
432
458
            for section, option, possible_values in possible_value_list:
434
460
                if self.msg:
435
461
                    return
436
462
 
 
463
            check_format = [
 
464
                ('backup', 'auto_bck_next_exec_date', self.check_datetime),
 
465
                ('autosync', 'next_exec_date', self.check_datetime),
 
466
                ('stockmission', 'next_exec_date', self.check_datetime),
 
467
                ('silentupgrade', 'hour_from', self.check_time),
 
468
                ('silentupgrade', 'hour_to', self.check_time),
 
469
 
 
470
            ]
 
471
            for section, option, check_fct in check_format:
 
472
                check_fct(config, section, option)
 
473
                if self.msg:
 
474
                    return
 
475
 
437
476
            if config.get('instance', 'instance_level') == 'project':
438
477
                if len(config.get('instance', 'group_names').split(',')) != 3:
439
478
                    self.msg = {
462
501
                }
463
502
                return
464
503
 
 
504
            # silent uprade True only if autosync True
 
505
            if (not config.has_option('silentupgrade', 'active') or config.get('silentupgrade', 'active') in TRUE_LIST) and config.has_option('autosync', 'active') and config.get('autosync', 'active') not in TRUE_LIST:
 
506
                self.msg = {
 
507
                    'message': _('Silent Upgrade active value and Autosync active value are not consistent '),
 
508
                    'title': _('Auto sync / silent upgrade'),
 
509
                }
 
510
                return
 
511
 
 
512
            # check date betweens auto patch and sync scheduler
 
513
            if config.has_option('autosync', 'next_exec_date') and config.has_option('silentupgrade', 'hour_to') and config.has_option('silentupgrade', 'hour_from'):
 
514
                if (not config.has_option('autosync', 'active') or config.get('autosync', 'active') in TRUE_LIST) and (not config.has_option('silentupgrade', 'active') or config.get('silentupgrade', 'active') in TRUE_LIST):
 
515
                    time_from = time.strptime(config.get('silentupgrade', 'hour_from'), '%H:%M')
 
516
                    time_to = time.strptime(config.get('silentupgrade', 'hour_to'), '%H:%M')
 
517
                    if not server_rpc.execute('object', 'execute', 'sync.client.sync_server_connection', 'is_automatic_patching_allowed',
 
518
                            config.get('autosync', 'next_exec_date'), True, time_from.tm_hour + time_from.tm_min/60. , time_to.tm_hour + time_to.tm_min/60.
 
519
                            ):
 
520
                        self.msg = {
 
521
                            'message': _('"Scheduler autosync next date" and "Silent Upgrade interval" are not consistent'),
 
522
                            'title': _('Auto sync / silent upgrade'),
 
523
                        }
 
524
                        return
465
525
            config_groups = config.get('instance', 'group_names').split(',')
466
526
            found_group = []
467
527
            groups_ids = server_rpc.execute('object', 'execute', 'sync.server.entity_group', 'search', [('name', 'in', config_groups)])
495
555
                }
496
556
                return
497
557
 
 
558
            import_path = os.path.join(os.path.dirname(file_path), 'import')
 
559
            if os.path.exists(import_path):
 
560
                for file_name in os.listdir(import_path):
 
561
                    if not file_name.endswith('.csv'):
 
562
                        self.msg = {
 
563
                            'message': 'File to import %s: incorrect name (must end with .csv:' % file_name,
 
564
                            'title': 'File name error',
 
565
                        }
 
566
                        return
 
567
                    import_object = file_name.split('.csv')[0]
 
568
                    if not server_rpc.execute('object', 'execute', 'ir.model', 'search', [('model', '=', import_object)]):
 
569
                        self.msg = {
 
570
                            'message': 'File to import %s: incorrect name object %s does not exist' % (file_name, import_object),
 
571
                            'title': 'File name error',
 
572
                        }
 
573
                        return
 
574
 
498
575
 
499
576
        except NoOptionError as e:
500
577
            self.msg = {'message': ustr(_('No option \'%s\' found for the section \'[%s]\' in the config file \'%s\'') % (e.option, e.section, file_path)),