Skip to content

Instantly share code, notes, and snippets.

@jev-odoo
Last active October 27, 2020 09:17
Show Gist options
  • Select an option

  • Save jev-odoo/04bf1fcf5926396deb9b5178f663ff96 to your computer and use it in GitHub Desktop.

Select an option

Save jev-odoo/04bf1fcf5926396deb9b5178f663ff96 to your computer and use it in GitHub Desktop.
POS - Rescue
# FOR VERSION 10,11,12,13,...
# Set sessions ids between the brackets, separated by a coma
# I.E. SESSION_ID = [12, 18, 132] or SESSION_ID = [4]
SESSION_ID = []
# DO NOT MODIFY
VALID_VERSIONS = (10, 11, 12, 13, 14)
VALID_SESSION_STATES = ('opened', 'closing_control')
MESSAGES = []
def iter_to_string(iterable):
return ', '.join(str(x) for x in iterable)
def get_version(env):
latest_version = env['ir.module.module'].search([('name', '=', 'base')]).latest_version.replace('~', '-')
actual_version = 0
for version in VALID_VERSIONS:
if latest_version.startswith(str(version)) or latest_version.startswith('saas-%s' % str(version)):
actual_version = version
if not actual_version:
raise Warning('Invalid version. (Actual version: {} - Valid versions: {})'.format(actual_version, iter_to_string(VALID_VERSIONS)))
return actual_version
def get_session(env):
sessions = env['pos.session']
if not SESSION_ID:
raise Warning('Please define the session id.')
sessions = sessions.search([('id', 'in', SESSION_ID)])
if not sessions:
raise Warning('No session found. ids: {}'.format(iter_to_string(SESSION_ID)))
return sessions
def set_rescue(env, session):
if session.rescue:
MESSAGES.append('Session is already in rescue modus. (id: {})'.format(session.id))
return
session.write({'rescue': True})
def process(env):
version = get_version(env)
sessions = get_session(env)
for session in sessions:
if session.state not in VALID_SESSION_STATES:
MESSAGES.append('Invalid session state, skipping... (id: {} - Actual state: {} - Valid states: {})'.format(session.id, session.state, iter_to_string(VALID_SESSION_STATES)))
continue
try:
set_rescue(env, session)
MESSAGES.append('Session has been put in rescue modus. (id: {})'.format(session.id))
env.cr.commit()
except Exception as e:
MESSAGES.append('Error setting session into rescue mode. (id: {} - error: {}'.format(session.id, str(e)))
env.cr.rollback()
if MESSAGES:
raise Warning('\n'.join(MESSAGES))
else:
raise Warning('Nothing happened...')
process(env)
# FOR VERSION 10,11,12,13,...
# Set sessions ids between the brackets, separated by a coma
# I.E. SESSION_ID = [12, 18, 132] or SESSION_ID = [4]
SESSION_ID = []
if not SESSION_ID:
raise Warning('Please define the session id.')
sessions = env['pos.session'].sudo().search([('id', 'in', SESSION_ID)])
messages = []
for session in sessions:
if not session:
messages.append('No session found with id: {}'.format(SESSION_ID))
continue
if session.state not in ['opened', 'closing_control']:
messages.append('Session state must be Opened or Closing Control. (id: {} - state: {})'.format(session.id, session.state))
continue
if session.rescue:
messages.append('Session is already in rescue modus. (id: {})'.format(session.id))
continue
session.write({'rescue': True})
env.cr.commit()
messages.append('Session has been put in rescue modus. (id: {})'.format(session.id))
if messages:
raise Warning('\n'.join(messages))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment