Exemplo n.º 1
0
def new_notification(notification_object: dict) -> None:
    """
    Each time an alarm goes off, a change in weather has been detected
    or there's a new news story, the data is sent to this function. The
    data for that notification is then appended to the json file.
    """
    # Attempts to load contents of the file. If it's empty, an
    # empty list is defined and a warning is sent to the log file.

    if filename.is_file():
        with open(tmpdir + '/notifications.json', 'r') as notification_file:
            try:
                notifications = json.load(notification_file)
            except Exception as error:
                notifications = []
                warning_log(error)

        notifications.append(notification_object.copy())

        with open(tmpdir + '/notifications.json', 'w') as notification_file:
            json.dump(notifications, notification_file, indent=2)
    else:
        print("file created")
        with open(tmpdir + '/notifications.json', 'x') as notification_file:
            notification_file.close()
            print("file closed")
Exemplo n.º 2
0
def center_exists(_login):
    """Check if some center exists

    :param _login: login of some center
    :return: boolean
    """
    center = User.query.filter_by(login=_login).first()
    if center is not None:
        warning_log(f"Center by this login: {_login} is already exists")
        return True
    else:
        return False
Exemplo n.º 3
0
def age_validation(obj):
    """Check age for correct input

    :param obj: some object
    :return: boolean
    """
    regular_number = re.compile(r'^\d+(?:.\d*)?$')
    if 'Age' in obj and regular_number.match(obj['Age']):
        return True
    else:
        warning_log(f"The age passed incorrect. Age should be positive number")
        return False
Exemplo n.º 4
0
def valid_specie_for_animal(animal_object):
    """Check if specie exists

    :param animal_object: some animal
    :return: boolean
    """
    specie = Specie.query.filter_by(name=animal_object['Species']).first()
    if specie is None:
        warning_log("Specie does not exists. You should add it to database")
        return False
    else:
        return True
Exemplo n.º 5
0
def valid_token(token, config):
    """Check if token is valid

    :param token: token of center
    :param config: secret config
    :return: if except return boolean
    """
    try:
        decode(token, config)
    except:
        warning_log("Invalid token")
        return True
Exemplo n.º 6
0
def specie_exists(_name):
    """Check if some specie exists

    :param _name: name of some specie
    :return: boolean
    """
    specie = Specie.query.filter_by(name=_name).first()
    if specie is not None:
        warning_log(f"Specie by this name: {_name}  exists")
        return False
    else:
        return True
    def check_user(_login, _password):
        """Check if center exists

        :param _login: login of center
        :param _password: password of center
        :return: boolean
        """
        user = User.query.filter_by(login=_login).first()
        if user is None:
            warning_log('Center is not defined')
            return False
        else:
            return True
Exemplo n.º 8
0
def valid_specie_form(specie):
    """Valid input form 'Name', 'Description', 'Price'

    :param specie: some specie
    :return: boolean
    """
    if ('Name' in specie and 'Description' in specie and 'Price' in specie):
        return True
    else:
        warning_log(
            "Register data of specie are invalid. Input form should be similar to this: "
            "{'Name': name, 'Description': description, 'Price': price}")
        return False
Exemplo n.º 9
0
def valid_animal_form(animal):
    """Valid input form 'Name', 'Age', 'Species'

    :param animal: some animal
    :return: boolean
    """
    if ('Name' in animal and 'Age' in animal and 'Species' in animal):
        return True
    else:
        warning_log(
            "Register data of animal are invalid. Input form should be similar to this: "
            "{'Name': name, 'Age': age, 'Species': specie}")
        return False
Exemplo n.º 10
0
def valid_user_login(user):
    """Valid input form 'Login' and 'Password'

    :param user: some user
    :return: boolean
    """
    if ('Login' in user and 'Password' in user):
        return True
    else:
        warning_log(
            "Register data of center is invalid. Login form should be similar to this:"
            " {'Login': login, 'Password': password}")
        return False
Exemplo n.º 11
0
def valid_user(user_object):
    """Valid center information

    :param user_object: some center
    :return: boolean
    """
    if ('Login' in user_object and 'Password' in user_object
            and 'Address' in user_object):
        return True
    else:
        warning_log(
            "Register data of center is invalid. Register form should be similar to this:"
            " {'Login': Login, 'Password': password, 'Address': address")
        return False
Exemplo n.º 12
0
def update_notifications() -> dict:
    """
    This function will run each time the page is refreshed.
    It returns the appropriate list of
    notifications depending if the 'Filter' button was pressed or the
    page was refreshed.
    """
    with open(tmpdir + '/notifications.json', 'r') as notification_file:
        try:
            notifications = json.load(notification_file)
        except Exception as error:
            notifications = []
            warning_log(error)
    return notifications
Exemplo n.º 13
0
def check_center_before_delete(_center_id, _id):
    """Check center before delete

        :param _center_id: id of some center
        :param _id: id of some animal
        :return: boolean
        """
    animal = Animal.query.filter_by(id=_id).filter_by(
        center_id=_center_id).first()
    if animal is not None:
        return True
    else:
        warning_log(
            f"Center by this id: {_center_id} does not have animal by this id {_id}"
        )
        return False
Exemplo n.º 14
0
def start_alarm() -> None:
    """
    This function is responsible for setting timers for each alarm.
    For each alarm, the difference in time between now and the time of
    the alarm  and adds each alarm to the
    schedular. This function runs on a separate thread each time the
    user loads the program.
    """
    alarm_schedule = sched.scheduler(time.time, time.sleep)
    time_when_alarm_is_created = datetime.datetime.now().strftime(
        "%Y-%m-%d %H:%M:%S")
    time_when_alarm_is_created = datetime.datetime.strptime(
        time_when_alarm_is_created, "%Y-%m-%d %H:%M:%S")
    DAY = 86400
    news_call = False
    weather_call = False
    with open(tmpdir + '/alarms.json', 'r') as alarms_file:
        try:
            alarm_list = json.load(alarms_file)
        except Exception:
            alarm_list = []
            warning_log(Exception)

    for alarm in alarm_list:
        # Alarm is a standard, one off alarm.
        # Date and time the alarm is set to go off.
        alarm_time = datetime.datetime.strptime(alarm['title'],
                                                '%Y-%m-%d-%H:%M:%S')
        # Time in seconds to when the alarm is due to go off.
        final_time = int(
            (alarm_time - time_when_alarm_is_created).total_seconds())
        if alarm['news'] == "News Enabled":
            news_call = True
        if alarm['weather'] == "Weather Enabled":
            weather_call = True

        # If the alarm is set at a time that is eariler than the
        # time of the alarm, add 24 hours to the time.
        if final_time < 0:
            final_time += DAY
        # Adds alarm to the scheduler.
        alarm_schedule.enter(final_time, 1, alarm_end,
                             (alarm_time, alarm, news_call, weather_call))

    alarm_schedule.run()
    schedule.run_pending()