def post_pet(user_id): """ Creates a new Pet """ user = storage.get(User, user_id) if not user: abort(404) if not request.get_json(): abort(400, description="Not a JSON") if 'name' not in request.get_json(): abort(400, description="Missing name") data = request.get_json() data["user_id"] = user_id pet = Pet(**data) pet.save() # creating the instance Picture for this pet picture = Picture(pet_id=pet.id) picture.save() return make_response(jsonify(pet.to_dict()), 201)
def purchase () : current_user = get_jwt_identity() purchased_artwork = Picture.select().where(Picture.buyer_id==current_user) bidding_artwork = Picture.select().where((Picture.bidder_id== current_user) & (Picture.buyer_id == None) ) purchased_info = [] for p in purchased_artwork : art_art = {} art_art['name'] = p.name art_art['category'] = p.category art_art['description'] = p.description art = Picture.get(Picture.name == p.name).profilepic_url art_art['image'] = art art_art['id'] = p.id art_art['price'] = p.price art_art['paid'] = p.paid purchased_info.append(art_art) bidding_info = [] for b in bidding_artwork : bid_bid = {} bid_bid['name'] = b.name bid_bid['category'] = b.category bid_bid['description'] = b.description art = Picture.get(Picture.name == b.name).profilepic_url bid_bid['image'] = art bid_bid['id'] = b.id bid_bid['price'] = b.price bid_bid['paid'] = b.paid bidding_info.append(bid_bid) return jsonify(purchase=purchased_info, bid =bidding_info)
def upload_file(username): # A if "user_file" not in request.files: return "No user_file key in request.files" # B file = request.files["user_file"] # C. if file.filename == "": return "Please select a file" # D. if file and allowed_file(file.filename): file.filename = secure_filename(file.filename) output = upload_file_to_s3(file, S3_BUCKET) picture = Picture(picture_name=str(output), user=current_user.id) if picture.save(): flash('Photo successfully uploaded') return redirect(url_for('users.show', username=current_user.name)) else: return redirect(url_for('users.upload_file')) else: return redirect(url_for('users.upload'))
def otherprofiles(id): user = User.get(User.id == id) allinfo = {} allinfo['username'] = user.username allinfo['bio'] = user.bio allinfo['profilepic'] = user.profilepic_url allpic = Picture.select().where(Picture.artist_id == id) pics_info = [] for a in allpic: picpic = {} picpic['name'] = a.name picpic['category'] = a.category picpic['description'] = a.description paint = Picture.get(Picture.name == a.name).profilepic_url picpic['image'] = paint picpic['id'] = a.id picpic['price'] = a.price picpic['sold'] = a.sold if a.bidder_id: user = User.get(User.id == a.bidder_id) picpic['bidder_name'] = user.username pics_info.append(picpic) else: picpic['bidder_name'] = "None" pics_info.append(picpic) allinfo['artwork'] = pics_info return jsonify(allinfo)
def upload_pet_pictures(pet_id): """ Upload the picture of a pet """ data = request.get_data() if not data: abort(400, "Missing Photo") picture = Picture(data=data, pet_id=pet_id, name='photo_' + pet_id) picture.save() return make_response(jsonify({"status": "OK"}), 201)
def _store_image(self, picture, pic_id): """ Store an image with an associated pic_id into the storage. :param picture: that is sent from a user. :param pic_id: is that newly generated for the image. :return: None """ # Store the picture in a database pic = Picture(uuid=pic_id, data=picture) pic.insert() return None
def offer(): allpic = Picture.select() pics_info = [] for a in allpic: picpic = {} picpic['name'] = a.name picpic['category'] = a.category picpic['description'] = a.description paint = Picture.get(Picture.name == a.name).profilepic_url picpic['image'] = paint picpic['id'] = a.id pics_info.append(picpic) return jsonify(pics_info)
def client_token(picture_id): picture = Picture.get_by_id(picture_id) client_token = gateway.client_token.generate() return render_template('/payment/donate.html', client_token=client_token, picture=picture, picture_id=picture_id)
def put(self, pessoa_id: int, album_id: int, id: int): args = self.req.parse_args() descri = args['descri'] date_create = args['date_create'] conteudo = args['conteudo'] picture = Picture(descri, date_create, conteudo, id) return self.dao.update(picture)
def post(self, pessoa_id: int, album_id: int): args = self.req.parse_args() descri = args['descri'] date_create = args['date_create'] conteudo = args['conteudo'] picture = Picture(conteudo, descri, date_create) return self.dao.create(picture), 201
def update(): form = request.json print('form', form) data = Img.update(id=form['id'], show=str(form['enable'])) print('data', data) r = Res.success(data) return make_response(jsonify(r))
def addProductType(): # form : product_id, bar_code form = request.form.to_dict() print('form', form) # 上传图片 file = request.files['file'] if file is not None: # 储存图片获取数据 data = Img.save_one(file, form) print('upload data', data) if data['src'] is not None and base_url not in data['src']: data['src'] = base_url + '/' + data['src'] form['cover'] = data['src'] if data is not None: r = Res.success(data) else: r = Res.fail({}, msg='图片已存在') print('新图片', form) product = Product.add(form) if type(product) is str: r = Res.fail(msg=product) else: all = Product.all() print('all', all) r = Res.success(all) return make_response(jsonify(r))
def sold () : pic_id = request.form.get('id') picture = Picture.get(Picture.id == pic_id) picture.buyer_id = picture.bidder_id picture.sold = True picture.save() return jsonify(message="You Have Accepted the Bidder's Offer")
def detail(id): a = Picture.get(Picture.id == id) artist = a.artist_id user = User.get(User.id == artist) artist_name = user.username picpic = {} picpic['name'] = a.name picpic['category'] = a.category picpic['description'] = a.description paint = Picture.get(Picture.name == a.name).profilepic_url picpic['image'] = paint picpic['price'] = a.price picpic['artist'] = artist_name picpic['artist_id'] = str(artist) picpic['sold'] = a.sold return jsonify(picpic)
def paid () : pic_id = request.form.get('id') picture = Picture.get(Picture.id == pic_id) picture.paid = True picture.save() return jsonify(message="You Purchased the Artwork!")
def get(self): key = self.request.path.split('/')[-1] picture = Picture.get(key) if not picture: self.error(404) return self.response.headers['Content-Type'] = picture.content_type.__str__() self.response.out.write(picture.picture)
def delete_one(): id = request.json.get('id') data = Img.delete_one(id=id) if data is None: r = Res.success() else: r = Res.fail() return make_response(jsonify(r))
def delete(): form = request.json data = Img.delete_one(id=form.get('id')) print('delete form', data is None) if data is None: r = Res.success() else: r = Res.fail(msg='图片删除失败') return make_response(jsonify(r))
def profile_add_picture(form, file): def allowed_file(filename): return '.' in filename and filename.rsplit( '.', 1)[1].lower() in ALLOWED_EXTENSIONS if 'picture' not in file: session['error'] = "Aucun fichier n'a été sélectionné." file_to_save = file['picture'] if file_to_save.filename == '': session['error'] = "Aucun fichier n'a été sélectionné." if not allowed_file(file_to_save.filename): session[ 'error'] = "Mauvaise extension, veuillez sélectionner un autre fichier." if file_to_save and allowed_file(file_to_save.filename): filename = secure_filename(file_to_save.filename) user = User.find_by('username', form['username']) ext = filename.rsplit('.', 1)[1].lower() filename = user.getUserName() + "_" + form['number'] + "." + ext pathlib.Path('static/users_pictures').mkdir(parents=True, exist_ok=True) path_to_upload = os.path.join(app.config['UPLOAD_FOLDER'], filename) file_to_save.save(path_to_upload) if Picture.getPicName(user.getId(), form['number']) == None: infos = {} infos['user_id'] = user.getId() infos['data'] = filename picture = Picture.create(infos) else: expression = user.getUserName() + "_" + form['number'] + ".%" picture = Picture.find_like('data', expression) picture.modif('data', filename) picture.save() # SUPPR. if ext differente else bourrage du dossier users_pictures if form['number'] == '1': user.modif('main_picture', picture.getId()) user.save() return redirect(url_for('profile', username=form['username']))
def delete_more(): form = request.json print('delete_more form', form) data = Img.delete_by_ids(ids=form['ids']) print('delete_more len', len(data)) if len(data) is 0: r = Res.success() else: r = Res.fail() return make_response(jsonify(r))
def upload_image(): user_id = request.form.get("user_id") if request.files.getlist("images"): files = request.files.getlist("images") for file_obj in files: is_valid, error_msg = _is_file_valid(file_obj, request) if False in is_valid.values(): resp = response(HTTPStatus.BAD_REQUEST, error_msg[0]) return jsonify(resp), HTTPStatus.BAD_REQUEST image = Picture(str(uuid.uuid4()), file_obj.filename) uploaded = image.add_image(file_obj, user_id) if uploaded: resp = response(HTTPStatus.CREATED, "Successfully uploaded {} images".format(len(files))) return jsonify(resp), HTTPStatus.CREATED else: resp = ( response(HTTPStatus.NO_CONTENT, "Could not upload image"), HTTPStatus.NO_CONTENT, )
def delete_image(): user_id = request.form.get("user_id") if request.form.get("image_id"): file_name = request.form.get("image_id") deleted = Picture().delete_image(file_name, user_id) elif request.args.get("bulk"): deleted = Picture().bulk_delete(user_id) else: resp = response( HTTPStatus.BAD_REQUEST, "Image id not provided, nor bulk_delete option was not selected", ) return jsonify(resp), HTTPStatus.BAD_REQUEST if deleted: resp = response(HTTPStatus.MOVED_PERMANENTLY, "Successfully deleted image(s)") return jsonify(resp), HTTPStatus.MOVED_PERMANENTLY resp = response(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not delete image") return jsonify(resp), HTTPStatus.INTERNAL_SERVER_ERROR
def queryImageByName(): form = request.args.to_dict() print('根据标题搜索数据', form) data, count = Img.queryImageByCondition(**form) d = dict( list=data, count=count ) r = Res.success(d) print('queryImageByName 结果', r) return make_response(jsonify(r))
def upload(): form = request.files['file'] # 储存图片获取数据 data = Img.save_one(form) print('upload data', data) if data['src'] is not None and base_url not in data['src']: data['src'] = base_url + '/' + data['src'] if data is not None: r = Res.success(data) else: r = Res.fail({}, msg='图片已存在') return make_response(jsonify(r))
def list_all(self): connection = MySQLdb.connect(host="mysql.zuplae.com", user="******", passwd="lend4s", database="zuplae15") cursor = connection.cursor() cursor.execute("SELECT * FROM FOTO") list_picture = [] for p in cursor.fetchall(): picture = Picture(p[1], p[2], p[3], p[0]) list_picture.append(picture.__dict__) connection.close() return list_picture
def picture_by_id(self, id): connection = MySQLdb.connect(host="mysql.zuplae.com", user="******", passwd="lend4s", database="zuplae15") cursor = connection.cursor() cursor.execute( f"SELECT ID, CONTEUDO, DESCRICAO, DATA_CRIACAO FROM PESSOA WHERE ID = {id}" ) p = cursor.fetchone() picture = Picture(p[1], p[2], p[3], p[0]) connection.close() return picture.__dict__
def create(self, picture: Picture): connection = MySQLdb.connect(host="mysql.zuplae.com", user="******", passwd="lend4s", database="zuplae15") cursor = connection.cursor() cursor.execute( f"INSERT INTO FOTO (CONTEUDO, DESCRICAO, DATA_CRIACAO) VALUES('{picture.conteudo}','{ picture.descri}','{ picture.date_create}')" ) picture.id = cursor.lastrowid connection.commit() connection.close() return picture.__dict__
def bid(): description = request.form.get('description') price = request.form.get('price') bidprice = int(price) user_id = request.form.get('id') pic = Picture.get(Picture.description == description) currentprice = int(pic.price) if currentprice < bidprice: pic.price = price pic.bidder_id = user_id pic.save() return jsonify(message="You are currently the highest bidder") else: return jsonify(message="You are not the highest bidder")
def post(self): try: logging.info('Updating started - %s' % self.request.get('key')) comic = self.comics_classes[self.request.get('key')]() response = comic.fetch() content_type = response.headers["content-type"] picture = response.content if not content_type.startswith("image/"): logging.info('Content-type is not an image') return rows = db.GqlQuery('SELECT * FROM Picture WHERE date = :1 and name = :2 and host = :3', date.today(), comic.name, comic.host) if rows.count() > 0: if rows[0].picture == picture: return else: for row in rows: row.delete() db_object = Picture( name=comic.name, host=comic.host, group=comic.group, url=comic.url, picture=picture, content_type=content_type, date=date.today(), sort_order=comic.sort_order) logging.info('Saving new picture') db_object.put() except Exception, e: logging.error(e)
def profile(): current_user = get_jwt_identity() allpic = Picture.select().where(Picture.artist_id == current_user) pics_info = [] for a in allpic: picpic = {} picpic['name'] = a.name picpic['category'] = a.category picpic['description'] = a.description paint = Picture.get(Picture.name == a.name).profilepic_url picpic['image'] = paint picpic['id'] = a.id picpic['price'] = a.price picpic['sold'] = a.sold if a.bidder_id: user = User.get(User.id == a.bidder_id) picpic['bidder_name'] = user.username pics_info.append(picpic) else: picpic['bidder_name'] = "None" pics_info.append(picpic) # purchased_artwork = Picture.select().where(Picture.buyer_id==current_user) # purchased_info = [] # for p in purchased_artwork : # art_art = {} # art_art['name'] = p.name # art_art['category'] = p.category # art_art['description'] = p.description # art = Picture.get(Picture.name == p.name).profilepic_url # art_art['image'] = art # art_art['id'] = p.id # art_art['price'] = p.price # purchased_info.append(art_art) return jsonify(artwork=pics_info)
def post(self): parse = reqparse.RequestParser() parse.add_argument("image", type=FileStorage, location="files") args = parse.parse_args() picture = PictureDB() picture.id = str(uuid.uuid4()) picture.id_property = request.form.get("id_property") picture.title = request.form.get("title") picture.subtitle = request.form.get("subtitle") picture.description = request.form.get("description") picture.blob_identifier = DriveHelper().upload_file( args.get("image").stream, args.get("image").content_type, args.get("image").filename) picture.order = request.form.get("order") picture.year = request.form.get("year") db.session.add(picture) db.session.commit() return {"message": "Picture saved", "data": picture.id}, 200
def paintingsubmit(): name = request.form.get('name') category = request.form.get('category') description = request.form.get('description') userid = request.form.get('id') file = request.files["picture"] if file and allowed_file(file.filename): file.filename = secure_filename(file.filename) output = upload_file_to_s3(file, app.config["S3_BUCKET"]) picture = Picture.create(name=name, description=description, category=category, image=file.filename, artist_id=userid) return make_response('Artwork Submitted')