# Path: hashview/agents/forms.py
# class AgentsForm(FlaskForm):
#     name = StringField('Agent Name', validators=[DataRequired()])
#     id = StringField('agent_id', validators=[DataRequired()])
#     
#     submit = SubmitField('Update')
# 
# Path: hashview/models.py
# class Agents(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)         # can probably be reduced
#     src_ip = db.Column(db.String(15), nullable=False)
#     uuid = db.Column(db.String(60), nullable=False)          # can probably be reduced
#     status = db.Column(db.String(20), nullable=False)        # Pending, Syncing, Working, Idle
#     hc_status = db.Column(db.String(6000))
#     last_checkin = db.Column(db.DateTime)
#     benchmark = db.Column(db.String(20))
#     cpu_count = db.Column(db.Integer)
#     gpu_count = db.Column(db.Integer)
# 
# class JobTasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     job_id = db.Column(db.Integer, nullable=False)
#     task_id = db.Column(db.Integer, nullable=False)
#     command = db.Column(db.String(1024))                  
#     status = db.Column(db.String(50), nullable=False)       # Running, Paused, Not Started, Completed, Queued, Canceled, Importing
#     agent_id = db.Column(db.Integer, db.ForeignKey('agents.id'))
# 
# Path: hashview/utils/utils.py
# def getHashviewVersion():
#     with open('VERSION.TXT') as f:
#         return f.read().split('\n')[0]
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, abort, flash, redirect, url_for, send_from_directory
from flask_login import login_required, current_user
from hashview.agents.forms import AgentsForm
from hashview.models import Agents, JobTasks
from hashview.utils.utils import getHashviewVersion
from hashview import db
import os
            flash('Agent was working. The active task was not stopped and you will not receive the results.', 'warning')

        agent.status = 'Pending'
        db.session.commit()

        flash('Agent Deauthorized', 'success')
        return redirect(url_for('agents.agents_list'))
    else:
        abort(403)        


@agents.route("/agents/delete/<int:agent_id>", methods=['GET', 'POST'])
@login_required
def agents_delete(agent_id):
    if current_user.admin:
        jobtasks = JobTasks.query.filter_by(agent_id = agent_id).count()
        if jobtasks > 0:
            flash('Error: Agent is active with a task.', 'danger')
        else:
            agent = Agents.query.get(agent_id)
            db.session.delete(agent)
            db.session.commit()
            flash('Agent removed', 'success')
        return redirect(url_for('agents.agents_list'))
    else:
        abort(403)

@agents.route("/agents/download", methods=['GET'])
@login_required
def agents_download():

# Path: hashview/agents/forms.py
# class AgentsForm(FlaskForm):
#     name = StringField('Agent Name', validators=[DataRequired()])
#     id = StringField('agent_id', validators=[DataRequired()])
#     
#     submit = SubmitField('Update')
# 
# Path: hashview/models.py
# class Agents(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)         # can probably be reduced
#     src_ip = db.Column(db.String(15), nullable=False)
#     uuid = db.Column(db.String(60), nullable=False)          # can probably be reduced
#     status = db.Column(db.String(20), nullable=False)        # Pending, Syncing, Working, Idle
#     hc_status = db.Column(db.String(6000))
#     last_checkin = db.Column(db.DateTime)
#     benchmark = db.Column(db.String(20))
#     cpu_count = db.Column(db.Integer)
#     gpu_count = db.Column(db.Integer)
# 
# class JobTasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     job_id = db.Column(db.Integer, nullable=False)
#     task_id = db.Column(db.Integer, nullable=False)
#     command = db.Column(db.String(1024))                  
#     status = db.Column(db.String(50), nullable=False)       # Running, Paused, Not Started, Completed, Queued, Canceled, Importing
#     agent_id = db.Column(db.Integer, db.ForeignKey('agents.id'))
# 
# Path: hashview/utils/utils.py
# def getHashviewVersion():
#     with open('VERSION.TXT') as f:
#         return f.read().split('\n')[0]
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/agents/routes.py
from flask import Blueprint, render_template, abort, flash, redirect, url_for, send_from_directory
from flask_login import login_required, current_user
from hashview.agents.forms import AgentsForm
from hashview.models import Agents, JobTasks
from hashview.utils.utils import getHashviewVersion
from hashview import db
import os

            flash('Agent was working. The active task was not stopped and you will not receive the results.', 'warning')

        agent.status = 'Pending'
        db.session.commit()

        flash('Agent Deauthorized', 'success')
        return redirect(url_for('agents.agents_list'))
    else:
        abort(403)        


@agents.route("/agents/delete/<int:agent_id>", methods=['GET', 'POST'])
@login_required
def agents_delete(agent_id):
    if current_user.admin:
        jobtasks = JobTasks.query.filter_by(agent_id = agent_id).count()
        if jobtasks > 0:
            flash('Error: Agent is active with a task.', 'danger')
        else:
            agent = Agents.query.get(agent_id)
            db.session.delete(agent)
            db.session.commit()
            flash('Agent removed', 'success')
        return redirect(url_for('agents.agents_list'))
    else:
        abort(403)

@agents.route("/agents/download", methods=['GET'])
@login_required
def agents_download():

    version = getHashviewVersion()