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


class Command(ImportFromCSVCommand):
    scotch_operators = {
        operator['code']: operator
        for operator in requests.get('http://www.travelinescotland.com/lts/operatorList').json()['body']
    }

    @classmethod
    def handle_row(cls, row):
        if row['SC']:
            scotch = cls.scotch_operators.get(row['SC'])
            if scotch and len(row['NOCCODE']) == 4:
                operator = Operator.objects.get(pk=row['NOCCODE'])
                operator.name = scotch['name']
                operator.address = scotch['address']
                operator.url = scotch['url']
                operator.email = scotch['email']
                operator.phone = scotch['phone']
                operator.save()

    @staticmethod
    def process_rows(rows):
        return sorted(rows, reverse=True,
                      key=lambda r: (r['Duplicate'] != 'OK', r['Date Ceased']))


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


class Command(ImportFromCSVCommand):
    scotch_operators = {
        operator['code']: operator
        for operator in requests.get('http://www.travelinescotland.com/lts/operatorList').json()['body']
    }

    @classmethod
    def handle_row(cls, row):
        if row['SC']:
            scotch = cls.scotch_operators.get(row['SC'])
            if scotch and len(row['NOCCODE']) == 4:
                operator = Operator.objects.filter(pk=row['NOCCODE']).first()
                if not operator:
                    return
                operator.name = scotch['name']
                operator.address = scotch['address']
                operator.url = scotch['url']
                operator.email = scotch['email']
                operator.phone = scotch['phone']
                operator.save()

    @staticmethod
    def process_rows(rows):
        return sorted(rows, reverse=True,
                      key=lambda r: (r['Duplicate'] != 'OK', r['Date Ceased']))

Fix scotch operator contact import
Fix scotch operator contact import

  
  import requests
  from ..import_from_csv import ImportFromCSVCommand
  from ...models import Operator
  
  
  class Command(ImportFromCSVCommand):
      scotch_operators = {
          operator['code']: operator
          for operator in requests.get('http://www.travelinescotland.com/lts/operatorList').json()['body']
      }
  
      @classmethod
      def handle_row(cls, row):
          if row['SC']:
              scotch = cls.scotch_operators.get(row['SC'])
              if scotch and len(row['NOCCODE']) == 4:
-                 operator = Operator.objects.get(pk=row['NOCCODE'])
+                 operator = Operator.objects.filter(pk=row['NOCCODE']).first()
+                 if not operator:
+                     return
                  operator.name = scotch['name']
                  operator.address = scotch['address']
                  operator.url = scotch['url']
                  operator.email = scotch['email']
                  operator.phone = scotch['phone']
                  operator.save()
  
      @staticmethod
      def process_rows(rows):
          return sorted(rows, reverse=True,
                        key=lambda r: (r['Duplicate'] != 'OK', r['Date Ceased']))
  
Fix scotch operator contact import
## Code Before:

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


class Command(ImportFromCSVCommand):
    scotch_operators = {
        operator['code']: operator
        for operator in requests.get('http://www.travelinescotland.com/lts/operatorList').json()['body']
    }

    @classmethod
    def handle_row(cls, row):
        if row['SC']:
            scotch = cls.scotch_operators.get(row['SC'])
            if scotch and len(row['NOCCODE']) == 4:
                operator = Operator.objects.get(pk=row['NOCCODE'])
                operator.name = scotch['name']
                operator.address = scotch['address']
                operator.url = scotch['url']
                operator.email = scotch['email']
                operator.phone = scotch['phone']
                operator.save()

    @staticmethod
    def process_rows(rows):
        return sorted(rows, reverse=True,
                      key=lambda r: (r['Duplicate'] != 'OK', r['Date Ceased']))

## Instruction:
Fix scotch operator contact import
## Code After:

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


class Command(ImportFromCSVCommand):
    scotch_operators = {
        operator['code']: operator
        for operator in requests.get('http://www.travelinescotland.com/lts/operatorList').json()['body']
    }

    @classmethod
    def handle_row(cls, row):
        if row['SC']:
            scotch = cls.scotch_operators.get(row['SC'])
            if scotch and len(row['NOCCODE']) == 4:
                operator = Operator.objects.filter(pk=row['NOCCODE']).first()
                if not operator:
                    return
                operator.name = scotch['name']
                operator.address = scotch['address']
                operator.url = scotch['url']
                operator.email = scotch['email']
                operator.phone = scotch['phone']
                operator.save()

    @staticmethod
    def process_rows(rows):
        return sorted(rows, reverse=True,
                      key=lambda r: (r['Duplicate'] != 'OK', r['Date Ceased']))
