Fix tracker code to fix tests

cache-user-ips-for-tracker
Kevin Chung 2020-04-30 02:45:12 -04:00
parent 4e28e30cb8
commit 87895918f5
3 changed files with 20 additions and 7 deletions

View File

@ -44,3 +44,9 @@ def clear_pages():
cache.delete_memoized(get_pages)
cache.delete_memoized(get_page)
def clear_user_ips(user_id):
from CTFd.utils.user import get_user_ips
cache.delete_memoized(get_user_ips, user_id=user_id)

View File

@ -7,6 +7,7 @@ from flask import abort, redirect, render_template, request, session, url_for
from sqlalchemy.exc import IntegrityError, InvalidRequestError
from werkzeug.wsgi import DispatcherMiddleware
from CTFd.cache import clear_user_ips
from CTFd.exceptions import UserNotFoundException, UserTokenExpiredException
from CTFd.models import Tracking, db
from CTFd.utils import config, get_config, markdown
@ -179,9 +180,10 @@ def init_request_processors(app):
if authed():
user_ips = get_current_user_ips()
ip = get_ip()
track = None
if ip not in user_ips:
visit = Tracking(ip=get_ip(), user_id=session["id"])
db.session.add(visit)
track = Tracking(ip=get_ip(), user_id=session["id"])
db.session.add(track)
else:
if request.method != "GET":
track = Tracking.query.filter_by(
@ -189,11 +191,13 @@ def init_request_processors(app):
).first()
track.date = datetime.datetime.utcnow()
try:
db.session.commit()
except (InvalidRequestError, IntegrityError):
db.session.rollback()
logout_user()
if track:
try:
db.session.commit()
except (InvalidRequestError, IntegrityError):
db.session.rollback()
logout_user()
clear_user_ips(user_id=session["id"])
if authed():
user = get_current_user()

View File

@ -58,6 +58,9 @@ def test_reset():
gen_fail(app.db, user_id=user_obj.id, challenge_id=random.randint(1, 10))
gen_tracking(app.db, user_id=user_obj.id)
print("eet")
print(Tracking.query.all())
# Add PageFiles
for x in range(5):
gen_file(