-
Notifications
You must be signed in to change notification settings - Fork 0
/
motd.py
220 lines (169 loc) · 6.32 KB
/
motd.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from flask import Flask, render_template, request, flash, redirect, url_for, session
from pubmotd import booking
from chat import Chat
import firebase_admin
from firebase_admin import credentials, db
from wtforms import Form, StringField, TextAreaField, RadioField, SelectField, validators, PasswordField
import random
app = Flask(__name__)
@app.route('/home')
def home():
return render_template('motdhome.html')
@app.route('/motdpage')
def motd():
bookings = root.child('bookings').get()
list = [] # create a list to store all the booking objects
for pubid in bookings:
eachpub = bookings[pubid]
print(eachpub)
pub = booking(eachpub['title'], eachpub['description'])
pub.set_pubid(pubid)
print(pub.get_pubid())
list.append(pub)
rndmsg = random.choice(list)
print(rndmsg.get_title())
return render_template('MOTD.html', rndmsg=rndmsg)
@app.route('/viewbookings')
def viewbookings():
bookings = root.child('bookings').get()
list = [] # create a list to store all the booking objects
for pubid in bookings:
eachpub = bookings[pubid]
print(eachpub)
pub = booking(eachpub['title'], eachpub['description'])
pub.set_pubid(pubid)
print(pub.get_pubid())
list.append(pub)
print(list)
return render_template('view_all_motd.html', bookings=list)
class RequiredIf(object):
def __init__(self, *args, **kwargs):
self.conditions = kwargs
def __call__(self, form, field):
for name, data in self.conditions.items():
if name not in form._fields:
validators.Optional()(field)
else:
condition_field = form._fields.get(name)
if condition_field.data == data:
validators.DataRequired().__call__(form, field)
else:
validators.Optional().__call__(form, field)
class SendMessage(Form):
message = TextAreaField('Message', [
validators.Length(min=1),
validators.DataRequired()
])
@app.route('/chatroom', methods=['GET', 'POST'])
def msgs():
form = SendMessage(request.form)
if request.method == 'POST' and form.validate():
message = form.message.data
msg = Chat(message)
msg_db = root.child('chathistory')
msg_db.push({
'message': msg.get_message()
})
flash('Message Sent', 'success')
chathist = root.child('chathistory').get()
list = []
for chatid in chathist:
eachmsg = chathist[chatid]
print(eachmsg)
msg = Chat(eachmsg['message'])
msg.set_chatid(chatid)
print(msg.get_chatid())
list.append(msg)
print(list)
print(chathist)
return render_template('chat.html', form=form, chathist=list)
# @app.route('/viewbookings')
# def viewbookings():
# bookings = root.child('bookings').get()
# list = [] # create a list to store all the booking objects
# for pubid in bookings:
# eachpub = bookings[pubid]
# print(eachpub)
# pub = booking(eachpub['title'], eachpub['description'])
# pub.set_pubid(pubid)
# print(pub.get_pubid())
# list.append(pub)
# print(list)
# return render_template('view_all_motd.html', bookings=list)
class bookingForm(Form):
title = StringField('Title', [
validators.Length(min=1, max=150),
validators.DataRequired()])
description = TextAreaField('Description')
@app.route('/newbooking', methods=['GET', 'POST'])
def new():
form = bookingForm(request.form)
if request.method == 'POST' and form.validate():
title = form.title.data
description = form.description.data
pub = booking(title, description)
pub_db = root.child('bookings')
pub_db.push({
'title': pub.get_title(),
'description': pub.get_description()
})
flash('Message Inserted Successfully.', 'success')
return redirect(url_for('viewbookings'))
return render_template('create_motd.html', form=form)
@app.route('/update/<string:id>/', methods=['GET', 'POST'])
def update_booking(id):
form = bookingForm(request.form)
if request.method == 'POST' and form.validate():
title = form.title.data
description = form.description.data
pub = booking(title, description)
pub_db = root.child('bookings/' + id)
pub_db.set({
'title': pub.get_title(),
'description': pub.get_description()
})
flash('Magazine Updated Sucessfully.', 'success')
return redirect(url_for('viewbookings'))
else:
url = 'bookings/' + id
eachpub = root.child(url).get()
pub = booking(eachpub['title'], eachpub['description'])
pub.set_pubid(id)
return render_template('update_motd.html', form=form)
@app.route('/delete_booking/<string:id>', methods=['POST'])
def delete_booking(id):
pub_db = root.child('bookings/' + id)
pub_db.delete()
flash('booking Deleted', 'success')
return redirect(url_for('viewbookings'))
@app.route('/delete_msg/<string:id>', methods=['POST'])
def delete_msg(id):
msg_db = root.child('chathistory/' + id)
msg_db.delete()
flash('Message Deleted', 'success')
return redirect(url_for('msgs'))
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm(request.form)
if request.method == 'POST' and form.validate():
username = form.username.data
password = form.password.data
if username == 'admin' and password == 'P@ssw0rd': # harcoded username and password=
session['logged_in'] = True # this is to set a session to indicate the user is login into the system.
return redirect(url_for('viewbookings'))
else:
error = 'Invalid login'
flash(error, 'danger')
return render_template('motdlogin.html', form=form)
return render_template('motdlogin.html', form=form)
@app.route('/logout')
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
class LoginForm(Form):
username = StringField('Username', [validators.DataRequired()])
password = PasswordField('Password', [validators.DataRequired()])
if __name__ == '__main__':
app.secret_key = 'sekret123'
app.run()