def create(self): """Create file on upload The file will be relayed to FTP server The server only keeps a record of the file """ # The file uploaded file = request.files['file'] name = file.filename # extract the extension ext = ('.' + name.rsplit('.', 1)[1]) if '.' in name else '' # Generate a unique random filename new_filename = str(uuid.uuid4()) + ext while File.select().where(File.url == new_filename).count() > 0: new_filename = str(uuid.uuid4()) + ext # upload to FTP server try: upload_file(new_filename, file) except Exception: return jsonify(errno=500, error="Upload failed") # save record instance = File.create(name=name, url=new_filename) return self.object_detail(instance)
def donation_add(): form = forms.CreateDonationForm(request.form) form.donation_type.choices = mysql_connector.get_types(mysql) if request.method == 'POST' and form.validate(): title = form.title.data description = form.description.data city = form.city.data address = form.address.data donation_type = form.donation_type.data donation_date = form.donation_date.data.strftime('%y-%m-%d') file = request.files['file'] if file.filename == '': filename = 'none.jpg' else: filename = secure_filename(file.filename) args = (session['account_id'], title, description, city, donation_type, donation_date, address, filename) offer_id = mysql_connector.add_donation(mysql, args) file_path = os.path.join(app.config['UPLOAD_FOLDER_DONATIONS'], str(offer_id) + '.jpg') if file.filename != '': helpers.upload_file(file, file_path) return redirect(url_for('index')) args = session['account_id'] n_requests = mysql_connector.view_number_notifications(mysql,session['type'], args) myrequests = mysql_connector.view_notifications(mysql,session['type'], args) return render_template('donation_add.html', form=form,myrequests=myrequests ,n_requests=n_requests[0][0])
def organization_signup(): form = forms.OrganizationSignupForm(request.form) if request.method == 'POST' and form.validate(): name = form.name.data email = form.email.data username = form.username.data password = form.password.data bio = form.bio.data address = form.address.data city = form.city.data phone_number = form.phone_number.data certification_code = form.certification_code.data file = request.files['file'] if file.filename == '': filename = 'none.jpg' else: filename = secure_filename(file.filename) args = (email, username, password, bio, 2, name, address, city, phone_number, certification_code, filename) result = mysql_connector.create_organization(mysql, args) if result[0] == True: file_path = os.path.join(app.config['UPLOAD_FOLDER_ACCOUNTS'], str(result[1]) + '.jpg') if file.filename != '': helpers.upload_file(file, file_path) return redirect(url_for('login')) else: return render_template('organization_signup.html', form=form, error="Username or email already exists!") return render_template('organization_signup.html',form=form)
def test_copy_file(self): destination = self.root_folder.folder(DESTINATION_FOLDER_NAME) destination.create() uploaded_file = upload_file(self.egnyte, FILE_FIRST_VERSION_NAME, self.filepath) copied_file = uploaded_file.copy(destination.path + EGNYTE_FILE_NAME_IMAGE) self.assertIsNone(uploaded_file.check()) self.assertIsNone(copied_file.check())
def test_delete_file(self): uploaded_file = upload_file(self.egnyte, FILE_FIRST_VERSION_NAME, self.filepath) self.assertIsNone(uploaded_file.check()) uploaded_file.delete() with self.assertRaises(exc.NotFound): uploaded_file.check()
def test_move_file(self): destination = self.root_folder.folder(DESTINATION_FOLDER_NAME) destination.create() uploaded_file = upload_file(self.egnyte, FILE_FIRST_VERSION_NAME, self.filepath) moved_file = uploaded_file.move(destination.path + EGNYTE_FILE_NAME_IMAGE) with self.assertRaises(exc.NotFound): uploaded_file.check() self.assertIsNone(moved_file.check())
def test_delete_file_comment(self): uploaded_file = upload_file(self.egnyte, FILE_NAME, self.filepath) comment = uploaded_file.add_note(COMMENT) all_comments = self.egnyte.notes.list() self.assertIn(comment, all_comments) comment.delete() all_comments = self.egnyte.notes.list() self.assertNotIn(comment, all_comments)
def test_delete_file_comment(self): uploaded_file = upload_file(self.egnyte, FILE_NAME, self.filepath) comment = uploaded_file.add_note(COMMENT) all_comments = self.egnyte.notes.list() self.assertIn(comment, all_comments) comment.delete() all_comments = self.egnyte.notes.list() self.assertNotIn(comment, all_comments)
def parse_tsv_annotations(db_session, tsv_file, filename, template_type, username): db_session.execute('SET LOCAL ROLE ' + username) try: if not filename.endswith('.tsv'): raise ValueError( 'File format not accepted. Please upload a valid TSV file.') raw_file_content = csv.reader(tsv_file, delimiter='\t', dialect=csv.excel_tab) tsv_file.seek(0) except: traceback.print_exc() db_session.close() raise ValueError( 'File format not accepted. Please upload a valid TSV file.') try: upload_file(username, tsv_file, filename=filename, data_id=248375, description='summary upload', display_name=filename, format_id=248824, format_name='TSV', file_extension='tsv', topic_id=250482) except IntegrityError: db_session.rollback() db_session.close() raise ValueError( 'That file has already been uploaded and cannot be reused. Please change the file contents and try again.' ) tsv_file.seek(0) raw_file_content = csv.reader(tsv_file, delimiter='\t', dialect=csv.excel_tab) annotations = load_summaries(db_session, raw_file_content, username) db_session.close() return annotations
def test_list_comments(self): uploaded_file = upload_file(self.egnyte, FILE_NAME, self.filepath) comment = uploaded_file.add_note(COMMENT) all_comments = self.egnyte.notes.list() self.assertIn(comment, all_comments) self.assertEqual(uploaded_file, comment.get_file()) comments = uploaded_file.get_notes() self.assertEqual(tuple(comments), (comment, )) comment.delete() comments = uploaded_file.get_notes() self.assertEqual(tuple(comments), ()) all_comments = self.egnyte.notes.list() self.assertNotIn(comment, all_comments)
def test_list_comments(self): uploaded_file = upload_file(self.egnyte, FILE_NAME, self.filepath) comment = uploaded_file.add_note(COMMENT) all_comments = self.egnyte.notes.list() self.assertIn(comment, all_comments) self.assertEqual(uploaded_file, comment.get_file()) comments = uploaded_file.get_notes() self.assertEqual(tuple(comments), (comment,)) comment.delete() comments = uploaded_file.get_notes() self.assertEqual(tuple(comments), ()) all_comments = self.egnyte.notes.list() self.assertNotIn(comment, all_comments)
def upload_and_share(session, filename): #print(f'Upload to OneDrive ------->', # f'https://graph.microsoft.com/beta/me/drive/root/children/{profile_pic}/content') upload_response = upload_file(session, filename=filename) print(28 * ' ' + f'<Response [{upload_response.status_code}]>') if not upload_response.ok: #pprint.pprint(upload_response.json()) # show error message requests.get(config.SERVER_JIANG + '?text=upload出问题啦~?desp=' + str(upload_response.json())) return #print('Create sharing link ------>', # 'https://graph.microsoft.com/beta/me/drive/items/{id}/createLink') response, link_url = sharing_link(session, item_id=upload_response.json()['id']) print(28 * ' ' + f'<Response [{response.status_code}]>', f'bytes returned: {len(response.text)}') if not response.ok: #pprint.pprint(response.json()) # show error message requests.get(config.SERVER_JIANG + '?text=share出问题啦~?desp=' + str(response.json())) return return link_url
def sendmail_sample(session): """Send email from authenticated user. session = requests.Session() instance with a valid access token for Microsoft Graph in its default HTTP headers This sample retrieves the user's profile photo, uploads it to OneDrive, creates a view-only sharing link for the photo, and sends an email with the photo attached. The code in this function includes many print statements to provide information about which endpoints are being called and the status and size of Microsoft Graph responses. This information is helpful for understanding how the sample works with Graph, but would not be included in a typical production application. """ print('\nGet user profile ---------> https://graph.microsoft.com/beta/me') user_profile = session.get(api_endpoint('me')) print(28*' ' + f'<Response [{user_profile.status_code}]>', f'bytes returned: {len(user_profile.text)}\n') if not user_profile.ok: pprint.pprint(user_profile.json()) # display error return user_data = user_profile.json() email = user_data['mail'] display_name = user_data['displayName'] print(f'Your name ----------------> {display_name}') print(f'Your email ---------------> {email}') email_to = input(f'Send-to (ENTER=self) -----> ') or email print('\nGet profile photo --------> https://graph.microsoft.com/beta/me/photo/$value') photo, photo_status_code, _, profile_pic = profile_photo(session, save_as='me') print(28*' ' + f'<Response [{photo_status_code}]>', f'bytes returned: {len(photo)}, saved as: {profile_pic}') if not 200 <= photo_status_code <= 299: return print(f'Upload to OneDrive ------->', f'https://graph.microsoft.com/beta/me/drive/root/children/{profile_pic}/content') upload_response = upload_file(session, filename=profile_pic) print(28*' ' + f'<Response [{upload_response.status_code}]>') if not upload_response.ok: pprint.pprint(upload_response.json()) # show error message return print('Create sharing link ------>', 'https://graph.microsoft.com/beta/me/drive/items/{id}/createLink') response, link_url = sharing_link(session, item_id=upload_response.json()['id']) print(28*' ' + f'<Response [{response.status_code}]>', f'bytes returned: {len(response.text)}') if not response.ok: pprint.pprint(response.json()) # show error message return print('Send mail ---------------->', 'https://graph.microsoft.com/beta/me/microsoft.graph.sendMail') with open('email.html') as template_file: template = template_file.read().format(name=display_name, link_url=link_url) send_response = send_mail(session=session, subject='email from Microsoft Graph console app', recipients=email_to.split(';'), body=template, attachments=[profile_pic]) print(28*' ' + f'<Response [{send_response.status_code}]>') if not send_response.ok: pprint.pprint(send_response.json()) # show error message
def test_upload_new_version(self): upload_file(self.egnyte, FILE_FIRST_VERSION_NAME, self.filepath) second_version = upload_file(self.egnyte, FILE_SECOND_VERSION_NAME, self.filepath) file_attributes = second_version._fetch_attributes() self.assertEqual(file_attributes['num_versions'], 2)
def test_create_file_comment(self): uploaded_file = upload_file(self.egnyte, FILE_NAME, self.filepath) comment = uploaded_file.add_note(COMMENT) self.assertEqual(comment.file_path, self.filepath) self.assertEqual(comment.message, COMMENT)
def test_upload_file(self): uploaded_file = upload_file(self.egnyte, FILE_FIRST_VERSION_NAME, self.filepath) self.assertIsNone(uploaded_file.check())
def test_create_file_link(self): uploaded_file = upload_file(self.egnyte, FILE_NAME, self.filepath) link = uploaded_file.link(ACCESSIBILITY_ANYONE) self.__verify_link(link[0], self.filepath, ACCESSIBILITY_ANYONE, 'file')
def test_create_file_comment(self): uploaded_file = upload_file(self.egnyte, FILE_NAME, self.filepath) comment = uploaded_file.add_note(COMMENT) self.assertEqual(comment.file_path, self.filepath) self.assertEqual(comment.message, COMMENT)