Pārlūkot izejas kodu

CSV download feature ready for merge

Set the secure download method
control 3 gadi atpakaļ
vecāks
revīzija
53f641e427
3 mainītis faili ar 34 papildinājumiem un 10 dzēšanām
  1. 22 1
      app/logic.py
  2. 7 0
      app/static/generated/Et8PfVYZDWK5MQG1.csv
  3. 5 9
      app/templates/home.html

+ 22 - 1
app/logic.py

@@ -1,12 +1,14 @@
 # Site Back-End Logic
 from hmac import new
 from unicodedata import category
-from flask import Blueprint, render_template, request, flash, redirect, url_for
+from flask import Blueprint, render_template, request, flash, redirect, url_for, send_file
 from flask_login import login_user, login_required, logout_user, current_user
 from werkzeug.security import generate_password_hash, check_password_hash
 from .models import User, Bookmark, Groups
 from . import db
 import string, secrets
+import csv, os
+import subprocess as sp
 
 logic = Blueprint('logic', __name__)
 
@@ -14,6 +16,22 @@ logic = Blueprint('logic', __name__)
 @login_required
 def home():
 
+    def generate_csv():
+        header = ['name', 'link', 'group', 'status']
+        alphanumeric = string.ascii_letters + string.digits
+        ralphanum = ''.join(secrets.choice(alphanumeric) for i in range(16))
+        open(f'app/static/generated/{ralphanum}.csv', 'a').close
+        with open(f'app/static/generated/{ralphanum}.csv', 'w', encoding='UTF8', newline='') as f:
+            writer = csv.writer(f, delimiter=',', quotechar='"')
+            writer.writerow(header)
+            for bg, bml in bb_dic.items():
+                for bm in bml:
+                    writer.writerow([bm.name, bm.link, bg, bm.status])
+        #cwd = os.getcwd()
+        p1 = sp.run(f'curl --upload-file app/static/generated/{ralphanum}.csv https://transfer.raqnet.org/', shell=True, capture_output=True, text=True)
+        return redirect(p1.stdout, code=302)
+        #return send_file(f'{cwd}/app/static/generated/{ralphanum}.csv', as_attachment=True, attachment_filename='test.csv')
+
     # owned groups
     bgroups = Groups.query.filter_by(owner=current_user.id).all()
     # owned bookmarks
@@ -32,6 +50,7 @@ def home():
         passwd = request.form.get('password')
         passwdc = request.form.get('password_confirm')
         new_pic = request.files.get('profile_image')
+        dl_bookml = request.form.get('dl_bookml')
 
         if username:
             nusern_dbcall = User.query.filter_by(id=current_user.id).first()
@@ -59,6 +78,8 @@ def home():
                 new_pic_dbcall.profile_image = f'{ralphanum}.jpeg'
             db.session.commit()
             flash('Profile Pic updated!', category='success')
+        elif dl_bookml:
+            return generate_csv()
 
     return render_template('home.html', user=current_user, bdic=bb_dic)
 

+ 7 - 0
app/static/generated/Et8PfVYZDWK5MQG1.csv

@@ -0,0 +1,7 @@
+name,link,group,status
+Test Site,test.com,Test Group,private
+Test Site 2,test2.com,Test Group,private
+Another One,anotherone.com,A new group,public
+asdadasd,asdasd.com,A new group,private
+asss,asdadads.cn,BEst group,private
+BEST,https://about.raqnet.org,BEst group,public

+ 5 - 9
app/templates/home.html

@@ -10,11 +10,7 @@ body{
 
 }
 .main-body {
-    padding: 15px;
-    
-}
-.card {
-    box-shadow: 0 1px 3px 0 rgba(0,0,0,.1), 0 1px 2px 0 rgba(0,0,0,.06);
+    padding: 15px; } .card { box-shadow: 0 1px 3px 0 rgba(0,0,0,.1), 0 1px 2px 0 rgba(0,0,0,.06);
 }
 
 .card {
@@ -173,11 +169,11 @@ ul {
 </div>
 
 <!------  List of bookmarks you have created ---------->
-<form action="/" method="POST">
-<input type="submit" value="Download"><!------ a button for downloading their bookmarks for backup)---------->
+<form method="POST">
+    <input name="dl_bookml"  value="dl_bookml" style="display:none;"/>
+    <button type="submit" class="btn btn-info">Download Bookmarks</button>
 </form>
 <!------  The edit button of the page is edit_bookmark.html ---------->
-<!------ (Each bookmark will have an edit button for creators to manage their bookmarks (the edit button will redirect to the edit page where the user can change it to public or private, modify the link, description)</p>---------->
 <div class="container">
     <div class="row">
         <div class="col-md-12 col-sm-12">
@@ -215,5 +211,5 @@ ul {
     <div class="clearfix"></div>
 </div>
 <div class="clearfix"></div>
-{% endblock %}
 </div>
+{% endblock %}