Guest User

Untitled

a guest
Oct 18th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import json
  2. import xmlrpc.client as xmlrpclib
  3. from odoo import http
  4. from openerp.http import Response
  5.  
  6. class resUserController(http.Controller):
  7. url = '<my url>'
  8. db = '<name of my database>'
  9.  
  10. @http.route('/user/login', type='json', method='GET', auth='public')
  11. def get_login(self, **kwargs):
  12. username = kwargs.get('email')
  13. password = kwargs.get('password')
  14. common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(self.url), allow_none=True)
  15.  
  16. uid = common.authenticate(self.db, username, password, {})
  17. if uid:
  18. Response.status = '200 Succesful operation'
  19. json_result = {'token': uid}
  20. return json.dumps(json_result)
  21. Response.status = '400 Invalid credentials'
  22. return
  23.  
  24. @http.route('/user/getInfo', type='json', method='GET', auth='user')
  25. def get_info(self, **kwargs):
  26. uid = 1
  27. password = '<my admin password>'
  28. models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(self.url), allow_none=True)
  29. info = models.execute_kw(self.db, uid, password, 'res.users',
  30. 'search_read', [[['id', '=', kwargs.get('token')]]],
  31. {'fields': ['info']})[0]['invite_code']
  32. if info:
  33. Response.status = '200 Succesful operation'
  34. json_result = {'info': info}
  35. return json.dumps(json_result)
  36. Response.status = '404 User not found'
  37. return
  38.  
  39. import requests
  40. import json
  41.  
  42. url_connect = "<my url>/user/login"
  43. url = "<my url>/user/getInfo"
  44. headers = {'Content-Type': 'application/json'}
  45. data_connect = {
  46. "params": {
  47. "email": "<my test account email>",
  48. "password": "<my test account password>",
  49. }
  50. }
  51. data = {
  52. "params": {
  53. "token": <my test account id>,
  54. }
  55. }
  56. data_json = json.dumps(data)
  57. r = requests.get(url=url_connect, data=json.dumps(data_connect), headers=headers)
  58. print(r)
  59. print(r.json())
  60. r = requests.get(url=url, data=data_json, headers=headers)
  61. print(r)
  62. print(r.json())
Add Comment
Please, Sign In to add comment