Exemplo n.º 1
0
 def form_valid(self, request, form):
     """
     Sends the csv file to be processed
     """
     # probably needs to make a stringio object out of it
     csv_file = form.cleaned_data['csv_file']
     category = form.cleaned_data['category']
     csv_results = locations_from_csv(csv_file, category)
     if csv_results['errors']:
         messages.add_message(request, messages.ERROR,
                              'There was an error.')
     else:
         if csv_results['created_count']:
             messages.add_message(
                 request, messages.SUCCESS,
                 "%s new locations added" % csv_results['created_count'])
         if csv_results['skipped_count']:
             messages.add_message(
                 request, messages.WARNING,
                 "%s duplicates skipped" % csv_results['skipped_count'])
     if "_edit" in request.POST:
         if csv_results['created_count'] == 0:
             messages.add_message(request, messages.WARNING,
                                  "There are no new locations to edit")
         return HttpResponseRedirect('%s?upload_count=%s' %
                                     (self.get_success_url(),
                                      (csv_results['upload_count'])))
     return self.render_to_response(
         self.get_context_data(form=form, csv_results=csv_results))
Exemplo n.º 2
0
 def form_valid(self, request, form):
     """
     Sends the csv file to be processed
     """
     # probably needs to make a stringio object out of it
     csv_file = form.cleaned_data['csv_file']
     category = form.cleaned_data['category']
     csv_results = locations_from_csv(csv_file, category)
     if csv_results['errors']:
         messages.add_message(request, messages.ERROR, 'There was an error.')
     else:
         if csv_results['created_count']:
             messages.add_message(request, messages.SUCCESS,
                     "%s new locations added" % csv_results['created_count'])
         if csv_results['skipped_count']:
             messages.add_message(request, messages.WARNING,
                     "%s duplicates skipped" % csv_results['skipped_count'])
     if "_edit" in request.POST:
         if csv_results['created_count'] == 0:
             messages.add_message(request, messages.WARNING,
                     "There are no new locations to edit")
         return HttpResponseRedirect('%s?upload_count=%s' % (
             self.get_success_url(), (csv_results['upload_count'])))
     return self.render_to_response(self.get_context_data(
             form=form, csv_results=csv_results))
    def handle(self, *args, **options):
        """
        Open the file requested by the user, process any passed options, and
        send the data to the utility functions for processing.
        """
        # Get the file name and read any options
        try:
            file_name = args[0]
        except IndexError:
            raise CommandError("You need to provide the file name")
        categories = LocationCategory.objects.all()
        if len(categories) == 0:
            raise CommandError("""
                You must first create some location
                categories""")
        duplicates_field = options.get('duplicates_field')
        has_header = options.get('has_header')

        # Presuming we do not set the category with a command line option,
        # interrogate the user for which category should be assigned to each
        # new location
        category_choice = -1
        while category_choice not in range(len(categories)):
            self.stdout.write("Please pick an initial location category\r\n\r\n",)
            for (counter, category) in enumerate(categories):
                self.stdout.write("(%s) %s\r\n" % (counter, category))
            self.stdout.write("\r\n\r\n")
            category_choice = raw_input("Category choice: ")
            try:
                category_choice = int(category_choice)
            except ValueError:
                self.stdout.write("\r\nThat wasn't a valid choice!\r\n\r\n")
            else:
                try:
                    assert category_choice in range(len(categories))
                except AssertionError:
                    self.stdout.write("That wasn't a valid choice!\r\n")
        category = categories[category_choice]

        # Open the damn file!
        try:
            csv_file = open(file_name, 'rb')
        except IOError:
            raise CommandError(
                    "There was a problem reading the file, %s" % file_name)
        results = locations_from_csv(csv_file, category, has_header=has_header,
                duplicates_field=duplicates_field)
        self.stdout.write("----------------------------\r\n")
        if results['errors']:
            self.stdout.write("There were errors!\r\n")
            for warning in results['warnings']:
                self.stdout.write("%s\r\n" % warning)
            self.stdout.write("\r\n")
        else:
            self.stdout.write("No errors reported\r\n")
            self.stdout.write("%s locations created\r\n" % len(results['created']))
            self.stdout.write("%s duplicates skipped\r\n" % len(results['skipped']))
        self.stdout.write("----------------------------\r\n")
Exemplo n.º 4
0
    def handle(self, *args, **options):
        """
        Open the file requested by the user, process any passed options, and
        send the data to the utility functions for processing.
        """
        # Get the file name and read any options
        try:
            file_name = args[0]
        except IndexError:
            raise CommandError("You need to provide the file name")
        categories = LocationCategory.objects.all()
        if len(categories) == 0:
            raise CommandError("""
                You must first create some location
                categories""")
        duplicates_field = options.get('duplicates_field')
        has_header = options.get('has_header')

        # Presuming we do not set the category with a command line option,
        # interrogate the user for which category should be assigned to each
        # new location
        category_choice = -1
        while category_choice not in range(len(categories)):
            self.stdout.write(
                "Please pick an initial location category\r\n\r\n", )
            for (counter, category) in enumerate(categories):
                self.stdout.write("(%s) %s\r\n" % (counter, category))
            self.stdout.write("\r\n\r\n")
            category_choice = raw_input("Category choice: ")
            try:
                category_choice = int(category_choice)
            except ValueError:
                self.stdout.write("\r\nThat wasn't a valid choice!\r\n\r\n")
            else:
                try:
                    assert category_choice in range(len(categories))
                except AssertionError:
                    self.stdout.write("That wasn't a valid choice!\r\n")
        category = categories[category_choice]

        # Open the damn file!
        try:
            csv_file = open(file_name, 'rb')
        except IOError:
            raise CommandError("There was a problem reading the file, %s" %
                               file_name)
        results = locations_from_csv(csv_file,
                                     category,
                                     has_header=has_header,
                                     duplicates_field=duplicates_field)
        self.stdout.write("----------------------------\r\n")
        if results['errors']:
            self.stdout.write("There were errors!\r\n")
            for warning in results['warnings']:
                self.stdout.write("%s\r\n" % warning)
            self.stdout.write("\r\n")
        else:
            self.stdout.write("No errors reported\r\n")
            self.stdout.write("%s locations created\r\n" %
                              len(results['created']))
            self.stdout.write("%s duplicates skipped\r\n" %
                              len(results['skipped']))
        self.stdout.write("----------------------------\r\n")