root.py 19.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
import zipfile
from flask import Flask, Blueprint, jsonify, render_template, redirect, url_for
import os
import shutil
from flask_login import current_user
from flask_bcrypt import bcrypt
from flask_login import login_user, current_user,  logout_user, login_required
import requests
from datetime import datetime
from flask_login import UserMixin
import uuid
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Length
import subprocess
from flask import request
from db.database import db
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_migrate import Migrate
from flask import current_app as k2
from main import app
import string

with app.app_context():
    bcrypt = Bcrypt(app)
    login_manager = LoginManager(app)
    login_manager.login_view = 'login'

    #migrate = Migrate(app, db)
    def generate_id():
        generated_id = uuid.uuid4().hex[:32]
        return ''.join([generated_id[i:i + 4] for i in range(0, len(generated_id), 4)])

    class LoginForm(FlaskForm):
        username = StringField('Username', validators=[DataRequired(), Length(min=3, max=20)])
        password = PasswordField('Password', validators=[DataRequired()])
        submit = SubmitField('Login')

    class User(UserMixin, db.Model):
        __bind_key__ = 'db2'
        __tablename__ = 'user'
        __table_args__ = {'extend_existing': True}
        id = db.Column(db.String(36), primary_key=True, default=generate_id)
        username = db.Column(db.String(50))
        active = db.Column(db.Boolean)
        last_name = db.Column(db.String(50))
        first_name = db.Column(db.String(50))
        middle_name = db.Column(db.String(50))
        email = db.Column(db.String(100))
        phone = db.Column(db.String(20))
        rights = db.Column(db.String(50))
        manufacturer = db.Column(db.String(50))
        manufacturer_id = db.Column(db.Integer)
        created_date = db.Column(db.DateTime)
        created_by = db.Column(db.String(50))
        oblast_id = db.Column(db.Integer)
        password = db.Column(db.String(50))
        #role_id = db.Column(db.Integer, db.ForeignKey('role.id', name='fk_user_role_id'))
        #role = db.relationship('main.Role', backref='users')
        def __repr__(self):
            return f'<User {self.username}>'

    class Role(db.Model):
        __bind_key__ = 'db2'
        __tablename__ = 'role'
        __table_args__ = {'extend_existing': True}
        id = db.Column(db.String(36), primary_key=True, default=generate_id)
        name = db.Column(db.String(50))
        sorting = db.Column(db.Integer)
        note = db.Column(db.String(100))
        enabled = db.Column(db.Boolean)
        public = db.Column(db.Boolean)
        is_admin = db.Column(db.Boolean)
        actions = db.Column(db.String(100))
        def __repr__(self):
            return f'<Role {self.name}>'
        def __repr__(self):
            return f'<User {self.username}>'

    class Component(db.Model):
        __bind_key__ = 'db2'
        __tablename__ = 'component'
        __table_args__ = {'extend_existing': True}
        id = db.Column(db.String(36), primary_key=True, default=generate_id)
        name = db.Column(db.String(100), nullable=False)
        description = db.Column(db.String(200))
        version = db.Column(db.String(20))
        git_link = db.Column(db.String(200))
        dependencies = db.Column(db.String)
        date_installed = db.Column(db.DateTime, default=datetime.utcnow)
        installed = db.Column(db.Boolean, default=False)

        def __init__(self, name, description, version, git_link,  dependencies, installed):
            self.name = name
            self.description = description
            self.version = version
            self.git_link = git_link
            self.dependencies = dependencies
            self.installed = installed
        def __repr__(self):
            return f"Component('{self.name}', '{self.version}')"
        @classmethod
        def get_repository_by_id(self,rep_id):
            component = db.session.query(Component).filter(Component.id == rep_id).first()
            return component

    components_bp = Blueprint('components', __name__, template_folder='templates')
    login_manager = LoginManager(k2)
    @login_manager.user_loader
    def load_user(user_id):
        # Код для завантаження користувача з бази даних за його ідентифікатором
        return User.query.get(user_id)
    @components_bp.route('/', methods=['GET', 'POST'])
    def home():

        if current_user.is_authenticated:
            # Логіка для авторизованого користувача
            return redirect('/dashboard')
        else:
            # Логіка для неавторизованого користувача
            return redirect('/login')
    @components_bp.route('/login', methods=['GET', 'POST'])
    def login():

        form = LoginForm()
        if form.validate_on_submit():
            # Отримання даних з форми
            username = form.username.data
            password = form.password.data
            # Перевірка введених даних
            user = User.query.filter_by(username=username).first()
            if user and bcrypt.check_password_hash(user.password, password):   #
                login_user(user)
                return redirect('/dashboard')
            else:
                # Невірні дані для авторизації
                error_message = f'Неправильне ім`я користувача чи пароль {username} {password} {user.password}'

                return render_template('/login.html', form=form, error_message=error_message)

        return render_template('/login.html', form=form)
    @components_bp.route('/logout')
    @login_required
    def logout():
        logout_user()
        return redirect('/login')
    @components_bp.route('/dashboard')
    def dashboard():
        if not current_user.is_authenticated:
            return redirect('/login')
            # Компоненти доступні для встановлення
        try:
            # GET-запит до API
            response = requests.get('http://127.0.0.1:8001/api/components')
            json_data = response.json()
            # Перетворення JSON-об'єкту на масив
            component_server = [item for item in json_data]
        except:
            component_server = None

        # Встановлені компоненти
        components = Component.query.all()
        components_names = [component.name for component in components]

        if current_user.is_authenticated:
            username = current_user.username
            users_data = User.query.all()
        else:
            username = ''


        # Отримання значення пошукового запиту з параметрів URL
        search_query = request.args.get('search_query')
        #print(search_query)
        filtered_components = []
        if search_query:
            filtered_components = [component for component in component_server if
                                   (search_query.lower() in component['name'].lower() if component['name'] else False) or
                                   (search_query.lower() in component['description'].lower() if component[
                                       'description'] else False)]
        else:
            filtered_components = component_server

        return render_template('dashboard.html', username=username, users=users_data, components=components,
                               components_names=components_names, component_server=filtered_components,
                               search_query=search_query)

    @components_bp.route('/show_components/<string:component_id>')
    @login_required
    def show_components(component_id):
        response = requests.get('http://127.0.0.1:8001/api/components')
        json_data = response.json()
        selected_component = next((component for component in json_data if component['id'] == component_id), None)
        return render_template('component-info.html', selected_component=selected_component)

    @components_bp.route('/install_components_bl/<string:component_id>')
    @login_required
    def install_components_bl(component_id):
        # Шлях до головної папки проекту
        project_folder = 'components'
        #component = Component.get_repository_by_id(component_id)
        response = requests.get('http://127.0.0.1:8001/api/components')
        json_data = response.json()
        selected_component = next((component for component in json_data if component['id'] == component_id), None)
        # Назва репозиторія
        repository_name = selected_component['name']
        # Шлях до папки репозиторія в межах проекту
        repository_folder = os.path.join(project_folder, repository_name)
        # URL репозиторія


        git_repo_url = selected_component['git_link']
        try:
            # Перевірка наявності папки репозиторія
            if not os.path.exists(repository_folder):
                # Створення папки репозиторія
                os.makedirs(repository_folder)
            # Шлях до файлу __init__.py
            init_file = os.path.join(repository_folder, '__init__.py')
            # Перевірка наявності файлу __init__.py
            if not os.path.exists(init_file):
                # Створення пустого файлу __init__.py
                open(init_file, 'a').close()
            #підключення до приватного репозиторію
            #os.environ['GITLAB_PRIVATE_TOKEN'] = '8xxTpxrKVDGoXD5ynjiW'
            #git_repo_url_with_token = git_repo_url.replace('https://',
            #                                               f'https://oauth2:{os.environ["GITLAB_PRIVATE_TOKEN"]}@')

            # Команда для встановлення з використанням git_repo_url і повного шляху до папки репозиторія
            command = ['venv/Scripts/python.exe', '-m', 'pip', 'install', '--use-pep517', 'git+' + git_repo_url, '--target=' + repository_folder]
            # Виконуємо команду встановлення
            subprocess.check_call(command)

            # Шлях до файлу, до якого потрібно додати код
            file_path = 'routes.py'
            # Код, який потрібно додати
            code = selected_component['dependencies']
            # Відкриття файлу у режимі дозапису
            with open(file_path, 'a') as file:
                # Запис нового коду у файл
                file.write('\n')
                file.write(code)
                file.write('\n')
            component =  Component.query.filter_by(name=selected_component['name']).first()
            if not component:
                new_component = Component(
                        name=selected_component['name'],
                        description=selected_component['description'],
                        version=selected_component['version'],
                        git_link=selected_component['git_link'],
                        dependencies=selected_component['dependencies'],
                        installed=True
                    )
                # Add the new component to the database
                db.session.add(new_component)
                db.session.commit()

            # Повертаємо повідомлення про успішне встановлення
            return f'Installed successfully <meta http-equiv="refresh" content="1;url=/dashboard" />'
        except subprocess.CalledProcessError as e:
            return 'Error installing : ' + str(e)


    import os
    import requests
    import shutil
    import subprocess
    from flask import current_app


    @components_bp.route('/install_components/<string:component_id>')
    @login_required
    def install_component_from_archive(component_id):
        # Шлях до головної папки проекту
        project_folder = 'components'
        # Отримати відповідну компоненту зі списку компонент
        response = requests.get('http://127.0.0.1:8001/api/components')
        json_data = response.json()
        selected_component = next((component for component in json_data if component['id'] == component_id), None)
        if selected_component is None:
            return 'Component not found'
        # Отримати посилання на архів компоненти та версію
        archive_url = selected_component['latest_component_data']
        version = selected_component['latest_version']
        try:
            # Створити шлях до папки компоненти згідно назви та версії
            component_folder = os.path.join(project_folder)
            os.makedirs(component_folder, exist_ok=True)
            # Завантажити архів компоненти
            response = requests.get(archive_url, stream=True)
            response.raise_for_status()
            # Шлях до завантаженого архіву
            archive_path = os.path.join(component_folder, f"{selected_component['name']}.zip")
            # Зберегти архів на диск
            with open(archive_path, "wb") as file:
                for chunk in response.iter_content(chunk_size=8192):
                    file.write(chunk)
            # Розпакувати архів
            with zipfile.ZipFile(archive_path, "r") as zip_ref:
                zip_ref.extractall(component_folder)
            # Видалити архів
            os.remove(archive_path)
            # Встановити залежності з файлу requirements.txt
            requirements_file = os.path.join(component_folder, "requirements.txt")
            if os.path.isfile(requirements_file):
                pip_command = f"{current_app.config['VENV_BIN_PATH']}/python -m pip install -r {requirements_file}"
                subprocess.run(pip_command, shell=True, check=True)
            # Шлях до файлу, до якого потрібно додати код
            file_path = 'routes.py'
            # Код, який потрібно додати
            code = f'''from components.{selected_component['name']}.{selected_component['name']}.views import {selected_component['name']}\napp.register_blueprint({selected_component['name']}, url_prefix='/{selected_component['name']}')'''
            #selected_component['dependencies']

            # Відкриття файлу у режимі дозапису
            with open(file_path, 'a') as file:
                # Запис нового коду у файл
                file.write('\n')
                file.write(code)
                file.write('\n')

            # Оновити базу даних з встановленою компонентою
            component = Component.query.filter_by(name=selected_component['name']).first()
            if not component:
                component = Component(
                    name=selected_component['name'],
                    description=selected_component['description'],
                    version=version,
                    git_link=selected_component['git_link'],
                    dependencies=selected_component['dependencies'],
                    installed=True
                )
                db.session.add(component)
            else:
                component.version = version
                component.git_link = selected_component['git_link']
                component.dependencies = selected_component['dependencies']
                component.installed = True

            db.session.commit()

            return f'Component installed successfully: {selected_component["name"]} v{version} <meta http-equiv="refresh" content="1;url=/dashboard" />'

        except Exception as e:
            return f'Error installing component: {str(e)}'


    @components_bp.route('/remove_dependencies/<string:component_id>', methods=['GET'])
    @login_required
    def remove_dependencies(component_id):

        # Знаходимо компоненту за її ID
        component = Component.query.get(component_id)
        if not component:
            return 'Component not found <meta http-equiv="refresh" content="1;url=/dashboard" />'

        # Шлях до файлу, з якого потрібно видалити залежності
        file_path = 'routes.py'

        # Зчитуємо вміст файлу
        with open(file_path, 'r') as file:
            lines = file.readlines()

        # Видаляємо рядки, що містять залежності компоненти
        updated_lines = [line for line in lines if line.strip() not in component.dependencies]

        # Записуємо оновлений вміст у файл
        with open(file_path, 'w') as file:
            file.writelines(updated_lines)

        # Оновлюємо статус компоненти
        component.installed = False
        db.session.commit()

        return 'Dependencies removed successfully <meta http-equiv="refresh" content="1;url=/dashboard" />'

    @components_bp.route('/add_dependencies/<string:component_id>', methods=['GET'])
    @login_required
    def add_dependencies(component_id):

        # Знаходимо компоненту за її ID
        component = Component.query.get(component_id)
        if not component:
            return 'Component not found <meta http-equiv="refresh" content="1;url=/dashboard" />'

        # Шлях до файлу, з якого потрібно видалити залежності
        file_path = 'routes.py'
        code = component.dependencies
        # Відкриття файлу у режимі дозапису
        with open(file_path, 'a') as file:
            # Запис нового коду у файл
            file.write(code)

        # Оновлюємо статус компоненти
        component.installed = True
        db.session.commit()
        return 'Dependencies added successfully <meta http-equiv="refresh" content="1;url=/dashboard" />'

    @components_bp.route('/remove-component/<string:component_id>')
    @login_required
    def remove_component(component_id):
        # Шлях до головної папки проекту
        project_folder = 'components'
        component = Component.query.get(component_id)
        # Назва репозиторія
        repository_name = component.name
        # Шлях до папки репозиторія в межах проекту
        repository_folder = os.path.join(project_folder, repository_name)
        try:
            # Перевірка наявності папки репозиторія
            if os.path.exists(repository_folder):
                # Видалення папки репозиторія
                shutil.rmtree(repository_folder)
            # Видаляємо компоненту з бази
            if component:
                db.session.delete(component)
                db.session.commit()

            # Повертаємо повідомлення про успішне видалення
            return 'Component removed successfully <meta http-equiv="refresh" content="1;url=/dashboard" />'
        except Exception as e:
            # Повертаємо повідомлення про помилку видалення
            return 'Error removing component: ' + str(e)
    app.register_blueprint(components_bp)