Advertisement
hellsgate

csv file model

Aug 9th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. class CustomerCsvFile(models.Model):
  2.     source = models.CharField(max_length=100, blank=True)
  3.     csv_file = models.FileField(upload_to='csvimport/customer/', max_length=255)
  4.     uploaded_by = models.ForeignKey(UserProfile)
  5.     date_uploaded = models.DateField(auto_now_add=True)
  6.     date_imported = models.DateField(blank=True, null=True)
  7.     field_order = models.TextField(blank=True)
  8.     has_headings = models.BooleanField(default=True, verbose_name='First line is headings')
  9.     reimport_filename = models.CharField(max_length=255, blank=True)
  10.     parent = models.ForeignKey('self', blank=True, null=True)
  11.     celery_task_id = models.CharField(max_length=255, blank=True)
  12.  
  13.     def __unicode__(self):
  14.         return self.filename
  15.  
  16.     def _get_filename(self):
  17.         return Path(self.csv_file.path).name
  18.     filename = property(_get_filename)
  19.  
  20.     def _get_first_line(self):
  21.         if Path(self.csv_file.path).exists():
  22.             with open(self.csv_file.path, 'rb') as f:
  23.                 r = csv.reader(f)
  24.                 if self.has_headings:
  25.                     # skip the first line if it is headings
  26.                     r.next()
  27.                 first_line = r.next()
  28.                 f.close()
  29.                 return first_line
  30.         else:
  31.             return False
  32.     first_line = property(_get_first_line)
  33.  
  34.     def _file_exists(self):
  35.         return Path(self.csv_file.path).exists()
  36.     file_exists = property(_file_exists)
  37.  
  38.     def get_lines(self, num_lines):
  39.         if Path(self.csv_file.path).exists():
  40.             #csv_file = self.csv_file_object
  41.             rows = []
  42.             r = csv.reader(self.csv_file_object)
  43.             if self.has_headings:
  44.                 # skip the first line if it is headings
  45.                 r.next()
  46.             # We're going to work under the assumption that the first empty row
  47.             # indicates the end of the file and the loop should therefore end
  48.             for i in range(num_lines):
  49.                 try:
  50.                     row = r.next()
  51.                 except StopIteration:
  52.                     break
  53.                 if not any(row):
  54.                     break
  55.                 rows.append(row)
  56.             self.csv_file_object.close()
  57.             return rows
  58.  
  59.         return False
  60.  
  61.     def _get_file(self):
  62.         if Path(self.csv_file.path).exists():
  63.             f = open(self.csv_file.path, 'rb')
  64.             return f
  65.  
  66.         return False
  67.     csv_file_object = property(_get_file)
  68.  
  69.     def _get_best_row(self):
  70.         '''
  71.        Grab the row which has the most columns completed to allow for the most
  72.        comprehensive initial data assignation
  73.        '''
  74.         r = csv.reader(self.csv_file_object)
  75.         if self.has_headings:
  76.             # skip the first line if it is headings
  77.             r.next()
  78.  
  79.         current_counter = 0
  80.         biggest_row = []
  81.         for row in r:
  82.             counter = 0
  83.             # Check that the line isn't empty
  84.             if any(row):
  85.                 '''
  86.                For some reason some of the CSV exports have "\N" as a field
  87.                value. Don't count those fields, or the fields which are empty,
  88.                in the count
  89.                '''
  90.                 for value in row:
  91.                     if value.replace('\N', '').strip() != '':
  92.                         counter += 1
  93.  
  94.                 if (counter > current_counter):
  95.                     current_counter = counter
  96.                     biggest_row = row
  97.  
  98.         return biggest_row
  99.     best_row = property(_get_best_row)
  100.  
  101.     def _get_row_count(self):
  102.         r = csv.reader(self.csv_file_object)
  103.         if self.has_headings:
  104.             # skip the first line if it is headings
  105.             r.next()
  106.         counter = 0
  107.         for row in r:
  108.             if any(row):
  109.                 counter += 1
  110.  
  111.         self.csv_file_object.seek(0)
  112.         return counter
  113.     row_count = property(_get_row_count)
  114.  
  115.     def _get_csv_fields(self):
  116.         if self.field_order != '':
  117.             field_list = json.loads(self.field_order)
  118.             field_dict = {}
  119.             for counter, item in enumerate(field_list):
  120.                 if (item['field_name'] != 'unknown' and
  121.                     item['field_name'].replace('\N', '').strip() is not ''):
  122.                     field_dict[item['field_name']] = counter
  123.  
  124.             return field_dict
  125.  
  126.         return {}
  127.     csv_fields = property(_get_csv_fields)
  128.  
  129.     def _get_parent_field_order(self):
  130.         if self.parent is not None:
  131.             return self.parent.field_order
  132.         else:
  133.             return False
  134.     parent_field_order = property(_get_parent_field_order)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement