forked from meet-projects/Y2S17-seed-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
175 lines (132 loc) · 6.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# flask imports
from flask import Flask, render_template, request, redirect, url_for
# SQLAlchemy
from model import Base, User, Video, Comment
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# setup
app = Flask(__name__)
engine = create_engine('sqlite:///project.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
@app.route('/register', methods=['GET','POST'])
def register():
#If it's a simple get, return the wanted page.
if request.method == 'GET':
return render_template('register.html')
#If the user pressing register button it will try to sign him up
#and return "Login page".
else:
username = request.form.get('username')
name = request.form.get('fullname')
password = request.form.get('password')
email = request.form.get('email')
country = request.form.get('country')
sing = request.form.get('sing')
guitar = request.form.get('guitar')
drums = request.form.get('drums')
flute = request.form.get('flute')
boy = request.form.get('boy')
girl = request.form.get('girl')
#If this username is already exists it will bring you back.
if len(session.query(User).filter_by(username = username).all()) != 0:
return redirect(url_for('register'))
#If it's a new one create a new user and go to the "Login" page
else:
user = User(username = username, name = name, password = password, email = email, country = country, sing = sing, guitar = guitar, drums = drums, flute = flute, boy = boy, girl = girl)
session.add(user)
session.commit()
return redirect(url_for('login'))
@app.route('/login', methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
#Checking if it's a real username and real password
else:
check_username = request.form.get('check_username')
check_password = request.form.get('check_password')
wanted_user = session.query(User).filter_by(username = check_username).first()
#if it is correct continue to the homepage
if wanted_user.password == check_password:
return redirect(url_for('homepage',user_id = wanted_user.id))
#Else try again
else:
return redirect(url_for('login'))
@app.route('/homepage/<int:user_id>')
def homepage(user_id):
#Publishing on the feed all the videos and also saving the user (NOTE : there is a need?)
user = session.query(User).filter_by(id = user_id).first()
videos = session.query(Video).filter_by(publish = True).all()
owners = {}
for video in videos:
owners[video.video] = session.query(User).filter_by(id = video.owner).first().name
return render_template('homepage.html', user_id = user.id, videos = videos, owners=owners)
@app.route('/profile/<int:user_id>')
def profile(user_id):
#Showing the whole videos of the current user and showing all the info about him.
user = session.query(User).filter_by(id = user_id).first()
roles = []
if user.sing:
roles.append("Singer")
if user.guitar:
roles.append("Guitarist")
if user.drums:
roles.append("Drummer")
if user.flute:
roles.append("Flutist")
if user.boy:
roles.append("boy")
if user.girl:
roles.append("girl")
user_description = ','.join(roles)
videos = session.query(Video).filter_by(owner = user.name).all()
return render_template('profile.html', user = user, videos = videos, desc= user_description)
@app.route('/publish/<int:user_id>', methods=['GET','POST'])
def publish(user_id):
videos = session.query(Video).all()
user = session.query(User).filter_by(id = user_id).first()
if request.method == 'GET':
return render_template('post.html', user_id = user_id)
#Creare new video to publish
else:
video = request.form.get('video')
description = request.form.get('description')
owner = user_id
if video in videos:
return redirect('publish',user_id = user_id)
else:
new_vid = Video(video = video, description = description, owner = owner)
session.add(new_vid)
session.commit()
return redirect(url_for('homepage',user_id = user_id))
@app.route('/add/<int:user_id>/<int:video_id>', methods=['GET','POST'])
def add(user_id, video_id):
#Taking the old video ti present it to the user and also to mention the video last publisher to post it
#Interactivly
user = session.query(User).filter_by(id = user_id).first()
curr_video = session.query(Video).filter_by(id = video_id).first()
if request.method == 'GET':
return render_template('add.html', user_id = user_id, video_id = video_id)
#Adding the video with refrence to the old one
else:
video = request.form.get('video')
description = request.form.get('description')
owner = user.id
curr_video.other_video = video
publish = False
new_vid = Video(video = video, description = description, publish = publish, owner = owner)
session.add(new_vid)
session.commit()
return redirect(url_for('homepage',user_id = user_id))
############
# #
# NOTE'S #
# #
############
#Ask an instructor if it's possible to create a linked list out of the friend list as a feature (MODEL).
#Ask an instructor how to present an actual video (FRONTEND).
#Think about a more efficiant way to store the data (MODEL).
#FEATURE : create out own icon's to put insted of boring button like a picture button(CSS).
#Ask an instructor to help backend team to debug and understand if the code is good.
#Ask an instructor if it's possible to make a kind of a "scroll".