def add_image(user_id, files): file_name = files.filename if file_name == '': return response.gagal('failed', 'null file', 404) else: try: user_select = Users.query.get(user_id) path = os.path.join(app.config['UPLOAD_FOLDER'], chgName(files.filename)) if valid_ext(path) is True: image = Images(image=path, user_image=user_select) files.save(path) db.session.add(image) # add to database db.session.commit() return response.sukses('berhasil upload', '') else: return response.gagal('ekstensi harus .jpg .jpeg .png', '', 400) except Exception as e: return response.gagal('failed', f'Error : {type(e).__name__}, {e.args[0]}', '400')
def del_user(id_user): try: delUser = Users.query.filter_by(id_user=id_user).first() db.session.delete(delUser) db.session.commit() return response.sukses('user berhasil dihapus', '') except Exception as e: return response.gagal('user gagal dihapus', f'Error : {type(e).__name__}, {e.args[0]}', 400)
def profile(): token = get_jwt_identity() print(request.headers) try: users = Users.query.filter_by(id_user=token['id']).first() data = transform(users) except AttributeError as err_attr: return response.gagal(f'user dengan id={user_id} tidak ada!', f'Error : {type(err_attr).__name__}, {err_attr.args[0]}', 404) return response.sukses('user ditampilkan', data)
def show_all_product(): try: products = Products.query.all() data = transform_product(products) return response.sukses('products loaded!', data) except Exception as e: return response.gagal('can\'t load products!', f'Error : {type(e).__name__}, {e.args[0]}', '400') Error: {type(e).__name__}
def login(): try: data = request.get_json() username = data['username'] password = data['password'] except Exception as e: return response.gagal('login gagal', f'Error : {type(e).__name__} in {e.args[0]}', 404) return userController.login(username, password)
def update_user(): try: data = request.get_json() username = data['username'] password = data['password'] email = data['email'] except Exception as e: return response.gagal('user gagal diupdate', f'Error : {type(e).__name__} in {e.args[0]}', 404) return userController.upd_user(username, password, email)
def add_user(): data = request.get_json() try: name = data['username'] email = data['email'] pwd = data['password'] except Exception as e: return response.gagal('user gagal ditambah', f'Error : {type(e).__name__} in {e.args[0]}', 400) return userController.add_user(name, email, pwd)
def add_user(user, email, pwd, role='member'): hashedPw = bcrypt.generate_password_hash(pwd).decode('utf-8') try: addUser = Users(name=user, email=email, password=hashedPw, role=role) db.session.add(addUser) db.session.commit() return response.sukses('user berhasil ditambahkan', '') except Exception as e: db.session.rollback() return response.gagal('user gagal ditambahkan!', f'Error : {type(e).__name__}, {e.args[0]}', 400)
def login(username, passwd): user_select = Users.query.filter_by(name=username).first() if user_select: if bcrypt.check_password_hash(user_select.password, passwd): expires = datetime.timedelta(days=1) expires_refresh = datetime.timedelta(days=1) access_token = create_access_token({'id':user_select.id_user, 'expires':datetime.datetime.utcnow()+expires}, fresh=True, expires_delta=expires) refresh_token = create_refresh_token({'id':user_select.id_user, 'expires':datetime.datetime.utcnow()+expires_refresh}, expires_delta=expires_refresh) token = { 'token': access_token, 'refresh_token': refresh_token } return response.sukses('login berhasil!', token) else: return response.gagal('password salah!', '', 401) else: return response.gagal(f'user tidak terdaftar', '', 404)
def show_product(id_user, p): user = Users.query.get(id_user) try: products = user.product.all() except Exception as e: return response.gagal('product is empty!', f'Error : {type(e).__name__}, {e.args[0]}', '400') data = transform_product(products) arr = [] for product in data: arr.append(product) if p is None: return response.sukses('products reloaded!', data) else: try: return response.sukses('product reloaded!', arr[p - 1]) except Exception as e: return response.gagal('product not found!', f'Error : {type(e).__name__}, {e.args[0]}', '400')
def upd_user(uname, pwd, email): token = get_jwt_identity() try: user = Users.query.filter_by(id_user=token['id']).first() hashedPw = bcrypt.generate_password_hash(pwd).decode('utf-8') user.password = hashedPw user.name = uname user.email = email db.session.commit() return response.sukses('user berhasil diupdate', '') except Exception as e: return response.gagal('user gagal diupdate', f'Error : {type(e).__name__}, {e.args[0]}', 404 )
def create_product(id_user): try: data = request.get_json() name = data['name'] description = data['description'] price = data['price'] stock = data['stock'] category = data['category'] except Exception as e: return response.gagal('produk gagal ditambahkan', f'Error : {type(e).__name__} in {e.args[0]}', 404) return productController.create_product(name, description, price, category, stock, id_user)
def users_all(): arr = [] try: users = Users.query.all() for user in users: arr.append({ 'id': user.id_user, 'name': user.name, 'email': user.email, 'created_at':user.created_at, 'role':user.role }) return response.sukses('', arr) except Exception as e: return response.gagal('user gagal ditampilkan!', f'Error : {type(e).__name__}, {e.args[0]}', 404)
def create_product(name, desc, price, category, stock, user_id): try: user_select = Users.query.get(user_id) product = Products(name=name, description=desc, price=price, category=category, stock=stock, user_product=user_select) db.session.add(product) db.session.commit() return response.sukses('product created', '') except Exception as e: db.session.rollback() return response.gagal(f'failed to add product!', f'Error : {type(e).__name__}, {e.args[0]}', '400')