Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the app going #176

Merged
merged 3 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 52 additions & 32 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/usr/bin/env python3
from flask import Flask, render_template, request, session
from flask import Flask, render_template, request, session, redirect, url_for
import logging
from flask_debugtoolbar import DebugToolbarExtension
from mysql import connector
from passlib.hash import sha256_crypt

app = Flask(__name__)

# !--- For debugging switch to true ---!
app.debug = False

app.config["SECRET_KEY"] = "OCML3BRawWEUeaxcuKHLpw"
toolbar = DebugToolbarExtension(app)


def definedlog(fileHandler):
Expand All @@ -29,6 +35,24 @@ def connect_db(host, user, password, database):
conn = connect_db('localhost', 'root', 'LoginPass@@11223344', 'tiger')


@app.route('/sign_up', methods=['GET', 'POST'])
def sign_up():
if request.method == 'POST':
userDetails = request.form
username = userDetails['username']
password = sha256_crypt.encrypt(userDetails["password"])
mycursor = conn.cursor()
sql = "INSERT INTO users (username, password) VALUES (%s, %s)"
val = (username, password)
mycursor.execute(sql, val)
conn.commit()
# if authenticate_user(username, password):
session["USERNAME"] = username
session["PASSWORD"] = password
return redirect(url_for('send_message'))
return render_template('/sign_up.html')


@app.route('/')
def home():
return render_template('/home.html')
Expand All @@ -43,30 +67,34 @@ def contact_us():
def send_message():
if request.method == 'POST':
userDetails = request.form
username = userDetails['username']
username = session["USERNAME"]
msg = userDetails['content']
mycursor = conn.cursor()
sql = "INSERT INTO messages (username, content) VALUES (%s, %s)"
val = (username, msg)
mycursor.execute(sql, val)
conn.commit()
return redirect(url_for('messages_view'))
return render_template('/send_message.html')


def authenticate_user(username, password):
if check_username(username):
if check_password(username, password):
return True
else:
# TODO: flash a message about incorrect password
return False
# TODO: flash a message about incorrect username
def check_username(username, pas):
maulers = conn.cursor()
Fender = "SELECT * FROM users"
maulers.execute(Fender)
result = maulers.fetchall()
for user in result:
print(user)
if user[0] == username:
if sha256_crypt.verify(pas, user[1]):
return True
return False


def check_password(username, password):
# TODO: check password against the db
pass
def authenticate_user(username, password):
if check_username(username, password):
return True
return False


@app.route('/messages_view', methods=['GET', 'POST'])
Expand All @@ -87,32 +115,24 @@ def messages_view():
def log_in():
if request.method == "POST":
req = request.form
email = req.get("email")
username = req.get("username")
password = req.get("password")
# if authenticate_user(username, password):
session["EMAIL"] = email
session["PASSWORD"] = password
return render_template('home.html', email=session["EMAIL"])
return render_template('/sign_up.html')
if authenticate_user(username, password):
session["USERNAME"] = username
session["PASSWORD"] = password
return redirect(url_for('send_message'))
else:
return redirect(url_for('log_in'))
return render_template('send_message.html',
username=session["USERNAME"])
return render_template('/sign_in.html')


@app.route('/log_out')
def log_out():
session.clear()
return render_template('home.html')


if __name__ == '__main__':
app.run(host='0.0.0.0')


def check_username(username):
maulers = conn.cursor()
Fender = "SELECT * FROM users WHERE username =" + username
maulers.execute(Fender)
result = maulers.fetchall()
if not result:
# the user doesnt exist
return False
else:
# the user exists
return True
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Flask
mysql-connector-python
mysql-connector-python
passlib
flask-debugtoolbar
2 changes: 1 addition & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h1>Welcome to The Academic College of Tel Aviv-Yaffo chat-App!!!</h1>
<h2 style="color:red;">This app will allow you to send a message and view it. </h2>
<h2 style="color:purple;"> <b>You can view our app's messages whenever you want! </b></h2>
<h3>If you would like to use our app, you need to follow these steps:</h3>
<h3><b>1. <a href="{{ url_for('log_in') }}">Register </a> / <a href="{{ url_for('log_in') }}">Log in</a></b></h2>
<h3><b>1. <a href="{{ url_for('sign_up') }}">Register </a> / <a href="{{ url_for('log_in') }}">Log in</a></b></h2>
<h3><b>2. Send a message </b></h3>
<h3>If you would like to read about <a href="https://www.int.mta.ac.il/the-student-union" target=_blank>
us</a> and our collaboration with the <a href="https://www.redhat.com/en" target=_blank>RedHat</a> team, you can click on this
Expand Down
68 changes: 41 additions & 27 deletions templates/layout.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link
rel="stylesheet"
type="text/css"
href="{{ url_for('static',filename='css/cssHome.css') }}"
/>
{% block head %}
<title>{% block title %}{% endblock %} - ChatApp</title>
{% endblock %}
</head>

<head>
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/cssHome.css') }}">
{% block head %}
<title>{% block title %}{% endblock %} - ChatApp </title>
{% endblock %}

</head>

<body>
<body>
<header>
<a href="{{ url_for('home') }}" target=_self>
<img src="{{ url_for('static', filename='pic/logo_1.png') }}" id="homeLogo" alt="RedHat&MTA">
</a>
<a href="{{ url_for('home') }}" target="_self">
<img
src="{{ url_for('static', filename='pic/logo_1.png') }}"
id="homeLogo"
alt="RedHat&MTA"
/>
</a>
</header>
<nav>
<ul>
<li><a class="one" href="{{url_for('home') }}"> Home Page</a></li>
<li><a class="one" href="{{url_for('log_in') }}"> Log-In</a></li>
<li><a class="one" href="{{url_for('messages_view') }}"> View Messages</a></li>
<li><a class="one" href="{{url_for('send_message') }}"> Send Message</a></li>
<nav>
<ul>
<li><a class="one" href="{{url_for('home') }}"> Home Page</a></li>
<li><a class="one" href="{{url_for('log_in') }}"> Log-In</a></li>
<li><a class="one" href="{{url_for('log_out') }}"> Log-Out</a></li>
<li>
<a class="one" href="{{url_for('messages_view') }}"> View Messages</a>
</li>
<li>
<a class="one" href="{{url_for('send_message') }}"> Send Message</a>
</li>
</ul>
</nav>

</ul>
</nav>

<main>
{% block content %}{% endblock %}
<main>
{% block content %}{% endblock %}
</main>

<div class="footer">
<h2>&copy; Copyright 2020 by <a href="https://www.facebook.com/141518342586502/posts/3159131667491806/">MTA Students</a></h2>
<h2>
&copy; Copyright 2020 by
<a
href="https://www.facebook.com/141518342586502/posts/3159131667491806/"
>MTA Students</a
>
</h2>
</div>

</body>

</body>
</html>
101 changes: 57 additions & 44 deletions templates/send_message.html
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@

<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width,initial-scale=1" name="viewport">
<meta content="description" name="description">
<meta name="google" content="notranslate" />
<meta content="Mashup templates have been developped by Orson.io team" name="author">

<!-- Disable tap highlight on IE -->
<meta name="msapplication-tap-highlight" content="no">

<title>Tiger</title>

<link rel="stylesheet" type="text/css" href=" {{ url_for('static',filename='style.css') }}">
</head>


<body>
{% extends "layout.html" %}
{% block content %}

<h1>Send Message</h1>
<form method="POST"><br>
Enter your email <input type="text" name="username"/> <br>
Enter your message<br>
<textarea rows="4" cols="40" name="content"> </textarea> <br>
<p id="time">
<script>
document.getElementById("time").innerHTML = Date();

</script><br>
</p>
<input type="submit" value="Send Message"> <input type="Reset" name="reset1" value="Reset">
</form><br>



{% endblock %}

</body>

<head>
<meta charset="UTF-8" />
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
<meta content="width=device-width,initial-scale=1" name="viewport" />
<meta content="description" name="description" />
<meta name="google" content="notranslate" />
<meta
content="Mashup templates have been developped by Orson.io team"
name="author"
/>

<!-- Disable tap highlight on IE -->
<meta name="msapplication-tap-highlight" content="no" />

<title>Tiger</title>

<link
rel="stylesheet"
type="text/css"
href=" {{ url_for('static',filename='style.css') }}"
/>
</head>

<body>
{% extends "layout.html" %} {% block content %}

<h1 style="color:black;">Send Message</h1>
{% if not session.get("USERNAME") %}
<h1>Please register or log in</h1>
{% else %}
<form method="POST">
<br />
Enter your user name
<input
type="text"
name="username"
readonly="readonly"
value='{{ session["USERNAME"] }}'
/>
<br />
Enter your message<br />
<textarea rows="4" cols="40" name="content"> </textarea> <br />
<p id="time">
<script>
document.getElementById("time").innerHTML = Date();
</script>
<br />
</p>
<input type="submit" value="Send Message" />
<input type="Reset" name="reset1" value="Reset" />
</form>
{% endif %}
<br />

{% endblock %}
</body>
</html>
Loading