def handle(self, *args, **kwargs): path = args[0] for instance in glob.glob(os.path.join(path, "*")): xml_files = glob.glob(os.path.join(instance, "*.xml")) if len(xml_files) < 1: continue # we need to figure out what to do if there are multiple # xml files in the same folder. xml_file = viewer_tools.django_file(xml_files[0], field_name="xml_file", content_type="text/xml") images = [] for jpg in glob.glob(os.path.join(instance, "*.jpg")): images.append( viewer_tools.django_file(jpg, field_name="image", content_type="image/jpeg")) try: models.get_or_create_instance(xml_file, images) except viewer_tools.MyError, e: print e # close the files xml_file.close() for i in images: i.close()
def handle(self, *args, **kwargs): path = args[0] for instance in glob.glob( os.path.join(path, "*") ): xml_files = glob.glob( os.path.join(instance, "*.xml") ) if len(xml_files)<1: continue # we need to figure out what to do if there are multiple # xml files in the same folder. xml_file = viewer_tools.django_file(xml_files[0], field_name="xml_file", content_type="text/xml") images = [] for jpg in glob.glob(os.path.join(instance, "*.jpg")): images.append( viewer_tools.django_file(jpg, field_name="image", content_type="image/jpeg") ) try: models.get_or_create_instance(xml_file, images) except viewer_tools.MyError, e: print e # close the files xml_file.close() for i in images: i.close()
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 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"))