def downloads(id, filename): """ Download method to fetch the required file from S3 Bucket """ # print("Resource id: {}".format(id)) paper = Paper.query.get(id) if paper == None: return render_template('404.html') s3_filename = '{}/{}'.format(paper.id, filename) try: target_file = s3.get_object(Bucket=app.config['S3_BUCKET'], Key=s3_filename) except Exception as e: print("Something Happened: ", e) return render_template('404.html') session_download_key = 'downloaded_{}'.format(paper.id) ### Updating paper download count based on session this logic might need to be re-designed if session_download_key not in session: session[session_download_key] = True paper.download_count += 1 db.session.flush() db.session.commit() response = Response( target_file['Body'], mimetype=target_file['ResponseMetadata']['HTTPHeaders']['content-type'], headers={"Content-Disposition": "attachment;filename={}".format(filename)} ) return response ## File as download attachment
def test_edit_payment_info(self): user = Organization.query.first() headers = auth_token_helper(user.email) response = test_app.post( '/edit_payment_info_charity', headers=headers, data=dict( accountHolder='Alex', bankBranchAddress='some location', bankName='citibank', accountType='checking', bankRoutingNumber="1", bankAccountNumber="123" ) ) self.assertEqual(response.status_code, 200) bucket = 'newspark-payment' file_name = 'charity/routers.json' payment_data = json.load(s3.get_object(Bucket=bucket, Key=file_name)['Body']) accounts = payment_data['accounts'] primary_ids = [accounts[i]['primary_id'] for i in range(len(accounts))] self.assertIn(user.email, primary_ids)
def get(self, name): if not name.endswith(".pdf"): raise Execption("Request not for PDF") file = s3.get_object(Bucket='ia-energy-rob-test-bucket', Key='test/' + name) return Response( file['Body'].read(), mimetype='application/pdf', headers={"Content-Disposition": "attachment;filename=" + name})
def check_charity_legal_record(): email = request.form.get('email') bucket = "newspark-legal" file_name = "charities/legal_records.json" legal_records = json.load(s3.get_object(Bucket=bucket, Key=file_name)['Body']) if email in legal_records: return jsonify(True) else: return jsonify(False)
def add_charity_legal_record(): email = request.form.get('email') now = datetime.now().strftime("%Y-%m-%d, %H:%M") bucket = "newspark-legal" file_name = "charities/legal_records.json" legal_records = json.load(s3.get_object(Bucket=bucket, Key=file_name)['Body']) legal_records[email] = {'version': 1, 'date_time': now} s3.put_object(Bucket=bucket, Key=file_name, Body=json.dumps(legal_records)) return jsonify("Success")
def save_new_match(match, last_id): info = collect_new_info(match, last_id) BUCKET = 'aurate-sku' from app import s3 response = s3.get_object(Bucket=BUCKET, Key=f'easypost_reference_match') previous_data = pickle.loads(response['Body'].read()) previous_data['shipments'].update(info['shipments']) previous_data['last_id'] = info['last_id'] s3.put_object(Body=pickle.dumps(previous_data), Bucket=BUCKET, Key=f'easypost_reference_match')
def get_easypost_record_by_reference(reference, sale_number): # delete BUCKET = 'aurate-sku' from app import s3 response = s3.get_object(Bucket=BUCKET, Key=f'easypost_reference_match') previous_data = pickle.loads(response['Body'].read()) keys = [] for key, value in previous_data['shipments'].items(): if (reference in value) or (sale_number in value): keys.append(key) if keys: return keys return get_easypost_record(reference, last_id=previous_data['last_id'])
def get_inventory_positions(): # get inventory from s3 from app import s3 response = s3.get_object(Bucket=BUCKET, Key=f'ryby_inventory') inventory = pickle.loads(response['Body'].read()) return inventory