Se dejo mas bonito, con una vista de preview donde puedo editar los nombres de los archivos o eliminarlos ademas de una vista de about, donde alguien puede contactarme en caso de haber algun problema con sus imagenes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
from flask import Flask, render_template
|
||||
from werkzeug import SharedDataMiddleware
|
||||
|
||||
def create_app():
|
||||
@@ -29,6 +29,10 @@ def create_app():
|
||||
from . import auth
|
||||
app.register_blueprint(auth.bp)
|
||||
|
||||
from . import about
|
||||
app.register_blueprint(about.bp)
|
||||
app.add_url_rule('/about', endpoint='about')
|
||||
|
||||
from . import files
|
||||
app.register_blueprint(files.bp)
|
||||
app.add_url_rule('/', endpoint='index')
|
||||
|
||||
40
files/about.py
Normal file
40
files/about.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from files.db import get_db
|
||||
|
||||
from flask import Blueprint, request, render_template, redirect, url_for, flash
|
||||
|
||||
bp = Blueprint('about', __name__, url_prefix='/about')
|
||||
|
||||
@bp.route('/', methods=('GET', 'POST'))
|
||||
def about():
|
||||
if request.method == 'POST':
|
||||
name = request.form['name']
|
||||
email = request.form['email']
|
||||
message = request.form['message']
|
||||
|
||||
db = get_db()
|
||||
error = None
|
||||
|
||||
if not name:
|
||||
error = 'Missing name'
|
||||
elif not email:
|
||||
error = 'Missing email'
|
||||
elif not message:
|
||||
error = 'Empty message'
|
||||
|
||||
if error is not None:
|
||||
flash(error)
|
||||
else:
|
||||
db.execute(
|
||||
'INSERT INTO messages'
|
||||
' (name, email, message)'
|
||||
' VALUES (?, ?, ?)',
|
||||
(name, email, message)
|
||||
)
|
||||
db.commit()
|
||||
return redirect(url_for('about.thanks'))
|
||||
|
||||
return render_template('about/about.html')
|
||||
|
||||
@bp.route('/thanks')
|
||||
def thanks():
|
||||
return render_template('about/thanks.html')
|
||||
@@ -1,16 +1,25 @@
|
||||
import os
|
||||
import random
|
||||
|
||||
from flask import Flask, Blueprint, flash, request, redirect, url_for, current_app, render_template, send_from_directory
|
||||
|
||||
from werkzeug.utils import secure_filename
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
from files.auth import admin_required
|
||||
|
||||
import random
|
||||
|
||||
bp = Blueprint('files', __name__)
|
||||
bp.add_url_rule('/uploads/<path:filename>', 'uploaded_file', build_only=True)
|
||||
|
||||
def get_extension(filename):
|
||||
return filename.rsplit('.', 1)[1].lower()
|
||||
|
||||
def get_path_in_upload(filename):
|
||||
return os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_EXTENSIONS']
|
||||
return '.' in filename and get_extension(filename) in current_app.config['ALLOWED_EXTENSIONS']
|
||||
|
||||
@bp.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
@@ -30,8 +39,45 @@ def upload_file():
|
||||
return rediret(request.url)
|
||||
if file and allowed_file(file.filename):
|
||||
filename = secure_filename(file.filename)
|
||||
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
|
||||
file.save(get_path_in_upload(filename))
|
||||
|
||||
return redirect(url_for('index'))
|
||||
return redirect(url_for('files.preview_file', filename=filename))
|
||||
return render_template('files/upload.html')
|
||||
|
||||
@bp.route('/preview/<path:filename>')
|
||||
def preview_file(filename):
|
||||
file = os.path.isfile(get_path_in_upload(filename))
|
||||
if os.path.isfile(get_path_in_upload(filename)):
|
||||
return render_template('files/preview.html', filename=filename)
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
|
||||
@bp.route('/rename/<path:filename>', methods=['POST'])
|
||||
@admin_required
|
||||
def rename_file(filename):
|
||||
if request.method == 'POST' and os.path.isfile(get_path_in_upload(filename)):
|
||||
new_name = request.form['new_name'].lower()
|
||||
extension = filename.rsplit('.', 1)[1].lower()
|
||||
|
||||
if "." in new_name and get_extension(new_name):
|
||||
new_name = new_name.rsplit('.',1)[0] + '.' + extension
|
||||
else:
|
||||
new_name = new_name + '.' + extension
|
||||
|
||||
new_name = secure_filename(new_name)
|
||||
os.rename(get_path_in_upload(filename), get_path_in_upload(new_name))
|
||||
|
||||
return redirect(url_for('files.preview_file', filename=new_name))
|
||||
|
||||
@bp.route('/delete/<path:filename>', methods=['POST'])
|
||||
@admin_required
|
||||
def delete_file(filename):
|
||||
full_path = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
|
||||
|
||||
if request.method == 'POST' and os.path.isfile(full_path):
|
||||
os.remove(full_path)
|
||||
return redirect(url_for('index'))
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
DROP TABLE IF EXISTS users;
|
||||
DROP TABLE IF EXISTS messages;
|
||||
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -6,4 +7,11 @@ CREATE TABLE users (
|
||||
password TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
sended TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
|
||||
@@ -8,6 +8,21 @@ a {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
img {
|
||||
display: block;
|
||||
max-width: 100%;
|
||||
max-height: 90%;
|
||||
margin: 1rem auto 5rem auto;
|
||||
}
|
||||
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
color: var(--primary-color);
|
||||
}
|
||||
|
||||
.flash {
|
||||
color: #ff0000;
|
||||
}
|
||||
|
||||
.navbar {
|
||||
padding-top: 2rem;
|
||||
padding-bottom: 1.8rem;
|
||||
|
||||
21
files/templates/about/about.html
Normal file
21
files/templates/about/about.html
Normal file
@@ -0,0 +1,21 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}about{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h3>About This</h3>
|
||||
<p>This page is a simple static file server mainly for things that i want to link in my main webpage. If for some reason, one of the files uploaded here belongs to you, and you dont want it here, send me a message and I will contact you.</p>
|
||||
|
||||
{% for message in get_flashed_messages() %}
|
||||
<li class="flash">{{ message }}!!</li>
|
||||
{% endfor %}
|
||||
<form method="post">
|
||||
<label for="name">~/name:</label>
|
||||
<input type="text" class="u-full-width" id="name" name="name">
|
||||
<label for="email">~/email:</label>
|
||||
<input type="email" class="u-full-width" id="email" name="email">
|
||||
<label for="message">~/message:</label>
|
||||
<textarea id="message" name="message" class="u-full-width"></textarea>
|
||||
<input type="submit" class="button-primary" value="Send">
|
||||
</form>
|
||||
{% endblock %}
|
||||
10
files/templates/about/thanks.html
Normal file
10
files/templates/about/thanks.html
Normal file
@@ -0,0 +1,10 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}about/thanks{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h3>Thanks</h3>
|
||||
<p>
|
||||
Thanks for sending a message, I will send you an email as soon as possible.
|
||||
</p>
|
||||
{% endblock %}
|
||||
@@ -12,6 +12,7 @@
|
||||
<nav class="navbar">
|
||||
<div class="container">
|
||||
<a href="{{ url_for('index') }}" class="nav-brand">/files</a>
|
||||
<a href="{{ url_for('about') }}" class="nav-link">/about</a>
|
||||
{% if g.user %}
|
||||
<a href="{{ url_for('files.upload_file') }}" class="nav-link">/upload</a>
|
||||
<a href="{{ url_for('auth.logout') }}" class="nav-link">/logout</a>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
{% block content %}
|
||||
<div class="file-listing">
|
||||
{% for filename in filenames %}
|
||||
<a href="{{ url_for('files.uploaded_file', filename=filename) }}">{{ filename }}</a>
|
||||
<a href="{{ url_for('files.preview_file', filename=filename) }}">{{ filename }}</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
26
files/templates/files/preview.html
Normal file
26
files/templates/files/preview.html
Normal file
@@ -0,0 +1,26 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block title %}preview/{{ filename }}{% endblock %}
|
||||
{% block content %}
|
||||
|
||||
{% if g.user %}
|
||||
<form method="post" action={{ url_for('files.delete_file', filename=filename) }}>
|
||||
<input type="submit" class="button-primary u-pull-right" value="delete">
|
||||
</form>
|
||||
{% endif %}
|
||||
|
||||
<div class="row">
|
||||
<h3 class="u-pull-left"><a href="{{ url_for('files.uploaded_file', filename=filename) }}">{{ filename }}</a></h3>
|
||||
</div>
|
||||
|
||||
<img src="{{ url_for('files.uploaded_file', filename=filename) }}">
|
||||
|
||||
{% if g.user %}
|
||||
<form action="{{ url_for('files.rename_file', filename=filename) }}" method="post">
|
||||
<label for="new_name">~/rename</label>
|
||||
<input type="text" class="u-full-width" id="new_name" name="new_name" value="{{ filename }}">
|
||||
<input type="submit" class="button-primary u-pull-right" value="rename">
|
||||
</form>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user