def perform_update(self): """ DANGER: we make use of json fields to store flexible extended data We also use the .update() methods for quick and easy updates that are less load inducing. Beware you cannot do an .update() on the Lawyer.data field. It must go via the .save() method or things get incorrectly encoded """ fields_to_update = {} if self.role: self.lawyer.role = self.role if self.data: self.data["bar_membership"] = ( json.loads(self.data.get("bar_membership", "[]")) if self.data.get("bar_membership", "[]") != "" else [] ) self.data["websites"] = ( json.loads(self.data.get("websites", "[]")) if self.data.get("websites", "[]") != "" else [] ) self.lawyer.data = self.data if self.data.get("photo", None) is not None: # pop so it does not get serialized self.save_photo(self.data.pop("photo")) # Update standard model fields fields_to_update.update(summary=self.data.get("summary", None)) fields_to_update.update(bio=self.data.get("bio", None)) # remove empty items fields_to_update = [(k, v) for k, v in fields_to_update.items() if v is not None] # perform volume matrix update lawyer_data = self.volume_matrix(lawyer_data=self.lawyer.data) # add the JSON object and perform lawyer save on that field only self.lawyer.data = lawyer_data self.lawyer.save() # Primary lawyer update query # Will always be present due to the previous get_or_create Lawyer.objects.filter(pk=self.lawyer.pk).update(**dict(fields_to_update)) logger.info("get_or_create:lawyer %s is_new: %s" % (self.lawyer.user.username, self.lawyer_is_new)) if self.firm_name is None: logger.info("Firm name not provided for lawyer %s " % (self.lawyer.user.username,)) else: logger.info('Firm name "%s" was provided for lawyer %s ' % (self.firm_name, self.lawyer.user.username)) self.data.update({"user": self.user, "lawyer": self.lawyer}) firm_service = EnsureFirmService(firm_name=self.firm_name, offices=self.offices, **self.data) firm_service.process() self.firm = firm_service.firm
def import_csv(self, csv_file): """ [''Firm Name', 'Address', 'Cities'] """ with open(csv_file, 'rb') as csv_file: print("starting firm csv") dialect = csv.Sniffer().sniff(csv_file.read(1024)) csv_file.seek(0) for i,r in enumerate(csv.reader(csv_file, dialect)): if i > 0: # unicodeify for k,v in enumerate(r): r[k] = smart_unicode(v) # get nice names firm_name, address, cities, = r address = '%s, %s' % (address, cities) firm_service = EnsureFirmService(firm_name=firm_name, offices=[address]) firm_service.process()