-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (51 loc) · 2.08 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
from flask import Flask, render_template, request, session, redirect, url_for
from os import urandom
from pymongo import MongoClient
from config import password, username
app = Flask(__name__)
app.secret_key = urandom(24)
client = MongoClient(f"mongodb+srv://{username}:{password}@cluster0.8ejth.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = client.get_database('coding-bubble')
records = db.admin
resource_collection = db.resource_collection
@app.route('/')
def home():
return render_template("index.html")
@app.route('/admin', methods=['post', 'get'])
def admin():
if "name" in session:
return redirect(url_for('insert'))
if request.method == "POST":
name = request.form.get("name")
password = request.form.get("password")
credential = records.find_one({"name": name})
if credential:
correct_name = credential['name']
correct_password = credential['password']
if correct_password == password and correct_name == name:
session['name'] = correct_name
# print(session)
return redirect(url_for('insert'))
else:
if "name" in session:
return redirect(url_for('insert'))
return render_template("admin.html")
@app.route('/insert', methods=['post', 'get'])
def insert():
if "name" in session:
if request.method == "POST":
title = request.form.get('tittle')
category = request.form.get('category')
description = request.form.get('description')
url = request.form.get('url')
resource_details = {"tittle": title, "category": category, "description": description, "url": url}
resource_collection.insert_one(resource_details)
return render_template('insert.html')
return render_template('insert.html')
return redirect(url_for('admin'))
@app.route('/resources')
def resources():
data = resource_collection.find()
return render_template("resources.html", data = data)
if __name__ == '__main__':
app.run()