def handle(self, *args, **options): try: xls_filepath = args[0] except IndexError: raise CommandError(_("You must provide the path to the xls file.")) # make sure path exists if not os.path.exists(xls_filepath): raise CommandError( _("The xls file '%s' does not exist.") % xls_filepath) try: username = args[1] except IndexError: raise CommandError( _("You must provide the username to publish the form to.")) # make sure user exists try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandError(_("The user '%s' does not exist.") % username) # wasteful but we need to get the id_string beforehand survey = create_survey_from_xls(xls_filepath) # check if a form with this id_string exists for this user form_already_exists = XForm.objects.filter( user=user, id_string=survey.id_string).count() > 0 # id_string of form to replace, if any id_string = None if form_already_exists: if options.has_key('replace') and options['replace']: id_string = survey.id_string self.stdout.write(_("Form already exist, replacing ..\n")) else: raise CommandError( _("The form with id_string '%s' already exists, use the -r option to replace it." ) % survey.id_string) else: self.stdout.write(_("Form does NOT exist, publishing ..\n")) # publish xls_file = django_file(xls_filepath, 'xls_file', 'application/vnd.ms-excel') dd = publish_xls_form(xls_file, user, id_string) self.stdout.write(_("Done..\n"))
def publish(self, user, id_string=None): if self.is_valid(): # If a text (csv) representation of the xlsform is present, # this will save the file and pass it instead of the 'xls_file' # field. if 'text_xls_form' in self.cleaned_data\ and self.cleaned_data['text_xls_form'].strip(): csv_data = self.cleaned_data['text_xls_form'] # assigning the filename to a random string (quick fix) rand_name = "uploaded_form_%s.csv" % ''.join( random.sample("abcdefghijklmnopqrstuvwxyz0123456789", 6)) cleaned_xls_file = \ default_storage.save( upload_to(None, rand_name, user.username), ContentFile(csv_data)) else: cleaned_xls_file = self.cleaned_data['xls_file'] if cleaned_xls_file and not settings.TESTING_MODE: #We need to save it here so if the file already exists we get the _N filename cleaned_xls_file = default_storage.save(\ cleaned_xls_file.name, \ ContentFile(cleaned_xls_file.read())) if not cleaned_xls_file: cleaned_url = self.cleaned_data['xls_url'] if cleaned_url.strip() == u'': cleaned_url = self.cleaned_data['dropbox_xls_url'] cleaned_xls_file = urlparse(cleaned_url) cleaned_xls_file = \ '_'.join(cleaned_xls_file.path.split('/')[-2:]) if cleaned_xls_file[-4:] != '.xls': cleaned_xls_file += '.xls' cleaned_xls_file = \ upload_to(None, cleaned_xls_file, user.username) self.validate(cleaned_url) xls_data = ContentFile(urllib2.urlopen(cleaned_url).read()) cleaned_xls_file = \ default_storage.save(cleaned_xls_file, xls_data) # publish the xls return publish_xls_form(cleaned_xls_file, user, id_string)
def publish(self, user, id_string=None): if self.is_valid(): cleaned_xls_file = self.cleaned_data['xls_file'] if not cleaned_xls_file: cleaned_url = self.cleaned_data['xls_url'] cleaned_xls_file = urlparse(cleaned_url) cleaned_xls_file = \ '_'.join(cleaned_xls_file.path.split('/')[-2:]) if cleaned_xls_file[-4:] != '.xls': cleaned_xls_file += '.xls' cleaned_xls_file = \ upload_to(None, cleaned_xls_file, user.username) self.validate(cleaned_url) xls_data = ContentFile(urllib2.urlopen(cleaned_url).read()) cleaned_xls_file = \ default_storage.save(cleaned_xls_file, xls_data) # publish the xls return publish_xls_form(cleaned_xls_file, user, id_string)
def handle(self, *args, **options): try: xls_filepath = args[0] except IndexError: raise CommandError(_("You must provide the path to the xls file.")) # make sure path exists if not os.path.exists(xls_filepath): raise CommandError( _("The xls file '%s' does not exist.") % xls_filepath) try: username = args[1] except IndexError: raise CommandError(_("You must provide the username to publish the form to.")) # make sure user exists try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandError(_("The user '%s' does not exist.") % username) # wasteful but we need to get the id_string beforehand survey = create_survey_from_xls(xls_filepath) # check if a form with this id_string exists for this user form_already_exists = XForm.objects.filter(user=user, id_string=survey.id_string).count() > 0 # id_string of form to replace, if any id_string = None if form_already_exists: if options.has_key('replace') and options['replace']: id_string = survey.id_string self.stdout.write(_("Form already exist, replacing ..\n")) else: raise CommandError(_("The form with id_string '%s' already exists, use the -r option to replace it.") % survey.id_string) else: self.stdout.write(_("Form does NOT exist, publishing ..\n")) # publish xls_file = django_file(xls_filepath, 'xls_file', 'application/vnd.ms-excel') dd = publish_xls_form(xls_file, user, id_string) self.stdout.write(_("Done..\n"))
def publish(self, user, id_string=None): if self.is_valid(): cleaned_xls_file = self.cleaned_data['xls_file'] if not cleaned_xls_file: cleaned_url = self.cleaned_data['xls_url'] if cleaned_url.strip() == u'': cleaned_url = self.cleaned_data['dropbox_xls_url'] cleaned_xls_file = urlparse(cleaned_url) cleaned_xls_file = \ '_'.join(cleaned_xls_file.path.split('/')[-2:]) if cleaned_xls_file[-4:] != '.xls': cleaned_xls_file += '.xls' cleaned_xls_file = \ upload_to(None, cleaned_xls_file, user.username) self.validate(cleaned_url) xls_data = ContentFile(urllib2.urlopen(cleaned_url).read()) cleaned_xls_file = \ default_storage.save(cleaned_xls_file, xls_data) # publish the xls return publish_xls_form(cleaned_xls_file, user, id_string)
def publish(self, user, id_string=None): if self.is_valid(): # If a text (csv) representation of the xlsform is present, # this will save the file and pass it instead of the 'xls_file' # field. if 'text_xls_form' in self.cleaned_data\ and self.cleaned_data['text_xls_form'].strip(): csv_data = self.cleaned_data['text_xls_form'] # assigning the filename to a random string (quick fix) import random rand_name = "uploaded_form_%s.csv" % ''.join( random.sample("abcdefghijklmnopqrstuvwxyz0123456789", 6)) cleaned_xls_file = \ default_storage.save( upload_to(None, rand_name, user.username), ContentFile(csv_data)) else: cleaned_xls_file = self.cleaned_data['xls_file'] if not cleaned_xls_file: cleaned_url = self.cleaned_data['xls_url'] if cleaned_url.strip() == u'': cleaned_url = self.cleaned_data['dropbox_xls_url'] cleaned_xls_file = urlparse(cleaned_url) cleaned_xls_file = \ '_'.join(cleaned_xls_file.path.split('/')[-2:]) if cleaned_xls_file[-4:] != '.xls': cleaned_xls_file += '.xls' cleaned_xls_file = \ upload_to(None, cleaned_xls_file, user.username) self.validate(cleaned_url) xls_data = ContentFile(urllib2.urlopen(cleaned_url).read()) cleaned_xls_file = \ default_storage.save(cleaned_xls_file, xls_data) # publish the xls return publish_xls_form(cleaned_xls_file, user, id_string)