Kgshop commited on
Commit
77a445f
·
verified ·
1 Parent(s): 63e4900

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -10
app.py CHANGED
@@ -8,10 +8,8 @@ from datetime import datetime
8
  from huggingface_hub import HfApi, hf_hub_download
9
  from huggingface_hub.utils import RepositoryNotFoundError
10
  from werkzeug.utils import secure_filename
11
- import hashlib
12
 
13
  app = Flask(__name__)
14
- app.secret_key = 'your_secret_key_here' # Замените на безопасный ключ
15
  DATA_FILE = 'data_detobuv.json'
16
  USERS_FILE = 'users.json'
17
 
@@ -71,9 +69,6 @@ def save_users(users):
71
  with open(USERS_FILE, 'w', encoding='utf-8') as file:
72
  json.dump(users, file, ensure_ascii=False, indent=4)
73
 
74
- def hash_password(password):
75
- return hashlib.sha256(password.encode()).hexdigest()
76
-
77
  def upload_db_to_hf():
78
  try:
79
  api = HfApi()
@@ -451,6 +446,7 @@ def catalog():
451
  <i class="fas fa-moon"></i>
452
  </button>
453
  </div>
 
454
  <div class="filters-container">
455
  <button class="category-filter active" data-category="all">Все категории</button>
456
  {% for category in categories %}
@@ -487,7 +483,6 @@ def catalog():
487
  </div>
488
  {% endfor %}
489
  </div>
490
- <div class="store-address">{{ store_address }}</div>
491
  </div>
492
 
493
  <!-- Product Modal -->
@@ -545,6 +540,18 @@ def catalog():
545
  document.querySelector('.theme-toggle i').classList.replace('fa-moon', 'fa-sun');
546
  }
547
 
 
 
 
 
 
 
 
 
 
 
 
 
548
  function openModal(index) {
549
  loadProductDetails(index);
550
  document.getElementById('productModal').style.display = "block";
@@ -674,8 +681,8 @@ def catalog():
674
  orderText += `${index + 1}. ${item.name} - ${item.price} с × ${item.quantity} (Цвет: ${item.color})%0A`;
675
  });
676
  orderText += `Итого: ${total} с%0A`;
677
- orderText += `Страна: {{ session['country'] }}%0A`;
678
- orderText += `Город: {{ session['city'] }}`;
679
  window.open(`https://api.whatsapp.com/send?phone=996555360556&text=${orderText}`, '_blank');
680
  }
681
 
@@ -786,7 +793,7 @@ def register():
786
  return "Пользователь с таким логином уже существует", 400
787
 
788
  users[login] = {
789
- 'password': hash_password(password),
790
  'country': country,
791
  'city': city,
792
  'purchase_type': purchase_type
@@ -871,6 +878,12 @@ def register():
871
  <a href="{{ url_for('login') }}">Уже есть аккаунт? Войти</a>
872
  </p>
873
  </div>
 
 
 
 
 
 
874
  </body>
875
  </html>
876
  ''')
@@ -882,7 +895,7 @@ def login():
882
  password = request.form.get('password')
883
  users = load_users()
884
 
885
- if login in users and users[login]['password'] == hash_password(password):
886
  session['user'] = login
887
  session['country'] = users[login]['country']
888
  session['city'] = users[login]['city']
@@ -954,10 +967,28 @@ def login():
954
  <a href="{{ url_for('register') }}">Нет аккаунта? Зарегистрироваться</a>
955
  </p>
956
  </div>
 
 
 
 
 
 
957
  </body>
958
  </html>
959
  ''')
960
 
 
 
 
 
 
 
 
 
 
 
 
 
961
  @app.route('/logout')
962
  def logout():
963
  session.pop('user', None)
 
8
  from huggingface_hub import HfApi, hf_hub_download
9
  from huggingface_hub.utils import RepositoryNotFoundError
10
  from werkzeug.utils import secure_filename
 
11
 
12
  app = Flask(__name__)
 
13
  DATA_FILE = 'data_detobuv.json'
14
  USERS_FILE = 'users.json'
15
 
 
69
  with open(USERS_FILE, 'w', encoding='utf-8') as file:
70
  json.dump(users, file, ensure_ascii=False, indent=4)
71
 
 
 
 
72
  def upload_db_to_hf():
73
  try:
74
  api = HfApi()
 
446
  <i class="fas fa-moon"></i>
447
  </button>
448
  </div>
449
+ <div class="store-address">{{ store_address }}</div>
450
  <div class="filters-container">
451
  <button class="category-filter active" data-category="all">Все категории</button>
452
  {% for category in categories %}
 
483
  </div>
484
  {% endfor %}
485
  </div>
 
486
  </div>
487
 
488
  <!-- Product Modal -->
 
540
  document.querySelector('.theme-toggle i').classList.replace('fa-moon', 'fa-sun');
541
  }
542
 
543
+ // Автоматическая авторизация из localStorage
544
+ const storedUser = localStorage.getItem('user');
545
+ if (storedUser && !{{ is_authenticated|tojson }}) {
546
+ fetch('/auto_login', {
547
+ method: 'POST',
548
+ headers: { 'Content-Type': 'application/json' },
549
+ body: JSON.stringify({ login: storedUser })
550
+ }).then(response => {
551
+ if (response.ok) window.location.reload();
552
+ });
553
+ }
554
+
555
  function openModal(index) {
556
  loadProductDetails(index);
557
  document.getElementById('productModal').style.display = "block";
 
681
  orderText += `${index + 1}. ${item.name} - ${item.price} с × ${item.quantity} (Цвет: ${item.color})%0A`;
682
  });
683
  orderText += `Итого: ${total} с%0A`;
684
+ orderText += `Страна: {{ session.get('country', 'Не указана') }}%0A`;
685
+ orderText += `Город: {{ session.get('city', 'Не указан') }}`;
686
  window.open(`https://api.whatsapp.com/send?phone=996555360556&text=${orderText}`, '_blank');
687
  }
688
 
 
793
  return "Пользователь с таким логином уже существует", 400
794
 
795
  users[login] = {
796
+ 'password': password, # Сохранение пароля без хеширования
797
  'country': country,
798
  'city': city,
799
  'purchase_type': purchase_type
 
878
  <a href="{{ url_for('login') }}">Уже есть аккаунт? Войти</a>
879
  </p>
880
  </div>
881
+ <script>
882
+ document.querySelector('form').addEventListener('submit', function() {
883
+ const login = document.querySelector('input[name="login"]').value;
884
+ localStorage.setItem('user', login);
885
+ });
886
+ </script>
887
  </body>
888
  </html>
889
  ''')
 
895
  password = request.form.get('password')
896
  users = load_users()
897
 
898
+ if login in users and users[login]['password'] == password:
899
  session['user'] = login
900
  session['country'] = users[login]['country']
901
  session['city'] = users[login]['city']
 
967
  <a href="{{ url_for('register') }}">Нет аккаунта? Зарегистрироваться</a>
968
  </p>
969
  </div>
970
+ <script>
971
+ document.querySelector('form').addEventListener('submit', function() {
972
+ const login = document.querySelector('input[name="login"]').value;
973
+ localStorage.setItem('user', login);
974
+ });
975
+ </script>
976
  </body>
977
  </html>
978
  ''')
979
 
980
+ @app.route('/auto_login', methods=['POST'])
981
+ def auto_login():
982
+ data = request.get_json()
983
+ login = data.get('login')
984
+ users = load_users()
985
+ if login in users:
986
+ session['user'] = login
987
+ session['country'] = users[login]['country']
988
+ session['city'] = users[login]['city']
989
+ return "OK", 200
990
+ return "Ошибка авторизации", 401
991
+
992
  @app.route('/logout')
993
  def logout():
994
  session.pop('user', None)