Bläddra i källkod

clean up icons folder and database if they get unsynced in the manage site assets page

cube 3 dagar sedan
förälder
incheckning
5058c326c5
3 ändrade filer med 106 tillägg och 20 borttagningar
  1. 64
    19
      myriad/manage.py
  2. 1
    1
      myriad/templates/base.html
  3. 41
    0
      myriad/templates/manage/assets.html

+ 64
- 19
myriad/manage.py Visa fil

@@ -62,26 +62,10 @@ def edit(mid):
62 62
 
63 63
             # this specific chunk here is checking whether icons in the myriad/static/icons folder have a link in the database
64 64
             # in case the database was rebuilt, or something else happened, it is a waste of storage keeping an unlinked image
65
-            icons = db.execute("SELECT * FROM icons").fetchall()
66
-            icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
67
-            for icon in icon_storage:
68
-                in_database = False
69
-                for i in icons:
70
-                    print(i[2], icon)
71
-                    if i[2] == icon:
72
-                        in_database = True
73
-                if not in_database:
74
-                    os.remove(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], icon))
65
+            
75 66
 
76 67
             # and now for the same in reverse - clean the database of references to images that don't exist
77
-            for i in icons:
78
-                in_storage = False
79
-                print(i[2], i[0])
80
-                if i[2] in icon_storage:
81
-                    in_storage = True
82
-                if not in_storage:
83
-                    db.execute("DELETE FROM icons WHERE id=(?)", (i[0],),)
84
-                    db.commit()
68
+            
85 69
 
86 70
             # the above cleanup operations should be a button in the manage sidebar but for now they are here.
87 71
 
@@ -187,4 +171,65 @@ def reset_theme(mid):
187 171
     db.execute("UPDATE member SET card_border=(?), card_bg=(?), heading_bg=(?), heading_border=(?), heading_name=(?), heading_subtitle=(?), card_text=(?), icon_border=(?), a1=(?), a2=(?) WHERE id=(?)",(c9, c10, c11, c12, c13, c14, c15, c16, c21, c22, mid))
188 172
     db.commit()
189 173
 
190
-    return redirect(url_for("manage.edit", mid=mid))
174
+    return redirect(url_for("manage.edit", mid=mid))
175
+
176
+
177
+@bp.route("/assets")
178
+@login_required
179
+def assets():
180
+    db = get_db()
181
+    icons = db.execute("SELECT * FROM icons").fetchall()
182
+    icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
183
+    
184
+    i_storage = []
185
+    for icon in icon_storage:
186
+        in_database = False
187
+        for i in icons:
188
+            if i[2] == icon:
189
+                in_database = True
190
+        if not in_database:
191
+            i_storage.append(icon)
192
+
193
+    unlinked_icons = []
194
+    for i in icons:
195
+        in_storage = False
196
+        if i[2] in icon_storage:
197
+            in_storage = True
198
+        if not in_storage:
199
+            unlinked_icons.append(i)
200
+
201
+    return render_template("manage/assets.html", icons=unlinked_icons, icon_storage=i_storage)
202
+
203
+@bp.route("/delete_idb")
204
+@login_required
205
+def delete_idb():
206
+    db = get_db()
207
+    icons = db.execute("SELECT * FROM icons").fetchall()
208
+    icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
209
+
210
+    for i in icons:
211
+        in_storage = False
212
+        if i[2] in icon_storage:
213
+            in_storage = True
214
+        if not in_storage:
215
+            db.execute("DELETE FROM icons WHERE id=(?)", (i[0],))
216
+            db.commit()
217
+
218
+    return redirect(url_for("manage.assets"))
219
+
220
+@bp.route("/delete_ifiles")
221
+@login_required
222
+def delete_ifiles():
223
+    db = get_db()
224
+    icons = db.execute("SELECT * FROM icons").fetchall()
225
+    icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
226
+
227
+    for icon in icon_storage:
228
+        in_database = False
229
+        for i in icons:
230
+            if i[2] == icon:
231
+                in_database = True
232
+        if not in_database:
233
+            os.remove(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], icon))
234
+
235
+    return redirect(url_for("manage.assets"))

+ 1
- 1
myriad/templates/base.html Visa fil

@@ -14,7 +14,7 @@
14 14
     {% if g.user %}
15 15
     <div class="heading">Manage</div>
16 16
     <div class="navitem">> <a href="{{ url_for('manage.new') }}">Add New Member</a></div>
17
-    <div class="navitem">> <a href="">Site Assets</a></div>
17
+    <div class="navitem">> <a href="{{ url_for('manage.assets') }}">Site Assets</a></div>
18 18
     {% endif %}
19 19
 
20 20
     <div class="heading">Blog</div>

+ 41
- 0
myriad/templates/manage/assets.html Visa fil

@@ -0,0 +1,41 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+  <div class="title">{% block title %}Manage site assets{% endblock %}</div>
5
+{% endblock %}
6
+
7
+{% block content %}
8
+
9
+  <div class="heading big">Icons</div>
10
+
11
+  {% if icon_storage|length > 0 %}
12
+    {% for icon in icons %}
13
+      <img class="icon" src="{{ url_for('static', filename='icons/'+icon[2]) }}">
14
+    {% endfor %}
15
+    <br class="clear" />
16
+    <div class="maintext">
17
+      If there are icons above that won't load, the file itself may be changed or removed<br><br>
18
+      <a href="{{ url_for('manage.delete_idb') }}">Delete links to missing icons from database</a><br><br>
19
+    </div>
20
+  {% else %}
21
+    <div class="maintext">The database does not contain any moved/deleted icons!</div>
22
+  {% endif %}
23
+  
24
+  {% if icon_storage|length > 0 %}
25
+    {% for i in icon_storage %}
26
+      <img class="icon" src="{{ url_for('static', filename='icons/'+i) }}">
27
+    {% endfor %}
28
+    <br class="clear" />
29
+    <div class="maintext">
30
+      These icons are not linked to any particular member but were found in the icons folder
31
+      <a href="{{ url_for('manage.delete_ifiles') }}">Delete files in the icons folder that have no link in the database</a>
32
+    </div>
33
+  {% else %}
34
+    <div class="maintext">There are no images in the icons folder without a database link!</div>
35
+  {% endif %}
36
+
37
+  <div class="heading big">Blinkies</div>
38
+
39
+  <div class="heading big">Stamps</div>
40
+
41
+{% endblock %}