Skip to content

Commit 22df5dc

Browse files
committed
Socket working when creating new channel
1 parent 65182ff commit 22df5dc

28 files changed

+488
-0
lines changed

project2/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Project 2
2+
3+
Web Programming with Python and JavaScript
3.47 KB
Binary file not shown.
1.28 KB
Binary file not shown.
2.62 KB
Binary file not shown.

project2/application.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import os
2+
3+
import json
4+
5+
from flask import Flask, session, render_template, request, session, jsonify
6+
from flask_session import Session
7+
from flask_socketio import SocketIO, emit
8+
from flask_sqlalchemy import SQLAlchemy
9+
10+
11+
from models import *
12+
13+
app = Flask(__name__)
14+
15+
database = SQLAlchemy()
16+
17+
# Check for environment variable
18+
if not os.getenv("DATABASE_URL"):
19+
raise RuntimeError("DATABASE_URL is not set")
20+
21+
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
22+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
23+
db.init_app(app)
24+
25+
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
26+
socketio = SocketIO(app)
27+
28+
# Configure session to use filesystem
29+
app.config["SESSION_PERMANENT"] = False
30+
app.config["SESSION_TYPE"] = "filesystem"
31+
Session(app)
32+
33+
34+
@app.route("/")
35+
def index():
36+
if "user" not in session.keys() or session["user"] is None:
37+
return render_template("login.html")
38+
else:
39+
return render_template("index.html")
40+
41+
42+
@app.route("/login", methods=["GET", "POST"])
43+
def login():
44+
45+
if request.method == "POST":
46+
email = request.form.get("email")
47+
password = request.form.get("password")
48+
#Selec user
49+
u = User.query.filter_by(email=email).first()
50+
if u is None or u.password != password:
51+
return render_template("error.html", message="Username or password incorrect")
52+
else:
53+
session["user"] = u
54+
return render_template("index.html")
55+
56+
return render_template("login.html")
57+
58+
59+
@app.route("/logout")
60+
def logout():
61+
""" Logout the user and clean the cache session"""
62+
session["user"] = None
63+
return render_template("login.html")
64+
65+
66+
@app.route("/register", methods=["GET", "POST"])
67+
def register():
68+
if request.method == 'GET':
69+
return render_template("register.html")
70+
else:
71+
name = request.form.get("name")
72+
email = request.form.get("email")
73+
password = request.form.get("password")
74+
# Create new user
75+
u = User(name=name, email=email, password=password)
76+
77+
#Add new user in the database
78+
u.add_user()
79+
80+
return render_template("login.html")
81+
82+
83+
@socketio.on("create channel")
84+
def socket_create_channel(data):
85+
c = Channel(name=data['name'], user_id=session['user'].id)
86+
c = c.add_channel()
87+
data['id'] = c.id
88+
emit("channel created", data, broadcast=True)
89+
90+
91+
92+
@app.route("/create_channel", methods=["GET"])
93+
def create_channel():
94+
if request.method == "GET":
95+
return render_template("create_channel.html")
96+
97+
98+
@app.route("/join/<string:id>", methods=["GET"])
99+
def join(id):
100+
if request.method == "GET":
101+
c = Channel.query.get(id)
102+
c.add_user_in_channel(session['user'].id)
103+
104+
return render_template("messages.html", channel=c)
105+
106+
@app.route("/my_channels", methods=["GET"])
107+
def my_channels():
108+
my_channels = Channel.query.filter_by(user_id=session["user"].id).all()
109+
return render_template("my_channels.html", my_channels=my_channels)
110+
111+
112+
@app.route("/others_channels", methods=["GET"])
113+
def others_channels():
114+
channels = User_has_channel.query.filter_by(user_id=session["user"].id)
115+
my_channels = []
116+
for channel in channels:
117+
c = Channel.query.filter_by(id=channel.channel_id).first()
118+
my_channels.append(c)
119+
120+
others_channels = Channel.query.all()
121+
122+
123+
for channel in my_channels:
124+
if channel in others_channels:
125+
others_channels.remove(channel)
126+
return render_template("others_channels.html", others_channels=others_channels)

project2/create.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
3+
from flask import Flask, render_template, request
4+
from models import *
5+
6+
app = Flask(__name__)
7+
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
8+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
9+
db.init_app(app)
10+
11+
def main():
12+
db.create_all()
13+
14+
if __name__ == "__main__":
15+
with app.app_context():
16+
main()
457 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
457 Bytes
Binary file not shown.
457 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)