Eluza133 commited on
Commit
c2b0505
·
verified ·
1 Parent(s): 2a35527

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -23
app.py CHANGED
@@ -93,7 +93,6 @@ def register():
93
  flash('Пользователь уже существует!')
94
  return redirect(url_for('register'))
95
 
96
- # Хешируем пароль
97
  hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
98
  data['users'][username] = {'password': hashed_password}
99
  save_data(data)
@@ -202,6 +201,7 @@ def video_feed():
202
  data = load_data()
203
  videos = data['videos']
204
  is_authenticated = 'username' in session
 
205
  html = '''
206
  <!DOCTYPE html>
207
  <html lang="ru">
@@ -210,27 +210,31 @@ def video_feed():
210
  <title>Видеохостинг</title>
211
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
212
  <style>
213
- body { font-family: 'Poppins', sans-serif; background: #f0f2f5; padding: 20px; }
214
- .container { max-width: 1200px; margin: 0 auto; }
 
215
  .video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }
216
  .video-item { background: #fff; padding: 15px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); text-decoration: none; color: #2d3748; }
217
  .video-item img { width: 100%; border-radius: 8px; height: 200px; object-fit: cover; }
218
- .auth-links, .upload-btn, .logout-btn { display: inline-block; margin: 20px 10px; padding: 10px 20px; background: #3b82f6; color: white; text-align: center; text-decoration: none; border-radius: 8px; }
219
  .logout-btn { background: #ef4444; }
 
220
  </style>
221
  </head>
222
  <body>
 
 
 
 
 
 
 
 
 
 
 
223
  <div class="container">
224
  <h1>Лента видео</h1>
225
- <div class="auth-links">
226
- {% if is_authenticated %}
227
- <a href="/upload" class="upload-btn">Загрузить видео</a>
228
- <a href="/logout" class="logout-btn">Выйти</a>
229
- {% else %}
230
- <a href="/login" class="auth-links">Войти</a>
231
- <a href="/register" class="auth-links">Зарегистрироваться</a>
232
- {% endif %}
233
- </div>
234
  <div class="video-grid">
235
  {% for video in videos %}
236
  <a href="{{ url_for('video_page', title=video['title']) }}" class="video-item">
@@ -245,7 +249,7 @@ def video_feed():
245
  </body>
246
  </html>
247
  '''
248
- return render_template_string(html, videos=videos, is_authenticated=is_authenticated)
249
 
250
  # Страница отдельного видео (доступна всем)
251
  @app.route('/video/<title>')
@@ -256,6 +260,7 @@ def video_page(title):
256
  return "Видео не найдено", 404
257
 
258
  is_authenticated = 'username' in session
 
259
  html = '''
260
  <!DOCTYPE html>
261
  <html lang="ru">
@@ -264,13 +269,27 @@ def video_page(title):
264
  <title>{{ video['title'] }}</title>
265
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
266
  <style>
267
- body { font-family: 'Poppins', sans-serif; background: #f0f2f5; padding: 20px; }
268
- .container { max-width: 800px; margin: 0 auto; background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
 
269
  video { width: 100%; border-radius: 8px; }
270
- .back-btn { display: inline-block; margin-top: 20px; padding: 10px 20px; background: #3b82f6; color: white; text-decoration: none; border-radius: 8px; }
 
 
271
  </style>
272
  </head>
273
  <body>
 
 
 
 
 
 
 
 
 
 
 
274
  <div class="container">
275
  <h1>{{ video['title'] }}</h1>
276
  <video controls>
@@ -286,7 +305,80 @@ def video_page(title):
286
  </body>
287
  </html>
288
  '''
289
- return render_template_string(html, video=video, repo_id=REPO_ID, is_authenticated=is_authenticated)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
 
291
  # Страница загрузки видео (только для авторизованных)
292
  @app.route('/upload', methods=['GET', 'POST'])
@@ -336,8 +428,9 @@ def upload_video():
336
  if os.path.exists(temp_path):
337
  os.remove(temp_path)
338
 
339
- return redirect(url_for('video_feed'))
340
 
 
341
  html = '''
342
  <!DOCTYPE html>
343
  <html lang="ru">
@@ -346,15 +439,25 @@ def upload_video():
346
  <title>Загрузка видео</title>
347
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
348
  <style>
349
- body { font-family: 'Poppins', sans-serif; background: #f0f2f5; padding: 20px; }
350
- .container { max-width: 600px; margin: 0 auto; background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
 
351
  input, textarea { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #e2e8f0; border-radius: 8px; }
352
  button { padding: 10px 20px; background: #3b82f6; color: white; border: none; border-radius: 8px; cursor: pointer; }
353
  #progress-container { margin-top: 10px; }
354
  #progress-bar { width: 0%; height: 20px; background: #3b82f6; border-radius: 8px; transition: width 0.3s; }
 
 
 
355
  </style>
356
  </head>
357
  <body>
 
 
 
 
 
 
358
  <div class="container">
359
  <h1>Загрузить видео</h1>
360
  <form id="upload-form" enctype="multipart/form-data">
@@ -389,7 +492,7 @@ def upload_video():
389
 
390
  xhr.onload = function() {
391
  if (xhr.status === 200) {
392
- window.location = '/';
393
  } else {
394
  alert('Ошибка загрузки');
395
  }
@@ -401,7 +504,7 @@ def upload_video():
401
  </body>
402
  </html>
403
  '''
404
- return render_template_string(html)
405
 
406
  if __name__ == '__main__':
407
  backup_thread = threading.Thread(target=periodic_backup, daemon=True)
 
93
  flash('Пользователь уже существует!')
94
  return redirect(url_for('register'))
95
 
 
96
  hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
97
  data['users'][username] = {'password': hashed_password}
98
  save_data(data)
 
201
  data = load_data()
202
  videos = data['videos']
203
  is_authenticated = 'username' in session
204
+ username = session.get('username', None)
205
  html = '''
206
  <!DOCTYPE html>
207
  <html lang="ru">
 
210
  <title>Видеохостинг</title>
211
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
212
  <style>
213
+ body { font-family: 'Poppins', sans-serif; background: #f0f2f5; padding: 20px; margin: 0; }
214
+ .container { max-width: 1200px; margin: 0 auto; padding-left: 250px; }
215
+ .sidebar { position: fixed; left: 0; top: 0; width: 200px; height: 100%; background: #fff; padding: 20px; box-shadow: 2px 0 5px rgba(0,0,0,0.1); }
216
  .video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }
217
  .video-item { background: #fff; padding: 15px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); text-decoration: none; color: #2d3748; }
218
  .video-item img { width: 100%; border-radius: 8px; height: 200px; object-fit: cover; }
219
+ .auth-links, .upload-btn, .logout-btn, .profile-link { display: inline-block; margin: 10px 0; padding: 10px 20px; background: #3b82f6; color: white; text-align: center; text-decoration: none; border-radius: 8px; }
220
  .logout-btn { background: #ef4444; }
221
+ .profile-link { background: #10b981; }
222
  </style>
223
  </head>
224
  <body>
225
+ <div class="sidebar">
226
+ {% if is_authenticated %}
227
+ <a href="{{ url_for('profile') }}" class="profile-link">
228
+ <span style="font-size: 1.2em;">👤</span> {{ username }}
229
+ </a>
230
+ <a href="{{ url_for('logout') }}" class="logout-btn">Выйти</a>
231
+ {% else %}
232
+ <a href="{{ url_for('login') }}" class="auth-links">Войти</a>
233
+ <a href="{{ url_for('register') }}" class="auth-links">Зарегистрироваться</a>
234
+ {% endif %}
235
+ </div>
236
  <div class="container">
237
  <h1>Лента видео</h1>
 
 
 
 
 
 
 
 
 
238
  <div class="video-grid">
239
  {% for video in videos %}
240
  <a href="{{ url_for('video_page', title=video['title']) }}" class="video-item">
 
249
  </body>
250
  </html>
251
  '''
252
+ return render_template_string(html, videos=videos, is_authenticated=is_authenticated, username=username)
253
 
254
  # Страница отдельного видео (доступна всем)
255
  @app.route('/video/<title>')
 
260
  return "Видео не найдено", 404
261
 
262
  is_authenticated = 'username' in session
263
+ username = session.get('username', None)
264
  html = '''
265
  <!DOCTYPE html>
266
  <html lang="ru">
 
269
  <title>{{ video['title'] }}</title>
270
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
271
  <style>
272
+ body { font-family: 'Poppins', sans-serif; background: #f0f2f5; padding: 20px; margin: 0; }
273
+ .sidebar { position: fixed; left: 0; top: 0; width: 200px; height: 100%; background: #fff; padding: 20px; box-shadow: 2px 0 5px rgba(0,0,0,0.1); }
274
+ .container { max-width: 800px; margin: 0 auto; padding-left: 250px; background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
275
  video { width: 100%; border-radius: 8px; }
276
+ .back-btn, .profile-link, .logout-btn { display: inline-block; margin-top: 20px; padding: 10px 20px; background: #3b82f6; color: white; text-decoration: none; border-radius: 8px; }
277
+ .profile-link { background: #10b981; }
278
+ .logout-btn { background: #ef4444; }
279
  </style>
280
  </head>
281
  <body>
282
+ <div class="sidebar">
283
+ {% if is_authenticated %}
284
+ <a href="{{ url_for('profile') }}" class="profile-link">
285
+ <span style="font-size: 1.2em;">👤</span> {{ username }}
286
+ </a>
287
+ <a href="{{ url_for('logout') }}" class="logout-btn">Выйти</a>
288
+ {% else %}
289
+ <a href="{{ url_for('login') }}" class="profile-link">Войти</a>
290
+ <a href="{{ url_for('register') }}" class="profile-link">Зарегистрироваться</a>
291
+ {% endif %}
292
+ </div>
293
  <div class="container">
294
  <h1>{{ video['title'] }}</h1>
295
  <video controls>
 
305
  </body>
306
  </html>
307
  '''
308
+ return render_template_string(html, video=video, repo_id=REPO_ID, is_authenticated=is_authenticated, username=username)
309
+
310
+ # Страница профиля (только для авторизованных)
311
+ @app.route('/profile', methods=['GET', 'POST'])
312
+ def profile():
313
+ if 'username' not in session:
314
+ flash('Войдите, чтобы просмотреть профиль!')
315
+ return redirect(url_for('login'))
316
+
317
+ data = load_data()
318
+ username = session['username']
319
+ user_videos = [v for v in data['videos'] if v['uploader'] == username]
320
+
321
+ if request.method == 'POST':
322
+ video_id = request.form.get('video_id')
323
+ if video_id:
324
+ data['videos'] = [v for v in data['videos'] if v['id'] != video_id or v['uploader'] != username]
325
+ save_data(data)
326
+ return redirect(url_for('profile'))
327
+
328
+ html = '''
329
+ <!DOCTYPE html>
330
+ <html lang="ru">
331
+ <head>
332
+ <meta charset="UTF-8">
333
+ <title>Профиль - {{ username }}</title>
334
+ <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
335
+ <style>
336
+ body { font-family: 'Poppins', sans-serif; background: #f0f2f5; padding: 20px; margin: 0; }
337
+ .sidebar { position: fixed; left: 0; top: 0; width: 200px; height: 100%; background: #fff; padding: 20px; box-shadow: 2px 0 5px rgba(0,0,0,0.1); }
338
+ .container { max-width: 800px; margin: 0 auto; padding-left: 250px; }
339
+ .video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }
340
+ .video-item { background: #fff; padding: 15px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
341
+ .video-item img { width: 100%; border-radius: 8px; height: 200px; object-fit: cover; }
342
+ .upload-btn, .delete-btn, .profile-link, .logout-btn { display: inline-block; margin: 10px 0; padding: 10px 20px; background: #3b82f6; color: white; text-decoration: none; border-radius: 8px; }
343
+ .delete-btn { background: #ef4444; }
344
+ .profile-link { background: #10b981; }
345
+ .logout-btn { background: #ef4444; }
346
+ </style>
347
+ </head>
348
+ <body>
349
+ <div class="sidebar">
350
+ <a href="{{ url_for('profile') }}" class="profile-link">
351
+ <span style="font-size: 1.2em;">👤</span> {{ username }}
352
+ </a>
353
+ <a href="{{ url_for('logout') }}" class="logout-btn">Выйти</a>
354
+ </div>
355
+ <div class="container">
356
+ <h1>Профиль: {{ username }}</h1>
357
+ <a href="{{ url_for('upload') }}" class="upload-btn">Добавить видео</a>
358
+ <h2>Ваши видео</h2>
359
+ <div class="video-grid">
360
+ {% if user_videos %}
361
+ {% for video in user_videos %}
362
+ <div class="video-item">
363
+ <img src="https://via.placeholder.com/300x200?text={{ video['title'] }}" alt="{{ video['title'] }}">
364
+ <h3>{{ video['title'] }}</h3>
365
+ <p>{{ video['description'] }}</p>
366
+ <p>{{ video['upload_date'] }}</p>
367
+ <form method="POST">
368
+ <input type="hidden" name="video_id" value="{{ video['id'] }}">
369
+ <button type="submit" class="delete-btn">Удалить</button>
370
+ </form>
371
+ </div>
372
+ {% endfor %}
373
+ {% else %}
374
+ <p>Вы пока не загрузили ни одного видео.</p>
375
+ {% endif %}
376
+ </div>
377
+ </div>
378
+ </body>
379
+ </html>
380
+ '''
381
+ return render_template_string(html, username=username, user_videos=user_videos)
382
 
383
  # Страница загрузки видео (только для авторизованных)
384
  @app.route('/upload', methods=['GET', 'POST'])
 
428
  if os.path.exists(temp_path):
429
  os.remove(temp_path)
430
 
431
+ return redirect(url_for('profile'))
432
 
433
+ username = session['username']
434
  html = '''
435
  <!DOCTYPE html>
436
  <html lang="ru">
 
439
  <title>Загрузка видео</title>
440
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
441
  <style>
442
+ body { font-family: 'Poppins', sans-serif; background: #f0f2f5; padding: 20px; margin: 0; }
443
+ .sidebar { position: fixed; left: 0; top: 0; width: 200px; height: 100%; background: #fff; padding: 20px; box-shadow: 2px 0 5px rgba(0,0,0,0.1); }
444
+ .container { max-width: 600px; margin: 0 auto; padding-left: 250px; background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
445
  input, textarea { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #e2e8f0; border-radius: 8px; }
446
  button { padding: 10px 20px; background: #3b82f6; color: white; border: none; border-radius: 8px; cursor: pointer; }
447
  #progress-container { margin-top: 10px; }
448
  #progress-bar { width: 0%; height: 20px; background: #3b82f6; border-radius: 8px; transition: width 0.3s; }
449
+ .profile-link, .logout-btn { display: inline-block; margin: 10px 0; padding: 10px 20px; background: #3b82f6; color: white; text-decoration: none; border-radius: 8px; }
450
+ .profile-link { background: #10b981; }
451
+ .logout-btn { background: #ef4444; }
452
  </style>
453
  </head>
454
  <body>
455
+ <div class="sidebar">
456
+ <a href="{{ url_for('profile') }}" class="profile-link">
457
+ <span style="font-size: 1.2em;">👤</span> {{ username }}
458
+ </a>
459
+ <a href="{{ url_for('logout') }}" class="logout-btn">Выйти</a>
460
+ </div>
461
  <div class="container">
462
  <h1>Загрузить видео</h1>
463
  <form id="upload-form" enctype="multipart/form-data">
 
492
 
493
  xhr.onload = function() {
494
  if (xhr.status === 200) {
495
+ window.location = '/profile';
496
  } else {
497
  alert('Ошибка загрузки');
498
  }
 
504
  </body>
505
  </html>
506
  '''
507
+ return render_template_string(html, username=username)
508
 
509
  if __name__ == '__main__':
510
  backup_thread = threading.Thread(target=periodic_backup, daemon=True)