Example #1
0
 def _update_or_create(cls, malt_dict):
     try:
         # try update
         unique_field_dict = {
             k: v
             for (k, v) in malt_dict.iteritems()
             if k in MALTRow.get_unique_fields()
         }
         prev_obj = MALTRow.objects.get(**unique_field_dict)
         for k, v in malt_dict.iteritems():
             setattr(prev_obj, k, v)
         prev_obj.save()
     except MALTRow.DoesNotExist:
         # create
         try:
             MALTRow(**malt_dict).save()
         except Exception as ex:
             logger.error(
                 "Failed to insert malt-row {}. Exception is {}".format(
                     str(malt_dict), str(ex)),
                 exc_info=True)
     except Exception as ex:
         logger.error(
             "Failed to insert malt-row {}. Exception is {}".format(
                 str(malt_dict), str(ex)),
             exc_info=True)
Example #2
0
def _save_malt_row_dicts_to_db(malt_row_dicts):
    try:
        MALTRow.objects.bulk_create(
            [MALTRow(**malt_dict) for malt_dict in malt_row_dicts]
        )
    except IntegrityError:
        for malt_row_dict in malt_row_dicts:
            _update_or_create_malt_row(malt_row_dict)
Example #3
0
 def _save_to_db(cls, malt_rows_to_save, domain_id):
     try:
         MALTRow.objects.bulk_create(
             [MALTRow(**malt_dict) for malt_dict in malt_rows_to_save]
         )
     except IntegrityError:
         # no update_or_create in django-1.6
         for malt_dict in malt_rows_to_save:
             cls._update_or_create(malt_dict)
     except Exception as ex:
         logger.error("Failed to insert rows for domain with id {id}. Exception is {ex}".format(
                      id=domain_id, ex=str(ex)), exc_info=True)
Example #4
0
 def handle(self, *args, **options):
     for arg in args:
         with open(arg, 'r') as file:
             rows = []
             reader = csv.reader(file)
             header_row = True
             for row in reader:
                 if header_row:
                     headers = row
                     header_row = False
                 else:
                     rows.append({headers[index]: item for index, item in enumerate(row)})
             MALTRow.objects.bulk_create(
                 [MALTRow(**malt_dict) for malt_dict in rows]
             )