Last active
July 12, 2017 21:36
-
-
Save vb100/6ba14ebed1987c5f3e4656519d18ae05 to your computer and use it in GitHub Desktop.
This Flask (Python) application read the input data of webpage visitor: email and salary. Then save this record to PostreSQL database and send mail to visitor inbox about average salary of other visitors and other relevant data. Used modules and libraries: Flask, SQAlchemy. Also I wrote an externat Pythoc app for sending a mail. Before testing P…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html eng="en"> | |
| <title>Data Collector app</title> | |
| <head> | |
| <link href="../static/main.css" rel="stylesheet"> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1>Collecting salaries data</h1> | |
| <h3>Please fill the entries to get population statistics on salaries</h3> | |
| <div class="message"> | |
| {{text | safe}} | |
| </div> | |
| <form action="{{url_for('success')}}" method="POST"> | |
| <input title="Your email will be safe with us" placeholder="Enter your email address" type="email" name="email_name" required> <br> | |
| <input title="Your data will be safe" placeholder="Enter your salary in Euros" type="number" min="100" max="10000" step="0.1" name="salary_name" required> <br> | |
| <button type="submit">Submit</button> | |
| </form> | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from email.mime.text import MIMEText | |
| import smtplib | |
| def send_email(email, salary, average_salary, count): | |
| from_email = "vytautas.bielinskas@gmail.com" | |
| from_password = "skateboard89" | |
| to_email=email | |
| subject = "Salary data" | |
| message = "Hey! Your salary is <strong>%s</strong>. EUR. <br> Average salary of all is %s. and it is calculated out of %s people." % (salary, average_salary, count) | |
| msg = MIMEText(message, 'html') | |
| msg['Subject'] = subject | |
| msg['To'] = to_email | |
| msg['From'] = from_email | |
| gmail = smtplib.SMTP('smtp.gmail.com', 587) | |
| gmail.ehlo() | |
| gmail.starttls() | |
| gmail.login(from_email, from_password) | |
| gmail.send_message(msg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| html, body { | |
| height: 100%; | |
| margin: 0; | |
| } | |
| .container { | |
| margin: 0 auto; | |
| width: 100%; | |
| height: 100%; | |
| background-color: #006666; | |
| color: #e6ffff; | |
| overflow: hidden; | |
| text-align: center; | |
| } | |
| .container h1 { | |
| font-family: Arial, sans-serif; | |
| font-size: 30px; | |
| color: #DDCCEE; | |
| margin-top: 80px; | |
| } | |
| .container p { | |
| margin: 100px; | |
| } | |
| .cotainer form { | |
| margin: 20px; | |
| } | |
| .container input { | |
| width: 350px; | |
| height: 15px; | |
| font-size: 15px; | |
| margin: 2px; | |
| padding: 20px; | |
| transition: all 0.2s ease-in-out | |
| } | |
| .container button { | |
| width: 70px; | |
| height: 30px; | |
| background-color: steelblue; | |
| margin: 3px; | |
| } | |
| .message { | |
| font-family: Arial, sans-serif; | |
| font-size: 15px; | |
| color: #ff9999; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html eng="en"> | |
| <title>Data Collector app</title> | |
| <head> | |
| <link href="../static/main.css" rel="stylesheet"> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <p> | |
| Thank you for your submissions.</br> | |
| You will receive an email with survey results. | |
| </p> | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, render_template, request | |
| from flask.ext.sqlalchemy import SQLAlchemy | |
| from send_email import send_email | |
| from sqlalchemy.sql import func | |
| app = Flask(__name__) | |
| db = SQLAlchemy(app) | |
| app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:skateboard@localhost/salary_collector' | |
| class Data(db.Model): | |
| __tablename__ = "data" | |
| id = db.Column(db.Integer, primary_key=True) | |
| email_ = db.Column(db.String(120), unique=True) | |
| salary_ = db.Column(db.Integer) | |
| def __init__(self, email_, salary_): | |
| self.email_ = email_ | |
| self.salary_ = salary_ | |
| @app.route("/") | |
| def index(): | |
| return render_template("index.html") | |
| @app.route("/success", methods = ['POST']) | |
| def success(): | |
| if request.method == 'POST': | |
| email = request.form["email_name"] | |
| salary = request.form["salary_name"] | |
| if db.session.query(Data).filter(Data.email_ == email).count() == 0: | |
| data=Data(email, salary) | |
| db.session.add(data) | |
| db.session.commit() | |
| average_salary = round(db.session.query(func.avg(Data.salary_)).scalar(),1) | |
| count=db.session.query(Data.salary_).count() | |
| print(average_salary) | |
| send_email(email, salary, average_salary, count) | |
| return render_template("success.html") | |
| else: | |
| return render_template('index.html', | |
| text = "Seems like we have got something from this email already") | |
| if __name__ == '__main__': | |
| app.debug = True | |
| app.run(port=5001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment