feat: Add site edit
All checks were successful
Build Docker / Build Main Image (push) Successful in 20s
Build Docker / Build SLDs Image (push) Successful in 19s

This commit is contained in:
Nathan Woodburn 2023-11-08 20:43:33 +11:00
parent b02d8934c2
commit a0bb9dc032
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
6 changed files with 102 additions and 21 deletions

34
db.py
View File

@ -84,3 +84,37 @@ def update_tokens(id,tokens):
connection.commit() connection.commit()
cursor.close() cursor.close()
connection.close() connection.close()
def get_website_data(domain):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
SELECT * FROM site WHERE domain = %s
""", (domain,))
data = cursor.fetchall()
cursor.close()
connection.close()
if data == []:
# Create new entry
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
INSERT INTO site (domain, data)
VALUES (%s, %s)
""", (domain, ""))
connection.commit()
cursor.close()
connection.close()
return ""
return data[0][2]
def update_website_data(domain):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
UPDATE site SET data = %s WHERE domain = %s
""", (domain, domain))
connection.commit()
cursor.close()
connection.close()

36
main.py
View File

@ -75,6 +75,40 @@ def login():
resp.set_cookie('token', user['token']) resp.set_cookie('token', user['token'])
return resp return resp
@app.route('/edit')
def edit():
token = request.cookies['token']
if not accounts.validate_token(token):
return error('Invalid token')
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
resp = make_response(redirect('/'))
resp.set_cookie('token', '', expires=0)
return resp
data = db.get_website_data(user['domain'])
return render_template('edit.html',account=user['email'],account_link="account",data=data)
@app.route('/edit', methods=['POST'])
def send_edit():
token = request.cookies['token']
if not accounts.validate_token(token):
return error('Invalid token')
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
resp = make_response(redirect('/'))
resp.set_cookie('token', '', expires=0)
return resp
data = request.form['data']
db.update_website_data(user['domain'],data)
return redirect('/edit')
@app.route('/logout') @app.route('/logout')
@ -105,6 +139,8 @@ def catch_all(path):
account = user['email'] account = user['email']
account_link = "account" account_link = "account"
site = user['domain'] + ".exampledomainnathan1" site = user['domain'] + ".exampledomainnathan1"
elif path != "signup" and path != "login":
return redirect('/')
# If file exists, load it # If file exists, load it
if os.path.isfile('templates/' + path): if os.path.isfile('templates/' + path):

View File

@ -12,28 +12,26 @@ dbargs = {
'database':os.getenv('DB_NAME') 'database':os.getenv('DB_NAME')
} }
def check_tables(): def get_website_data(domain):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS site (
id INT(11) NOT NULL AUTO_INCREMENT,
domain VARCHAR(255) NOT NULL,
data VARCHAR(2048) NOT NULL,
PRIMARY KEY (id)
)
""")
cursor.close()
connection.close()
def get_site(domain):
connection = mysql.connector.connect(**dbargs) connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(""" cursor.execute("""
SELECT * FROM site WHERE domain = %s SELECT * FROM site WHERE domain = %s
""", (domain,)) """, (domain,))
site = cursor.fetchall() data = cursor.fetchall()
cursor.close() cursor.close()
connection.close() connection.close()
return site
if data == []:
# Create new entry
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
INSERT INTO site (domain, data)
VALUES (%s, %s)
""", (domain, ""))
connection.commit()
cursor.close()
connection.close()
return ""
return data[0][2]

View File

@ -1,10 +1,11 @@
from flask import Flask, make_response, redirect, request, jsonify, render_template, send_from_directory from flask import Flask, make_response, redirect, render_template_string, request, jsonify, render_template, send_from_directory
import os import os
import dotenv import dotenv
import requests import requests
import json import json
import schedule import schedule
import time import time
import db
app = Flask(__name__) app = Flask(__name__)
@ -23,7 +24,13 @@ def error(message):
@app.route('/') @app.route('/')
def index(): def index():
host = request.host host = request.host
return jsonify({'success': True, 'message': host}) if len(host.split('.')) < 2:
return error('Invalid domain')
# Get website data
data = db.get_website_data(host)
# Render as HTML
return render_template_string(data)
@app.route('/<path:path>') @app.route('/<path:path>')

View File

@ -25,6 +25,12 @@
<div class="d-none d-md-block"><a class="btn btn-primary" role="button" href="/account">{{account}}</a></div> <div class="d-none d-md-block"><a class="btn btn-primary" role="button" href="/account">{{account}}</a></div>
</div> </div>
</nav> </nav>
<section style="margin-top: 50px;">
<h1 style="text-align: center;">Edit your page</h1>
</section>
<section style="width: 80%;margin: auto;">
<form method="post"><textarea class="form-control form-control-lg" rows="25" name="data" placeholder="Website data">{{data}}</textarea><input class="btn btn-primary" type="submit" style="margin-top: 10px;"></form>
</section>
<script src="assets/bootstrap/js/bootstrap.min.js"></script> <script src="assets/bootstrap/js/bootstrap.min.js"></script>
</body> </body>

View File

@ -22,7 +22,7 @@
</ul> </ul>
<div class="d-md-none my-2"><button class="btn btn-light me-2" type="button">Button</button><button class="btn btn-primary" type="button">Button</button></div> <div class="d-md-none my-2"><button class="btn btn-light me-2" type="button">Button</button><button class="btn btn-primary" type="button">Button</button></div>
</div> </div>
<div class="d-none d-md-block"><a class="btn btn-primary" role="button" href="#">{{account}}</a></div> <div class="d-none d-md-block"><a class="btn btn-primary" role="button" href="/{{account_link}}">{{account}}</a></div>
</div> </div>
</nav> </nav>
<section style="width: 50%;margin: auto;margin-top: 50px;"> <section style="width: 50%;margin: auto;margin-top: 50px;">