fix tz
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -2,16 +2,20 @@ from flask import Flask, redirect, url_for, render_template
|
|||||||
from views.index_views import index
|
from views.index_views import index
|
||||||
from views.user_views import user
|
from views.user_views import user
|
||||||
from views.group_views import group
|
from views.group_views import group
|
||||||
from config import app_config
|
from config import app_config as config_class
|
||||||
from database import init_app
|
from database import init_app
|
||||||
|
|
||||||
import logging, os
|
import logging, os
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
|
# Instantiate config class
|
||||||
|
app_config = config_class()
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config.from_object(app_config)
|
app.config.from_object(app_config)
|
||||||
init_app(app)
|
init_app(app)
|
||||||
|
|
||||||
|
# Logging
|
||||||
if app.config.get('LOG_TO_FILE'):
|
if app.config.get('LOG_TO_FILE'):
|
||||||
log_file = app.config.get('LOG_FILE_PATH', '/app/logs/app.log')
|
log_file = app.config.get('LOG_FILE_PATH', '/app/logs/app.log')
|
||||||
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
||||||
|
|||||||
@@ -16,9 +16,14 @@ class Config:
|
|||||||
OUI_API_LIMIT_PER_SEC = int(os.getenv('OUI_API_LIMIT_PER_SEC', '2'))
|
OUI_API_LIMIT_PER_SEC = int(os.getenv('OUI_API_LIMIT_PER_SEC', '2'))
|
||||||
OUI_API_DAILY_LIMIT = int(os.getenv('OUI_API_DAILY_LIMIT', '10000'))
|
OUI_API_DAILY_LIMIT = int(os.getenv('OUI_API_DAILY_LIMIT', '10000'))
|
||||||
|
|
||||||
# Timezone
|
# These get set in __init__
|
||||||
APP_TIMEZONE = os.getenv('APP_TIMEZONE', 'UTC')
|
APP_TIMEZONE = 'UTC'
|
||||||
TZ = pytz.timezone(APP_TIMEZONE)
|
TZ = pytz.utc
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
tz_name = os.getenv('APP_TIMEZONE', 'UTC')
|
||||||
|
self.APP_TIMEZONE = tz_name
|
||||||
|
self.TZ = pytz.timezone(tz_name)
|
||||||
|
|
||||||
class DevelopmentConfig(Config):
|
class DevelopmentConfig(Config):
|
||||||
"""Development configuration."""
|
"""Development configuration."""
|
||||||
|
|||||||
Binary file not shown.
@@ -1,23 +1,31 @@
|
|||||||
from flask import Blueprint, render_template, request, jsonify
|
from flask import Blueprint, render_template, request, jsonify
|
||||||
from database import get_db
|
from database import get_db
|
||||||
from pytz import timezone
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import requests
|
import requests, pytz
|
||||||
|
|
||||||
index = Blueprint('index', __name__)
|
index = Blueprint('index', __name__)
|
||||||
OUI_API_URL = 'https://api.maclookup.app/v2/macs/{}'
|
OUI_API_URL = 'https://api.maclookup.app/v2/macs/{}'
|
||||||
|
|
||||||
|
|
||||||
|
import pytz # make sure it's imported if not already
|
||||||
|
|
||||||
def time_ago(dt):
|
def time_ago(dt):
|
||||||
from config import LOCAL_TZ
|
if not dt:
|
||||||
|
return "n/a"
|
||||||
|
|
||||||
|
tz_name = current_app.config.get('APP_TIMEZONE', 'UTC')
|
||||||
|
local_tz = current_app.config.get('TZ', pytz.utc)
|
||||||
|
|
||||||
|
# Only assign UTC tzinfo if naive
|
||||||
if dt.tzinfo is None:
|
if dt.tzinfo is None:
|
||||||
dt = dt.replace(tzinfo=pytz.UTC)
|
dt = dt.replace(tzinfo=pytz.utc)
|
||||||
local_dt = dt.astimezone(LOCAL_TZ)
|
|
||||||
|
# Convert to app timezone
|
||||||
|
dt = dt.astimezone(local_tz)
|
||||||
|
now = datetime.now(local_tz)
|
||||||
|
diff = now - dt
|
||||||
|
|
||||||
now = datetime.now(LOCAL_TZ)
|
|
||||||
diff = now - local_dt
|
|
||||||
seconds = int(diff.total_seconds())
|
seconds = int(diff.total_seconds())
|
||||||
|
|
||||||
if seconds < 60:
|
if seconds < 60:
|
||||||
return f"{seconds}s ago"
|
return f"{seconds}s ago"
|
||||||
elif seconds < 3600:
|
elif seconds < 3600:
|
||||||
@@ -28,6 +36,7 @@ def time_ago(dt):
|
|||||||
return f"{seconds//86400}d{(seconds%86400)//3600}h ago"
|
return f"{seconds//86400}d{(seconds%86400)//3600}h ago"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def lookup_vendor(mac):
|
def lookup_vendor(mac):
|
||||||
prefix = mac.replace(":", "").replace("-", "").upper()[:6]
|
prefix = mac.replace(":", "").replace("-", "").upper()[:6]
|
||||||
db = get_db()
|
db = get_db()
|
||||||
|
|||||||
Reference in New Issue
Block a user