Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. from contrib.saas.config import Config
  2. from contrib.saas.server_name import ServerName
  3. from contrib.saas.state import AccountState
  4.  
  5. __author__ = 'Igor Katrichenko'
  6.  
  7.  
  8. class Task(ITask):
  9.     @property
  10.     def name(self):
  11.         return os.path.splitext(os.path.basename(__file__))[0]
  12.  
  13.     def __get_client_info(self, login):
  14.         server_id, server_name = ServerName.get_saas_name()
  15.         command = {
  16.             'module': 'user',
  17.             'task': 'info',
  18.             'parameters':
  19.                 {
  20.                     'login': login,
  21.                     'server_name': server_name,
  22.                 }
  23.         }
  24.         url = self.config['manage_url']
  25.         headers = {'content-type': 'application/json'}
  26.         response = requests.post(url, data=json.dumps(command), headers=headers, timeout=5)
  27.         result = json.loads(response.text)
  28.         if result['code'] != 0:
  29.             raise Exception("Can't get domains for client '{}'".format(login))
  30.         user_info = result['data'][login]
  31.         return user_info
  32.  
  33.     def do(self):
  34.         if self.config['type'] != 'free':
  35.             login = self._parameters['login']
  36.             # Получение ключа программы
  37.             acc = AccountState(self.config)
  38.             user_info = acc.get(login)
  39.             force = self.parameters.setdefault('force', False)
  40.             if user_info == {} or force:
  41.                 user_info = acc.get_client_info(login, self.config['manage_url'])
  42.                 acc.set(login, user_info)
  43.             if int(user_info['tariff_id']) != 1 or int(user_info['revision']) >= int('19455'):
  44.                 key = AccountState.generate_license_key(user_info)
  45.             config_file = self.config['client_config_file'].format(login)
  46.             config = Config.get(config_file)
  47.             connection = pymysql.connect(
  48.                 host=self.config['db_host'],
  49.                 user=self.config['db_user'],
  50.                 password=self.config['db_password'],
  51.                 db=config['dbname'],
  52.                 charset='utf8',
  53.                 cursorclass=pymysql.cursors.DictCursor
  54.             )
  55.             # Установка ключа программы
  56.             cursor = connection.cursor()
  57.             table_prefix = config['table_prefix']
  58.             sql = "DELETE FROM {}key;".format(table_prefix)
  59.             cursor.execute(sql)
  60.             if int(user_info['tariff_id']) != 1:
  61.                 sql = "INSERT INTO {}key (`code`) VALUES (%s);".format(table_prefix)
  62.                 cursor.execute(sql, (key,))
  63.             connection.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement