# add extra libs to the python sys path sys.path.extend(libs) path = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(path) os.environ['RAPIDSMS_INI'] = os.path.join(path, "local.ini") os.environ['DJANGO_SETTINGS_MODULE'] = 'rapidsms.webui.settings' # import manager now that the path is correct from rapidsms import manager ### ### END - SETUP RAPIDSMS ENVIRONMENT ### from reversion import revision from childcount.models import Clinic from locations.models import Location revision.start() #the name contains hospital locations = Location.objects.filter(name__icontains='.H.C') for location in locations: clinic = Clinic(location_ptr=location) clinic.save_base(raw=True) revision.end()
def import_locations(csv_file): ''' create Location and Clinic objects from CSV file FORMAT of CSV: | Name | Code | TYPE | PARENT TYPES: - V: Village - Z: ZOne - H: Health Unit PARENT: Parent must be ID of another location ''' from locations.models import Location, LocationType from childcount.models import Clinic try: fhandler = open(csv_file) except IOError: print "Unable to open file %s" % csv_file return None # maps character code to LocationType ID in fixtures TYPES_MAP = {'V': 1, 'Z': 3, 'H': 2} for line in fhandler: data = line.strip().split(",") name = data[0].strip() code = data[1].strip().lower() or None type_code = data[2].strip() parent_id = data[3].strip() or None type_ = LocationType.objects.get(id=TYPES_MAP[type_code]) # generate code once None if not code: code = u"%(type)s_%(location)s" \ % {'type': type_.name.lower().replace(' ', '_'), \ 'location': name.lower().replace(' ', '_')} if parent_id: parent = Location.objects.get(id=parent_id) else: parent = None print "N: %s - C: %s - T: %s (%s) - P: %s (%s)" \ % (name, code, type_code, type_, parent_id, parent) # Create Clinic object if Health Unit if type_.id in (2,): location = Clinic() else: location = Location() location.type = type_ location.name = name location.code = code location.parent = parent location.save()