Advertisement
Guest User

Untitled

a guest
Oct 29th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. # IMPORTS
  2. from flask import Flask, render_template, request
  3. from werkzeug.utils import secure_filename
  4. import os
  5. from xmlrpc import client
  6. import ssl
  7. import base64
  8. import hashlib
  9.  
  10. from tools import *
  11.  
  12. # CONFIG
  13. app = Flask(__name__, instance_relative_config=True)
  14. app.config.from_object("config.DevelopmentConfig")
  15.  
  16. ALLOWED_EXTENSIONS = app.config["ALLOWED_EXTENSIONS"]
  17.  
  18.  
  19. # ROUTES
  20. def allowed_file(filename):
  21. return '.' in filename and \
  22. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  23.  
  24.  
  25. @app.route("/", methods=['GET', 'POST'])
  26. def index():
  27. url = 'http://support.warungpintar.co:8069'
  28. db = 'odoo'
  29.  
  30. username = 'karim@warungpintar.co'
  31. password = 'warungpintar'
  32.  
  33. if request.method == 'POST':
  34.  
  35. context = ssl.SSLContext()
  36. common = client.ServerProxy('{}/xmlrpc/2/common'.format(url), context=context)
  37. uid = common.authenticate(db, username, password, {})
  38.  
  39. models = client.ServerProxy('{}/xmlrpc/2/object'.format(url), context=context)
  40.  
  41. if "file_resume" not in request.files:
  42. return "No file_resume key in request.files"
  43.  
  44. file_resume = request.files["file_resume"]
  45. # print (file_resume)
  46. applicant_name = request.form["applicant_name"]
  47.  
  48. if bool(applicant_name) is False:
  49. return "Name is required"
  50.  
  51. description = request.form["description"]
  52. email = request.form["email"]
  53. job_id = request.form["job_id"]
  54. job_id = int(job_id) # string to int
  55. mobile = request.form["mobile"]
  56. source_name = request.form["source_name"]
  57.  
  58. source_name = source_name.strip()
  59. print(source_name)
  60.  
  61. try:
  62. source_id = models.execute_kw(db, uid, password, 'utm.source', 'search_read',
  63. [[["name", "=", source_name]]])
  64. source_id = source_id[0]["id"]
  65. except Exception as e:
  66. source_id = models.execute_kw(db, uid, password, 'utm.source', 'create', [{
  67. 'name': source_name, 'create_uid': 1, 'write_uid': 1
  68. }])
  69. print(source_id)
  70. source_id = source_id[0]["id"]
  71.  
  72. print(source_id)
  73.  
  74. hr_job = models.execute_kw(db, uid, password,
  75. 'hr.job', 'read',
  76. [job_id], {'fields': ['name', 'department_id', 'user_id']})
  77.  
  78. department_id = hr_job[0]['department_id'][0]
  79. user_id = hr_job[0]['user_id'][0]
  80. # print (hr_job)
  81. hr_job_name = hr_job[0]['name']
  82.  
  83. stage_id = '10' # 10 is default id job in odoo
  84. company_id = '1'
  85. priority = '0'
  86.  
  87. delay_close = '0.00'
  88. color = '0'
  89. create_uid = 1
  90. write_uid = 1
  91.  
  92. # print (hr_job_name)
  93. # read category applicant
  94. hr_applicant_category_id = models.execute_kw(db, uid, password, 'hr.applicant.category', 'search_read',
  95. [[["name", "=", hr_job_name]]])
  96.  
  97. hr_applicant_category_id = hr_applicant_category_id[0]['id']
  98. print(hr_applicant_category_id)
  99.  
  100. idx = models.execute_kw(db, uid, password, 'hr.applicant', 'create', [{
  101. 'name': applicant_name, 'source_id': source_id, 'active': 'true', 'description': description,
  102. 'email_from': email, 'stage_id': 10, 'company_id': 1, 'user_id': user_id, 'priority': priority,
  103. 'job_id': job_id, 'partner_name': applicant_name, 'partner_phone': mobile, 'partner_mobile': mobile,
  104. 'department_id': department_id, 'delay_close': delay_close, 'color': color, 'create_uid': '1',
  105. 'write_uid': '1', #no need create_uid and write_uid
  106. # add category
  107. 'categ_ids': [(6, 0, [hr_applicant_category_id])],
  108. # add attachment
  109. 'attachment_ids': [(0, 0, [{
  110. 'name': "Resume",
  111. 'mimetype': 'application/pdf',
  112. 'datas': base64.b64encode(file_resume),
  113. }])]
  114. }])
  115.  
  116.  
  117.  
  118. # # write to hr_applicant_hr_applicant_category_rel
  119. # test = models.execute_kw(db, uid, password, 'hr.applicant.hr.applicant.category.rel', 'create', [{
  120. # 'hr_applicant_id': idx, 'hr_applicant_category_id': hr_applicant_category_id
  121. # }])
  122. #
  123. # print(test)
  124.  
  125. # There is no file selected to upload
  126. if file_resume.filename == "":
  127. return "Please select a file resume"
  128.  
  129. # File is selected, upload to S3 and show S3 URL
  130. if file_resume and allowed_file(file_resume.filename):
  131. file_resume.filename = secure_filename(file_resume.filename)
  132. output = upload_file_to_s3(file_resume, app.config["S3_BUCKET"])
  133.  
  134. bin_value = bytes(file_resume.filename, 'utf-8')
  135. fname_resume = hashlib.sha1(bin_value).hexdigest()
  136.  
  137. models.execute_kw(db, uid, password, 'ir.attachment', 'create', [{
  138. 'name': 'Resume', 'datas_fname': file_resume.filename, 'res_name': applicant_name,
  139. 'res_model': 'hr.applicant', 'res_id': idx, 'create_uid': 1, 'company_id': '797', 'type': 'binary',
  140. 'public': 'false', 'store_fname': 'hr.applicant/' + fname_resume, 'file_size': '1000',
  141. 'checksum': fname_resume, 'mimetype': 'application/pdf', 'write_uid': 1
  142. }])
  143.  
  144. # print (fname)
  145. if "file_portofolio" not in request.files:
  146. status = "no portofolio"
  147. else:
  148. file_portofolio = request.files["file_portofolio"]
  149. output = upload_file_to_s3(file_portofolio, app.config["S3_BUCKET"])
  150.  
  151. bin_value_portofolio = bytes(file_portofolio.filename, 'utf-8')
  152. fname_portofolio = hashlib.sha1(bin_value_portofolio).hexdigest()
  153.  
  154. models.execute_kw(db, uid, password, 'ir.attachment', 'create', [{
  155. 'name': 'Portofolio', 'datas_fname': file_portofolio.filename, 'res_name': applicant_name,
  156. 'res_model': 'hr.applicant', 'res_id': idx, 'create_uid': 1, 'company_id': '797', 'type': 'binary',
  157. 'public': 'false', 'store_fname': 'hr.applicant/' + fname_portofolio, 'file_size': '1000',
  158. 'checksum': fname_portofolio, 'mimetype': 'application/pdf', 'write_uid': 1
  159. }])
  160.  
  161. return str('oke')
  162.  
  163.  
  164. if __name__ == "__main__":
  165. app.run(debug=False, host='0.0.0.0', port='5000')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement