"""
Usage:

    ./manage.py import_operators < NOC_db.csv
"""

from ..import_from_csv import ImportFromCSVCommand
from ...models import Operator


class Command(ImportFromCSVCommand):
    @staticmethod
    def get_region_id(region_id):
        if region_id in ('ADMIN', 'Admin', 'Taxi', ''):
            return 'GB'
        elif region_id in ('SC', 'YO', 'WA', 'LO'):
            return region_id[0]

        return region_id

    @staticmethod
    def is_rubbish_name(name):
        """Given an OperatorPublicName, return True if it should be
        ignored in favour of the RefNm or OpNm fields
        """
        return (
            name in ('First', 'Arriva', 'Stagecoach') or
            name.startswith('inc.') or
            name.startswith('formerly') or
            name in ('Oakwood Travel')
        )

    @classmethod
    def get_name(cls, row):
        """Given a row dictionary, returns the best-seeming name string"""
        if cls.is_rubbish_name(row['OperatorPublicName']):
            if row['RefNm'] != '':
                return row['RefNm']
            return row['OpNm']
        if row['OperatorPublicName'] != '':
            return row['OperatorPublicName']
        return row['OpNm']

    @classmethod
    def handle_row(cls, row):
        """Given a CSV row (a list), returns an Operator object"""

        operator_id = row['NOCCODE'].replace('=', '')
        if operator_id in ('TVSR', 'HBSY', 'OWML') or (operator_id == 'FMAN' and row['Duplicate'] != 'OK'):
            return None

        name = cls.get_name(row).replace('\'', '\u2019')  # Fancy apostrophe

        mode = row['Mode'].lower()
        if mode == 'ct operator':
            mode = 'community transport'
        elif mode == 'drt':
            mode = 'demand responsive transport'

        defaults = {
            'name': name.strip(),
            'vehicle_mode': mode,
            'region_id': cls.get_region_id(row['TLRegOwn']),
        }

        operator = Operator.objects.update_or_create(
            id=operator_id,
            defaults=defaults
        )
        return operator

@@ -776,20 +776,19 @@
    name 
-in (
+== 
 'Oakwood
@@ -795,16 +795,45 @@
  Travel'
+ or%0A            name.isupper(
 )%0A      
@@ -1415,16 +1415,34 @@
      if 
+(%0A                
 operator
@@ -1476,13 +1476,28 @@
 ML')
+%0A               
  or 
-(
 oper
@@ -1542,16 +1542,25 @@
  != 'OK'
+%0A        
 ):%0A     
@@ -1576,13 +1576,8 @@
 turn
- None
 %0A%0A  
@@ -1683,24 +1683,73 @@
 e'%5D.lower()%0A
+        if mode == 'airline':%0A            return%0A
         if m

Exclude airline operators, try to avoid bad operator names