Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # IMPORTS
- from flask import Flask, render_template, request
- from werkzeug.utils import secure_filename
- import os
- from xmlrpc import client
- import ssl
- import base64
- import hashlib
- from tools import *
- # CONFIG
- app = Flask(__name__, instance_relative_config=True)
- app.config.from_object("config.DevelopmentConfig")
- ALLOWED_EXTENSIONS = app.config["ALLOWED_EXTENSIONS"]
- # ROUTES
- def allowed_file(filename):
- return '.' in filename and \
- filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
- @app.route("/", methods=['GET', 'POST'])
- def index():
- url = 'http://support.warungpintar.co:8069'
- db = 'odoo'
- username = 'karim@warungpintar.co'
- password = 'warungpintar'
- if request.method == 'POST':
- context = ssl.SSLContext()
- common = client.ServerProxy('{}/xmlrpc/2/common'.format(url), context=context)
- uid = common.authenticate(db, username, password, {})
- models = client.ServerProxy('{}/xmlrpc/2/object'.format(url), context=context)
- if "file_resume" not in request.files:
- return "No file_resume key in request.files"
- file_resume = request.files["file_resume"]
- # print (file_resume)
- applicant_name = request.form["applicant_name"]
- if bool(applicant_name) is False:
- return "Name is required"
- description = request.form["description"]
- email = request.form["email"]
- job_id = request.form["job_id"]
- job_id = int(job_id) # string to int
- mobile = request.form["mobile"]
- source_name = request.form["source_name"]
- source_name = source_name.strip()
- print(source_name)
- try:
- source_id = models.execute_kw(db, uid, password, 'utm.source', 'search_read',
- [[["name", "=", source_name]]])
- source_id = source_id[0]["id"]
- except Exception as e:
- source_id = models.execute_kw(db, uid, password, 'utm.source', 'create', [{
- 'name': source_name, 'create_uid': 1, 'write_uid': 1
- }])
- print(source_id)
- source_id = source_id[0]["id"]
- print(source_id)
- hr_job = models.execute_kw(db, uid, password,
- 'hr.job', 'read',
- [job_id], {'fields': ['name', 'department_id', 'user_id']})
- department_id = hr_job[0]['department_id'][0]
- user_id = hr_job[0]['user_id'][0]
- # print (hr_job)
- hr_job_name = hr_job[0]['name']
- stage_id = '10' # 10 is default id job in odoo
- company_id = '1'
- priority = '0'
- delay_close = '0.00'
- color = '0'
- create_uid = 1
- write_uid = 1
- # print (hr_job_name)
- # read category applicant
- hr_applicant_category_id = models.execute_kw(db, uid, password, 'hr.applicant.category', 'search_read',
- [[["name", "=", hr_job_name]]])
- hr_applicant_category_id = hr_applicant_category_id[0]['id']
- print(hr_applicant_category_id)
- idx = models.execute_kw(db, uid, password, 'hr.applicant', 'create', [{
- 'name': applicant_name, 'source_id': source_id, 'active': 'true', 'description': description,
- 'email_from': email, 'stage_id': 10, 'company_id': 1, 'user_id': user_id, 'priority': priority,
- 'job_id': job_id, 'partner_name': applicant_name, 'partner_phone': mobile, 'partner_mobile': mobile,
- 'department_id': department_id, 'delay_close': delay_close, 'color': color, 'create_uid': '1',
- 'write_uid': '1', #no need create_uid and write_uid
- # add category
- 'categ_ids': [(6, 0, [hr_applicant_category_id])],
- # add attachment
- 'attachment_ids': [(0, 0, [{
- 'name': "Resume",
- 'mimetype': 'application/pdf',
- 'datas': base64.b64encode(file_resume),
- }])]
- }])
- # # write to hr_applicant_hr_applicant_category_rel
- # test = models.execute_kw(db, uid, password, 'hr.applicant.hr.applicant.category.rel', 'create', [{
- # 'hr_applicant_id': idx, 'hr_applicant_category_id': hr_applicant_category_id
- # }])
- #
- # print(test)
- # There is no file selected to upload
- if file_resume.filename == "":
- return "Please select a file resume"
- # File is selected, upload to S3 and show S3 URL
- if file_resume and allowed_file(file_resume.filename):
- file_resume.filename = secure_filename(file_resume.filename)
- output = upload_file_to_s3(file_resume, app.config["S3_BUCKET"])
- bin_value = bytes(file_resume.filename, 'utf-8')
- fname_resume = hashlib.sha1(bin_value).hexdigest()
- models.execute_kw(db, uid, password, 'ir.attachment', 'create', [{
- 'name': 'Resume', 'datas_fname': file_resume.filename, 'res_name': applicant_name,
- 'res_model': 'hr.applicant', 'res_id': idx, 'create_uid': 1, 'company_id': '797', 'type': 'binary',
- 'public': 'false', 'store_fname': 'hr.applicant/' + fname_resume, 'file_size': '1000',
- 'checksum': fname_resume, 'mimetype': 'application/pdf', 'write_uid': 1
- }])
- # print (fname)
- if "file_portofolio" not in request.files:
- status = "no portofolio"
- else:
- file_portofolio = request.files["file_portofolio"]
- output = upload_file_to_s3(file_portofolio, app.config["S3_BUCKET"])
- bin_value_portofolio = bytes(file_portofolio.filename, 'utf-8')
- fname_portofolio = hashlib.sha1(bin_value_portofolio).hexdigest()
- models.execute_kw(db, uid, password, 'ir.attachment', 'create', [{
- 'name': 'Portofolio', 'datas_fname': file_portofolio.filename, 'res_name': applicant_name,
- 'res_model': 'hr.applicant', 'res_id': idx, 'create_uid': 1, 'company_id': '797', 'type': 'binary',
- 'public': 'false', 'store_fname': 'hr.applicant/' + fname_portofolio, 'file_size': '1000',
- 'checksum': fname_portofolio, 'mimetype': 'application/pdf', 'write_uid': 1
- }])
- return str('oke')
- if __name__ == "__main__":
- app.run(debug=False, host='0.0.0.0', port='5000')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement