def put(self): args = Arguments(request.json) args.integer("id") args.boolean("is_primary") args.string("image64", required=True) args.string("image_type", required=True) args.validate() user = get_jwt_identity() data = dict(args) data["user_id"] = user["id"] image = Image.get(id=data.get("id", None), user_id=user["id"]) if image: image.image64 = data["image64"] image.image_type = data["image_type"] image.is_primary = data.get("is_primary", False) else: image = Image(data) try: image.save() return {"message": "Image saved"}, 200 except Exception as e: return {"message": str(e)}, 400
def upload_image(): file = request.files.get('user_image') file.filename = secure_filename(file.filename) caption = request.form.get('caption') if file: if not upload_file_to_s3(file): flash('Something wrong', 'warning') return redirect(url_for('users.profile', id=current_user.id)) user = User.get_or_none(User.id == current_user.id) image = Image(user_image=f'http://jynmunbucket.s3.amazonaws.com/' + file.filename, user_id=user.id, user_caption=caption) # image.user_image = file.filename image.save() else: flash("file can't be uploaded", 'warning') return redirect(url_for('users.profile', id=current_user.id)) flash('Successfully updated') return redirect(url_for('users.profile', id=current_user.id))
def get_image(img_id): if any(char in img_id for char in punctuation): flash('Invalid Post ID') return redirect('/', 302) if len(img_id) != 32: flash('Invalid Post ID') return redirect('/', 302) img = User.getImagebyImgID(img_id) if img: image_binary = base64.b64decode(img.get('image')) if "/images/display/" not in request.path: usr = User.getUserByID(img.get('userID')) if usr: usr.update({'totalDownloads': usr.get('totalDownloads') + 1}) User.updateUserInfo(user=usr, _id=usr.get('_id')) ImageC.updateImgData(img_id=img_id, Download=True) else: print('Error: Couldn\'t find User..') # image_binary = read_image(pid) # response = make_response(image_binary) # response.headers.set('Content-Type', 'image/jpeg') if img.get('name'): return send_file( BytesIO(image_binary), mimetype='image/jpeg', as_attachment=True, attachment_filename=f"{img.get('name').title()}\'s Picture - \ Atypical.jpeg") return jsonify({'error': 'Image Corrupt!'}) else: return jsonify({'error': 'Image Not Found!'})
def uploadImage(_cu, categories, image, img_id, description="", hashtags=[]): usr = User.verifySession(_cu) if usr: hashtags_ = re.findall(r"#(\w+)", description) if hashtags_: hashtags_ = [hashtag.lower() for hashtag in hashtags_] hashtags_.extend(hashtags) img = Image(userID=usr.get('_id'), categories=categories, image=image, description=description, tags=hashtags_, img_id=img_id) img.saveImg() Category.updateCategories(categories, inc=1) # Category.updateCategory(cat) User.updateUploads(_cu) return True return 0
def create(): if "user_file" not in request.files: return "No user_file key in request.files" file = request.files["user_file"] output = upload_file_to_s3(file) Image.create(image_name=str(output), user_id=current_user.id) return render_template('images/create.html', current_user=current_user)
def changeDescription(description, img_id): hashtags = re.findall(r"#(\w+)", description) if hashtags: hashtags = [hashtag.lower() for hashtag in hashtags] print('calling updateImgData') return Image.updateImgData(img_id, description, hashtags)
def post(self): """ Doc string to describe function """ args = Arguments(request.json) args.string("matchee_id", required=True) args.validate() user = get_jwt_identity() images = Image.check_images(user_id=user["id"]) if not images["has_images"]: return { "message": "You cannot like a user if you have no profile images.", "no_photo": True }, 401 if Match.get(matchee_id=args.matchee_id, matcher_id=user["id"]): return {"message": "Already liked."}, 200 try: match = Match(matchee_id=args.matchee_id, matcher_id=user["id"]) match.save() except Exception as e: return {"message": str(e)}, 500 return {"message": "User liked."}, 200
def delete(): if (request.method == "GET"): return render_template("delete_image.html") if (request.method == "POST"): new_image = Image.objects(title=request.form["title"]).first() if new_image is not None: new_image.delete() return render_template("delete_image.html")
def new(): if current_user: got_image = Image.select().where(Image.user_id == current_user.id) followers = FollowerFollowing.select().where( FollowerFollowing.fan == current_user.id) following = FollowerFollowing.select().where( FollowerFollowing.idol != current_user.id, FollowerFollowing.fan == current_user.id) return render_template('sessions/new.html', currentuser_name=current_user.name, got_image=got_image, followers=followers, following=following)
def add(): if (request.method == "GET"): return render_template("add_image.html") if (request.method == "POST"): file = request.files["source"] if file: filename = secure_filename(file.filename) if os.path.exists( os.path.join( os.path.join(app.config["UPLOAD_PATH"], filename))): name_index = 0 #filename =home.png original_name = filename.rsplit('.', 1)[0] original_extension = filename.rsplit('.', 1)[1] while os.path.join(app.config["UPLOAD_PATH"], filename): name_index += 1 # new filename = home (1).png filename = "{0} ({1}).{2}".format(original_name, name_index, original_extension) #change filename add(name_index) file.save(os.path.join(app.config["UPLOAD_PATH"], filename)) new_image = Image() new_image.src = url_for('uploaded_file', filename=filename) new_image.title = request.form["title"] new_image.description = request.form["description"] new_image.save() return render_template("add_image.html")
def upload_form(): if "user_file" not in request.files: flash('No user_file key in request.files', 'danger') return render_template('users/upload.html') file = request.files["user_file"] if file.filename == "": return flash('Please select a file', 'danger') if file and allowed_file(file.filename): file.filename = secure_filename(file.filename) # breakpoint() output = upload_file_to_s3(file, S3_BUCKET) new_image = Image(user_id=current_user.id, image_url=str(output)) if new_image.save(): # breakpoint() return redirect(url_for('users.upload'))
def get_full_user(user_id): user = User.get(id=user_id) images = Image.get_many(user_id=user_id) # Get the images for that user user.append_field("images", Field(list, images)) return user
def usr_img_upload(): usr_img = request.files.get('user_image') if not 'user_image' in request.files: flash('no image has been provided', 'danger') return redirect(url_for('sessions.new')) if not upload_file_to_s3(usr_img): file.filename = secure_filename(usr_img.filename) flash('Oops! Something went wrong while uploading', 'warning') return redirect(url_for('sessions.new')) else: user = User.get_or_none(User.id == current_user.id) caption = request.form.get('img_caption') user_img = Image( user=user.id, user_img=usr_img.filename, caption=caption) user_img.save() flash('Image successfully uploaded!', 'success') return redirect(url_for('sessions.new'))
def update(): if (request.method == "GET"): return render_template("update_image.html") if (request.method == "POST"): new_image = Image.objects(title=request.form["title"]).first() if new_image is not None: new_image.src = request.form["new_source"] new_image.description = request.form["new_description"] new_image.save() return render_template("update_image.html")
def parse_image(x): md5 = x.get(u"图片文件名") chapter, section, subsection = parse_section(x.get(u"章节")) p_id = x.get(u"段落") or 0 paragraph = Paragraph.query.filter(Paragraph.chapter == chapter, Paragraph.section == section, Paragraph.subsection == subsection).all()[p_id] paragraph_id = paragraph.id desc = x.get(u"图片说明") image = Image(paragraph_id, md5, desc) db.session.add(image)
def upload_photos(id): if request.method == 'POST': file_obj = request.files print(len(file_obj)) user=User.get_or_none(User.id == id) for f in file_obj: file = request.files.get(f) # file.filename = secure_filename(file.filename) if upload_file_to_s3(file, S3_BUCKET): image = Image(file_path=file.filename, user=user) if image.save(): flash(f'Successfully saved image {file.filename} in database') # return redirect(url_for('images.results')) else: flash(f'Image {file.filename} could not be saved.') # return redirect(url_for('images.share', id=id)) return redirect(url_for('images.results', id = user.id))
def upload(): session_token = request.cookies.get("session_token") user = db.query(User).filter_by(session_token=session_token).first() img = db.query(Image).filter_by(author_id=user.id).first() image_url = request.json.get('image_url') if user.image_count >= 1: db.delete(img) db.commit user.image_count += 1 db.commit() if image_url: newImage = Image(author=user, image_url=image_url) newImage.insert() return jsonify(newImage.to_dict) else: return jsonify({"success": False, "message": "fdfdfdfdfdfdfd"})
def upload_images(): if "user_image" not in request.files: flash('No image has been provided', 'warning') return redirect(url_for('session.new')) file = request.files.get("user_image") caption = request.form.get("caption") file.filename = secure_filename(file.filename) if not upload_file_to_s3(file): flash('Oops, something went wrong while uploading', 'warning') return redirect(url_for('sessions.new')) user = User.get_or_none(User.id == current_user.id) img_upload = Image(user=user.id, user_image=file.filename, caption=caption) img_upload.save() flash('Successfully uploaded Image', 'success') return redirect(url_for('sessions.new'))
def new(image_id): image = Image.get_or_none(Image.id == image_id) if not image: flash('No image found', 'warning') return redirect(url_for('users.home')) client_token = gateway.client_token.generate() if not client_token: flash('Oh no', 'warning') return redirect(url_for('users.home')) return render_template('donations/new.html', image=image, client_token=client_token)
def get_book(book_id): book = Book.query.filter(Book.id == book_id).first() if book is None: return pack({}, False, 1) contents = Paragraph.query.filter(Paragraph.book_id == book.id).all() content_list = [] for content in contents: content_info = content.to_dict() content_info["audioUrl"] = Audio.get_audio_filename( book.id, content.chapter) content_info["imageUrl"] = Image.get_image_filename(content.id) content_info["annotation"] = Annotation.get_annotations(content.id) content_list.append(content_info) return pack({"book": book.to_dict(), "content": content_list})
def new(image_id): image = Image.get_or_none(Image.id == image_id) if not image: flash('No image found with id provided', 'warning') return redirect(url_for('users.index')) client_token = gateway.client_token.generate() if not client_token: flash('Unable to obtain token', 'warning') return redirect(url_for('users.index')) return render_template('donations/new.html', image=image, client_token=client_token)
def getImagebyImgID(img_id): """ Returns Image uploaded by the User By Using Image ID """ img = Image.GetByImgID(img_id) if img: userID = img.get('userID') usr = User.getUserByID(userID) if usr: img.update({ 'name': usr.get('name'), 'gender': usr.get('gender'), 'profilePicture': usr.get('profilePicture'), 'created_at': Database.created_at(img.get('_id')) }) return img
def create_purchase(img_id): nonce = request.form.get("nonce") amount = request.form.get("dollar") message = request.form.get("message") result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if result.is_success: payment = Payment(payment=amount, donator_id=current_user.id, image_id=img_id, message=message) if payment.save(): image = Image.get_or_none(id=img_id) image_owner = User.get_or_none(id=image.user_id) message = Mail( from_email="*****@*****.**", to_emails=image_owner.email, subject=f"Donation from {current_user.username}", html_content= f'<strong>A donation of RM{amount} is made on your image{img_id} from {current_user.username}</strong>' ) try: sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(str(e)) flash( f"Transaction successful! Thanks on the behalf of Nextagram and {image_owner.username}!", 'success') return redirect(url_for('home')) else: return render_template('payment/new.html') else: flash('Transaction failed', 'danger') return render_template('payment/new.html')
def create(image_id): nonce = request.form.get('payment_method_nonce') if not nonce: flash('invalid credit card details', 'warning') return redirect(url_for('users.index')) image = Image.get_or_none(Image.id == image_id) if not image: flash('No image found with the provided id', 'waning') return redirect(url_for('users.index')) amount = request.form.get('amount') if not amount: flash('No donation amount provided', 'warning') return redirect(url_for('users.index')) result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, # "device_data": device_data_from_the_client, "options": { "submit_for_settlement": True } }) if not result.is_success: flash('Unable to complete transaction', 'warning') return redirect(request.referrer) donation = Donation(amount=amount, image_id=image.id, user_id=current_user.id) if not donation.save(): flash('Donation successful but error creating record', 'warning') return redirect(url_for('users.index')) flash(f'Donation successful. ${amount} donated', 'success') user = User.select().join(Image).where(Image.id == image_id) send_simple_message(user[0].email) return redirect(url_for('users.index'))
def delete_image(img_id): image = Image.get_or_none(id=img_id) user = User.get_or_none(id=image.user_id) for i in image.donation: query = Payment.get_or_none(id=i) if query: query.delete_instance() else: if image.delete_instance(): flash(f"Image ({img_id}) has successfully been removed!", "success") return redirect(url_for('users.show', username=user.username)) if image.delete_instance(): flash(f"Image ({img_id}) has successfully been removed!", "success") return redirect(url_for('users.show', username=user.username)) else: flash("Removing image failed", 'danger') return redirect(url_for('users.show', username=user.username))
def create(image_id): nonce = request.form.get('payment_method_nonce') if not nonce: flash(f"Error with payment method nonce", 'warning') return redirect(url_for('users.index')) image = Image.get_or_none(Image.id == image_id) # if not image: # flash('Could not find image with provided ID', 'warning') # return redirect(url_for('users.index')) amount = request.form.get('amount') if not amount: flash('No amount is provided for donation', 'warning') return redirect(url_for('users.index')) result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if not result.is_success: flash('unable to complete transaction', 'warning') return redirect(request.referrer) donation = Donation(amount=amount, image=image.id, user=current_user.id) if not donation.save(): ('Donation successfull but error creating record', 'warning') return redirect(url_for('users.index')) flash('You have successfully donated. Thank you!') return redirect(url_for('sessions.show', username=current_user.username))
def home(): images = Image.select().order_by(Image.created_at.desc()) return render_template('home.html', images=images)
def profile(id): user = User.get(User.id == id) image_list = Image.select().where(Image.user_id == user.id) return render_template('users/profilepage.html', image_list=image_list, user=user)
def food(): return render_template("food.html", list_food=Image.objects())
def show(username): if current_user: get_images = Image.select().where(Image.user_id == current_user.id) return render_template('sessions/show.html', currentuser_username=current_user.username, get_images=get_images)