Ejemplo n.º 1
0
 def import_notes(self, lines):
     source_domain = self.auth.domain_write_permission
     records = importer.utf8_decoder(generate_note_record_ids(convert_time_fields(csv.reader(lines))))
     try:
         records = [complete_record_ids(r, source_domain) for r in records]
     except csv.Error, e:
         self.error(400, message="The CSV file is formatted incorrectly. (%s)" % e)
         return
Ejemplo n.º 2
0
 def import_notes(self, lines):
     source_domain = self.auth.domain_write_permission
     records = importer.utf8_decoder(generate_note_record_ids(
         convert_time_fields(csv.reader(lines))))
     try:
         records = [complete_record_ids(r, source_domain) for r in records]
     except csv.Error, e:
         self.error(400, message=
             'The CSV file is formatted incorrectly. (%s)' % e)
         return
Ejemplo n.º 3
0
    def import_persons(self, lines):
        # TODO(ryok): support non-UTF8 encodings.

        source_domain = self.auth.domain_write_permission
        records = importer.utf8_decoder(convert_time_fields(csv.reader(lines)))
        try:
            records = [complete_record_ids(r, source_domain) for r in records]
        except csv.Error, e:
            self.error(400, message=
                'The CSV file is formatted incorrectly. (%s)' % e)
            return
Ejemplo n.º 4
0
def import_from_file(host, kind, converter, filename):
  print '%s: importing %s records from %s' % (host, kind, filename)
  written, skipped, total = importer.import_records(
      source_domain, converter,
      importer.utf8_decoder(csv.DictReader(open(filename))))
  for error, record in skipped[:SHOW_ERRORS]:
    print '  - %s: %r' % (error, record)
  if len(skipped) > SHOW_ERRORS:
    print '  (more errors not shown)'

  print 'wrote %d of %d (skipped %d with errors)' % (
      written, total, len(skipped))
Ejemplo n.º 5
0
def import_from_file(host, repo, kind, converter, filename):
    print('%s: importing %s records from %s' % (host, kind, filename))
    written, skipped, total = importer.import_records(
        repo, source_domain, converter,
        importer.utf8_decoder(csv.DictReader(open(filename))))
    for error, record in skipped[:SHOW_ERRORS]:
        print('    - %s: %r' % (error, record))
    if len(skipped) > SHOW_ERRORS:
        print('    (more errors not shown)')

    print('wrote %d of %d (skipped %d with errors)' %
          (written, total, len(skipped)))
Ejemplo n.º 6
0
    def post(self):
        if not (self.auth and self.auth.domain_write_permission):
            self.response.set_status(403)
            # TODO(ryok): i18n
            self.write('Missing or invalid authorization key.')
            return

        content = self.request.get('content')
        if not content:
            self.response.set_status(400)
            self.write('You need to specify at least one CSV file.')
            return

        # TODO(ryok): let the user select timezone.
        # TODO(ryok): accept more flexible date time format.
        # TODO(ryok): support non-UTF8 encodings.

        source_domain = self.auth.domain_write_permission
        records = importer.utf8_decoder(
                csv.DictReader(StringIO.StringIO(content)))
        try:
            records = [complete_record_ids(r, source_domain) for r in records]
        except csv.Error:
            self.response.set_status(400)
            self.write('The CSV file is formatted incorrectly.')
            return

        is_empty = lambda x: (x or '').strip()
        persons = [r for r in records if is_empty(r.get('full_name'))]
        notes = [r for r in records if is_empty(r.get('note_record_id'))]

        people_written, people_skipped, people_total = importer.import_records(
            self.repo, source_domain, importer.create_person, persons)
        notes_written, notes_skipped, notes_total = importer.import_records(
            self.repo, source_domain, importer.create_note, notes)

        utils.log_api_action(self, ApiActionLog.WRITE,
                             people_written, notes_written,
                             len(people_skipped), len(notes_skipped))

        self.render('import.html',
                    stats=[
                        Struct(type='Person',
                               written=people_written,
                               skipped=people_skipped,
                               total=people_total),
                        Struct(type='Note',
                               written=notes_written,
                               skipped=notes_skipped,
                               total=notes_total)],
                    **get_tag_params(self))