* Use python-sql * Add convert_domain method on Fields * Add sql_format and sql_type methods on Fields * Allow to return a full domain for Function.searcher * Replace static backend by dynamic get method * Replace order_field by order_<field name> method review1001002
85 lines
2.1 KiB
Python
85 lines
2.1 KiB
Python
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
#this repository contains the full copyright notices and license terms.
|
|
try:
|
|
import simplejson as json
|
|
except ImportError:
|
|
import json
|
|
import uuid
|
|
import datetime
|
|
|
|
from trytond.model import ModelSQL, fields
|
|
from trytond.config import CONFIG
|
|
from .. import backend
|
|
from ..transaction import Transaction
|
|
|
|
__all__ = [
|
|
'Session', 'SessionWizard',
|
|
]
|
|
|
|
|
|
class Session(ModelSQL):
|
|
"Session"
|
|
__name__ = 'ir.session'
|
|
_rec_name = 'key'
|
|
|
|
key = fields.Char('Key', required=True, select=True)
|
|
|
|
@classmethod
|
|
def __setup__(cls):
|
|
super(Session, cls).__setup__()
|
|
cls.__rpc__ = {}
|
|
|
|
@classmethod
|
|
def __register__(cls, module_name):
|
|
TableHandler = backend.get('TableHandler')
|
|
super(Session, cls).__register__(module_name)
|
|
|
|
table = TableHandler(Transaction().cursor, cls, module_name)
|
|
table.index_action('create_uid', 'add')
|
|
|
|
@staticmethod
|
|
def default_key():
|
|
return uuid.uuid4().hex
|
|
|
|
@classmethod
|
|
def check(cls, user, key):
|
|
"Check user key and delete old one"
|
|
now = datetime.datetime.now()
|
|
timeout = datetime.timedelta(seconds=int(CONFIG['session_timeout']))
|
|
sessions = cls.search([
|
|
('create_uid', '=', user),
|
|
])
|
|
find = False
|
|
for session in sessions:
|
|
timestamp = session.write_date or session.create_date
|
|
if abs(timestamp - now) < timeout:
|
|
if session.key == key:
|
|
find = True
|
|
else:
|
|
cls.delete([session])
|
|
return find
|
|
|
|
@classmethod
|
|
def reset(cls, session):
|
|
"Reset session timestamp"
|
|
sessions = cls.search([
|
|
('key', '=', session),
|
|
])
|
|
cls.write(sessions, {})
|
|
|
|
|
|
class SessionWizard(ModelSQL):
|
|
"Session Wizard"
|
|
__name__ = 'ir.session.wizard'
|
|
|
|
data = fields.Text('Data')
|
|
|
|
@classmethod
|
|
def __setup__(cls):
|
|
super(SessionWizard, cls).__setup__()
|
|
cls.__rpc__ = {}
|
|
|
|
@staticmethod
|
|
def default_data():
|
|
return json.dumps({})
|