frontend/database.py
def auto_update_function(cities):
    """Auto-update weather function
        The function takes a list of the cities to update.

        If the error connecting to sources - an error with
        a status of 500 and JSON with the cause of the error and URL.

        If the connection is successful, it enters the
        data into the database and returns an empty response with code 200.

    """
    try:
            connect = psycopg2.connect(database = 'django_test', user = 'roman',
                                       host = 'localhost', password = 'admin')
            cursor = connect.cursor()
            cursor.execute(
                'SELECT city_name FROM frontend_city;'
            )
            utc_timezone = pytz.timezone('UTC')

            #read current city list from database
            cities_list = []
            cities_cursor = cursor.fetchall()

            #list of tuple to just list
            for i in range(len(cities_cursor)):
                cities_list.append(cities_cursor[i][0])

            for i in range(len(cities)):

                yandex_value = yandex(cities[i])
                open_weather_value = open_weather_map(cities[i])

                # error in yandex source
                if type(yandex_value[0]) == error.HTTPError:
                    data = {
                        'Error': 'Error in auto update function.',
                        'Time': str(datetime.datetime.now(utc_timezone)),
                        'Reason': '{}. Please, check url: {}'.format(yandex_value[0], yandex_value[1])
                    }
                    json_data_error = json.dumps(data)
                    response = HttpResponse(json_data_error, status=500, content_type='application/json', charset='utf-8')
                    return response

                # error in open weather source
                elif (type(open_weather_value[0]) == error.HTTPError):
                    data = {
                        'Error': 'Error in auto update function.',
                        'Time': datetime.datetime.now(utc_timezone),
                        'Reason': '{}. Please, check url: {}'.format(open_weather_value[0], open_weather_value[1])
                    }
                    json_data_error = json.dumps(data)
                    response = HttpResponse(json_data_error, status=500, content_type='application/json', charset='utf-8')
                    return response

                #If the city has not been checked before
                elif (cities[i] not in cities_list):
                    cursor.execute("INSERT INTO frontend_city (city_name) values ('{}');".format(cities[i]))
                    connect.commit()
                data = {
                    'Yandex': str(yandex_value[0]),
                    'Open weather': str(open_weather_value[0])
                }
                cursor.execute("SELECT id FROM frontend_city WHERE city_name = '{}';".format(cities[i]))
                city_id = cursor.fetchall()
                city_id = city_id[0][0]
                json_data = json.dumps(data)
                cursor.execute(
                    "INSERT INTO frontend_history (city_id, temp_values, created) \
                       VALUES ({},'{}', '{}');".format(city_id, json_data,
                                                       datetime.datetime.now(utc_timezone)))
                connect.commit()
            connect.close()
            response = HttpResponse(status=200, content_type='text/html', charset='utf-8')
            return response

    except Exception as connection_db_error:
        data = {
            'Error': 'Error in auto update function.',
            'Time': str(datetime.datetime.now(utc_timezone)),
            'Reason': '{}'.format(connection_db_error)
        }
        json_data_error = json.dumps(data)
        response = HttpResponse(json_data_error, status=500, content_type='application/json', charset='utf-8')
        return response
    connect.close()
Auto-update weather function
        The function takes a list of the cities to update.

        If the error connecting to sources - an error with
        a status of 500 and JSON with the cause of the error and URL.

        If the connection is successful, it enters the
        data into the database and returns an empty response with code 200.

    
Auto-update weather function
The function takes a list of the cities to update.

If the error connecting to sources - an error with
a status of 500 and JSON with the cause of the error and URL.

If the connection is successful, it enters the
data into the database and returns an empty response with code 200.
Auto - update weather function The function takes a list of the cities to update . If the error connecting to sources - an error with a status of 500 and JSON with the cause of the error and URL . If the connection is successful it enters the data into the database and returns an empty response with code 200 .
def auto_update_function(cities):
    try:
            connect = psycopg2.connect(database = 'django_test', user = 'roman',
                                       host = 'localhost', password = 'admin')
            cursor = connect.cursor()
            cursor.execute(
                'SELECT city_name FROM frontend_city;'
            )
            utc_timezone = pytz.timezone('UTC')
            cities_list = []
            cities_cursor = cursor.fetchall()
            for i in range(len(cities_cursor)):
                cities_list.append(cities_cursor[i][0])
            for i in range(len(cities)):
                yandex_value = yandex(cities[i])
                open_weather_value = open_weather_map(cities[i])
                if type(yandex_value[0]) == error.HTTPError:
                    data = {
                        'Error': 'Error in auto update function.',
                        'Time': str(datetime.datetime.now(utc_timezone)),
                        'Reason': '{}. Please, check url: {}'.format(yandex_value[0], yandex_value[1])
                    }
                    json_data_error = json.dumps(data)
                    response = HttpResponse(json_data_error, status=500, content_type='application/json', charset='utf-8')
                    return response
                elif (type(open_weather_value[0]) == error.HTTPError):
                    data = {
                        'Error': 'Error in auto update function.',
                        'Time': datetime.datetime.now(utc_timezone),
                        'Reason': '{}. Please, check url: {}'.format(open_weather_value[0], open_weather_value[1])
                    }
                    json_data_error = json.dumps(data)
                    response = HttpResponse(json_data_error, status=500, content_type='application/json', charset='utf-8')
                    return response
                elif (cities[i] not in cities_list):
                    cursor.execute("INSERT INTO frontend_city (city_name) values ('{}');".format(cities[i]))
                    connect.commit()
                data = {
                    'Yandex': str(yandex_value[0]),
                    'Open weather': str(open_weather_value[0])
                }
                cursor.execute("SELECT id FROM frontend_city WHERE city_name = '{}';".format(cities[i]))
                city_id = cursor.fetchall()
                city_id = city_id[0][0]
                json_data = json.dumps(data)
                cursor.execute(
                    "INSERT INTO frontend_history (city_id, temp_values, created) \
                       VALUES ({},'{}', '{}');".format(city_id, json_data,
                                                       datetime.datetime.now(utc_timezone)))
                connect.commit()
            connect.close()
            response = HttpResponse(status=200, content_type='text/html', charset='utf-8')
            return response
    except Exception as connection_db_error:
        data = {
            'Error': 'Error in auto update function.',
            'Time': str(datetime.datetime.now(utc_timezone)),
            'Reason': '{}'.format(connection_db_error)
        }
        json_data_error = json.dumps(data)
        response = HttpResponse(json_data_error, status=500, content_type='application/json', charset='utf-8')
        return response
    connect.close()
def auto_update_function ( cities ) : try : connect = psycopg2 . connect ( database = 'django_test' , user = 'roman' , host = 'localhost' , password = 'admin' ) cursor = connect . cursor ( ) cursor . execute ( 'SELECT city_name FROM frontend_city;' ) utc_timezone = pytz . timezone ( 'UTC' ) cities_list = [ ] cities_cursor = cursor . fetchall ( ) for i in range ( len ( cities_cursor ) ) : cities_list . append ( cities_cursor [ i ] [ 0 ] ) for i in range ( len ( cities ) ) : yandex_value = yandex ( cities [ i ] ) open_weather_value = open_weather_map ( cities [ i ] ) if type ( yandex_value [ 0 ] ) == error . HTTPError : data = { 'Error' : 'Error in auto update function.' , 'Time' : str ( datetime . datetime . now ( utc_timezone ) ) , 'Reason' : '{}. Please, check url: {}' . format ( yandex_value [ 0 ] , yandex_value [ 1 ] ) } json_data_error = json . dumps ( data ) response = HttpResponse ( json_data_error , status = 500 , content_type = 'application/json' , charset = 'utf-8' ) return response elif ( type ( open_weather_value [ 0 ] ) == error . HTTPError ) : data = { 'Error' : 'Error in auto update function.' , 'Time' : datetime . datetime . now ( utc_timezone ) , 'Reason' : '{}. Please, check url: {}' . format ( open_weather_value [ 0 ] , open_weather_value [ 1 ] ) } json_data_error = json . dumps ( data ) response = HttpResponse ( json_data_error , status = 500 , content_type = 'application/json' , charset = 'utf-8' ) return response elif ( cities [ i ] not in cities_list ) : cursor . execute ( "INSERT INTO frontend_city (city_name) values ('{}');" . format ( cities [ i ] ) ) connect . commit ( ) data = { 'Yandex' : str ( yandex_value [ 0 ] ) , 'Open weather' : str ( open_weather_value [ 0 ] ) } cursor . execute ( "SELECT id FROM frontend_city WHERE city_name = '{}';" . format ( cities [ i ] ) ) city_id = cursor . fetchall ( ) city_id = city_id [ 0 ] [ 0 ] json_data = json . dumps ( data ) cursor . execute ( "INSERT INTO frontend_history (city_id, temp_values, created) \
                       VALUES ({},'{}', '{}');" . format ( city_id , json_data , datetime . datetime . now ( utc_timezone ) ) ) connect . commit ( ) connect . close ( ) response = HttpResponse ( status = 200 , content_type = 'text/html' , charset = 'utf-8' ) return response except Exception as connection_db_error : data = { 'Error' : 'Error in auto update function.' , 'Time' : str ( datetime . datetime . now ( utc_timezone ) ) , 'Reason' : '{}' . format ( connection_db_error ) } json_data_error = json . dumps ( data ) response = HttpResponse ( json_data_error , status = 500 , content_type = 'application/json' , charset = 'utf-8' ) return response connect . close ( )
Auto-update weather function
The function takes a list of the cities to update.
Auto - update weather function The function takes a list of the cities to update .
"""Auto-update weather function
        The function takes a list of the cities to update.

        If the error connecting to sources - an error with
        a status of 500 and JSON with the cause of the error and URL.

        If the connection is successful, it enters the
        data into the database and returns an empty response with code 200.

    """ #read current city list from database #list of tuple to just list # error in yandex source # error in open weather source #If the city has not been checked before
cities