def _prepare_data(self, csv_filename, delimiter, tmpl): f = file(csv_filename, "rb") csvdata = f.read() f.close() fieldnames = get_params_from_template(tmpl) m = CSVManager(fieldnames=fieldnames, delimiter=delimiter, encoding=ENCODING) data = m.read(csvdata) log.debug(pprint(m.read(csvdata))) return data
def _write_data(self, csv_filename, delimiter, csvdata, tmpl): print "self.simulate", self.simulate if(self.simulate): log.debug(pprint("SIMULATING write. Content is: %s" % csvdata)) else: log.debug(pprint("WRITING on file %s. Content is: %s" % (csv_filename,csvdata))) f = file(csv_filename, "wb") fieldnames = get_params_from_template(tmpl) m = CSVManager(fieldnames=fieldnames, delimiter=delimiter, encoding=ENCODING) data = m.write(csvdata) #log.debug(pprint(m.read(csvdata))) f.write(data) f.close() return
def _write_data(self, csv_filename, delimiter, csvdata, tmpl): print "self.simulate", self.simulate if (self.simulate): log.debug(pprint("SIMULATING write. Content is: %s" % csvdata)) else: log.debug( pprint("WRITING on file %s. Content is: %s" % (csv_filename, csvdata))) f = file(csv_filename, "wb") fieldnames = get_params_from_template(tmpl) m = CSVManager(fieldnames=fieldnames, delimiter=delimiter, encoding=ENCODING) data = m.write(csvdata) #log.debug(pprint(m.read(csvdata))) f.write(data) f.close() return
def handle(self, *args, **options): try: gas_pk = int(args[0]) csv_filename = args[1] except: raise CommandError("Usage import_gasmembers: %s" % (self.args)) if len(args) > 2: delimiter = args[2] else: delimiter = ";" if len(args) == 4: tmpl = args[3] else: tmpl = "%(name)s %(surname)s %(email)s %(city)s" # STEP 0: prepare data in dicts f = file(csv_filename, "rb") csvdata = f.read() f.close() fieldnames = get_params_from_template(tmpl) m = CSVManager(fieldnames=fieldnames, delimiter=delimiter, encoding=ENCODING) data = m.read(csvdata) log.debug(pprint(m.read(csvdata))) # Data prepared g = GAS.objects.get(pk=gas_pk) g.config.auto_populate_products = True g.config.save() # STEP 2: process data and create instances with transaction.commit_on_success(): for d in data: log.info("#### ---- start new user import... ----####") try: user, updated = self._get_or_create_user(d) try: pers = user.person except Person.DoesNotExist: contacts = self._get_or_create_contacts(d) place = self._get_or_create_place(d) pers = self._get_or_create_person(d, contacts, place) pers.user = user pers.save() else: # This is a user of an already created person log.info(("PERSON %s ALREADY EXISTENT" % user.person).decode(ENCODING)) if updated: log.debug("UPDATE PERSON DETAILS") contacts = self._update_contacts(user.person, d) place = self._update_place(user.person, d) pers = self._update_person(user.person, d, contacts, place, force=True) else: log.debug("SKIP IT") except KeyError, e: if e.message not in self.allowed_keys: raise CommandError("Invalid key '%s' provided. Allowed keys in python template are: %s" % (e.message, self.allowed_keys)) else: raise CommandError("Key '%s' is REQUIRED." % e.message) gm, created = GASMember.objects.get_or_create(person=pers, gas=g) gm.save() log.info(("CREATED GASMEMBER %s" % gm).decode(ENCODING))