def update_series(curr_user, id): user = curr_user if not user.is_admin(): return jsonify({"success": False, "message": "You're not authorized!"}) data = request.get_json() try: series = Series.query.get(id) if not series: response = ResponseMessage(False, message="Invalid series id!") return response.resp() except: response = ResponseMessage(False, message="Invalid series id!") return response.resp() if series.next_num != series.start_num: response = ResponseMessage(False, message="Unable to update series! \ Series in already used!") return response.resp() code = data['code'] name = data['name'] whsecode = data['whsecode'] objtype = data['objtype'] start_num = data['start_num'] next_num = data['next_num'] end_num = data['end_num'] if Series.query.filter_by(code=code, objtype=objtype).first(): raise Exception("Already exist!") if code: series.code = code if whsecode: series.whsecode = whsecode if name: series.name = name if objtype: series.objtype = objtype if start_num: series.start_num = start_num if next_num: series.next_num = next_num if end_num: series.end_num = end_num try: db.session.commit() series_schema = SeriesSchema() result = series_schema.dump(series) response = ResponseMessage(True, data=result) return response.resp() except: db.session.rollback() response = ResponseMessage(False, message="Unable to update!") return response.resp() finally: db.session.close()
def delete_objtype(curr_user, id): user = curr_user if not user.is_admin(): response = ResponseMessage(False, message="You're not authorized!") return response.resp() try: obj = ObjectType.query.get(id) if not obj: response = ResponseMessage(False, message=f"Invalid object type id!") return response.resp() except: response = ResponseMessage(False, message=f"Invalid object type id!") return response.resp() try: db.session.delete(obj) db.session.commit() obj_schema = ObjectTypeSchema() result = obj_schema.dump(obj) response = ResponseMessage(True, message="Successfully deleted", data=result) return response.resp() except: db.session.rollback() response = ResponseMessage(False, message=f"Unable to delete!") return response.resp() finally: db.session.close()
def delete_branch(curr_user, id): user = curr_user if not user.is_admin(): response = ResponseMessage(False, message="You're not authorized!") return response.resp() branch = Branch.query.get(id) if not branch: response = ResponseMessage(False, message="Invalid branch id!") return response.resp() try: db.session.delete(branch) db.session.commit() except Exception as err: db.session.rollback() response = ResponseMessage(False, message=f"{err}") return response.resp() except: db.session.rollback() response = ResponseMessage(False, message="Unable to delete!") return response.resp() finally: db.session.close() branch_schema = BranchSchema() result = branch_schema.dump(branch) response = ResponseMessage(True, message=f"Successfully deleted!", data=result) return response.resp()
def get_series(curr_user, id): try: series = Series.query.get(id) if not series: response = ResponseMessage(False, message="Invalid series id!") return response.resp() except: response = ResponseMessage(False, message="Invalid series id!") return response.resp() series_schema = SeriesSchema() result = series_schema.dump(series) response = ResponseMessage(True, data=result) return response.resp()
def delete_branch(curr_user, id): # Check if user is admin if not curr_user.is_admin(): return ResponseMessage(False, message="Unauthorized user!").resp(), 401 try: u = User.query.get(id) if not u: return ResponseMessage(False, message="Invalid user id!").resp(), 401 db.session.delete(u) db.session.commit() user_schema = UserSchema() result = user_schema.dump(u) response = ResponseMessage(True, message=f"Successfully deleted!", data=result) return response.resp() except (exc.IntegrityError, pyodbc.IntegrityError) as err: db.session.rollback() return ResponseMessage(False, message=f"{err}").resp(), 500 except Exception as err: db.session.rollback() return ResponseMessage(False, message=f"{err}").resp(), 500 finally: db.session.close()
def get_all_branch(curr_user): q = request.args.get('q') if q: branch = Branch.query.filter( Branch.code.contains(q) | Branch.name.contains(q)).all() else: branch = Branch.query.all() if not branch: response = ResponseMessage(False, message="Invalid search keyword!") return response.resp() branch_schema = BranchSchema(many=True) result = branch_schema.dump(branch) response = ResponseMessage(True, data=result) return response.resp()
def update_objtype(curr_user, id): if not curr_user.is_admin(): return ResponseMessage(False, message="You're not authorized!").resp(), 401 try: obj = ObjectType.query.get(id) if not obj: return ResponseMessage( False, message=f"Invalid object type id!").resp(), 401 except Exception as err: return ResponseMessage(False, message=f"{err}").resp(), 500 objtype = request.args.get('objtype') description = request.args.get('description') table = request.args.get('table') if objtype: obj.objtype = objtype if description: obj.description = description if table: obj.table = table obj.updated_by = user.id obj.date_updated = datetime.now try: db.session.commit() except: db.session.rollback() response = ResponseMessage(False, message=f"Unable to update!") return response.resp() finally: db.session.close() obj_schema = ObjectTypeSchema() result = obj_schema.dump(obj) response = ResponseMessage(True, message="Successfully updated", data=result) return response.resp()
def delete_series(curr_user, id): user = curr_user if not user.is_admin(): return jsonify({"success": False, "message": "You're not authorized!"}) try: series = Series.query.get(id) if not series: response = ResponseMessage(False, message="Invalid series id!") return response.resp() except: response = ResponseMessage(False, message="Invalid series id!") return response.resp() try: db.session.delete(series) db.session.commit() series_schema = SeriesSchema() result = series_schema.dump(series) response = ResponseMessage(True, data=result) return response.resp() except Exception as err: db.session.rollback() response = ResponseMessage(False, message=err) return response.resp() except (IntegrityError, pyodbc.IntegrityError) as err: db.session.rollback() response = ResponseMessage(False, message=err) return response.resp() except: db.session.rollback() response = ResponseMessage(False, message="Unable to delete!") return response.resp() finally: db.session.close()
def get_branch(curr_user, id): try: branch = Branch.query.get(id) if not branch: return ResponseMessage(False, message="Invalid branch id!").resp(), 401 branch_schema = BranchSchema() result = branch_schema.dump(branch) response = ResponseMessage(True, data=result) return response.resp() except IntegrityError as err: return ResponseMessage(False, message=f"{err}").resp(), 500 except DataError as err: return ResponseMessage(False, message=f"{err}").resp(), 500
def get_all_series(curr_user): q = request.args.get('q') if q: series = Series.query.filter( Series.code.contains(q) | Series.name.contains(q)).all() else: series = Series.query.all() series_schema = SeriesSchema(many=True, only=("id", "code", "name", "objtype", \ "start_num", "next_num", "end_num")) result = series_schema.dump(series) response = ResponseMessage(True, data=result) return response.resp()
def get_all_objtype(curr_user): q = request.args.get('q') if q: obj = ObjectType.query.filter_by(ObjectType.objtype.contains(q) | ObjectType.description.contains(q) \ | ObjectType.table.contains(q)).all() else: obj = ObjectType.query.all() obj_schema = ObjectTypeSchema(many=True, only=("id", "objtype", "description", "table")) result = obj_schema.dump(obj) response = ResponseMessage(True, data=result) return response.resp()
def new_series(curr_user): user = curr_user if not user.is_admin(): return jsonify({"success": False, "message": "You're not authorized!"}) data = request.get_json() code = data['code'] name = data['name'] whsecode = data['whsecode'] objtype = data['objtype'] start_num = data['start_num'] next_num = data['next_num'] end_num = data['end_num'] if not code or not name or not objtype or not start_num or not next_num \ or not end_num or not whsecode: response = ResponseMessage(False, message=f"Missing required field!") return response.resp() try: if not Warehouses.query.filter_by(whsecode=whsecode).first(): raise Exception("Invalid whsecode") if Series.query.filter_by(code=code, objtype=objtype).first(): raise Exception("Already exist!") series = Series(code=code, name=name, objtype=objtype, start_num=start_num, \ next_num=next_num, end_num=end_num, created_by=user.id, whsecode=whsecode \ # ,updated_by=user.id ) db.session.add(series) db.session.commit() series_schema = SeriesSchema() result = series_schema.dump(series) return ResponseMessage(True, message="Successfully added", data=result).resp() except Exception as err: db.session.rollback() return ResponseMessage(False, message=f"{err}").resp() except (pyodbc.IntegrityError, IntegrityError) as err: db.session.rollback() return ResponseMessage(False, message=f"{err}").resp() except: db.session.rollback() return ResponseMessage(False, message=f"Unable to add!").resp() finally: db.session.close()
def get_auth_token(): try: username = request.args.get('username') password = request.args.get('password') user = User.query.filter_by(username=username).first() if user: if user.verify_password(password): token = user.generate_auth_token() user_schema = UserSchema(exclude=("password", "date_created")) result = user_schema.dump(user) response = ResponseMessage(True, message="You logged in successfully!", token=token, data=result) return response.resp() else: return ResponseMessage(False, message='Invalid password!').resp(), 401 else: return ResponseMessage(False, message='Invalid username').resp(), 401 except Exception as err: return ResponseMessage(False, message=str(err)).resp(), 500