def register():
    form = RegisterForm(request.form, csrf_enabled=False)
    if form.validate_on_submit():
        new_user = User.create(username=form.username.data,
                        email=form.email.data,
                        password=form.password.data,
                        active=True)
        flash("Thank you for registering. You can now log in.", 'success')
        return redirect(url_for('public.home'))
    else:
        flash_errors(form)
    return render_template('public/register.html', form=form)
def home():
    form = LoginForm(request.form)
    # Handle logging in
    if request.method == 'POST':
        if form.validate_on_submit():
            login_user(form.user)
            flash("You are logged in.", 'success')
            redirect_url = request.args.get("next") or url_for("user.members")
            return redirect(redirect_url)
        else:
            flash_errors(form)
    return render_template("public/home.html", form=form)
def contactLandOwner(farmlandId):
    form = ContactLandOwnerForm(request.form)
    farmland = Farmland.query.filter(Farmland.id == farmlandId).all()[0]
    if form.validate_on_submit():
        address = "Unknown" if farmland.address is None else farmland.address
        mainBodyContent = ("<p style=\"margin-left: 50px;\">"
                                "<b>Name:</b> " + form.name.data + "<br>"
                                "<b>Email:</b> " + form.email.data + "<br>"
                                "<b>Phone:</b> " + form.phone.data + "<br>"
                            "</p>"
                            "<p style=\"margin-left: 50px;\">"
                                "<b>What is your past experience farming?</b><br>"
                                "" + form.experience.data + "</p>"
                            "<p><br>Thanks,<br>"
                                "Acres"
                            "</p>")
        # msg = Message("Inquiry: " + address + " Property", recipients=["*****@*****.**")
        msg = Message("Inquiry: " + address + " Property", recipients=[farmland.email])
        msg.html = ("<html>"
                        "<body>"
                            "<p>Someone has contacted you about your " + address + " property:</p>"
                            "" + mainBodyContent + ""
                        "</body>"
                    "</html>")
        mail.send(msg)
        Email.create(sender=msg.sender,
                        recipients=",".join(msg.recipients),
                        body=msg.html)
        msg = Message("Inquiry: " + address + " Property", recipients=[form.email.data])
        msg.html = ("<html>"
                        "<body>"
                            "<p>Just a note that we sent your request for more information about the " + address + " property to " + farmland.ownerName + ":</p>"
                            "" + mainBodyContent + ""
                        "</body>"
                    "</html>")
        mail.send(msg)
        Email.create(sender=msg.sender,
                        recipients=",".join(msg.recipients),
                        body=msg.html)
        flash("Thanks for your inquiry! We sent your email for more information about the property. " + farmland.ownerName + " will follow up with you shortly.", 'info')
        return redirect(url_for('public.home'))
    else:
        flash_errors(form)
    return render_template("public/contact-land-owner.html", form=form, farmland=farmland)