# Path: hashview/settings/forms.py
# class HashviewSettingsForm(FlaskForm):
#     retention_period = StringField('Retention Period (in days)', validators=[DataRequired()])
#     submit = SubmitField('Update')
# 
#     def validate_rention_period(self, retention_period):
#         if int(retention_period.data) < 1 or int(retention_period.data) > 65535:
#             raise ValidationError('Range must be between 1 and 65535.')
# 
# Path: hashview/models.py
# class Settings(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     retention_period = db.Column(db.Integer)
#     version = db.Column(db.String(10))
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, abort, url_for, flash, request, redirect
from flask_login import login_required, current_user
from hashview.settings.forms import HashviewSettingsForm
from hashview.models import Settings
from hashview import db
import os

settings = Blueprint('settings', __name__)


#############################################
# Settings
#############################################

@settings.route("/settings", methods=['GET', 'POST'])
@login_required
def settings_list():
    if current_user.admin:
        HashviewForm = HashviewSettingsForm()
        settings = Settings.query.first()

        tmp_folder_size = 0
        for file in os.scandir('hashview/control/tmp/'):
            tmp_folder_size += os.stat(file).st_size

        if HashviewForm.validate_on_submit():
            settings.retention_period = HashviewForm.retention_period.data

# Path: hashview/settings/forms.py
# class HashviewSettingsForm(FlaskForm):
#     retention_period = StringField('Retention Period (in days)', validators=[DataRequired()])
#     submit = SubmitField('Update')
# 
#     def validate_rention_period(self, retention_period):
#         if int(retention_period.data) < 1 or int(retention_period.data) > 65535:
#             raise ValidationError('Range must be between 1 and 65535.')
# 
# Path: hashview/models.py
# class Settings(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     retention_period = db.Column(db.Integer)
#     version = db.Column(db.String(10))
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/settings/routes.py
from flask import Blueprint, render_template, abort, url_for, flash, request, redirect
from flask_login import login_required, current_user
from hashview.settings.forms import HashviewSettingsForm
from hashview.models import Settings
from hashview import db
import os


settings = Blueprint('settings', __name__)


#############################################
# Settings
#############################################

@settings.route("/settings", methods=['GET', 'POST'])
@login_required
def settings_list():
    if current_user.admin:
        HashviewForm = HashviewSettingsForm()
        settings = Settings.query.first()

        tmp_folder_size = 0
        for file in os.scandir('hashview/control/tmp/'):
            tmp_folder_size += os.stat(file).st_size

        if HashviewForm.validate_on_submit():
            settings.retention_period = HashviewForm.retention_period.data

            db.session.commit()