# Path: app/models.py
# class Respondent(db.Model):
# class Campaign(db.Model):
# class Audience(db.Model):
# class RandomBias(db.Model):
# class PossibleAbuses(db.Model):
# class BlockedClients(db.Model):
# class CallStatistics(db.Model):
# class Role(db.Model, RoleMixin):
# class User(db.Model, UserMixin):
#     def __repr__(self):


from hashlib import sha256
from app.models import BlockedClients, db
from datetime import date
from flask import current_app


def is_blocked(request):
    register_ip_address(request)
    hashed_ip = get_hashed_ip_addr(get_ip_addr(request))
    client = BlockedClients().query.filter_by(id=hashed_ip).first()
    if client.persistent:
        return True
    elif client.date == date.today() and client.calls > current_app.config['MAX_CALLS_PER_IP_PER_DAY']:
        return True
    else:
        return False


def get_ip_addr(request):
    if 'X-Forwarded-For' in request.headers and request.remote_addr == '127.0.0.1':
        return request.headers['X-Forwarded-For']
    return request.remote_addr


def get_hashed_ip_addr(address):
    return sha256(bytes(str(address), 'utf-8')).hexdigest()


def register_ip_address(request):
    hashed_ip = get_hashed_ip_addr(get_ip_addr(request))
    client = BlockedClients().query.filter_by(id=hashed_ip).first()
    if client is None:

# Path: app/models.py
# class Respondent(db.Model):
# class Campaign(db.Model):
# class Audience(db.Model):
# class RandomBias(db.Model):
# class PossibleAbuses(db.Model):
# class BlockedClients(db.Model):
# class CallStatistics(db.Model):
# class Role(db.Model, RoleMixin):
# class User(db.Model, UserMixin):
#     def __repr__(self):


# Path: app/abuse/client.py
from hashlib import sha256
from app.models import BlockedClients, db
from datetime import date
from flask import current_app



def is_blocked(request):
    register_ip_address(request)
    hashed_ip = get_hashed_ip_addr(get_ip_addr(request))
    client = BlockedClients().query.filter_by(id=hashed_ip).first()
    if client.persistent:
        return True
    elif client.date == date.today() and client.calls > current_app.config['MAX_CALLS_PER_IP_PER_DAY']:
        return True
    else:
        return False


def get_ip_addr(request):
    if 'X-Forwarded-For' in request.headers and request.remote_addr == '127.0.0.1':
        return request.headers['X-Forwarded-For']
    return request.remote_addr


def get_hashed_ip_addr(address):
    return sha256(bytes(str(address), 'utf-8')).hexdigest()


def register_ip_address(request):
    hashed_ip = get_hashed_ip_addr(get_ip_addr(request))
    client = BlockedClients().query.filter_by(id=hashed_ip).first()
    if client is None:

        client = BlockedClients()