|
@@ -6,7 +6,7 @@ from flask import Flask, render_template, request, redirect, url_for, session, s
|
|
from gevent.pywsgi import WSGIServer
|
|
from gevent.pywsgi import WSGIServer
|
|
import socket
|
|
import socket
|
|
import logging
|
|
import logging
|
|
-from peewee import Model, CharField, DateTimeField, SqliteDatabase, BooleanField
|
|
|
|
|
|
+from peewee import Model, CharField, DateTimeField, SqliteDatabase, BooleanField, fn
|
|
import hashlib
|
|
import hashlib
|
|
import os
|
|
import os
|
|
import csv
|
|
import csv
|
|
@@ -43,7 +43,7 @@ class Settings(Model):
|
|
|
|
|
|
|
|
|
|
class Schedule(Model):
|
|
class Schedule(Model):
|
|
- days = CharField()
|
|
|
|
|
|
+ interval = CharField()
|
|
|
|
|
|
class Meta:
|
|
class Meta:
|
|
database = db
|
|
database = db
|
|
@@ -195,6 +195,36 @@ app = Flask(__name__)
|
|
app.secret_key = os.urandom(24)
|
|
app.secret_key = os.urandom(24)
|
|
|
|
|
|
|
|
|
|
|
|
+def format_time_ago(timestamp):
|
|
|
|
+ """Calculate the time passed since a datetime stamp and format it as a human-readable string."""
|
|
|
|
+ now = datetime.datetime.utcnow()
|
|
|
|
+ diff = now - timestamp
|
|
|
|
+
|
|
|
|
+ if diff.days > 365:
|
|
|
|
+ years = diff.days // 365
|
|
|
|
+ return f"{years} year{'s' if years > 1 else ''} ago"
|
|
|
|
+
|
|
|
|
+ if diff.days > 30:
|
|
|
|
+ months = diff.days // 30
|
|
|
|
+ return f"{months} month{'s' if months > 1 else ''} ago"
|
|
|
|
+
|
|
|
|
+ if diff.days > 0:
|
|
|
|
+ return f"{diff.days} day{'s' if diff.days > 1 else ''} ago"
|
|
|
|
+
|
|
|
|
+ if diff.seconds > 3600:
|
|
|
|
+ hours = diff.seconds // 3600
|
|
|
|
+ return f"{hours} hour{'s' if hours > 1 else ''} ago"
|
|
|
|
+
|
|
|
|
+ if diff.seconds > 60:
|
|
|
|
+ minutes = diff.seconds // 60
|
|
|
|
+ return f"{minutes} minute{'s' if minutes > 1 else ''} ago"
|
|
|
|
+
|
|
|
|
+ return "just now"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+app.jinja_env.filters['time_since'] = format_time_ago
|
|
|
|
+
|
|
|
|
+
|
|
@app.before_request
|
|
@app.before_request
|
|
def before_request():
|
|
def before_request():
|
|
db.connect()
|
|
db.connect()
|
|
@@ -345,7 +375,7 @@ def admin_ils_users():
|
|
|
|
|
|
IlsUser.create(username=username, email=email, reset_datetime=datetime.datetime.now()).save()
|
|
IlsUser.create(username=username, email=email, reset_datetime=datetime.datetime.now()).save()
|
|
message = 'ILS User: %s created successfully' % username
|
|
message = 'ILS User: %s created successfully' % username
|
|
- Log.create(username=session['username'], action='Created ILS User: %s' % username,).save()
|
|
|
|
|
|
+ Log.create(username=session['username'], action='Created ILS User: %s' % username, ).save()
|
|
|
|
|
|
# Get all admin users from the DB
|
|
# Get all admin users from the DB
|
|
users = IlsUser.select().execute()
|
|
users = IlsUser.select().execute()
|
|
@@ -480,6 +510,59 @@ def settings():
|
|
return render_template('settings.html', context=context)
|
|
return render_template('settings.html', context=context)
|
|
|
|
|
|
|
|
|
|
|
|
+@app.route('/admin/schedule', methods=['GET', 'POST'])
|
|
|
|
+def schedule():
|
|
|
|
+ # Check to see if user is logged in
|
|
|
|
+ if not requires_auth():
|
|
|
|
+ return redirect(url_for('login'))
|
|
|
|
+
|
|
|
|
+ message = None
|
|
|
|
+
|
|
|
|
+ # add schedule
|
|
|
|
+ if request.method == 'POST':
|
|
|
|
+ # Assign form values to variables
|
|
|
|
+ interval = request.form.get('interval')
|
|
|
|
+
|
|
|
|
+ # Check if the schedule already exists
|
|
|
|
+ try:
|
|
|
|
+ schedule = Schedule.get(Schedule.interval == interval)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(e)
|
|
|
|
+ schedule = None
|
|
|
|
+
|
|
|
|
+ if schedule:
|
|
|
|
+ message = 'Schedule already exists'
|
|
|
|
+ else:
|
|
|
|
+ # Create the schedule
|
|
|
|
+ Schedule.create(interval=interval).save()
|
|
|
|
+ Log.create(username=session['username'], action='Created schedule for %s day interval.' % interval).save()
|
|
|
|
+ message = 'Schedule: %s created successfully' % interval
|
|
|
|
+
|
|
|
|
+ # Get all schedules from the DB
|
|
|
|
+ schedules = Schedule.select().order_by(Schedule.interval.cast("INTEGER")).execute()
|
|
|
|
+
|
|
|
|
+ context = {
|
|
|
|
+ 'schedules': schedules,
|
|
|
|
+ 'message': message,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return render_template('schedule.html', context=context)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# remove schedule
|
|
|
|
+@app.route('/admin/schedule/remove/<int:id>', methods=['GET', 'POST'])
|
|
|
|
+def schedule_remove(id):
|
|
|
|
+ # Check to see if user is logged in
|
|
|
|
+ if not requires_auth():
|
|
|
|
+ return redirect(url_for('login'))
|
|
|
|
+
|
|
|
|
+ # Get the schedule from the DB
|
|
|
|
+ schedule = Schedule.get(Schedule.id == id)
|
|
|
|
+ schedule.delete_instance()
|
|
|
|
+ Log.create(username=session['username'], action='Removed schedule for a %s day reminder.' % schedule.interval).save()
|
|
|
|
+ return redirect(url_for('schedule'))
|
|
|
|
+
|
|
|
|
+
|
|
@app.route('/admin/system/log')
|
|
@app.route('/admin/system/log')
|
|
def system_log():
|
|
def system_log():
|
|
# Check to see if user is logged in
|
|
# Check to see if user is logged in
|
|
@@ -560,7 +643,7 @@ def login():
|
|
|
|
|
|
|
|
|
|
# on exit of the program make sure the http server is stopped
|
|
# on exit of the program make sure the http server is stopped
|
|
-#@app.teardown_appcontext
|
|
|
|
|
|
+# @app.teardown_appcontext
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|