def register_user(self,param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): form_req = param['form'] if form_req: validation = ValidationInput().validation_register(form_req) if validation['code'] == 200: input_data = validation['result'] code_activated = secrets.token_urlsafe(16) encode_validation = Helpers().create_session(input_data,'not_confirm',code_activated) Email = SendEmail().send_email_confirm_register(params=input_data,encode_validation=encode_validation,cek_connection=True) if Email['code'] == 200: try: Companys = CompanyModels.query.filter_by(name=input_data["company_name"]).first() data_companys = company_schema.dump(Companys) if not data_companys: new_company = CompanyModels(input_data['company_name'], input_data['address'], input_data['no_telp'], '', '', input_data['email'], '') db.session.add(new_company) db.session.commit() Company = CompanyModels.query.filter_by(name=input_data["company_name"]).first() data_company = company_schema.dump(Company) if data_company: new_user = UsersModel(input_data['name'], input_data['email'] , input_data['no_telp'], input_data['password'], input_data['role_id'], 'null', data_company["id"], input_data['address'],'not_confirm',code_activated) db.session.add(new_user) db.session.commit() encode_validation = Helpers().create_session(input_data,'not_confirm',code_activated) Email = SendEmail().send_email_confirm_register(params=input_data,encode_validation=encode_validation,cek_connection=False) print(Email) # pdb.run('mymodule.test()') data = { "name" : input_data['name'], "email" : input_data['email'] } result = ResponseApi().error_response(200, "Register", "Register Succes Cek Your Email", start_time, data) else: result = ResponseApi().error_response(400, "Register", "Company cannot be saved", start_time) else: result = ResponseApi().error_response(400, "Register", "Company Already Exist Please Change Your Name Company", start_time) except Exception as e: error = str(e) result = ResponseApi().error_response(400, "Register", error, start_time) else: result = ResponseApi().error_response(Email['code'], "Register", Email['Message'], start_time) else: result = ResponseApi().error_response(validation['code'], "Logout", validation['message'], start_time) else: result = ResponseApi().error_response(400, "Register", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(400, "Register", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return jsonify(response)
def update_cart(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: id_cart = form_value['id_cart'] id_transaction = form_value['id_transaction'] id_product = form_value['id_product'] qty = form_value['qty'] price = form_value['price'] data = { "id_transaction": id_transaction, "id_product": id_product, "qty": qty, "price": price, } Cart = CartModels.query.filter_by(id=id_cart) Cart.update(data) db.session.commit() resultData.append(data) result = { "code": 200, "SpeedTime": ResponseApi().speed_response(start_time), "endpoint": "Update Cart", "message": "Update Cart Succes", "result": resultData } result = ResponseApi().error_response( 200, "Update Cart", "Cart Update Succes", start_time, resultData) # print(result) # pdb.run('mymodule.test()') except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Update Cart", error, start_time) else: result = ResponseApi().error_response( 400, "Update Cart", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Update Cart", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Update Cart", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def update_user(self,param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] data = {} if form_req: try: for form_value in form_req: id_user = form_value['id_user'] name = form_value['name'] old_password = form_value['old_password'] new_password = form_value['new_password'] no_telp = form_value['no_telp'] role_id = form_value['role_id'] address = form_value['address'] email = form_value['email'] data['name'] = name data['user_role_id'] = role_id data['no_telp'] = no_telp data['address'] = address data['email'] = email user = UsersModel.query.filter_by(id=id_user).first() user_response = user_schema.dump(user) if old_password: if hashlib.md5(old_password.encode('utf-8')).hexdigest() == user_response['password']: data['password'] = hashlib.md5(new_password.encode('utf-8')).hexdigest() else: result = ResponseApi().error_response(400, "Update User", "Old Password is Wrong", start_time) response = ResponseApi().response_api(result) return response Users = UsersModel.query.filter_by(id=id_user) Users.update(data) db.session.commit() resultData.append(data) result = ResponseApi().error_response(200, "Update Company", "Update Company Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response(400, "Update User", error, start_time) else: result = ResponseApi().error_response(400, "Update User", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Update User", cek_session['message'], start_time) else: result = ResponseApi().error_response(400, "Update User", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def delete_product(self, param): if Helpers().cek_auth(param): # cek_session = Helpers().cek_session(param) # if cek_session['code'] == 200: form_req = param['form'] if form_req: try: resultData = [] for form_value in form_req: id_product = form_value['id'] product = ProductModels.query.filter_by( id=id_product).first() product_value = product_schema.dump(product) db.session.delete(product) db.session.commit() data = { 'id': product_value['id'], "name": product_value['name'], } resultData.append(data) result = { "code": 200, "endpoint": "Delete Product", "message": "Delete Product Succes", "result": resultData } except Exception as e: error = str(e) result = { "code": 400, "endpoint": "Delete Product", "message": error, "result": {} } else: result = { "code": 400, "endpoint": "Delete Product", "message": "Form Request Is Empty", "result": {} } # else: # result = { # "code" : cek_session['code'], # "endpoint": "Update Product", # "message": cek_session['message'], # "result": {} # } else: result = { "code": 400, "endpoint": "Delete Product", "message": "Authentication signature calculation is wrong", "result": {} } response = ResponseApi().response_api(result) return response
def get_user(self, param): if Helpers().cek_auth(param): # cek_session = Helpers().cek_session(param) # if cek_session['code'] == 200: form_req = param['form'] validation = ValidationInput().validation_get_users(form_req) if validation['code'] == 200: input_data = validation['result'] email = input_data['email'] user = UsersModel.query.filter_by(email=email).first() if user: user_response = user_schema.dump(user) result = { "code": 200, "endpoint": "Get Users", "message": "Succes Get Users", "result": { "id": user_response['id'], "name": user_response['name'], "email": user_response['email'], "role_id": user_response['role_id'], "status": user_response['status'], "position": user_response['position'], "token": user_response['token'], } } else: result = { "code": 400, "endpoint": "Get Users", "message": "Your account email not found", "result": {} } else: result = { "code": validation['code'], "endpoint": "Get Users", "message": validation['message'], "result": {} } # else: # result = { # "code" : cek_session['code'], # "endpoint": "Get Users", # "message": cek_session['message'], # "result": {} # } else: result = { "code": 400, "endpoint": "Get Users", "message": "Authentication signature calculation is wrong", "result": {} } response = ResponseApi().response_api(result) return response
def get_category(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: show_all = form_req['show_all'] id_category = form_req['id_category'] if show_all: Category = CategoryModels.query.all() data_category = categorys_schema.dump(Category) for category_value in data_category: id_category = int(category_value['id']) total_product = ProductModels.query.filter_by( id_category=id_category).count() data = { "id_category": category_value['id'], "name": category_value['name'], 'total_product': total_product } resultData.append(data) else: Category = CategoryModels.query.filter_by( id=id_category).first() data_category = category_schema.dump(Category) id_category = int(data_category['id']) total_product = ProductModels.query.filter_by( id_category=id_category).count() data = { "id_category": data_category['id'], "name": data_category['name'], 'total_product': total_product } resultData.append(data) result = ResponseApi().error_response( 200, "Get Category", "Get Category Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Get Category", error, start_time) else: result = ResponseApi().error_response( 400, "Get Category", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Get Category", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Get Category", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def get_user(self,param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] validation = ValidationInput().validation_get_users(form_req) if validation['code'] == 200: input_data = validation['result'] email = input_data['email'] user = UsersModel.query.filter_by(email=email).first() if user: user_response = user_schema.dump(user) data = { "id":user_response['id'], "name":user_response['name'], "email":user_response['email'], "role_id":user_response['user_role_id'], "token":user_response['token'], } result = ResponseApi().error_response(200, "Get User", "Get User Succes", start_time, data) else: result = ResponseApi().error_response(400, "Get User", "Your account email not found", start_time) else: result = ResponseApi().error_response(validation['code'], "Get User", validation['message'], start_time) else: result = ResponseApi().error_response(cek_session['code'], "Get User", cek_session['message'], start_time) else: result = ResponseApi().error_response(400, "Get Users", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def register_user(self, param): if Helpers().cek_auth(param): form_req = param['form'] if form_req: try: validation = ValidationInput().validation_register( form_req) if validation['code'] == 200: input_data = validation['result'] new_user = UsersModel(input_data['name'], input_data['email'], input_data['password'], True, input_data['position'], input_data['role_id'], 'null') db.session.add(new_user) db.session.commit() result = { "code": 200, "endpoint": "Register", "message": "Register Succes", "result": { "name": input_data['name'], "email": input_data['email'] } } else: result = { "code": validation['code'], "endpoint": "Register", "message": validation['message'], "result": {} } except Exception as e: error = str(e) result = { "code": 400, "endpoint": "Register", "message": error, "result": {} } else: result = { "code": 400, "endpoint": "Register", "message": "Form Request Is Empty", "result": {} } else: result = { "code": 400, "endpoint": "Register", "message": "Authentication signature calculation is wrong", "result": {} } response = ResponseApi().response_api(result) return jsonify(response)
def delete_category(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: delete_All = form_value['delete_all'] if delete_All: category = CategoryModels.query.order_by( CategoryModels.id.asc()) data_category = categorys_schema.dump(category) for category_value in data_category: data = { 'id_category': category_value['id'], "name": category_value['name'], } resultData.append(data) CategoryModels.query.delete() db.session.commit() else: id_category = form_value['id_category'] category = CategoryModels.query.filter_by( id=id_category).first() category_value = category_schema.dump(category) db.session.delete(category) db.session.commit() data = { 'id_category': category_value['id'], "name": category_value['name'], } resultData.append(data) result = ResponseApi().error_response( 200, "Delete Category", "Delete Category Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Delete Category", error, start_time) else: result = ResponseApi().error_response( 400, "Delete Category", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Delete Category", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Delete Category", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def insert_company(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: name_company = form_value['name_company'] address_company = form_value['address_company'] no_tlp_company = form_value['no_tlp_company'] facebook_company = form_value['facebook_company'] instagram_company = form_value['instagram_company'] email_company = form_value['email_company'] website_company = form_value['website_company'] new_company = CompanyModels( name_company, address_company, no_tlp_company, facebook_company, instagram_company, email_company, website_company) db.session.add(new_company) db.session.commit() data = { "name": name_company, "address": address_company, "no_telp": no_tlp_company, "facebook": facebook_company, "instagram": instagram_company, "email": email_company, "website": website_company } resultData.append(data) result = ResponseApi().error_response( 200, "Insert Company", "Insert Company Succes", start_time, resultData) # print(result) # pdb.run('mymodule.test()') except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Insert Company", error, start_time) else: result = ResponseApi().error_response( 400, "Insert Company", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Insert Company", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Insert Company", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def insert_transaction(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: id_company = form_value['id_company'] id_user_company = form_value['id_user_company'] id_user_buyer = form_value['id_user_buyer'] buyer_name = form_value['buyer_name'] date_time = form_value['date_time'] amount_of_charge = form_value['amount_of_charge'] new_transaction = TransactionModels( id_company, id_user_company, id_user_buyer, buyer_name, date_time, amount_of_charge) db.session.add(new_transaction) db.session.commit() data = { "id_company": id_company, "id_user_company": id_user_company, "id_user_buyer": id_user_buyer, "buyer_name": buyer_name, "date_time": date_time, "amount_of_charge": amount_of_charge, } resultData.append(data) result = ResponseApi().error_response( 200, "Insert Transaction", "Insert Transaction Succes", start_time, resultData) # print(result) # pdb.run('mymodule.test()') except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Insert Transaction", error, start_time) else: result = ResponseApi().error_response( 400, "Insert Transaction", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Insert Transaction", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Insert Transaction", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def get_authority(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: show_all = form_req['show_all'] id_authority = form_req['id_authority'] if show_all: Authority = AuthorityModels.query.all() data_auth = authoritys_schema.dump(Authority) for auth_value in data_auth: if (auth_value): data = { "id_authority": auth_value['id'], "name_authority": auth_value['name_authority'], } resultData.append(data) else: Authority = AuthorityModels.query.filter_by( id=id_authority).first() data_auth = authority_schema.dump(Authority) data = { "id_authority": data_auth['id'], "name_authority": data_auth['name_authority'], } resultData.append(data) result = ResponseApi().error_response( 200, "Delete Authority", "Get Authority Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Get Authority", error, start_time) else: result = ResponseApi().error_response( 400, "Get Authority", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Get Authority", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Get Authority", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def get_dashboard(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: try: total_category = CategoryModels.query.count() total_product = ProductModels.query.count() product_data = {'total_product': total_product} category_data = {'total_category': total_category} # print(xample) # pdb.run('mymodule.test()') data = {'Product': product_data, 'Category': category_data} result = ResponseApi().error_response( 200, "Get Dashboard", "Get Dashboard Succes", start_time, data) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Get Dashboard", error, start_time) else: result = ResponseApi().error_response(cek_session['code'], "Get Dashboard", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Get Dashboard", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def logout_user(self,param): start_time = ResponseApi().microtime(True) result ={} if Helpers().cek_auth(param): form_req = param['form'] if form_req: try: validation = ValidationInput().validation_logout(form_req) if validation['code'] == 200: input_data = validation['result'] email = input_data['email'] user = UsersModel.query.filter_by(email=email).first() if user: user_response = user_schema.dump(user) expired_session = (datetime.now() + timedelta(minutes = int(os.getenv('SESSION_EXPIRED')))) data_user = { "name":user_response['name'], "email":user_response['email'], "role_id":user_response['user_role_id'], "expired_session":expired_session.strftime('%Y/%m/%d %H:%M:%S'), "status":"Logout" } encode_token = Helpers().encode_token(data_user) update_session = self.update_session_user(user_response['id'],encode_token,"Logout") if update_session: data = { "name":update_session['name'], "email":update_session['email'], "role_id":update_session['user_role_id'], "token":update_session['token'], } result = ResponseApi().error_response(200, "Logout", "Logout Succes", start_time, data) else: result = ResponseApi().error_response(400, "Logout", "Your account email is incorrect", start_time) else: result = ResponseApi().error_response(validation['code'], "Logout", validation['message'], start_time) except Exception as e: error = str(e) result = ResponseApi().error_response(400, "Logout", error, start_time) else: result = ResponseApi().error_response(400, "Logout", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(400, "Logout", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def insert_category(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: name_category = form_value['name'] id_company = cek_session['data']['id_company'] new_category = CategoryModels( name_category, id_company) db.session.add(new_category) db.session.commit() Category = CategoryModels.query.filter_by( name=name_category).first() data_category = category_schema.dump(Category) data = { "id_company": data_category['id_company'], "id_category": data_category['id'], "name": name_category, } resultData.append(data) result = ResponseApi().error_response( 200, "Insert Category", "Insert Category Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Insert Category", error, start_time) else: result = ResponseApi().error_response( 400, "Insert Category", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Insert Category", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Insert Category", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def insert_cart(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: id_transaction = form_value['id_transaction'] id_product = form_value['id_product'] qty = form_value['qty'] price = form_value['price'] new_cart = CartModels(id_transaction, id_product, qty, price) db.session.add(new_cart) db.session.commit() data = { "id_transaction": id_transaction, "id_product": id_product, "qty": qty, "price": price, } resultData.append(data) result = ResponseApi().error_response( 200, "Insert Cart", "Insert Cart Succes", start_time, resultData) # print(result) # pdb.run('mymodule.test()') except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Insert Cart", error, start_time) else: result = ResponseApi().error_response( 400, "Insert Cart", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Insert Cart", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Insert Cart", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def update_authority(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: id_authority = form_value['id_authority'] name_authority = form_value['name_authority'] data = { "name_authority": name_authority, } Authority = AuthorityModels.query.filter_by( id=id_authority) Authority.update(data) db.session.commit() resultData.append(data) result = ResponseApi().error_response( 200, "Update Authority", "Update Authority Succes", start_time, resultData) # print(result) # pdb.run('mymodule.test()') except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Update Authority", error, start_time) else: result = ResponseApi().error_response( 400, "Update Authority", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Update Authority", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Update Authority", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def get_transaction(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] if form_req: try: show_all = form_req['show_all'] id_transaction = form_req['id_transaction'] if show_all: Transaction = TransactionModels.query.all() data = transactions_schema.dump(Transaction) else: Transaction = TransactionModels.query.filter_by( id=id_transaction).first() data = transactions_schema.dump(Transaction) result = ResponseApi().error_response( 200, "Get Transaction", "Get Transaction Succes", start_time, data) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Get Transaction", error, start_time) else: result = ResponseApi().error_response( 400, "Get Transaction", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Get Transaction", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Get Transaction", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def update_category(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: # try: for form_value in form_req: id_category = form_value['id_category'] id_company = cek_session['data']['id_company'] name_category = form_value['name'] data = { "id": id_category, "id_company": id_company, "name": name_category, } Category = CategoryModels.query.filter_by( id=id_category) Category.update(data) db.session.commit() resultData.append(data) result = ResponseApi().error_response( 200, "Update Category", "Update Category Succes", start_time, resultData) # except Exception as e: # error = str(e) # result = ResponseApi().error_response(400, "Update Category", error, start_time) else: result = ResponseApi().error_response( 400, "Update Category", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Update Category", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Update Category", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def verify_account(self,param): start_time = ResponseApi().microtime(True) # if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: try: email = cek_session['data']['email'] user = UsersModel.query.filter_by(email=email).first() data = {} resultData = [] if user: user_response = user_schema.dump(user) if user_response['code_activated'] == cek_session['code_activated']: data['status'] = 'confirm' data['email'] = user_response['email'] Users = UsersModel.query.filter_by(id=user_response['id']) Users.update(data) db.session.commit() resultData.append(data) result = ResponseApi().error_response(200, "Verify Users", "Your account email is verify", start_time, resultData) else: result = ResponseApi().error_response(400, "Verify Users", "Your account email code is wrong", start_time) else: result = ResponseApi().error_response(400, "Verify Users", "Your account email is wrong", start_time) except Exception as e: error = str(e) result = ResponseApi().error_response(400, "Verify Users", error, start_time) else: result = ResponseApi().error_response(cek_session['code'], "Verify Users", cek_session['message'], start_time) # print(decode_token) # pdb.run('mymodule.test()') # else: # result = ResponseApi().error_response(400, "Verify Users", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def update_product(self, param): if Helpers().cek_auth(param): # cek_session = Helpers().cek_session(param) # if cek_session['code'] == 200: form_req = param['form'] if form_req: # try: resultData = [] for form_value in form_req: id_product = form_value['id_product'] name_product = form_value['name'] id_category = form_value['id_category'] description = form_value['description'] price = form_value['price'] stock = form_value['stock'] image = "" if form_value['image']: image_encode = form_value['image']['image_blob'] image_name = form_value['image']['name'] image_type = form_value['image']['type'] image = image_name + "." + image_type if image_encode != "" and image_name != "" and image_type != "": image_path = UPLOAD_FOLDER + image_name + '.' + image_type image_decode = Helpers().decode_image(image_encode) if image_decode != 0: image_file = Image.open( io.BytesIO(image_decode)) image_file = image_file.convert('RGB') image_file.save(image_path) else: result = { "code": 400, "endpoint": "Insert Product", "message": "image_blob is wrong", "result": {} } response = ResponseApi().response_api(result) return response Product = ProductModels.query.filter_by(id=id_product) ge_product = ProductModels.query.filter_by( id=id_product).first() data_product = product_schema.dump(ge_product) if image == "": image = data_product["image"] data = { "id": id_product, "name": name_product, "category_id": id_category, "description": description, "image": image, "stock": stock, "price": price, } Product.update(data) db.session.commit() resultData.append(data) result = { "code": 200, "endpoint": "Update Product", "message": "Update Product Succes", "result": resultData } # except Exception as e: # error = str(e) # result = { # "code" : 400, # "endpoint": "Update Product", # "message": error, # "result": {} # } else: result = { "code": 400, "endpoint": "Update Product", "message": "Form Request Is Empty", "result": {} } # else: # result = { # "code" : cek_session['code'], # "endpoint": "Update Product", # "message": cek_session['message'], # "result": {} # } else: result = { "code": 400, "endpoint": "Update Product", "message": "Authentication signature calculation is wrong", "result": {} } response = ResponseApi().response_api(result) return response
def login_user(self, param): if Helpers().cek_auth(param): form_req = param['form'] if form_req: try: validation = ValidationInput().validation_login(form_req) if validation['code'] == 200: input_data = validation['result'] email = input_data['email'] password = input_data['password'] user = UsersModel.query.filter_by(email=email).first() if user: user_response = user_schema.dump(user) if user_response['password'] == password: expired_session = (datetime.now() + timedelta( minutes=int(os.getenv('SESSION_EXPIRED')))) data_user = { "name": user_response['name'], "email": user_response['email'], "role_id": user_response['role_id'], "status": user_response['status'], "position": user_response['position'], "expired_session": expired_session.strftime( '%Y/%m/%d %H:%M:%S') } encode_token = Helpers().encode_token( data_user) update_session = self.update_session_user( user_response['id'], encode_token) if update_session: result = { "code": 200, "message": "Succes Login", "endpoint": "Login", "result": { "name": update_session['name'], "email": update_session['email'], "role_id": update_session['role_id'], "status": update_session['status'], "position": update_session['position'], "token": update_session['token'], } } else: result = { "code": 400, "endpoint": "Login", "message": "Failed Session", "result": {} } else: result = { "code": 400, "endpoint": "Login", "message": "Your account email or password is incorrect", "result": {} } else: result = { "code": 400, "endpoint": "Login", "message": "Your account email or password is incorrect", "result": {} } else: result = { "code": validation['code'], "endpoint": "Login", "message": validation['message'], "result": {} } except Exception as e: error = str(e) result = { "code": 400, "endpoint": "Login", "message": error, "result": {} } else: result = { "code": 400, "endpoint": "Login", "message": "Form Request Is Empty", "result": {} } else: result = { "code": 400, "endpoint": "Login", "message": "Authentication signature calculation is wrong", "result": {} } response = ResponseApi().response_api(result) return response
def delete_transaction(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: delete_All = form_value['delete_all'] if delete_All: Transaction = TransactionModels.query.order_by( TransactionModels.id.asc()) data_transaction = transactions_schema.dump( Transaction) for transaction_value in data_transaction: data = { 'id': transaction_value['id'], "id_company": transaction_value['id_company'], "id_user_company": transaction_value['id_user_company'], "id_user_buyer": transaction_value['id_user_buyer'], "buyer_name": transaction_value['buyer_name'], "date_time": transaction_value['date_time'], "amount_of_charge": transaction_value['amount_of_charge'], } resultData.append(data) TransactionModels.query.delete() db.session.commit() else: id_transaction = form_value['id_transaction'] Transaction = TransactionModels.query.filter_by( id=id_transaction).first() transaction_value = transaction_schema.dump( Transaction) db.session.delete(Transaction) db.session.commit() data = { 'id': transaction_value['id'], "id_company": transaction_value['id_company'], "id_user_company": transaction_value['id_user_company'], "id_user_buyer": transaction_value['id_user_buyer'], "buyer_name": transaction_value['buyer_name'], "date_time": transaction_value['date_time'], "amount_of_charge": transaction_value['amount_of_charge'], } resultData.append(data) result = ResponseApi().error_response( 200, "Delete Transaction", "Delete Transaction Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Delete Transaction", error, start_time) else: result = ResponseApi().error_response( 400, "Delete Transaction", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Delete Transaction", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Delete Transaction", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def delete_cart(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: delete_All = form_value['delete_all'] if delete_All: Cart = CartModels.query.order_by( CartModels.id.asc()) data_cart = carts_schema.dump(Cart) for cart_value in data_cart: data = { 'id_cart': cart_value['id_cart'], "id_transaction": cart_value['id_transaction'], "id_product": cart_value['id_product'], "qty": cart_value['qty'], "price": cart_value['price'], } resultData.append(data) CartModels.query.delete() db.session.commit() else: id_cart = form_value['id_cart'] Cart = CartModels.query.filter_by( id=id_cart).first() Cart_value = cart_schema.dump(Cart) db.session.delete(Cart) db.session.commit() data = { 'id_cart': Cart_value['id_cart'], "id_transaction": Cart_value['id_transaction'], "id_product": Cart_value['id_product'], "qty": Cart_value['qty'], "price": Cart_value['price'], } resultData.append(data) result = ResponseApi().error_response( 200, "Delete Cart", "Cart Delete Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Delete Cart", error, start_time) else: result = ResponseApi().error_response( 400, "Delete Cart", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Delete Cart", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Delete Cart", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def delete_product(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: delete_All = form_value['delete_all'] if delete_All: product = ProductModels.query.order_by( ProductModels.id.asc()) data_product = products_schema.dump(product) for product_value in data_product: data = { 'id_product': product_value['id'], "name": product_value['name'], } # Delete cloudinary cloudinary.api.delete_resources( [product_value['id_cloudinary']]) # Delete imagekit imagekit.delete_file( product_value['id_imagekit']) resultData.append(data) ProductModels.query.delete() db.session.commit() else: id_product = form_value['id_product'] product = ProductModels.query.filter_by( id=id_product).first() product_value = product_schema.dump(product) # Delete cloudinary cloudinary.api.delete_resources( [product_value['id_cloudinary']]) # Delete imagekit imagekit.delete_file( product_value['id_imagekit']) db.session.delete(product) db.session.commit() data = { 'id_product': product_value['id'], "name": product_value['name'], } resultData.append(data) result = ResponseApi().error_response( 200, "Delete Product", "Delete Product Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Delete Product", error, start_time) else: result = ResponseApi().error_response( 400, "Delete Product", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Delete Product", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Delete Product", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def update_product(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] image = "" if form_req: try: for form_value in form_req: id_product = form_value['id_product'] name_product = form_value['name'] id_category = form_value['id_category'] description = form_value['description'] id_company = cek_session['data']['id_company'] price = form_value['price'] stock = form_value['stock'] Product = ProductModels.query.filter_by( id=id_product) ge_product = ProductModels.query.filter_by( id=id_product).first() data_product = product_schema.dump(ge_product) id_cloudinary = data_product['id_cloudinary'] id_imagekit = data_product['id_imagekit'] save_imagekit = '' if form_value['image']: image_encode = form_value['image'][ 'image_blob'] image_name = form_value['image']['name'] image_type = form_value['image']['type'] if image_encode != "" and image_name != "" and image_type != "": image_path = UPLOAD_FOLDER + image_name + '.' + image_type image_decode = Helpers().decode_image( image_encode) if image_decode != 0: image_file = Image.open( io.BytesIO(image_decode)) image_file = image_file.convert('RGB') image_file.save(image_path) # Upload cloudinary if bool(UseCloundiary) is True: cloudinary.api.delete_resources([ data_product['id_cloudinary'] ]) upload_cloudinary = upload( image_path, folder="assets/images/", public_id=image_name + '.' + image_type) id_cloudinary = upload_cloudinary[ 'public_id'] # Upload imagekitt if bool(UseImagekit) is True: imagekit.delete_file( data_product['id_imagekit']) save_imagekit = imagekit.upload_file( file=image_encode, # required file_name=image_name + '.' + image_type, # required options={ "folder": "/assets/images/", "tags": ["sample-tag"], "is_private_file": False, "use_unique_file_name": True, "response_fields": [ "is_private_file", "tags" ], }) id_imagekit = save_imagekit[ 'response']['fileId'] # print(UseImagekit) # pdb.run('mymodule.test()') # print(upload_cloudinary) # pdb.run('mymodule.test()') else: result = { "code": 400, "SpeedTime": ResponseApi().speed_response( start_time), "endpoint": "Insert Product", "message": "image_blob is wrong", "result": {} } response = ResponseApi().response_api( result) return response if bool(UseCloundiary) is True: get_image_cloudinary = cloudinary.api.resources_by_ids( [id_cloudinary]) image = get_image_cloudinary['resources'][0][ 'secure_url'], if bool(UseImagekit) is True: get_imagekit = imagekit.get_file_details( id_imagekit) image = get_imagekit['response']['url'] if bool(UseImagekit) is False and bool( UseCloundiary) is False: image = request.url_root + display + data_product[ 'image'] datas = { "id": id_product, "name": name_product, "id_category": id_category, "id_company": id_company, "description": description, "image": image, 'id_cloudinary': id_cloudinary, 'id_imagekit': id_imagekit, "stock": stock, "price": price, } Product.update(datas) db.session.commit() Product = ProductModels.query.filter_by( id=id_product).first() data_product = product_schema.dump(Product) data = { "id": data_product['id'], "price": data_product['price'], "stock": data_product['stock'], "description": data_product['description'], "id_category": data_product['id_category'], "id_company": data_product['id_company'], "image": image, "name": data_product['name'], } resultData.append(data) result = ResponseApi().error_response( 200, "Update Product", "Update Product Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Update Product", error, start_time) else: result = ResponseApi().error_response( 400, "Update Product", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Update Product", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Update Product", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def get_product(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: image = '' show_all = form_req['show_all'] id_product = form_req['id_product'] if show_all: Product = ProductModels.query.order_by( ProductModels.id.asc()) data_product = products_schema.dump(Product) for product_value in data_product: if (product_value): id_category = ( product_value['id_category']) Category = CategoryModels.query.filter_by( id=id_category).first() data_category = category_schema.dump( Category) if bool(UseCloundiary) is True: # print(UseCloundiary) # pdb.run('mymodule.test()') get_image_cloudinary = cloudinary.api.resources_by_ids( [product_value['id_cloudinary']]) image = get_image_cloudinary[ 'resources'][0]['secure_url'] if bool(UseImagekit) is True: get_imagekit = imagekit.get_file_details( product_value['id_imagekit']) # print(get_imagekit) # pdb.run('mymodule.test()') image = get_imagekit['response']['url'] if bool(UseImagekit) is False and bool( UseCloundiary) is False: image = request.url_root + display + product_value[ 'image'] data = { "id": product_value['id'], "price": product_value['price'], "stock": product_value['stock'], "description": product_value['description'], "id_category": product_value['id_category'], "category_name": data_category.get('name'), "image": image, "name": product_value['name'], } resultData.append(data) else: Product = ProductModels.query.filter_by( id=id_product).first() data_product = product_schema.dump(Product) if (data_product): id_category = int(data_product['id_category']) Category = CategoryModels.query.filter_by( id=id_category).first() data_category = category_schema.dump(Category) if bool(UseCloundiary) is True: get_image_cloudinary = cloudinary.api.resources_by_ids( [data_product['id_cloudinary']]) image = get_image_cloudinary['resources'][ 0]['secure_url'] if bool(UseImagekit) is True: get_imagekit = imagekit.get_file_details( data_product['id_imagekit']) image = get_imagekit['response']['url'] if bool(UseImagekit) is False and bool( UseCloundiary) is False: image = request.url_root + display + data_product[ 'image'] data = { "id": data_product['id'], "price": data_product['price'], "stock": data_product['stock'], "description": data_product['description'], "id_category": data_product['id_category'], "category_name": data_category.get('name'), "image": image, "name": data_product['name'], } resultData.append(data) result = ResponseApi().error_response( 200, "Get Product", "Get Product Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Get Product", error, start_time) else: result = ResponseApi().error_response( 400, "Get Product", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Get Product", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Get Product", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def delete_company(self, param): start_time = ResponseApi().microtime(True) if Helpers().cek_auth(param): cek_session = Helpers().cek_session(param) if cek_session['code'] == 200: form_req = param['form'] resultData = [] if form_req: try: for form_value in form_req: delete_All = form_value['delete_all'] if delete_All: Company = CompanyModels.query.order_by( CompanyModels.id.asc()) data_company = companys_schema.dump(Company) for Company_value in data_company: data = { 'id': Company_value['id'], "name": Company_value['name'], "address": Company_value['address'], "no_telp": Company_value['no_telp'], "facebook": Company_value['facebook'], "instagram": Company_value['instagram'], "email": Company_value['email'], "website": Company_value['website'], } resultData.append(data) CompanyModels.query.delete() db.session.commit() else: id_company = form_value['id_company'] Company = CompanyModels.query.filter_by( id=id_company).first() Company_value = company_schema.dump(Company) db.session.delete(Company) db.session.commit() data = { 'id': Company_value['id'], "name": Company_value['name'], "address": Company_value['address'], "no_telp": Company_value['no_telp'], "facebook": Company_value['facebook'], "instagram": Company_value['instagram'], "email": Company_value['email'], "website": Company_value['website'], } resultData.append(data) result = ResponseApi().error_response( 200, "Delete Company", "Delete Company Succes", start_time, resultData) except Exception as e: error = str(e) result = ResponseApi().error_response( 400, "Delete Company", error, start_time) else: result = ResponseApi().error_response( 400, "Delete Company", "Form Request Is Empty", start_time) else: result = ResponseApi().error_response(cek_session['code'], "Delete Company", cek_session['message'], start_time) else: result = ResponseApi().error_response( 400, "Delete Company", "Authentication signature calculation is wrong", start_time) response = ResponseApi().response_api(result) return response
def insert_product(self, param): if Helpers().cek_auth(param): # cek_session = Helpers().cek_session(param) # if cek_session['code'] == 200: form_req = param['form'] if form_req: try: resultData = [] for form_value in form_req: name_product = form_value['name'] id_category = form_value['id_category'] description = form_value['description'] price = form_value['price'] stock = form_value['stock'] image = "" if form_value['image']: image_encode = form_value['image']['image_blob'] image_name = form_value['image']['name'] image_type = form_value['image']['type'] image = image_name + "." + image_type if image_encode != "" and image_name != "" and image_type != "": image_path = UPLOAD_FOLDER + image_name + '.' + image_type image_decode = Helpers().decode_image( image_encode) if image_decode != 0: image_file = Image.open( io.BytesIO(image_decode)) image_file = image_file.convert('RGB') image_file.save(image_path) else: result = { "code": 400, "endpoint": "Insert Product", "message": "image_blob is wrong", "result": {} } response = ResponseApi().response_api( result) return response new_product = ProductModels(id_category, name_product, description, image, stock, price) db.session.add(new_product) db.session.commit() data = { "name": name_product, "category_id": id_category, "description": description, "image": request.url_root + "display/" + image, "stock": stock, "price": price, } resultData.append(data) result = { "code": 200, "endpoint": "Insert Product", "message": "Insert Product Succes", "result": resultData } except Exception as e: error = str(e) result = { "code": 400, "endpoint": "Insert Product", "message": error, "result": {} } else: result = { "code": 400, "endpoint": "Insert Product", "message": "Form Request Is Empty", "result": {} } # else: # result = { # "code" : cek_session['code'], # "endpoint": "Insert Product", # "message": cek_session['message'], # "result": {} # } else: result = { "code": 400, "endpoint": "Insert Product", "message": "Authentication signature calculation is wrong", "result": {} } response = ResponseApi().response_api(result) return response
def get_product(self, param): if Helpers().cek_auth(param): # cek_session = Helpers().cek_session(param) # if cek_session['code'] == 200: form_req = param['form'] if form_req: try: resultData = [] showAll = form_req['ShowAll'] id_product = form_req['id_product'] if showAll: Product = ProductModels.query.all() data_product = products_schema.dump(Product) for product_value in data_product: data = { "image": request.url_root + "display/" + product_value['image'], "id": product_value['id'], "price": product_value['price'], "stock": product_value['stock'], "description": product_value['description'], "category_id": product_value['category_id'], "name": product_value['name'], } resultData.append(data) else: Product = ProductModels.query.filter_by( id=id_product).first() data_product = product_schema.dump(Product) data = { "image": request.url_root + "display/" + data_product['image'], "id": data_product['id'], "price": data_product['price'], "stock": data_product['stock'], "description": data_product['description'], "category_id": data_product['category_id'], "name": data_product['name'], } resultData.append(data) result = { "code": 200, "endpoint": "Get Product", "message": "Get Product Succes", "result": resultData } except Exception as e: error = str(e) result = { "code": 400, "endpoint": "Get Product", "message": error, "result": {} } else: result = { "code": 400, "endpoint": "Get Product", "message": "Form Request Is Empty", "result": {} } # else: # result = { # "code" : cek_session['code'], # "endpoint": "Get Product", # "message": cek_session['message'], # "result": {} # } else: result = { "code": 400, "endpoint": "Get Product", "message": "Authentication signature calculation is wrong", "result": {} } response = ResponseApi().response_api(result) return response