Skip to content

Commit 57a02b1

Browse files
committed
[12.0][ADD] Add script for update structure agreement
1 parent d10cabe commit 57a02b1

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed

z_migration/controller/connection.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
For only connection. You can change config file name from keyword
4+
"Config file name" in comment line, ex: migration.conf.
5+
In config file has hostname, port, database, login, password, user_id and
6+
protocal.
7+
Prepare config file
8+
1. migration_remote.conf (For server production).
9+
2. migration.conf (For server localhost).
10+
"""
11+
import openerplib
12+
import ConfigParser
13+
import os
14+
15+
# Config file name
16+
conf = 'migration.conf'
17+
18+
19+
def get_connection(config_file):
20+
file_dir = os.path.dirname(os.path.realpath(__file__))
21+
file_path = '%s/%s' % (file_dir, config_file)
22+
config = ConfigParser.ConfigParser()
23+
config.readfp(open(file_path))
24+
hostname = config.get('server', 'hostname')
25+
port = int(config.get('server', 'port'))
26+
database = config.get('server', 'database')
27+
login = config.get('server', 'login')
28+
password = config.get('server', 'password')
29+
user_id = int(config.get('server', 'user_id'))
30+
protocol = config.get('server', 'protocol')
31+
connection = openerplib.get_connection(
32+
hostname=hostname, port=port, database=database, login=login,
33+
password=password, protocol=protocol, user_id=user_id)
34+
return connection
35+
36+
37+
connection = get_connection(conf)
38+
connection.check_login()

z_migration/controller/log.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Create log files
4+
# Reference source code
5+
https://gist.githubusercontent.com/mcdeid/ccd7c00e9ae9a80e45297abb7b06ea1d/raw/8f8423e066228779b8129e8bca836dcde8bdcece/log.py
6+
"""
7+
import logging
8+
import os
9+
import time
10+
11+
12+
def setup_custom_logger(filename):
13+
module_dir = \
14+
os.path.dirname(os.path.realpath(__file__)).split('/controller')[0]
15+
log_path = '%s/logs/%s_%s.log' \
16+
% (module_dir, filename, time.strftime('%Y_%m_%d_%H_%M_%S'))
17+
formatter = logging.Formatter(
18+
fmt='%(asctime)s %(levelname)s %(message)s',
19+
datefmt='%Y-%m-%d %H:%M:%S')
20+
sthandler = logging.StreamHandler()
21+
sthandler.setFormatter(formatter)
22+
flhandler = logging.FileHandler(log_path)
23+
flhandler.setFormatter(formatter)
24+
logger = logging.getLogger('root')
25+
logger.setLevel(logging.INFO)
26+
if not logger.handlers:
27+
logger.addHandler(sthandler)
28+
logger.addHandler(flhandler)
29+
else:
30+
logger.info('Handlers already added to logger.')
31+
logger.info('Log created at level: ' + str(logger.getEffectiveLevel()))
32+
return logger

z_migration/controller/migration.conf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[server]
2+
hostname = localhost
3+
port = 8069
4+
database = ACM_TEST2
5+
login = admin
6+
password = admin
7+
user_id = 1
8+
protocol = xmlrpc

z_migration/logs/empty.log

Whitespace-only changes.

z_migration/models/agreement.py

+47
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,50 @@ def fields_view_get(self, view_id=None, view_type='form',
2020
root.set('delete', 'true')
2121
res['arch'] = etree.tostring(root)
2222
return res
23+
24+
@api.multi
25+
def mock_unlink_recital_structure(self):
26+
self.mapped('recital_ids').unlink()
27+
return True
28+
29+
@api.multi
30+
def mock_unlink_section_structure(self):
31+
self.mapped('sections_ids').unlink()
32+
return True
33+
34+
@api.multi
35+
def mock_unlink_clause_structure(self):
36+
self.mapped('clauses_ids').unlink()
37+
return True
38+
39+
@api.multi
40+
def mock_unlink_appendix_structure(self):
41+
self.mapped('appendix_ids').unlink()
42+
return True
43+
44+
@api.multi
45+
def mock_copy_recital_structure(self):
46+
self.ensure_one()
47+
for rec in self.template_id.recital_ids:
48+
recital = rec.copy()
49+
recital.agreement_id = self.id
50+
return True
51+
52+
@api.multi
53+
def mock_copy_section_structure(self):
54+
self.ensure_one()
55+
for rec in self.template_id.sections_ids:
56+
section = rec.copy()
57+
section.agreement_id = self.id
58+
section.mapped('clauses_ids').write({
59+
'agreement_id': self.id,
60+
})
61+
return True
62+
63+
@api.multi
64+
def mock_copy_appendix_structure(self):
65+
self.ensure_one()
66+
for rec in self.template_id.appendix_ids:
67+
appendix = rec.copy()
68+
appendix.agreement_id = self.id
69+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
This method will create structure (recitals, sections, clauses, appendices)
3+
in agreement
4+
"""
5+
import sys
6+
import os
7+
try:
8+
agreement_path = os.path.dirname(os.path.realpath(__file__))
9+
script_path = os.path.dirname(agreement_path)
10+
migration_path = os.path.dirname(script_path)
11+
controller_path = '%s/controller' % migration_path
12+
sys.path.insert(0, controller_path)
13+
from connection import connection
14+
import log
15+
except Exception:
16+
pass
17+
18+
# Model
19+
Agreement = connection.get_model('agreement')
20+
21+
# Domain
22+
dom = [('is_template', '=', False)]
23+
24+
# Search Agreement
25+
agreements = Agreement.search_read(dom)
26+
27+
log_agreement_ids = [[], []]
28+
logger = log.setup_custom_logger('update_structure')
29+
logger.info('Start process')
30+
logger.info('Total agreement: %s' % len(agreements))
31+
for agreement in agreements:
32+
try:
33+
# Make sure no have line in recital, section, clause, appendix
34+
Agreement.mock_unlink_recital_structure([agreement['id']])
35+
Agreement.mock_unlink_section_structure([agreement['id']])
36+
Agreement.mock_unlink_clause_structure([agreement['id']])
37+
Agreement.mock_unlink_appendix_structure([agreement['id']])
38+
# Update structure
39+
Agreement.mock_copy_recital_structure([agreement['id']])
40+
Agreement.mock_copy_section_structure([agreement['id']])
41+
Agreement.mock_copy_appendix_structure([agreement['id']])
42+
# Write log
43+
log_agreement_ids[0].append(agreement['id'])
44+
logger.info('Pass ID: %s' % agreement['id'])
45+
except Exception as ex:
46+
log_agreement_ids[1].append(agreement['id'])
47+
logger.error('Fail ID: %s (reason: %s)' % (agreement['id'], ex))
48+
summary = 'Summary: pass %s%s and fail %s%s' \
49+
% (len(log_agreement_ids[0]),
50+
log_agreement_ids[0] and ' %s' % str(tuple(log_agreement_ids[0]))
51+
or '', len(log_agreement_ids[1]),
52+
log_agreement_ids[1] and ' %s' % str(tuple(log_agreement_ids[1]))
53+
or '')
54+
logger.info(summary)
55+
logger.info('End process')

0 commit comments

Comments
 (0)