Exemplo n.º 1
0
        def commit_data(self,data,metric_data,imperial_data):
            
        
        #Prep model objects
            loc = Location()
            temp_data = Temperature()
            wind_data = WindSpeed()

        #Assign location fields from collected json data
            loc.longitude = data[u'coord'][u'lon']
            loc.latitude = data[u'coord'][u'lat']
            loc.city_id = data[u'id']
            loc.city_name = data[u'name']
        
        #Insert Location if it does not already exist
        
            try:
                comp = Location.objects.get(city_id = data[u'id'])
            except:
                loc.save()
            else:   
                print "location exists"
        
        
        #Assign temperature fields from collected json data
            temp_data.temp_min = Decimal(data[u'main'][u'temp_min'])
            temp_data.temp_max = Decimal(data[u'main'][u'temp_max'])
            temp_data.temp = Decimal(data[u'main'][u'temp'])
            temp_data.temp_min_imperial = Decimal(imperial_data[u'main'][u'temp_min'])
            temp_data.temp_max_imperial = Decimal(imperial_data[u'main'][u'temp_max'])
            temp_data.temp_imperial = Decimal(imperial_data[u'main'][u'temp'])
            temp_data.temp_min_metric = Decimal(metric_data[u'main'][u'temp_min'])
            temp_data.temp_max_metric = Decimal(metric_data[u'main'][u'temp_max'])
            temp_data.temp_metric = Decimal(metric_data[u'main'][u'temp'])
            temp_data.location = Location.objects.get(city_id = loc.city_id)
        
        #Save temperature data
            temp_data.save()
        
        #Assign windspeed fields from collected json data
            wind_data.location = Location.objects.get(city_id = loc.city_id)
            wind_data.wind_speed = Decimal(data[u'wind'][u'speed'])
            wind_data.degree = Decimal(data[u'wind'][u'deg'])
            wind_data.wind_speed_imperial = Decimal(imperial_data[u'wind'][u'speed'])
            wind_data.degree_imperial = Decimal(imperial_data[u'wind'][u'deg'])
            wind_data.wind_speed_metric = Decimal(metric_data[u'wind'][u'speed'])
            wind_data.degree_metric = Decimal(metric_data[u'wind'][u'deg'])
        
        #Save windspeed data
            wind_data.save()
Exemplo n.º 2
0
        def commit_data_hist(self,city,data):
            
        
        #Prep model objects
            temp_data = Temperature()
            wind_data = WindSpeed()
        
   
        #Assign temperature fields from collected json data
            temp_data.temp_min = Decimal(data[u'main'][u'temp_min'])
            temp_data.temp_max = Decimal(data[u'main'][u'temp_max'])
            temp_data.temp = Decimal(data[u'main'][u'temp'])
            temp_data.temp_min_imperial = Decimal(Decimal(1.8) * (Decimal(data[u'main'][u'temp_min']) - Decimal(273.5000)) + Decimal(32.0000))
            temp_data.temp_max_imperial = Decimal(Decimal(1.8) * (Decimal(data[u'main'][u'temp_max']) - Decimal(273.5000)) + Decimal(32.0000))
            temp_data.temp_imperial = Decimal(Decimal(1.8) * (Decimal(data[u'main'][u'temp_min']) - Decimal(273.5000)) + Decimal(32.0000))
            temp_data.temp_min_metric = (Decimal(data[u'main'][u'temp_min']) - Decimal(273.5000))
            temp_data.temp_max_metric = (Decimal(data[u'main'][u'temp_max']) - Decimal(273.5000))
            temp_data.temp_metric = (Decimal(data[u'main'][u'temp']) - Decimal(273.5000))
            temp_data.location = Location.objects.get(city_id = city)
            temp_data.timestamp = datetime.datetime.fromtimestamp(int(data[u'dt'])).strftime('%Y-%m-%d %H:%M:%S')

        
        #Save temperature data
            temp_data.save()
        
        #Assign windspeed fields from collected json data
            wind_data.location = Location.objects.get(city_id = city)
            wind_data.wind_speed = Decimal(data[u'wind'][u'speed'])
            wind_data.degree = Decimal(data[u'wind'][u'deg'])
            wind_data.wind_speed_imperial = Decimal(data[u'wind'][u'speed'])
            wind_data.degree_imperial = Decimal(data[u'wind'][u'deg'])
            wind_data.wind_speed_metric = Decimal(data[u'wind'][u'speed'])
            wind_data.degree_metric = Decimal(data[u'wind'][u'deg'])
            wind_data.timestamp = datetime.datetime.fromtimestamp(int(data[u'dt'])).strftime('%Y-%m-%d %H:%M:%S')

        #Save windspeed data
            wind_data.save()