def upload(folderid): if current_app.config['TIME_LIMIT'] and not checksession.check_session_timestamp(folderid, current_app.config['TIME_LIMIT']): return render_template('expire.html.j2') get_flashed_messages() folder = current_app.config['UPLOAD_FOLDER'] + folderid if request.method == 'POST': if current_app.config['TIME_LIMIT']: ct = cleanupThread.cleanupThread( current_app.config['UPLOAD_FOLDER'], folderid, current_app.config['TIME_LIMIT']) ct.start() if not os.path.exists(folder): os.makedirs(folder) count = 0 for key, f in request.files.items(): if key.startswith('file'): if f.filename.endswith('.pdf'): f.save(os.path.join(folder, f.filename)) print("Uploaded...", f.filename) count += 1 else: print("Not a .pdf file!") return render_template('upload.html.j2', id=folderid, expires=current_app.config['TIME_LIMIT']) if count == 0: print("No files uploaded!") return render_template('upload.html.j2', id=folderid, expires=current_app.config['TIME_LIMIT']) return render_template('arrange.html.j2', id=folderid, expires=current_app.config['TIME_LIMIT']) else: return render_template('upload.html.j2', id=folderid, expires=current_app.config['TIME_LIMIT'])
def register(): form = FormRegister() if form.validate_on_submit(): user = UserRegister(username=form.username.data, email=form.email.data, password=form.password.data) db.session.add(user) db.session.commit() get_flashed_messages('註冊成功') return render_template('base.html') return render_template('register.html', form=form)
def test_proposal_send_email(app, client): mail.init_app(app) # Re-load using test configuration. with mail.record_messages() as outbox: response = client.post('/de/article/proposal/new/', data={ 'email': '*****@*****.**', 'topic': 'Topic', 'name': 'Name', 'media': 'video', 'section': 'creative', 'city': 'City', 'angle': 'Angle', 'format': 'Format', 'additional': 'Additional' }, follow_redirects=True) assert response.status_code == 200 assert len(outbox) == 1 assert outbox[0].subject == 'Article proposal: Topic' assert outbox[0].body == f''' Language: de Name: Name Email: [email protected] City: City Topic: Topic Angle: Angle Media: video Format: Format Section: creative Addiction comment: Additional ''' assert (get_flashed_messages() == ['Nice one! You’ll be hearing back from your editor ASAP'])
def test_update_article_with_image_should_return_200(client, user, editor, article): login(client, editor.email, 'secret') article.status = 'published' article.save() with open(Path(__file__).parent / 'dummy-image.jpg', 'rb') as content: image_content = BytesIO(content.read()) data = { 'title': 'updated', 'author': user.id, 'image': (image_content, 'image-name.jpg') } response = client.post(f'/article/{article.id}/', data=data, content_type='multipart/form-data', follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your article was successfully saved.'] article.reload() assert article.title == 'updated' assert article.author == user assert article.editor == editor assert article.has_image assert (Path(app.config.get('ARTICLES_IMAGES_PATH') / str(article.id)).exists())
def test_update_article_with_image_should_return_200(app, client, user, editor, published_article): login(client, editor.email, 'password') with open(Path(__file__).parent / 'dummy-image.jpg', 'rb') as content: image_content = BytesIO(content.read()) data = { 'title': 'updated', 'authors': user.id, 'image': (image_content, 'image-name.jpg'), } response = client.post(f'/en/article/{published_article.id}/edit/', data=data, content_type='multipart/form-data', follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your article has been updated'] published_article.reload() assert published_article.title == 'updated' assert published_article.authors == [user] assert published_article.editor == editor assert published_article.image_filename == '/articles/image-name.jpg' assert Path( app.config.get('UPLOADS_FOLDER') / 'articles' / 'image-name.jpg').exists() assert ('<meta property=og:image ' 'content="http://localhost/articles/image-name.jpg">' in response)
def test_update_article_with_image_unallowed_extension(app, client, user, editor, published_article): login(client, editor.email, 'password') with open(Path(__file__).parent / 'dummy-image.jpg', 'rb') as content: image_content = BytesIO(content.read()) data = { 'title': 'updated', 'authors': user.id, 'image': (image_content, 'image-name.zip'), } assert 'zip' not in app.config.get('ALLOWED_EXTENSIONS') response = client.post(f'/en/article/{published_article.id}/edit/', data=data, content_type='multipart/form-data', follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == [ 'There was an error in your article submission: Unallowed extension.' ] published_article.reload() assert published_article.image_filename is None assert not Path( app.config.get('UPLOADS_FOLDER') / 'articles' / 'image-name.zip').exists()
def test_delete_article_should_return_200(client, editor, article): login(client, editor.email, 'secret') response = client.post(f'/article/{article.id}/delete/', follow_redirects=True) assert response.status_code == HTTPStatus.OK assert Article.objects.all().count() == 0 assert get_flashed_messages() == ['Article was deleted.']
def before_request(): # if no users found, send to setup try: users = User.query.all() except Exception: users = [] if users == []: return redirect(url_for("user_routes.initial_setup")) # Ignore check for some pages - these are mostly methods that need # to run even in setup mode exclude_list = [ "warden.setup", "warden.specter_auth", "warden.login", "warden.register", "warden.logout", "warden.show_broadcast", "warden.show_log", "warden.config_ini", "warden.newtrade" ] if request.endpoint in exclude_list: return if not current_user.is_authenticated: return redirect(url_for("warden.login")) txs = transactions_fx() if txs.empty: flash("No Transactions Found. You can start by including a transaction below. You may also Connect to Specter or import a CSV file.", "info") return redirect(url_for("warden.newtrade")) # Create empty status dictionary meta = { 'tor': current_app.tor, 'specter_reached': current_app.specter.specter_reached, 'specter_auth': current_app.specter.specter_auth } # Save this in Flask session session['status'] = json.dumps(meta) # Check if still downloading data, if so load files if current_app.downloading: # No need to test if still downloading txs flash("Downloading from Specter. In the mean time, some transactions may be outdated or missing. Leave the app running to finish download.", "info") # Check that Specter is > 1.1.0 version # (this is the version where tx API was implemented) try: specter_version = str(current_app.specter.home_parser()['version']) if version.parse(specter_version) < version.parse("1.1.0"): flash(f"Sorry, you need Specter version 1.1.0 or higher to connect to WARden. You are running version {specter_version}. Please upgrade.", "danger") return redirect(url_for('warden.specter_auth')) # An error below means no file was ever created - probably needs setup except Exception: pass # Remove duplicate messages from Flask Flash messages = get_flashed_messages(with_categories=True) messages = list(set(messages)) for category, message in messages: flash(message, category)
def test_tag_update_summary(app, client, tag, editor): login(client, editor.email, 'password') data = {'summary': 'custom summary'} response = client.post(f'/en/article/tag/{tag.slug}/edit/', data=data, follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your tag has been updated'] tag.reload() assert tag.summary == 'custom summary'
def signup(): errors = None if request.method == "POST": current_app.logger.debug(request.method + " Signup") form = SignupForm(request.form) if form.validate(): current_app.logger.debug("login validation successful") email = form.email.data password = form.password.data username = form.username.data users = User.get_users_by_email(email) if len(users) <= 0: user = User.add_user(username, email, password) if login_user(user, True): msg = u"Account created for " + username current_app.logger.debug(msg) helpers.get_flashed_messages() # to clean the flash cache return redirect(url_for("index")) else: errors = [] msg = u"Sorry, but you could not sign up." current_app.logger.debug(msg) errors.append(msg) else: errors = [] msg = u"This email already registered. Please login or use another email." current_app.logger.debug(msg) errors.append(msg) elif form.errors: current_app.logger.debug("login validation failed") errors = [] for field, err in form.errors.items(): for error in err: errors.append(getattr(form, field).label.text + " : " + error) else: form = SignupForm() return render_template("signup.html", form = form, errors = errors)
def result(): ans = get_flashed_messages() print(ans) if len(ans) > 0: if ans[0] == '0': x = "Nah dwag, you're fine!!" elif ans[0] == '1': x = "Sorry bud, I don't reccomend you go through with the suregery!" else: return (redirect(url_for('index'))) return render_template('result.html', x=x, title="Result")
def login(): if request.method == 'GET': return render_template('login.html') us = request.form['user_id'] if (us in users) and (request.form['password'] == users[us]['password']): user = User() user.id = us login_user(user) return redirect(url_for('index')) else: flash('登入失敗了...') return render_template('login.html')+"<h3> %s </h3>" % get_flashed_messages()
def login(): errors = None if request.method == "POST": current_app.logger.debug(request.method + " login ") form = LoginForm(request.form) if form.validate(): current_app.logger.debug("login validation successful") email = form.email.data.decode("UTF-8") password = form.password.data.decode("UTF-8") remember = form.remember.data users = User.get_users_by_email(email) if (len(users) > 0) and users[0].checkPassword(password): user = users[0] current_app.logger.debug("login and passwords are OK for user: "******"Logged in!") current_app.logger.debug(request.args.get("next") or url_for("index")) helpers.get_flashed_messages() # to clean the flash cache return redirect(request.args.get("next") or url_for("index")) else: error = u"could not log in for some reason" errors = [error] current_app.logger.debug(user.name + " " + error) else: error = u"Invalid email or password" errors = [error] current_app.logger.debug(error) else: error = u"Invalid email or password" errors = [error] current_app.logger.debug(error) else: form = LoginForm() current_app.logger.debug("Returning the form") return render_template("login.html", errors = errors, form = form)
def test_update_published_article_with_many_authors(app, client, user, user2, editor, published_article): login(client, editor.email, 'password') data = { 'authors': [user.id, user2.id], 'language': app.config['LANGUAGES'][1][0], } response = client.post(f'/en/article/{published_article.id}/edit/', data=data, follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your article has been updated'] published_article.reload() assert published_article.authors == [user, user2]
def test_update_translation_with_tag(client, user, editor, tag, translation): login(client, editor.email, 'password') data = {'title': 'Modified title', 'tag-1': tag.name} response = client.post(f'/fr/article/translation/{translation.id}/edit/', data=data, follow_redirects=True) assert response.status_code == HTTPStatus.OK assert (get_flashed_messages() == [ 'Your translation was successfully updated.' ]) translation.reload() assert translation.title == 'Modified title' tag = Tag.objects.get(name=tag.name, language=translation.language) assert translation.tags == [tag]
def test_update_published_article_should_return_200(client, user, editor, article): login(client, editor.email, 'secret') article.status = 'published' article.save() data = {'title': 'updated', 'author': user.id} response = client.post(f'/article/{article.id}/', data=data, follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your article was successfully saved.'] article.reload() assert article.title == 'updated' assert article.author == user assert article.editor == editor
def test_update_published_article_with_tag(app, client, user, editor, tag, published_article): login(client, editor.email, 'password') data = { 'title': 'updated', 'authors': user.id, 'language': app.config['LANGUAGES'][0][0], 'tag-1': tag.name } response = client.post(f'/en/article/{published_article.id}/edit/', data=data, follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your article has been updated'] published_article.reload() assert published_article.tags == [tag]
def test_profile_image_should_save_and_render(app, client, user): login(client, user.email, 'password') with open(Path(__file__).parent / 'dummy-image.jpg', 'rb') as content: image = BytesIO(content.read()) data = {'image': (image, 'image-name.jpg')} response = client.post(f'/en/profile/{user.id}/edit/', data=data, content_type='multipart/form-data', follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your profile has been updated :-)'] user.reload() assert user.profile.image_filename == '/users/image-name.jpg' assert Path(app.config.get('UPLOADS_FOLDER') / 'users' / 'image-name.jpg').exists() assert '<div style="background-image:url(/resized-images/' in response
def test_translation_update_values_should_redirect(client, user, translation): login(client, user.email, 'password') data = { 'title': 'Modified title', 'summary': 'Modified summary', 'body': 'Modified body', } response = client.post(f'/fr/article/translation/{translation.id}/edit/', data=data) assert response.status_code == HTTPStatus.FOUND translation.reload() assert (response.headers.get('Location') == f'http://localhost/fr/article/translation/{translation.id}/') assert translation.title == 'Modified title' assert (get_flashed_messages() == [ 'Your translation was successfully updated.' ])
def test_tag_update_image(app, client, tag, editor): login(client, editor.email, 'password') with open(Path(__file__).parent / 'dummy-image.jpg', 'rb') as content: image_content = BytesIO(content.read()) data = { 'summary': 'custom summary', 'image': (image_content, 'image-name.jpg'), } response = client.post(f'/en/article/tag/{tag.slug}/edit/', data=data, content_type='multipart/form-data', follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == ['Your tag has been updated'] tag.reload() assert tag.summary == 'custom summary' assert tag.image_filename == '/tags/image-name.jpg' assert Path(app.config.get('UPLOADS_FOLDER') / 'tags' / 'image-name.jpg').exists()
def test_translation_creation_should_redirect(app, client, user, article): login(client, user.email, 'password') language = app.config['LANGUAGES'][1][0] data = { 'title': 'title', 'summary': 'summary', 'body': 'body', 'original': article.id, 'language': language } response = client.post(f'/{language}/article/translation/new/', data=data) assert response.status_code == HTTPStatus.FOUND translation = Translation.objects.first() assert (response.headers.get('Location') == ( 'http://localhost' f'/{language}/article/translation/{translation.id}/')) assert (get_flashed_messages() == [ 'Thanks! Your translation has been saved' ])
def test_tag_update_image_unallowed_extension(app, client, tag, editor): login(client, editor.email, 'password') with open(Path(__file__).parent / 'dummy-image.jpg', 'rb') as content: image_content = BytesIO(content.read()) data = { 'summary': 'custom summary', 'image': (image_content, 'image-name.zip'), } assert 'zip' not in app.config.get('ALLOWED_EXTENSIONS') response = client.post(f'/en/article/tag/{tag.slug}/edit/', data=data, content_type='multipart/form-data', follow_redirects=True) assert response.status_code == HTTPStatus.OK assert get_flashed_messages() == [ 'There was an error in your tag submission: Unallowed extension.' ] tag.reload() assert tag.image_filename is None assert not Path( app.config.get('UPLOADS_FOLDER') / 'tags' / 'image-name.zip').exists()
def test_translation_creation_with_existing_tag(app, client, user, article): login(client, user.email, 'password') language = app.config['LANGUAGES'][1][0] tag = Tag.objects.create(name='Sensational', language=language) data = { 'title': 'title', 'summary': 'summary', 'body': 'body', 'original': article.id, 'language': language, 'tag-1': tag.name } response = client.post(f'/{language}/article/translation/new/', data=data) assert response.status_code == HTTPStatus.FOUND translation = Translation.objects.first() assert (response.headers.get('Location') == ( 'http://localhost' f'/{language}/article/translation/{translation.id}/')) assert (get_flashed_messages() == [ 'Thanks! Your translation has been saved' ]) assert translation.tags == [tag]