Browse Source

Improve file editing

JoostSijm 6 years ago
parent
commit
10f09b0236

+ 2 - 1
app/models.py

@@ -13,6 +13,7 @@ from app import db, argon2, login_manager
 
 
 ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
+IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
 
 page_file = db.Table(
     'page_file',
@@ -124,7 +125,7 @@ class File(db.Model):
 
     def is_image(self):
         """True if file is image"""
-        return self.extension() in ALLOWED_EXTENSIONS
+        return self.extension() in IMAGE_EXTENSIONS
 
 
     user_id = db.Column(

+ 23 - 8
app/modules/backend/modules/file/app.py

@@ -63,22 +63,37 @@ def create():
 @login_required
 def edit(file_id):
     """File editing"""
-    page = File.query.get(file_id)
+    db_file = File.query.get(file_id)
 
     if request.method == 'POST':
-        page.title = request.form['title']
-        page.source = request.form['source']
-        page.parent_id = request.form['parent_id'] if request.form['parent_id'] else None
-        page.user_id = current_user.id
+        file = None
+        if 'file' in request.files:
+            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))
+
+
+        if file is not None:
+            db_file.path = file.filename
+        if request.form['title']:
+            db_file.title = request.form['title']
+        else:
+            db_file.title = db_file.path
+
+        db_file.user_id = current_user.id
 
-        db.session.add(page)
+        db.session.add(db_file)
         db.session.commit()
 
-        flash('File "%s" successfully edit' % page.title, 'success')
+        flash('File "%s" successfully edit' % db_file.title, 'success')
 
     return render_template(
         'file/edit.j2',
-        page=page,
+        file=db_file,
     )
 
 

+ 23 - 26
app/modules/backend/modules/file/templates/file/edit.j2

@@ -1,31 +1,28 @@
 {% extends "layout/backend.j2" %}
 {% block content %}
-<h1>Edit: {{ page.title }}</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" value="{{ page.title }}">
+<h1>Edit: {{ file.title }}</h1>
+<div class="row">
+    <div class="col-sm">
+        <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" value="{{ file.title }}">
+            </div>
+            <div class="form-group">
+                <label class="text-normal text-dark">File</label>
+                <input type="file" class="form-control-file" name="file">
+            </div>
+            <div class="form-group float-right">
+                <button class="btn btn-primary">Save</button>
+            </div>
+        </form>
     </div>
-    <div class="form-group">
-        <label class="text-normal text-dark">Source</label>
-        <textarea class="form-control" name="source">{{ page.source }}</textarea>
+    <div class="col-sm">
+        {% if file.is_image() %}
+        <img src="/static/uploads/{{ file.path }}" alt="{{ file.title }}" class="img-thumbnail">
+        {% else %}
+        <a href="/static/uploads/{{ file.path }}">{{ file.title }}</a>
+        {% endif %}
     </div>
-    <div class="form-group">
-        <label class="text-normal text-dark">Parent</label>
-        <select class="form-control" name="parent_id">
-            <option></option>
-            {% for parent_page in pages %}
-            {% if parent_page.id == page.parent_id %}
-            <option value="{{ parent_page.id }}" selected>{{ parent_page.title }}</option>
-            {% else %}
-            <option value="{{ parent_page.id }}">{{ parent_page.title }}</option>
-            {% endif %}
-            {% endfor %}
-        </select>
-    </div>
-    <div class="form-group pull-right">
-        <button class="btn btn-primary">Save</button>
-    </div>
-</form>
-<p>{{ page.content() }}</p>
+</div>
 {% endblock %}