Browse Source

Improving file uploading

JoostSijm 6 years ago
parent
commit
7a4c195344

+ 1 - 0
app/__init__.py

@@ -19,6 +19,7 @@ load_dotenv()
 
 class Config(object):
     """Flask configuration"""
+    UPLOAD_FOLDER = 'app/static/uploads'
     SCHEDULER_API_ENABLED = True
     SQLALCHEMY_DATABASE_URI = os.environ["DATABASE_URI"]
     SECRET_KEY = os.environ["SECRET_KEY"]

+ 8 - 3
app/modules/backend/app.py

@@ -8,7 +8,7 @@ import os
 from flask_login import login_required
 from flask_menu import register_menu
 from flask import render_template, request, redirect, url_for, flash, Blueprint
-from app.models import Page
+from app.models import Page, File
 
 
 BLUEPRINT = Blueprint(
@@ -23,8 +23,13 @@ BLUEPRINT = Blueprint(
 @login_required
 def index():
     """Show homepage"""
-    pages = Page.query.filter(Page.parent_id == None).all()
-    return render_template('site/index.j2', pages=pages)
+    pages = Page.query.all()
+    files = File.query.all()
+    return render_template(
+        'site/index.j2',
+        pages=pages,
+        files=files
+    )
 
 
 @BLUEPRINT.route('/render')

+ 47 - 32
app/modules/backend/modules/file/app.py

@@ -3,11 +3,14 @@
 Backend
 """
 
+import os
 from flask_login import login_required, current_user
 from flask_menu import register_menu
 from flask import render_template, request, redirect, url_for, flash, Blueprint
-from app import db
-from app.models import Page
+from werkzeug.utils import secure_filename
+from app import app, db
+from app.models import File
+
 
 
 BLUEPRINT = Blueprint(
@@ -16,34 +19,47 @@ BLUEPRINT = Blueprint(
     template_folder='templates'
 )
 
+def allowed_file(filename):
+    allowed_extensions = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
+    return '.' in filename and \
+           filename.rsplit('.', 1)[1].lower() in allowed_extensions
+
 
 @BLUEPRINT.route('/create', methods=["GET", "POST"])
 @register_menu(BLUEPRINT, 'create_file', 'Create file')
 @login_required
 def create():
-    """Page creating"""
-    pages = Page.query.all()
+    """File creating"""
     if request.method == 'POST':
-        page = Page()
-        page.title = request.form['title']
-        page.source = request.form['source']
-        page.user_id = current_user.id
-        page.parent_id = request.form['parent_id'] if request.form['parent_id'] else None
+        if 'file' not in request.files:
+            flash('No file part', 'warning')
+            return redirect(request.url)
+        file = request.files['file']
+        if file.filename == '':
+            flash('No file selected', 'warning')
+            return redirect(request.url)
+        if file and allowed_file(file.filename):
+            filename = secure_filename(file.filename)
+            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
 
-        db.session.add(page)
-        db.session.commit()
+            db_file = File()
+            db_file.title = request.form['title']
+            db_file.user_id = current_user.id
+            db_file.path = file.filename
+
+            db.session.add(db_file)
+            db.session.commit()
 
-        flash('Page "%s" successfully created' % page.title, 'success')
+            flash('File "%s" successfully uploaded' % file.filename, 'success')
 
-    return render_template('page/create.j2', pages=pages)
+    return render_template('file/create.j2')
 
 
-@BLUEPRINT.route('/edit/<int:page_id>', methods=["GET", "POST"])
+@BLUEPRINT.route('/edit/<int:file_id>', methods=["GET", "POST"])
 @login_required
-def edit(page_id):
-    """Page editing"""
-    page = Page.query.get(page_id)
-    pages = Page.query.filter(Page.id != page.id).all()
+def edit(file_id):
+    """File editing"""
+    page = File.query.get(file_id)
 
     if request.method == 'POST':
         page.title = request.form['title']
@@ -54,31 +70,30 @@ def edit(page_id):
         db.session.add(page)
         db.session.commit()
 
-        flash('Page "%s" successfully edit' % page.title, 'success')
+        flash('File "%s" successfully edit' % page.title, 'success')
 
     return render_template(
-        'page/edit.j2',
+        'file/edit.j2',
         page=page,
-        pages=pages
     )
 
 
-@BLUEPRINT.route('/remove/<int:page_id>')
+@BLUEPRINT.route('/remove/<int:file_id>')
 @login_required
-def remove(page_id):
-    """Page removing"""
-    page = Page.query.get(page_id)
+def remove(file_id):
+    """File removing"""
+    file = File.query.get(file_id)
 
-    db.session.delete(page)
+    db.session.delete(file)
     db.session.commit()
 
-    flash('Page "%s" successfully remove' % page.title, 'success')
+    flash('File "%s" successfully remove' % file.title, 'success')
     return redirect(url_for('backend.index'))
 
 
-@BLUEPRINT.route('/view/<int:page_id>')
+@BLUEPRINT.route('/view/<int:file_id>')
 @login_required
-def view(page_id):
-    """Display page"""
-    page = Page.query.get(page_id)
-    return render_template('page/view.j2', page=page)
+def view(file_id):
+    """Display file"""
+    file = File.query.get(file_id)
+    return render_template('file/view.j2', file=file)

+ 17 - 0
app/modules/backend/modules/file/templates/file/create.j2

@@ -0,0 +1,17 @@
+{% extends "layout/backend.j2" %}
+{% block content %}
+<h1>File upload</h1>
+<form method="post" enctype="multipart/form-data">
+    <div class="form-group">
+        <label class="text-normal text-dark">Title</label>
+        <input type="text" class="form-control" name="title" placeholder="title">
+    </div>
+    <div class="form-group">
+        <label class="text-normal text-dark">File</label>
+        <input type="file" class="form-control-file border" name="file">
+    </div>
+    <div class="form-group float-right">
+        <button class="btn btn-primary">Create</button>
+    </div>
+</form>
+{% endblock %}

+ 0 - 0
app/modules/backend/modules/file/templates/edit.j2 → app/modules/backend/modules/file/templates/file/edit.j2


+ 7 - 0
app/modules/backend/modules/file/templates/file/view.j2

@@ -0,0 +1,7 @@
+{% extends "layout/backend.j2" %}
+{% block content %}
+<h1>Title: {{ file.title }}</h1>
+<a href="{{ url_for('backend_file.edit', file_id=file.id) }}">Edit</a><br>
+<p>{{ file.path }}</p>
+<img src="/static/uploads/{{ file.path }}" alt="" class="img-thumbnail">
+{% endblock %}

+ 0 - 26
app/modules/backend/modules/page/templates/create.j2

@@ -1,26 +0,0 @@
-{% extends "layout/backend.j2" %}
-{% block content %}
-<h1>Page create</h1>
-<form method="post">
-    <div class="form-group">
-        <label class="text-normal text-dark">title</label>
-        <input type="text" class="form-control" name="title" placeholder="title">
-    </div>
-    <div class="form-group">
-        <label class="text-normal text-dark">Source</label>
-        <textarea class="form-control" name="source"></textarea>
-    </div>
-    <div class="form-group">
-        <label class="text-normal text-dark">Parent</label>
-        <select class="form-control" name="parent_id">
-            <option></option>
-            {% for page in pages %}
-            <option value="{{ page.id }}">{{ page.title }}</option>
-            {% endfor %}
-        </select>
-    </div>
-    <div class="form-group pull-right">
-        <button class="btn btn-primary">Create</button>
-    </div>
-</form>
-{% endblock %}

+ 0 - 0
app/modules/backend/modules/file/templates/create.j2 → app/modules/backend/modules/page/templates/page/create.j2


+ 0 - 0
app/modules/backend/modules/page/templates/edit.j2 → app/modules/backend/modules/page/templates/page/edit.j2


+ 1 - 1
app/modules/backend/modules/file/templates/view.j2 → app/modules/backend/modules/page/templates/page/view.j2

@@ -1,6 +1,6 @@
 {% extends "layout/backend.j2" %}
 {% block content %}
 <h1>Title: {{ page.title }}</h1>
-<a href="{{ url_for('backend.edit_page', page_id=page.id) }}">Edit</a><br>
+<a href="{{ url_for('backend_page.edit', page_id=page.id) }}">Edit</a><br>
 <p>{{ page.content() }}</p>
 {% endblock %}

+ 0 - 6
app/modules/backend/modules/page/templates/view.j2

@@ -1,6 +0,0 @@
-{% extends "layout/backend.j2" %}
-{% block content %}
-<h1>Title: {{ page.title }}</h1>
-<a href="{{ url_for('backend.edit_page', page_id=page.id) }}">Edit</a><br>
-<p>{{ page.content() }}</p>
-{% endblock %}

+ 86 - 38
app/modules/backend/templates/site/index.j2

@@ -1,43 +1,91 @@
 {% extends "layout/backend.j2" %}
 {% block content %}
 <h1>Backend</h1>
-<p>List of pages</p>
-<div class="table-responsive">
-    <table class="table table-striped table-sm">
-        <thead>
-            <tr>
-                <th>Action</th>
-                <th>Title</th>
-                <th>Datum</th>
-                <th>Author</th>
-            </tr>
-        </thead>
-        <tbody>
-            {%- for page in pages recursive -%}
-            <tr>
-                <td>
-                    <div class="btn-group">
-                        <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
-                            Action
-                        </button>
-                        <div class="dropdown-menu">
-                            <a class="dropdown-item" href="{{ url_for('backend_page.view', page_id=page.id) }}">View</a>
-                            <a class="dropdown-item" href="{{ url_for('backend_page.edit', page_id=page.id) }}">Edit</a>
-                            <a class="dropdown-item" href="{{ url_for('backend_page.remove', page_id=page.id) }}">Remove</a>
-                        </div>
-                    </div>
-                </td>
-                <td>
-                    {{ '> ' * (loop.depth - 1) }}<a href="{{ url_for('backend_page.view', page_id=page.id) }}">
-                        {{ page.title if page.title else 'page %s' % page.id }}
-                    </a>
-                </td>
-                <td>{{ page.datetime }}</td>
-                <td>{{ page.user.name }}</td>
-            </tr>
-            {{ loop(page.children) }}
-            {%- endfor -%}
-        </tbody>
-    </table>
+<div class="row">
+    <div class="col-sm">
+        <div class="card">
+            <div class="card-header">
+                Pages
+            </div>
+            <div class="card-body">
+                <table class="table table-striped table-sm">
+                    <thead>
+                        <tr>
+                            <th>Action</th>
+                            <th>Title</th>
+                            <th>Author</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        {%- for page in pages recursive -%}
+                        <tr>
+                            <td>
+                                <div class="btn-group">
+                                    <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
+                                        Action
+                                    </button>
+                                    <div class="dropdown-menu">
+                                        <a class="dropdown-item" href="{{ url_for('backend_page.view', page_id=page.id) }}">View</a>
+                                        <a class="dropdown-item" href="{{ url_for('backend_page.edit', page_id=page.id) }}">Edit</a>
+                                        <a class="dropdown-item" href="{{ url_for('backend_page.remove', page_id=page.id) }}">Remove</a>
+                                    </div>
+                                </div>
+                            </td>
+                            <td>
+                                {{ '> ' * (loop.depth - 1) }}<a href="{{ url_for('backend_page.view', page_id=page.id) }}">
+                                    {{ page.title if page.title else 'page %s' % page.id }}
+                                </a>
+                            </td>
+                            <td>{{ page.user.name }}</td>
+                        </tr>
+                        {{ loop(page.children) }}
+                        {%- endfor -%}
+                    </tbody>
+                </table>
+            </div>
+        </div>
+    </div>
+    <div class="col-sm">
+        <div class="card">
+            <div class="card-header">
+                Files
+            </div>
+            <div class="card-body">
+                <table class="table table-striped table-sm">
+                    <thead>
+                        <tr>
+                            <th>Action</th>
+                            <th>Title</th>
+                            <th>Author</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        {% for file in files %}
+                        <tr>
+                            <td>
+                                <div class="btn-group">
+                                    <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
+                                        Action
+                                    </button>
+                                    <div class="dropdown-menu">
+                                        <a class="dropdown-item" href="{{ url_for('backend_file.view', file_id=file.id) }}">View</a>
+                                        <a class="dropdown-item" href="{{ url_for('backend_file.edit', file_id=file.id) }}">Edit</a>
+                                        <a class="dropdown-item" href="{{ url_for('backend_file.remove', file_id=file.id) }}">Remove</a>
+                                    </div>
+                                </div>
+                            </td>
+                            <td>
+                                <a href="{{ url_for('backend_file.view', file_id=file.id) }}">
+                                    {{ file.title if file.title else 'file %s' % file.id }}
+                                </a>
+                            </td>
+                            <td>{{ file.user.name }}</td>
+                        </tr>
+                        {%- endfor -%}
+                    </tbody>
+                </table>
+            </div>
+        </div>
+    </div>
 </div>
 {% endblock %}

+ 2 - 0
app/static/uploads/.gitignore

@@ -0,0 +1,2 @@
+*
+!.gitignore