使用自定义Django命令配置设置和导入模型时出错

时间:2014-09-08 18:09:32

标签: python django django-models django-settings

我对Django有些新意,并且一直在尝试编写自定义管理命令,以便将txt文件中的数据导入我的数据库。我似乎在运行命令时遇到了很多麻烦,我无法弄清楚出了什么问题。我有两个不同的问题,但也许它们是相关的。当我尝试运行该命令时,我收到此错误:

joshs-mbp-4:Commands Josh P$ python vatsim_pull.py
Traceback (most recent call last):
  File "vatsim_pull.py", line 10, in <module>
    from flights.models import (Personal, ActiveFlights, Flights, ActiveControllers,     Controllers, Airports)
ImportError: No module named flights.models

我的应用程序只在我的基础项目目录中,但是我在设置中做错了,这是我的settings.py:

from sys import path
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
path.append(BASE_DIR)
TEMPLATE_DIRS = os.path.join(BASE_DIR, 'templates')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
)


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'Users',
    'registration',
    'flights',
)



MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)





ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases


# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/New_York'

USE_I18N = True

USE_L10N = True

USE_TZ = True

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader')
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.FileSystemFinder",
 "django.contrib.staticfiles.finders.AppDirectoriesFinder")


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
AUTH_PROFILE_MODULE = 'Users.UserProfile'
STATIC_URL = '/static/'

我遇到的另一个问题是,如果我评论导入模型行,我会收到有关配置设置的错误消息

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

除了我尝试在命令行中运行此脚本之外,其他任何时间都没有发生过。

我在manage.py中有这个:

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

这是实际的命令(对不起,如果这是太多的代码,我是新的使用堆栈溢出)。我认为运行settings.configure()会这样做,但我没有运气:

import csv
import requests
import datetime
import math
import sys
from django.conf import settings
from django.core.management.base import NoArgsCommand, CommandError
from datetime import timedelta
from django.utils.timezone import utc
from flights.models import (Personal, ActiveFlights, Flights, ActiveControllers, Controllers, Airports)
from decimal import Decimal
from django.db.models import F
django.conf.settings.configure()

class Command(NoArgsCommand):
    help = "Scrapes flight info from Vatsim"


    def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
        # csv.py doesn't do Unicode; encode temporarily as UTF-8:
        csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
                                dialect=dialect, **kwargs)
        for row in csv_reader:
            #decode UTF-8 back to Unicode, cell by cell:
            yield [unicode(cell, 'utf-8') for cell in row]

    def utf_8_encoder(unicode_csv_data):
        for line in unicode_csv_data:
            yield line.encode('utf-8')

    def getNmFromLatLon(lat1, lon1, lat2, lon2):
        R = 3443.89849 # km
        theta1 = math.radians(lat1)
        theta2 = math.radians(lat2)
        dtheta = math.radians(lat2-lat1)
        dlon = math.radians(lon2-lon1)

        a = math.sin(dtheta/2) * math.sin(detheta/2) + math.cos(theta1) * math.cos(theta2) * math.sin(dlon/2) * math.sin(dlon/2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))

        d = R * c

        return round(d)



    def pilotInsert(row):


        call_sign = row[0]
        pilotId = row[1]
        user_rating = int(row[16])
        if (user_rating == 0):
            user_rating = 'Not Rated'
        elif (user_rating == 1):
            user_rating = 'VATSIM Online Pilot'
        elif (user_rating == 2):
            user_rating = 'VATSIM Airmanship Basics'
        elif (user_rating == 3):
            user_rating = 'VATSIM VFR Pilot'
        elif (user_rating == 4):
            user_rating = 'VATSIM IFR Pilot'
        elif (user_rating == 5):
            user_rating = 'VATSIM Advanced IFR Pilot'
        elif (user_rating == 6):
            user_rating = 'VATSIM International and Oceanic Pilot'
        elif (user_rating == 7):
            user_rating = 'Helicopter VFR and IFR Pilot'
        elif (user_rating == 8):
            user_rating = 'Military Special Operations Pilot'
        elif (user_rating == 9):
            user_rating = 'VATSIM Pilot Flight Instructor'

        real_name = row[2]
        date_time = updateTime
        flight_date = updateTime[0:11]
        client_type = "Pilot"
        alt = row[7]
        lon = Decimal(row[6])
        lat = Decimal(row[5])
        serv = row[14]
        ground_speed = int(row[8])
        transpond = int(row[17])
        flight_heading = int(row[37])
        aircraft = row[9]
        tascruise = int(row[10])
        depairport = row[11]
        altitude = int(row[12])
        destairport = row[13]
        deptime = row[21][0:2] + ":" + row[21][2:4]
        actdeptime = row[22][0:2] + ":" + row[22][2:4]
        altairport = row[27]
        flight_remarks= row[28]
        flight_route = row[29]
        Route_String = lat + "," + lon + ";"
        flight_duration = datetime.datetime.utcnow() - actdeptime
        flightStatus = ""


        #Deal with Personal Table
        #
        #
        if (Personal.objects.filter(cid=pilotId).exists() == False):
            newPersonal = Personal(cid = pilotId, realname = real_name, pilot_rating = user_rating)
            newPersonal.save()
        else:
            personal = Personal.objects.get(cid=pilotId)
            if (personal.pilot_rating != user_rating):
                personal.pilot_rating = user_rating

        #Deal with Flights Table
        #
        #
        if (Flights.objects.filter(date = flight_date, callsign = call_sign, cid = cid).exists() == False):
            origAirport = Airports.objects.get(icao=depairport)
            origLat = origAirport.lat
            origLon = origAirport.lon
            dist = getNmFromLatLon (lat, lon, origLat, origLon)
            newFlight = Flights(date = flight_date, callsign = call_sign, cid = pilotId, planned_aircraft = aircraft, planned_tascruise = tascruise, planned_depairport = depairport, planned_altitude = altitude, planned_destairport = destairport, planned_deptime = deptime, planned_actdepttime = actdeptime, plannedaltairport = altairport, planned_remarks = remarks, planned_route = flight_route, time_logon = timelogon, RouteString = Route_String, duration=flight_duration, total_distance = distance)
            newFlight.save()
        else:
            flight = Flights.objects.get(date = flight_date, callsign = call_sign, cid = cid)

            #Get last lat/lon
            colonCount = 0
            colon1 = 0
            colon2 = 0
            comma = 0
            for i in range(len(flight.RouteString), 0, -1):
                if ((flight.RouteString[i] == ";") and (colonCount == 0)):
                    colon1 = i
                    colonCount +=1
                elif ((flight.RouteString[i] == ",") and (colonCount == 1)):
                    comma = i
                elif ((flight.RouteString[i] == ";") and (colonCount == 1)):
                    colon2 = i
            prevLat = flight.RouteString[colon2+1:comma]
            prevLon = flight.RouteString[comma+1:colon1]

            #Update Total Distance
            flight.RouteString = F('total_distance') + getNmFromLatLon(lat, lon, prevLat, prevLon)



            #Update Route String
            flight.RouteString = F('RouteString') + Route_String
            if ((flight.outRamp == Null) and (ground_speed < 50) and (datetime.datetime.utcnow() > actdeptime)):
                flight.outRamp = updateTime
                flightStatus = "On The Ground"
            if ((flight.offGround == Null) and (ground_speed > 50)):
                flight.offGround = updateTime
                flightStatus = "Airborne"

            #Get Destination Airport Coords
            destAirport = Airports.objects.get(icao=destairport)
            destLat = destAirport.lat
            destlon = destAirport.lon

            if ((flight.onGround == Null) and (ground_speed< 50) and (getNmFromLatLon(lat, lon, destLat, destlon) < 5)):
                flight.onGround = updateTime
                flightStatus = "Arrived"
            flight.save()

        #Deal with ActiveFlights Table
        #
        #
        if (ActiveFlights.objects.filter(datetime = date_time, cid = pilotId, callsign = call_sign).exists() == False):
            if (flightStatus == ""):
                lastActive = ActiveFlights.objects.filter(cid = pilotId, callsign = call_sign).latest('datetime')
                flightStatus = lastActive.flight_status

            newActive = ActiveFlights(datetime = date_time, cid = pilotId, callsign = call_sign, clienttype = client_type, latitude = lat, longitude = lon, server  = serv, altitude = alt, groundspeed = ground_speed, transponder = transpond, heading = flight_heading, flight_status=flightStatus)
            newActive.save()

            #Delete old active flight entry
            oldActive = ActiveFlights.objects.filter(cid = pilotId, callsign = call_sign).exclude(datetime = updateTime)
            oldActive.delete()


    def atcInsert(row):
        date_time = updateTime
        _date = updateTime[0:10]
        call_sign = row[0]
        atc_id = row[1]

        user_rating = int(row[16])
        if (user_rating == 0):
            user_rating = 'Ground Controller'
        elif (user_rating == 1):
            user_rating = 'Tower Controller'
        elif (user_rating == 2):
            user_rating = 'TMA Controller'
        elif (user_rating == 3):
            user_rating = 'Enroute Controller'
        elif (user_rating == 4):
            user_rating = 'Senior Controller'

        client_type = "ATC"
        freq = row[4]
        lat = row[5]
        lon = row[6]
        serv = row[14]
        facility_type = row[18]
        if (facility_type == 0):
            facility_type = 'Other'
        elif (facility_type == 1):
            facility_type = 'Observer'
        elif (facility_type == 2):
            facility_type = 'Clearance Delivery'
        elif (facility_type == 3):
            facility_type = 'Ground'
        elif (facility_type == 4):
            facility_type = 'Tower'
        elif (facility_type == 5):
            facility_type = 'Approach/Departure'
        elif (facility_type == 6):
            facility_type = 'Center'

        visual_range = int(row[19])
        time_logon = row[36]
        date = updateTime
        totaltime = datetime.datetime.utcnow() - time_logon

        if (Personal.objects.filter(cid=atc_id).exists() == False):
            newPersonal = Personal(cid = atc_id, realname = real_name, atc_rating = user_rating)
            newPersonal.save()
        else:
            personal = Personal.objects.get(cid=atc_id)
            if (personal.atc_rating != user_rating):
                personal.atc_rating = user_rating

        if (ActiveControllers.objects.filter(cid=atc_id, callsign=call_sign, datetime=date_time).exists == False):
            newActiveATC = ActiveControllers(datetime=date_time, callsign=call_sign, cid=atc_id, clienttype=client_type, frequency=freq, latitude=lat, longitude=lon, server=serv, facilitytype=facility_type, visualrange=visual_range, time_logon=timelogon)
            #delete old active controller
            oldActiveATC = ActiveControllers.objects.filter(cid=atc_id, callsign = call_sign).exclude(datetime = updateTime)
            oldActiveATC.delete()

        if (Controllers.objects.filter(date=_date, callsign= call_sign, cid = atc_id).exists() == False):
            totaltime = datetime.datetime.utcnow() - time_logon
            newController(date=_date, callsign = call_sign, cid = atc_id, facilitytype = facility_type, TotalTime = totaltime)
        else:
            controller = Controllers.objects.get(date=date, callsign= call_sign, cid=atc_id)
            controller.TotalTime = controller.TotalTime + (updateTime - controller.lastUpdate)
            controller.lastUpdate = updateTime
            controller.save()






    # CSV READING BEGINS
    #
    #
    #

    def handle(self, **options):

        client_rows = []        
        r = requests.get('http://info.vroute.net/vatsim-data.txt')
        data = r.text.splitlines()
        update = ""
        updateTime = ""
        newUpdate = True

        reader = unicode_csv_reader(data,delimiter=":")

        for row in reader:
            if (row != []):
                if "UPDATE = " in row[0]:
                        date = row[0]
                        update = date[9:]
                        updateTime = update[0:4] + "-" + update[4:6] + "-" + update[6:8] + "T" + update[8:10] + ":" + update[10:12] + ":" + update[11:13] + "+00:00"
                        if (ActiveFlights.objects.filter(datetime=updateTime).exists() == True):
                            newUpdate = False

                elif (row[0] == u'!CLIENTS'):
                    for row in reader:
                        if (row[0] == ";"):
                            break
                        client_rows.append(row)
        if (newUpdate == True):
            for row in client_rows:
                print row[16]
                if (row[3] == 'PILOT'):
                    pilotInsert(row)
                elif (row[3] == 'ATC'):
                    atcInsert(row) 

        ## Delete flights that have been missing for an hour

        notUpdated = ActiveFlights.objects.exclude(datetime = updateTime)
        for flight in notUpdated:
            if ((flight.datetime + timedelta(hours=1)) <= datetime.datetime.utcnow()):
                flight.delete()
        ## Delete missing controllers
        missingController = ActiveFlights.objects.exclude(datetime = updateTime)
        for controller in missingController:
            controller.delete()

0 个答案:

没有答案