import csv
from django.core.management.base import BaseCommand
from frontend.models import PCT, STP


class Command(BaseCommand):
    help = "Imports CCG/PCT names and details from HSCIC organisational data. "
    help += "You should import CCG boundaries BEFORE running this."  # Why?!

    def add_arguments(self, parser):
        parser.add_argument("--ccg")

    def handle(self, **options):
        for row in csv.reader(open(options["ccg"], "rU")):
            row = [r.strip() for r in row]
            if row[2] == "Y99" or row[3] == "Q99":
                # This indicates a National Commissioning Hub which does not
                # belong to a region, and which in any case we ignore.
                continue

            ccg, created = PCT.objects.get_or_create(code=row[0])
            if created:
                ccg.name = row[1]
            ccg.regional_team_id = row[2]
            ccg.stp = STP.objects.get_or_create(code=row[3], name=f"ICB {row[3]}")[0]
            ccg.address = ", ".join([r for r in row[4:9] if r])
            ccg.postcode = row[9]
            od = row[10]
            ccg.open_date = od[:4] + "-" + od[4:6] + "-" + od[-2:]
            cd = row[11]
            if cd:
                ccg.close_date = cd[:4] + "-" + cd[4:6] + "-" + cd[-2:]
            if row[13] == "C":
                ccg.org_type = "CCG"
            ccg.save()

@@ -806,16 +806,186 @@
 reated:%0A
+                # For existing CCGs, we don't want to take the name in eccg.csv, since%0A                # it's a meaningless combination of the ICB name and the CCG code.%0A
         
@@ -1071,16 +1071,19 @@
  ccg.stp
+, _
  = STP.o
@@ -1095,32 +1095,49 @@
 s.get_or_create(
+%0A                
 code=row%5B3%5D, nam
@@ -1137,13 +1137,26 @@
 3%5D, 
+defaults=%7B%22
 name
-=
+%22: 
 f%22IC
@@ -1170,12 +1170,23 @@
 3%5D%7D%22
-)%5B0%5D
+%7D%0A            )
 %0A   

Fix import_ccgs