Skip to content

Commit 70c055c

Browse files
committed
Some advancement
1 parent 7967ad2 commit 70c055c

File tree

9 files changed

+114
-58
lines changed

9 files changed

+114
-58
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from flask import Flask, render_template, request, redirect, url_for
2+
from forms import FormToReverse
3+
4+
# Creating instances
5+
app = Flask(__name__)
6+
app.config.from_object('project.config')
7+
8+
9+
def reverse(string):
10+
return string[::-1]
11+
12+
13+
@app.route('/', methods=['GET', 'POST'])
14+
def index():
15+
form = FormToReverse(request.form)
16+
if request.method == 'POST' and form.validate_on_submit():
17+
return redirect(url_for('rev_str'))
18+
return render_template('index.html')
19+
20+
@app.route('/reversed/', methods=['POST'])
21+
def rev_str():
22+
rev_string = reverse(request.form['user_input'])
23+
return render_template('reversed.html', output = rev_string, user_input=request.form['user_input'])
24+
25+
@app.errorhandler(404)
26+
def page_not_found(error):
27+
return render_template('404.html'),404
28+
29+
@app.errorhandler(500)
30+
def server_error(error):
31+
return render_template('500.html'),500
32+
33+
#@app.errorhandler(403)
34+
#def access_forbidden(error):
35+
#return render_template('403.html'),403
36+
37+
38+
if __name__=='__main__':
39+
app.run(debug=True)

part2/reverse_flask_reza/project/__init__.py.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

part2/reverse_flask_reza/project/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
from wtforms.validators import DataRequired, Length
44

55
class FormToReverse(Form):
6-
string = StringField('reverse', validators=[DataRequired(), Length=(min=4)])
6+
string = StringField('reverse', validators=[DataRequired(), Length(min=4)])
77

88

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
text-align: center;
5+
}
6+
7+
.container{
8+
margin: 0 auto;
9+
text-align: left;
10+
width: 800px;
11+
position: relative;
12+
float: right;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "index.html" %}
2+
3+
{% block content %}
4+
5+
<div class="error">
6+
7+
<h1>Error 403</h1>
8+
<p>Access denied.</p>
9+
<p><em>Return <a href="{{url_for('home')}}">Home</a></em></p>
10+
11+
</div>
12+
13+
{% end block %}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends "index.html" %}
2+
3+
{% block content %}
4+
5+
<div class="error">
6+
7+
<h1>500</h1>
8+
<p>Something is wrong! We are working on it!</p>
9+
<p><em>Return <a href="{{url_for('home')}}">Home.</a></em></p>
10+
11+
</div>
12+
13+
14+
{% endblock %}

part2/reverse_flask_reza/project/templates/index.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
<html>
44
<head>
5-
<title>Flask - Behavior Driven Development</title>
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" media="screen">
5+
<title>Reversing string.</title>
6+
<link rel="stylesheet" type="text/css" href="main.css">
7+
88
</head>
99
<body>
1010
<div class="container">
11-
<!-- inheritance -->
12-
{% block content %}
13-
{% endblock %}
14-
<!-- end inheritance -->
15-
</div>
16-
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
17-
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
11+
<h2>Reverse your string!</h2>
12+
<form action ="{{ url_for('rev_str') }}" method="post">
13+
<input type="text" name="user_input">
14+
<input type="submit" value="Reverse!">
15+
</form>
16+
</div>
17+
1818
</body>
1919
</html>
2020

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
{% extends "index.html" %}
1+
<!DOCTYPE html>
22

3-
{% block content %}
3+
<html>
44

55
<div class="container">
6-
<h1>Entry your posts here.</h1>
7-
<form >
6+
<h2>Original String:</h2>
7+
<div class="output">{{ user_input }}</div>
8+
9+
<h2>Reversed String:</h2>
10+
<div class="output">{{ output }}</div>
11+
<br>
12+
<br>
13+
14+
<form action="{{ url_for('index') }}" method="post">
15+
<input type="submit" value="Back!">
16+
</form>
17+
</div>
818

9-
{% endblock %}
19+
</html>

part2/reverse_flask_reza/test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import unittest
22
from project import app
33

4-
class ReverseTest(unittest.TestCase):
4+
class TestReverse(unittest.TestCase):
55

66
def setup(self):
77
app.config['WTF_CSRF_ENABLED'] = False
8+
app.config['DEBUG'] = False
89
self.app = app.test_client()
910

1011
def test_if_user_can_access_the_first_page(self):
1112
response = self.app.get('/')
1213
self.assertEqual(response.status_code, 200)
13-
self.assertIn(b'', response.data)
14+
1415

1516
def test_if_page_redirects_and_string_is_reversed(self):
1617
test_string = 'Hello'
@@ -26,6 +27,11 @@ def test_prompt_the_user_to_input_atleast_two_lettered_string(self):
2627
response = self.app.post('/', data=dict(reverse='h', follow_redirects=True))
2728
self.assertIn(b'This field is required', response.data)
2829

29-
30+
def teardown(self):
31+
app.config['WTF_CSRF_ENABLED'] = True
32+
33+
34+
if __name__=="__main__":
35+
unittest.main()
3036

3137

0 commit comments

Comments
 (0)