From 1c87f673401bbbb281f0d313d74cdb6801640145 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:51:56 +0000 Subject: [PATCH 1/3] Setting up GitHub Classroom Feedback From e4491b124dacc1bef3b90b6c3754185e9eb74ba1 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:51:59 +0000 Subject: [PATCH 2/3] Add assignment deadline url --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 99967a7..fe62f03 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-24ddc0f5d75046c5622901739e7c5dd533143b0c8e959d652212380cedb1ea36.svg)](https://classroom.github.com/a/wjmO5Bst) [![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-718a45dd9cf7e7f842a935f5ebbe5719a5e09af4491e668f4dbf3b35d5cca122.svg)](https://classroom.github.com/online_ide?assignment_repo_id=10989966&assignment_repo_type=AssignmentRepo) From 96ffc63c4f16055f932dc37c838ea05a5689c94d Mon Sep 17 00:00:00 2001 From: Othmane El Gharime Date: Tue, 25 Apr 2023 23:51:45 +0100 Subject: [PATCH 3/3] commit --- project.py | 95 ++++++++++++++++++++++++++++++++++++++ template/css/directory.css | 55 ++++++++++++++++++++++ template/directory.html | 35 ++++++++++++++ template/login.html | 18 ++++++++ 4 files changed, 203 insertions(+) create mode 100644 project.py create mode 100644 template/css/directory.css create mode 100644 template/directory.html create mode 100644 template/login.html diff --git a/project.py b/project.py new file mode 100644 index 0000000..a67b7ec --- /dev/null +++ b/project.py @@ -0,0 +1,95 @@ +import os +import zipfile +import tempfile +from flask import( + Flask, + request, + render_template, + redirect, + session, + url_for, + +) +from werkzeug.utils import secure_filename +import spwd +import crypt + +app = Flask(__name__) +app.secret_key = os.urandom(24) + +@app.route('/') +def index(): + if 'username' in session: + return redirect(url_for('browse')) + return render_template(('login.html')) + +@app.route('/login', methods=['GET', 'POST']) +def login(): + error = None + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] + + if authenticate(username, password): + session['username'] = username + return redirect(url_for('browse')) + else: + error = 'Invalid credentials' + return render_template('login.html', error=error) + +def authenticate(username, password): + try: + user = spwd.getspnam(username) + if user: + return crypt.crypt(password, user.sp_pwd) == user.sp_pwd + except KeyError: + return False + return False + +@app.route('/logout') +def logout(): + session.pop('username', None) + print('Logged out') + return redirect(url_for('index')) + +@app.route('/browse', defaults={'path': ''}) +@app.route('/browse/') +def browse(path): + if 'username' not in session: + return redirect(url_for('login')) + base_dir = os.path.expanduser(f'~{session["username"]}') + full_path = os.path.join(base_dir, path) + return render_template('browse.html', path=path, content=list_directory(full_path)) + +def list_directory(path): + items = [] + for item in os.listdir(path): + item_path = os.path.join(path, item) + items.append({ + 'name': item, + 'is_file': os.path.isfile(item_path), + 'size': os.path.getsize(item_path), + 'path': item_path + }) + return items + +@app.route('/download') +def download(): + if 'username' not in session: + return redirect(url_for('login')) + + base_dir = os.path.expanduser(f'~{session["username"]}') + zip_filename = f'{session["username"]}_home.zip' + + with tempfile.TemporaryDirectory() as temp_dir: + temp_zip_path = os.path.join(temp_dir, zip_filename) + with zipfile.ZipFile(temp_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: + for root, _, files in os.walk(base_dir): + for file in files: + file_path = os.path.join(root, file) + arcname = os.path.relpath(file_path, base_dir) + zipf.write(file_path, arcname) + return send_file(temp_zip_path, as_attachment=True, attachment_filename=zip_filename) + +if __name__ == '__main__': + app.run(host="0.0.0.0",debug=True) \ No newline at end of file diff --git a/template/css/directory.css b/template/css/directory.css new file mode 100644 index 0000000..09fec42 --- /dev/null +++ b/template/css/directory.css @@ -0,0 +1,55 @@ +.buttons button { + margin-top: 60px; + width: 100px; + height: 50px; + margin-right: 30px; + border-radius: 20px; + color: white; + font-size: 20px; + border: none; + cursor: pointer; + } + + #download { + width: 50px; + height: 50px; + cursor: pointer; + position: absolute; + left: 360px; + top: 165px; + } + + #power-off { + width: 30px; + height: 30px; + cursor: pointer; + position: absolute; + right: 250px; + } + + #search-bar-icon { + top: 165px; + width: 20px; + height: 20px; + cursor: pointer; + } + + .search-bar { + margin-top: 10px; + margin-bottom: 20px; + } + + .search-bar input { + padding-left: 50px; + border: 2px solid black; + border-radius: 20px; + } + + table thead th { + font-family: Arial, Helvetica, sans-serif; + } + + h3{ + margin-left: 60px; + } + \ No newline at end of file diff --git a/template/directory.html b/template/directory.html new file mode 100644 index 0000000..159ac5b --- /dev/null +++ b/template/directory.html @@ -0,0 +1,35 @@ + + + + + + + directory + + + + +
+
+ + + + +
+ + + + + + + +
Dossier     | + Date de + Modification     | + Taille
+
+ + diff --git a/template/login.html b/template/login.html new file mode 100644 index 0000000..8c97c6c --- /dev/null +++ b/template/login.html @@ -0,0 +1,18 @@ + + + + + + + Login + + +

test

+
+ + + + +
+ + \ No newline at end of file