def teamProfile(teamId): if current_user.team: if int(teamId) == int(current_user.team_id): return redirect(url_for('team_page')) form = CommentForm() team = db.session.query(Team).filter(Team.id == teamId).first() comments = db.session.query(Comment).filter(Comment.idea_id == team.idea.id).all() if not team: flash('Team '+teamId+' not found.', 'error') return redirect(url_for('home')) else: if form.validate_on_submit(): comment = Comment() comment.content = form.comment.data team.idea.comment.append(comment) current_user.comment.append(comment) db.session.add(comment) for member in comment.idea.team.members: notif = PersonalNotification() notif.content = current_user.username + " commented on your team's idea." member.notification.append(notif) db.session.commit() return render_template('team_view.html', form=form, team=team, comments=comments) return render_template('team_view.html', form=form, team=team, comments=comments)
def team_invitation(): form = TeammateInvitationForm() user = None if form.email.data: user = user_manager.find_user_by_email(form.email.data) elif form.username.data: user = user_manager.find_user_by_username(form.username.data) if not user: flash("User does not exist.", 'error') return redirect(url_for('invitation')) if current_user.team_id or user.team_id: flash("Not both members are available to form a new team.", 'error') elif current_user.request_teammate: flash("You can only send one request at the same time. You have to wait for a response before you send your next request.", 'error') elif current_user.location != user.location: flash("You can only form team with people from the same location(US or CH)", "error") else: send_mail(user, 'invitation', sender=current_user) current_user.request_teammate = user.id db.session.commit() flash("Invitation sent. You will be notified by email when he/she makes a decision.", 'success') return redirect(url_for('invitation'))
def accept_invitation(uname): user = user_manager.find_user_by_username(uname) if user.team_id: flash('This user has already formed a team with someone else. Please pick another teammate.', 'error') return redirect(url_for('user')) else: team = Team() team.members.append(current_user) team.members.append(user) team.name = current_user.username db.session.add(team) user.request_teammate = None current_user.request_teammate = None lst1 = db.session.query(User).filter(User.request_teammate==user.id).all() lst2 = db.session.query(User).filter(User.request_teammate==current_user.id).all() lst1.extend(lst2) for usr in lst1: usr.request_teammate = None send_mail(usr, 'fail_invitation') send_mail(user, 'new_team', u1=user, u2=current_user) send_mail(current_user, 'new_team', u1=user, u2=current_user) db.session.commit() team.name = "New Team No." + str(team.id) team.idea = Idea() db.session.commit() return redirect(url_for('team_page'))
def userProfile(uname): if uname == current_user.username: return redirect(url_for('user')) user = user_manager.find_user_by_username(uname) if user is None: flash('User '+uname+' not found.', 'error') return redirect(url_for('home')) else: return render_template('user_view.html', user=user)
def test__url_for_uses_prefix(self, mock_current_app): bucket_path = 'foo.s3.amazonaws.com' flask_s3.FlaskS3(self.app) mock_current_app.config = self.app.config mock_bind = mock_current_app.url_map.bind flask_s3.url_for('static', **{'filename': 'test_file.txt'}) self.assertEqual(mock_bind.call_args_list, [call(bucket_path, url_scheme='https')]) for supported_prefix in ['bar', '/bar', 'bar/', '/bar/']: mock_bind.reset_mock() self.app.config['FLASKS3_PREFIX'] = supported_prefix flask_s3.url_for('static', **{'filename': 'test_file.txt'}) expected_path = '%s/%s' % (bucket_path, 'bar') self.assertEqual(mock_bind.call_args_list, [call(expected_path, url_scheme='https')])
def update_team(): form = UpdateTeamInfoForm() team_id = current_user.team_id team = db.session.query(Team).filter(Team.id == team_id).first() if form.validate_on_submit(): if team: team.name = form.name.data team.idea.content = form.idea.data team.caseNumber = int(form.caseNumber.data) db.session.commit() return redirect(url_for('team_page')) else: flash("Team does not exist.", "error") return redirect(url_for('invitation')) return render_template('team_update.html', form=form, team=team)
def get(self, word): filename = url_for('static', filename='exercise1/' + word + '.mp3') print(filename) mp3 = urllib.urlopen(filename).read() a = AudioSegment.from_mp3(BytesIO(mp3)) a.export('/tmp/test.mp3', format='mp3') return send_file('/tmp/test.mp3', mimetype='audio/mp3')
def convert_item_to_flask_url(self, ctx, item, filepath=None): """Given a relative reference like `foo/bar.css`, returns the Flask static url. By doing so it takes into account blueprints, i.e. in the aformentioned example, ``foo`` may reference a blueprint. If an absolute path is given via ``filepath``, it will be used instead. This is needed because ``item`` may be a glob instruction that was resolved to multiple files. If app.config("FLASK_ASSETS_USE_S3") exists and is True then we import the url_for function from flask_s3, otherwise we import url_for from flask directly. If app.config("FLASK_ASSETS_USE_CDN") exists and is True then we import the url_for function from flask. """ if ctx.environment._app.config.get("FLASK_ASSETS_USE_S3"): try: from flask_s3 import url_for except ImportError as e: print("You must have Flask S3 to use FLASK_ASSETS_USE_S3 option") raise e elif ctx.environment._app.config.get("FLASK_ASSETS_USE_CDN"): try: from flask_cdn import url_for except ImportError as e: print("You must have Flask CDN to use FLASK_ASSETS_USE_CDN option") raise e else: from flask import url_for directory, rel_path, endpoint = self.split_prefix(ctx, item) if filepath is not None: filename = filepath[len(directory)+1:] else: filename = rel_path flask_ctx = None if not _request_ctx_stack.top: flask_ctx = ctx.environment._app.test_request_context() flask_ctx.push() try: url = url_for(endpoint, filename=filename) # In some cases, url will be an absolute url with a scheme and hostname. # (for example, when using werkzeug's host matching). # In general, url_for() will return a http url. During assets build, we # we don't know yet if the assets will be served over http, https or both. # Let's use // instead. url_for takes a _scheme argument, but only together # with external=True, which we do not want to force every time. Further, # this _scheme argument is not able to render // - it always forces a colon. if url and url.startswith('http:'): url = url[5:] return url finally: if flask_ctx: flask_ctx.pop()
def send_notification(): form = SendNotificationForm() if form.validate_on_submit(): noti = Notification() noti.content = form.notification.data db.session.add(noti) db.session.commit() return redirect(url_for('notify.send_notification'))
def get(self, word): filename = url_for('static', filename='exercise1/' + word + '.zip') print(filename) audiozip = urllib.urlopen(filename).read() # Write to temp file output = open('/tmp/test.zip', 'w') output.write(audiozip) output.close() return send_file('/tmp/test.zip', mimetype='application/zip')
def dismiss_team(): team_id = current_user.team_id team = db.session.query(Team).filter(Team.id == team_id).first() for cmt in team.idea.comment: db.session.delete(cmt) db.session.delete(team.idea) db.session.delete(team) db.session.commit() flash("Team dismissed.", "success") return redirect(url_for('invitation'))
def upload_s3(file_name, data, directory): connection = boto.connect_s3(app.config['AWS_ACCESS_KEY_ID'], app.config['AWS_SECRET_ACCESS_KEY']) bucket = connection.get_bucket(app.config['S3_BUCKET_NAME']) file_path = os.path.join(directory, file_name) sml = bucket.new_key(os.path.join('static', file_path)) path = url_for('static', filename=file_path) sml.set_contents_from_file(data) sml.set_acl('public-read') return path
def reply_comment(comment_id): form = CommentReplyForm() comment = db.session.query(Comment).filter(Comment.id == comment_id).first() if not comment: flash("Invalid comment.", "error") return redirect(url_for('')) origin_comment = comment while (origin_comment.parent != None): origin_comment = origin_comment.parent team = origin_comment.idea.team if form.validate_on_submit(): comment.reply.append(Comment(content = form.reply.data, user=current_user)) notif = PersonalNotification() notif.content = current_user.username + " replied your comment." db.session.add(notif) comment.user.notification.append(notif) db.session.commit() return redirect(url_for('teamProfile', teamId=team.id)) return render_template('reply_comment.html', form=form, comment=comment)
def team_upload(): team_id = current_user.team_id team = db.session.query(Team).filter(Team.id == team_id).first() if team: form = UploadCompArticleForm() if form.validate_on_submit(): file_name = "team"+str(team.id)+".pdf" team.submission = upload_s3(file_name, form.article.data, app.config['S3_COMP_DIR']) db.session.commit() return render_template("team_upload.html", form=form, team=team) else: flash("You have not formed a team yet.") return redirect(url_for("invitation"))
def team_page(): team_id = current_user.team_id team = db.session.query(Team).filter(Team.id == team_id).first() if team: comments = db.session.query(Comment).filter(Comment.idea_id == team.idea.id).all() notifs = db.session.query(Notification).order_by(Notification.time).limit(10).all() return render_template("team_profile.html", team = team, \ show_result = app.config['COMPETITION_CLOSED'], competition_start = app.config["COMPETITION_START"], notifications = notifs, comments=comments) else: flash("You have not formed a team yet.") return redirect(url_for("invitation"))
def comment_idea(idea_id): form = CommentForm() idea = db.session.query(Idea).filter(Idea.id == idea_id).first() team = db.session.query(Team).filter(Team.id == idea.team_id).first() if form.validate_on_submit(): if idea: comment = Comment() comment.content = form.comment.data idea.comment.append(comment) current_user.comment.append(comment) db.session.add(comment) for member in comment.idea.team.members: notif = PersonalNotification() notif.content = current_user.username + " commented on your team's idea." member.notification.append(notif) db.session.commit() return redirect(url_for('teamProfile', teamId=team.id)) else: flash("Idea does not exist.", "error") return redirect(url_for('all_ideas')) return render_template('comment_idea.html', form=form, idea_id=idea_id, team=team)
def admin_news_uploads(): form = UploadNewsForm() if form.validate_on_submit(): news = News() db.session.add(news) db.session.commit() news.title = form.title.data news.content = form.content.data news.author = current_user.username file_name = secure_filename(form.image.data.filename) extension = file_name.split('.')[-1] file_name = '.'.join([str(news.id), extension]) path = upload_s3(file_name, form.image.data, app.config['S3_NEWS_IMAGE_DIR']) news.image = path db.session.commit() return redirect(url_for('news_and_resources'))
def delete_notification(notif_id): notif = db.session.query(PersonalNotification).filter(PersonalNotification.id == notif_id).first() notif.user.notification.remove(notif) db.session.delete(notif) db.session.commit() return redirect(url_for('personal_notifications'))
try: pm.listUnsubscribe(id='8d4e6caca8', email_address=current_user.email) except Exception, e: pass current_user.email = form.email.data db.session.commit() try: pm.listSubscribe(id='8d4e6caca8', email_address=current_user.email, double_optin=False) except Exception, e: pass return redirect(url_for('user')) return render_template('update_profile.html', form=form, user=current_user) @app.route('/news_upload', methods=['POST']) @login_required @roles_required('admin') def admin_news_uploads(): form = UploadNewsForm() if form.validate_on_submit(): news = News() db.session.add(news) db.session.commit() news.title = form.title.data news.content = form.content.data
def convert_item_to_flask_url(self, ctx, item, filepath=None): """Given a relative reference like `foo/bar.css`, returns the Flask static url. By doing so it takes into account blueprints, i.e. in the aformentioned example, ``foo`` may reference a blueprint. If an absolute path is given via ``filepath``, it will be used instead. This is needed because ``item`` may be a glob instruction that was resolved to multiple files. If app.config("FLASK_ASSETS_USE_S3") exists and is True then we import the url_for function from flask_s3, otherwise we import url_for from flask directly. If app.config("FLASK_ASSETS_USE_CDN") exists and is True then we import the url_for function from flask. """ if ctx.environment._app.config.get("FLASK_ASSETS_USE_S3"): try: from flask_s3 import url_for except ImportError as e: print( "You must have Flask S3 to use FLASK_ASSETS_USE_S3 option") raise e elif ctx.environment._app.config.get("FLASK_ASSETS_USE_CDN"): try: from flask_cdn import url_for except ImportError as e: print( "You must have Flask CDN to use FLASK_ASSETS_USE_CDN option" ) raise e elif ctx.environment._app.config.get("FLASK_ASSETS_USE_AZURE"): try: from flask_azure_storage import url_for except ImportError as e: print( "You must have Flask Azure Storage to use FLASK_ASSETS_USE_AZURE option" ) raise e else: from flask import url_for directory, rel_path, endpoint = self.split_prefix(ctx, item) if filepath is not None: filename = filepath[len(directory) + 1:] else: filename = rel_path flask_ctx = None if not _request_ctx_stack.top: flask_ctx = ctx.environment._app.test_request_context() flask_ctx.push() try: url = url_for(endpoint, filename=filename) # In some cases, url will be an absolute url with a scheme and hostname. # (for example, when using werkzeug's host matching). # In general, url_for() will return a http url. During assets build, we # we don't know yet if the assets will be served over http, https or both. # Let's use // instead. url_for takes a _scheme argument, but only together # with external=True, which we do not want to force every time. Further, # this _scheme argument is not able to render // - it always forces a colon. if url and url.startswith('http:'): url = url[5:] return url finally: if flask_ctx: flask_ctx.pop()
def reject_invitation(uname): user = user_manager.find_user_by_username(uname) user.request_teammate = None send_mail(user, 'fail_invitation') return redirect(url_for('user'))