# 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 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()
        client.id = hashed_ip
        client.calls = 1
        client.date = date.today()
        client.persistent = False
        db.session.add(client)
    elif client.date != date.today():
        client.calls = 1
        client.date = date.today()
    else:
        client.calls += 1
    db.session.commit()


def block_ip(ip):
    hashed_ip = get_hashed_ip_addr(ip)
    client = BlockedClients().query.filter_by(id=hashed_ip).first()
    if client is None:
        client = BlockedClients()
        client.id = hashed_ip
        client.calls = 1
        client.date = date.today()
        client.persistent = True
        db.session.add(client)
    else:
        client.persistent = True

# 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 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()
        client.id = hashed_ip
        client.calls = 1
        client.date = date.today()
        client.persistent = False
        db.session.add(client)
    elif client.date != date.today():
        client.calls = 1
        client.date = date.today()
    else:
        client.calls += 1
    db.session.commit()


def block_ip(ip):
    hashed_ip = get_hashed_ip_addr(ip)
    client = BlockedClients().query.filter_by(id=hashed_ip).first()
    if client is None:
        client = BlockedClients()
        client.id = hashed_ip
        client.calls = 1
        client.date = date.today()
        client.persistent = True
        db.session.add(client)
    else:
        client.persistent = True

    db.session.commit()