def add_post(uname): title = "add post" form = addPost() user = User.query.filter_by(username=uname).first() if user is None: abort(404) if form.validate_on_submit(): title = form.title.data content = form.content.data if "post_photo" in request.files: pic = photos.save(request.files["post_photo"]) path = f"photos/{pic}" image_url = path new_post = Post(title=title, content=content, user=user, image_url=image_url) new_post.save_post() maillist = MailList.query.all() mails = [] for mail in maillist: mails.append(mail.email) for mail in mails: mail_message("Update!", "email/update", mail, user=current_user) return redirect(url_for('main.index')) return render_template('addpost.html', form=form, title=title)
def register(): form = RegistrationForm() if form.validate_on_submit(): user = User(email=form.email.data, username=form.username.data, password=form.password.data) db.session.add(user) db.session.commit() mail_message("Welcome to watchlist", "email/welcome_user", user.email, user=user) return redirect(url_for('auth.login')) title = "New Account" return render_template('auth/register.html', registration_form=form)
def new_blog(): """ Function that enables one to start a blog """ form = PitchForm() if form.validate_on_submit(): content = form.content.data title = form.title.data author = form.author.data new_blog = Blog(content = content, title=title, author = author, user_id = current_user.id) new_blog.save_blog() subs = Subscription.query.all() for sub in subs: mail_message("New Blog", "email/new_blog", sub.email) return redirect(url_for('main.index')) return render_template('new-blog.html',form=form)
def dispatch_request(self): form = PostForm() if form.validate_on_submit(): post = Post(body=form.body.data, blog_id=current_user.id) db.session.add(post) db.session.commit() # message the subscribers subscribers = Subscribe.query.all() mail_message("a new post has been added, bloggy site", "email/subscriber", subscribers, post=post) return redirect(url_for('main.blog', uname=current_user.name)) # pitches = Pitch.query.order_by(Pitch.timestamp.desc()).all() return render_template(self.template_name, form=form)
def new_post(): if current_user.username != 'davidokwacha': abort(403) form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data) db.session.add(post) db.session.commit() subs = Subscription.query.all() for sub in subs: mail_message("New ", "email/new_post", sub.email) return redirect(url_for('main.index')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() mail_message("Welcome to the flask Blog", "email/welcome_user", user.email, user=user) flash(f'Account was successfully created, you can now login!', "success") return redirect(url_for('users.login')) return render_template('register.html', form=form)