diff --git a/.test_api.py.swp b/.test_api.py.swp new file mode 100644 index 0000000..216387f Binary files /dev/null and b/.test_api.py.swp differ diff --git a/__pycache__/app.cpython-311.pyc b/__pycache__/app.cpython-311.pyc index cca1b00..e40dc7a 100644 Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ diff --git a/app.py b/app.py index cc34bcc..47e82f5 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ import os -from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify +from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify, send_from_directory from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user from flask_bcrypt import Bcrypt @@ -41,12 +41,13 @@ migrate = Migrate(app, db) @login_manager.user_loader def load_user(user_id): - return db.session.get(User, int(user_id)) + return User.query.get(int(user_id)) # Modele pentru baza de date class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) + email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) role = db.Column(db.String(20), nullable=False, default='user') theme = db.Column(db.String(10), nullable=False, default='light') @@ -58,6 +59,9 @@ class Player(db.Model): password = db.Column(db.String(120), nullable=False) quickconnect_password = db.Column(db.String(120), nullable=False) + def verify_quickconnect_code(self, code): + return bcrypt.check_password_hash(self.quickconnect_password, code) + class Group(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), unique=True, nullable=False) @@ -99,9 +103,10 @@ def dashboard(): def register(): if request.method == 'POST': username = request.form['username'] + email = request.form['email'] password = request.form['password'] hashed_password = bcrypt.generate_password_hash(password).decode('utf-8') - new_user = User(username=username, password=hashed_password, role='user') + new_user = User(username=username, email=email, password=hashed_password, role='user') db.session.add(new_user) db.session.commit() return redirect(url_for('login')) @@ -217,10 +222,11 @@ def delete_user(user_id): @admin_required def create_user(): username = request.form['username'] + email = request.form['email'] password = request.form['password'] role = request.form['role'] hashed_password = bcrypt.generate_password_hash(password).decode('utf-8') - new_user = User(username=username, password=hashed_password, role=role) + new_user = User(username=username, email=email, password=hashed_password, role=role) db.session.add(new_user) db.session.commit() return redirect(url_for('admin')) @@ -534,19 +540,14 @@ def clean_unused_files(): @app.route('/api/playlists', methods=['GET']) def get_playlist(): + hostname = request.args.get('hostname') quickconnect_code = request.args.get('quickconnect_code') - if not quickconnect_code: - return jsonify({'error': 'Quick connect code is required'}), 400 + if not hostname or not quickconnect_code: + return jsonify({'error': 'Hostname and quick connect code are required'}), 400 - players = Player.query.all() - player = None - for p in players: - if p.verify_quickconnect_code(quickconnect_code): - player = p - break - - if not player: - return jsonify({'error': 'Invalid quick connect code'}), 404 + player = Player.query.filter_by(hostname=hostname).first() + if not player or not player.verify_quickconnect_code(quickconnect_code): + return jsonify({'error': 'Invalid hostname or quick connect code'}), 404 if player.groups: # If the player is part of a group, get the group's content @@ -556,9 +557,13 @@ def get_playlist(): # If the player is not part of a group, get the player's content content = Content.query.filter_by(player_id=player.id).all() - playlist = [{'file_name': item.file_name, 'duration': item.duration} for item in content] + playlist = [{'file_name': item.file_name, 'duration': item.duration, 'url': url_for('media', filename=item.file_name, _external=True)} for item in content] return jsonify({'playlist': playlist}) +@app.route('/media/') +def media(filename): + return send_from_directory(app.config['UPLOAD_FOLDER'], filename) + @app.context_processor def inject_theme(): if current_user.is_authenticated: @@ -570,4 +575,6 @@ def inject_theme(): if __name__ == '__main__': with app.app_context(): db.create_all() # Creează toate tabelele - app.run(debug=True) + app.run(debug=True, host='0.0.0.0') + + diff --git a/import requests.py b/import requests.py new file mode 100644 index 0000000..5676f53 --- /dev/null +++ b/import requests.py @@ -0,0 +1,19 @@ +import requests + +# Replace with the actual server IP address or domain name and quick connect code +server_ip = 'http://192.168.0.115:5000' +quickconnect_code = 'Initial01!' + +# Construct the URL with the quick connect code as a query parameter +url = f'{server_ip}/api/playlists?quickconnect_code={quickconnect_code}' + +# Make the GET request to the API +response = requests.get(url) + +# Check if the request was successful +if response.status_code == 200: + # Parse the JSON response + playlist = response.json().get('playlist', []) + print('Playlist:', playlist) +else: + print('Failed to retrieve playlist:', response.json()) \ No newline at end of file diff --git a/instance/dashboard.db b/instance/dashboard.db index bfc3043..791bf97 100644 Binary files a/instance/dashboard.db and b/instance/dashboard.db differ diff --git a/static/uploads/car_modular.jpg b/static/uploads/car_modular.jpg new file mode 100644 index 0000000..c129b85 Binary files /dev/null and b/static/uploads/car_modular.jpg differ diff --git a/static/uploads/harting_contact_2.jpg b/static/uploads/harting_contact_2.jpg new file mode 100644 index 0000000..efc69e4 Binary files /dev/null and b/static/uploads/harting_contact_2.jpg differ diff --git a/test_api.py b/test_api.py index 41c5a5b..2451340 100644 --- a/test_api.py +++ b/test_api.py @@ -1,19 +1,44 @@ import requests +import os -# Replace with the actual server IP address or domain name and quick connect code -server_ip = 'Http://192.168.0.115' +# Replace with the actual server IP address or domain name, hostname, and quick connect code +server_ip = 'http://192.168.0.115:5000' +hostname = 'TvHolBa1' quickconnect_code = 'Initial01!' -# Construct the URL with the quick connect code as a query parameter -url = f'{server_ip}/api/playlists?quickconnect_code={quickconnect_code}' +# Construct the URL with the hostname and quick connect code as query parameters +url = f'{server_ip}/api/playlists' +params = { + 'hostname': hostname, + 'quickconnect_code': quickconnect_code +} # Make the GET request to the API -response = requests.get(url) +response = requests.get(url, params=params) + +# Print the raw response content and status code for debugging +print(f'Status Code: {response.status_code}') +print(f'Response Content: {response.text}') # Check if the request was successful if response.status_code == 200: - # Parse the JSON response - playlist = response.json().get('playlist', []) - print('Playlist:', playlist) + try: + # Parse the JSON response + playlist = response.json().get('playlist', []) + print('Playlist:', playlist) + + # Download each file in the playlist + for item in playlist: + file_url = item['url'] + file_name = item['file_name'] + response = requests.get(file_url) + if response.status_code == 200: + with open(file_name, 'wb') as f: + f.write(response.content) + print(f'Downloaded {file_name}') + else: + print(f'Failed to download {file_name}: {response.status_code}') + except requests.exceptions.JSONDecodeError as e: + print(f'Failed to parse JSON response: {e}') else: - print('Failed to retrieve playlist:', response.json()) \ No newline at end of file + print('Failed to retrieve playlist:', response.text) \ No newline at end of file