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()
cursor.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'])
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')
@ -105,6 +139,8 @@ def catch_all(path):
account = user['email']
account_link = "account"
site = user['domain'] + ".exampledomainnathan1"
elif path != "signup" and path != "login":
return redirect('/')
# If file exists, load it
if os.path.isfile('templates/' + path):

View File

@ -12,28 +12,26 @@ dbargs = {
'database':os.getenv('DB_NAME')
}
def check_tables():
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):
def get_website_data(domain):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
SELECT * FROM site WHERE domain = %s
""", (domain,))
site = cursor.fetchall()
data = cursor.fetchall()
cursor.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 dotenv
import requests
import json
import schedule
import time
import db
app = Flask(__name__)
@ -23,7 +24,13 @@ def error(message):
@app.route('/')
def index():
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>')

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>
</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>
</body>

View File

@ -22,7 +22,7 @@
</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>
<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>
</nav>
<section style="width: 50%;margin: auto;margin-top: 50px;">