Eluza133 commited on
Commit
213b5da
·
verified ·
1 Parent(s): f4ff15e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -16
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, render_template_string, request, redirect, url_for, session, flash, send_file
2
  from flask_caching import Cache
3
  import json
4
  import os
@@ -302,8 +302,9 @@ def register():
302
  'files': []
303
  }
304
  save_data(data)
305
- flash('Регистрация успешна! Войдите в систему.')
306
- return redirect(url_for('login'))
 
307
 
308
  html = '''
309
  <!DOCTYPE html>
@@ -325,7 +326,7 @@ def register():
325
  {% endfor %}
326
  {% endif %}
327
  {% endwith %}
328
- <form method="POST">
329
  <input type="text" name="username" placeholder="Введите логин" required>
330
  <input type="password" name="password" placeholder="Введите пароль" required>
331
  <input type="text" name="code" placeholder="Введите регистрационный код" required>
@@ -347,9 +348,9 @@ def login():
347
 
348
  if username in data['users'] and data['users'][username]['password'] == password:
349
  session['username'] = username
350
- return redirect(url_for('dashboard'))
351
  else:
352
- flash('Неверный логин или пароль!')
353
 
354
  html = '''
355
  <!DOCTYPE html>
@@ -364,20 +365,66 @@ def login():
364
  <body>
365
  <div class="container">
366
  <h1>Zues Cloud</h1>
367
- {% with messages = get_flashed_messages() %}
368
- {% if messages %}
369
- {% for message in messages %}
370
- <div class="flash">{{ message }}</div>
371
- {% endfor %}
372
- {% endif %}
373
- {% endwith %}
374
- <form method="POST">
 
 
375
  <input type="text" name="username" placeholder="Введите логин" required>
376
  <input type="password" name="password" placeholder="Введите пароль" required>
377
  <button type="submit" class="btn">Войти</button>
378
  </form>
379
  <p style="margin-top: 20px;">Нет аккаунта? <a href="{{ url_for('register') }}">Зарегистрируйтесь</a></p>
380
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
  </body>
382
  </html>
383
  '''
@@ -396,7 +443,6 @@ def dashboard():
396
  flash('Пользователь не найден!')
397
  return redirect(url_for('login'))
398
 
399
- # Сортировка файлов по убыванию даты загрузки
400
  user_files = sorted(data['users'][username]['files'], key=lambda x: x['upload_date'], reverse=True)
401
 
402
  if request.method == 'POST':
@@ -493,7 +539,7 @@ def dashboard():
493
  <p>У вас пока нет загруженных файлов.</p>
494
  {% endif %}
495
  </div>
496
- <a href="{{ url_for('logout') }}" class="btn" style="margin-top: 20px;">Выйти</a>
497
  </div>
498
  <div class="modal" id="mediaModal" onclick="closeModal(event)">
499
  <div id="modalContent"></div>
@@ -563,6 +609,13 @@ def dashboard():
563
 
564
  xhr.send(formData);
565
  });
 
 
 
 
 
 
 
566
  </script>
567
  </body>
568
  </html>
 
1
+ from flask import Flask, render_template_string, request, redirect, url_for, session, flash, send_file, jsonify
2
  from flask_caching import Cache
3
  import json
4
  import os
 
302
  'files': []
303
  }
304
  save_data(data)
305
+ session['username'] = username
306
+ flash('Регистрация успешна!')
307
+ return redirect(url_for('dashboard'))
308
 
309
  html = '''
310
  <!DOCTYPE html>
 
326
  {% endfor %}
327
  {% endif %}
328
  {% endwith %}
329
+ <form method="POST" id="register-form">
330
  <input type="text" name="username" placeholder="Введите логин" required>
331
  <input type="password" name="password" placeholder="Введите пароль" required>
332
  <input type="text" name="code" placeholder="Введите регистрационный код" required>
 
348
 
349
  if username in data['users'] and data['users'][username]['password'] == password:
350
  session['username'] = username
351
+ return jsonify({'status': 'success', 'redirect': url_for('dashboard')})
352
  else:
353
+ return jsonify({'status': 'error', 'message': 'Неверный логин или пароль!'})
354
 
355
  html = '''
356
  <!DOCTYPE html>
 
365
  <body>
366
  <div class="container">
367
  <h1>Zues Cloud</h1>
368
+ <div id="flash-messages">
369
+ {% with messages = get_flashed_messages() %}
370
+ {% if messages %}
371
+ {% for message in messages %}
372
+ <div class="flash">{{ message }}</div>
373
+ {% endfor %}
374
+ {% endif %}
375
+ {% endwith %}
376
+ </div>
377
+ <form method="POST" id="login-form">
378
  <input type="text" name="username" placeholder="Введите логин" required>
379
  <input type="password" name="password" placeholder="Введите пароль" required>
380
  <button type="submit" class="btn">Войти</button>
381
  </form>
382
  <p style="margin-top: 20px;">Нет аккаунта? <a href="{{ url_for('register') }}">Зарегистрируйтесь</a></p>
383
  </div>
384
+ <script>
385
+ // Проверка localStorage и автоматический вход
386
+ const savedCredentials = JSON.parse(localStorage.getItem('zuesCredentials'));
387
+ if (savedCredentials) {
388
+ fetch('/', {
389
+ method: 'POST',
390
+ headers: {
391
+ 'Content-Type': 'application/x-www-form-urlencoded',
392
+ },
393
+ body: `username=${encodeURIComponent(savedCredentials.username)}&password=${encodeURIComponent(savedCredentials.password)}`
394
+ })
395
+ .then(response => response.json())
396
+ .then(data => {
397
+ if (data.status === 'success') {
398
+ window.location.href = data.redirect;
399
+ }
400
+ })
401
+ .catch(error => console.error('Ошибка автоматического входа:', error));
402
+ }
403
+
404
+ // Обработка формы входа
405
+ document.getElementById('login-form').addEventListener('submit', function(e) {
406
+ e.preventDefault();
407
+ const formData = new FormData(this);
408
+ fetch('/', {
409
+ method: 'POST',
410
+ body: formData
411
+ })
412
+ .then(response => response.json())
413
+ .then(data => {
414
+ if (data.status === 'success') {
415
+ const username = formData.get('username');
416
+ const password = formData.get('password');
417
+ localStorage.setItem('zuesCredentials', JSON.stringify({ username, password }));
418
+ window.location.href = data.redirect;
419
+ } else {
420
+ document.getElementById('flash-messages').innerHTML = `<div class="flash">${data.message}</div>`;
421
+ }
422
+ })
423
+ .catch(error => {
424
+ document.getElementById('flash-messages').innerHTML = `<div class="flash">Ошибка соединения!</div>`;
425
+ });
426
+ });
427
+ </script>
428
  </body>
429
  </html>
430
  '''
 
443
  flash('Пользователь не найден!')
444
  return redirect(url_for('login'))
445
 
 
446
  user_files = sorted(data['users'][username]['files'], key=lambda x: x['upload_date'], reverse=True)
447
 
448
  if request.method == 'POST':
 
539
  <p>У вас пока нет загруженных файлов.</p>
540
  {% endif %}
541
  </div>
542
+ <a href="{{ url_for('logout') }}" class="btn" style="margin-top: 20px;" id="logout-btn">Выйти</a>
543
  </div>
544
  <div class="modal" id="mediaModal" onclick="closeModal(event)">
545
  <div id="modalContent"></div>
 
609
 
610
  xhr.send(formData);
611
  });
612
+
613
+ // Обработка выхода
614
+ document.getElementById('logout-btn').addEventListener('click', function(e) {
615
+ e.preventDefault();
616
+ localStorage.removeItem('zuesCredentials');
617
+ window.location.href = '/logout';
618
+ });
619
  </script>
620
  </body>
621
  </html>