Skip to content

Commit

Permalink
basic flask app for pcc50
Browse files Browse the repository at this point in the history
  • Loading branch information
pybites committed Apr 1, 2018
1 parent f311ea8 commit d30b970
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 50/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Code Challenge 50 - Use Celery to offload a task

* Take the challenge [on our platform](https://codechalleng.es/challenges/50)
55 changes: 55 additions & 0 deletions 50/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import time

from flask import render_template, request, Flask

DEFAULT_BANNER = 'https://pbs.twimg.com/media/DZsKAs9W4AAEdpO.jpg:large'
DEFAULT_GREETING = 'I wish your a Happy Easter'
MSG = '''<p>Hey {name}, {greeting}!</p>
<p>Enjoy and keep calm and code in Python!</p>
<img width="400px;" src="{banner}" alt="nice Easter banner">
'''
TIMEOUT = 1

app = Flask(__name__)


def _emails_users(emails, banner, message):
emails_done = {}
for email in emails:
# just printing message, bonus challenge: make it work with Sendgrid
name = email.split('@')[0] # for demo purposes
mail_body = MSG.format(name=name,
greeting=message or DEFAULT_GREETING,
banner=banner,
message=message)

emails_done[email] = mail_body

# simulate some heavy processing
time.sleep(TIMEOUT)

return emails_done


@app.route('/', methods=['GET', 'POST'])
def login():
banner = emails = message = emails_done = None

if request.method == 'POST':
banner = request.form.get('url') or DEFAULT_BANNER
emails = [email.strip() for email in
request.form.get('emails').split(',')]
message = request.form.get('message')

emails_done = _emails_users(emails, banner, message).items()

return render_template("index.html",
default_banner=DEFAULT_BANNER,
banner=banner or '',
emails=emails and ', '.join(emails) or '',
message=message or '',
emails_done=emails_done)


if __name__ == "__main__":
app.run(debug=True)
12 changes: 12 additions & 0 deletions 50/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
amqp==2.2.2
billiard==3.5.0.3
celery==4.1.0
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.10
kombu==4.1.0
MarkupSafe==1.0
pytz==2018.3
vine==1.1.4
Werkzeug==0.14.1
92 changes: 92 additions & 0 deletions 50/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
body {
font : 85% "Lucida Grande", Helvetica, "Lucida Sans Unicode", Arial, Verdana, sans-serif;
color:#000;
margin: 0 auto;
width: 800px;
}

form {
margin: 20px 0;
}

table {
margin: 20px 0;
width: 800px;
}

th {
text-align: left;
}

a {
text-decoration: underline !important;
}
a.pure-button {
text-decoration: underline !important;
}

td, th {
border: 1px solid #ddd !important;
}
textarea {
height: 50px;
}
.pure-form-stacked input,
.pure-form-stacked textarea {
width: 600px;
margin: 10px 0;
}
.alert {
font-weight: bold;
color: #900;
}
.ok {
font-weight: bold;
color: green;
}
.done {
text-decoration: line-through;
}

.active {
background-color: #000 !important;
}
.active a {
color: #fff;
}

.button-success,
.button-error,
.button-warning,
.button-secondary {
color: white;
border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}

.button-success {
background: rgb(28, 184, 65); /* this is a green */
}

.button-error {
background: rgb(202, 60, 60); /* this is a maroon */
}

.button-warning {
background: rgb(223, 117, 20); /* this is an orange */
}

.button-secondary {
background: rgb(66, 184, 221); /* this is a light blue */
}

.button-inactive {
opacity: 0.3;
}

.currentDate {
border: 2px solid green;
}
.loginSection {
padding: 15px 0 0 10px;
}
24 changes: 24 additions & 0 deletions 50/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html>
<head>
{% block head %}
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
<link rel="shortcut icon" href="{{ url_for('static', filename='images/send_sms.png') }}">

<title>PyBites Easter Challenge</title>
{% endblock %}
</head>
<body>
<div class="pure-g">
<div class="pure-u-5-5">
<h1>PyBites Easter Challenge</h1>
<p>Don't forget to <a href="https://codechalleng.es/challenges/50/" target="_blank">PR when done</a> to obtain our special
<a href="https://codechalleng.es/badge/easter" target="_blank">PyBites Easter Badge</a>!</p>
</div>

</div>

<div id="content">{% block content %}{% endblock %}</div>
</body>
</html>
40 changes: 40 additions & 0 deletions 50/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extends "base.html" %}
{% block content %}

{% if emails_done %}

<h2 class='ok'>Messages sent! <small>(<a href="/">go back</a>)</small></h2>
<table class="pure-table">
<thead>
<tr>
<th>Email</th>
<th>Message</th>
</tr>
</thead>

<tbody>
{% for email, message in emails_done %}
<tr>
<td>{{ email }}</td>
<td>{{ message|safe }}</td>
</tr>
{% endfor %}
</tbody>
</table>

{% else %}

<img src="{{ default_banner }}" alt="PyBites Easter banner">
<br>

<h2>Send your friends a Easter card:</h2>
<form id="search" class="pure-form pure-form-stacked" action="/" method="post">
<input name="banner" value="{{ banner }}" placeholder="Choose a banner URL (leave empty to use PyBites banner above)">
<textarea name="emails" placeholder="Comma separated emails">{{ emails }}</textarea>
<textarea name="message" placeholder="Optional message">{{ message }}</textarea>
<button type="submit" class="pure-button pure-button-primary">Send</button>
</form>

{% endif%}

{% endblock %}

0 comments on commit d30b970

Please sign in to comment.