class WorkflowImportForm(forms.Form): # Worflow name name = forms.CharField(max_length=512, strip=True, required=True, label='Name') file = RestrictedFileField( max_upload_size=str(ontask_prefs.MAX_UPLOAD_SIZE), content_types=json.loads(str(ontask_prefs.CONTENT_TYPES)), allow_empty_file=False, label="File", help_text='File containing a previously exported workflow')
class ActionImportForm(forms.Form): # Action name name = forms.CharField(max_length=512, strip=True, required=True, label='Name') file = RestrictedFileField( max_upload_size=int(ontask_prefs.MAX_UPLOAD_SIZE), content_types=json.loads(str(ontask_prefs.CONTENT_TYPES)), allow_empty_file=False, label=_('File'), help_text=_('File containing a previously exported action'))
class UploadCSVFileForm(forms.Form): """ Form to read a csv file. It also allows to specify the number of lines to skip at the top and the bottom of the file. This functionality is offered by the underlyng function read_csv in Pandas """ file = RestrictedFileField( max_upload_size=str(ontask.ontask_prefs.MAX_UPLOAD_SIZE), content_types=json.loads(str(ontask.ontask_prefs.CONTENT_TYPES)), allow_empty_file=False, label="", help_text='File in CSV format (typically produced by a statistics' ' package or Excel)') skip_lines_at_top = forms.IntegerField( label='Lines to skip at the top', help_text="Number of lines to skip at the top when reading the file", initial=0, required=False ) skip_lines_at_bottom = forms.IntegerField( label='Lines to skip at the bottom', help_text="Number of lines to skip at the bottom when reading the " "file", initial=0, required=False ) def clean(self, *args, **kwargs): """ Function to check that the integers are positive. :return: The cleaned data """ data = super(UploadCSVFileForm, self).clean(*args, **kwargs) if data['skip_lines_at_top'] < 0: self.add_error( 'skip_lines_at_top', 'This number has to be zero or positive' ) if data['skip_lines_at_bottom'] < 0: self.add_error( 'skip_lines_at_bottom', 'This number has to be zero or positive' ) return data
class UploadExcelFileForm(forms.Form): """ Form to read an Excel file. """ file = RestrictedFileField( max_upload_size=str(ontask.ontask_prefs.MAX_UPLOAD_SIZE), content_types=[ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ], allow_empty_file=False, label="", help_text='File in Excel format (.xls or .xlsx)') sheet = forms.CharField(max_length=512, required=True, initial='Sheet 1')
class WorkflowImportForm(forms.Form): """Form to import a workflow (processing name and file).""" name = forms.CharField( max_length=CHAR_FIELD_LENGTH, strip=True, required=False, initial='', label='Name (leave empty to take the name stored in the file)') wf_file = RestrictedFileField( max_upload_size=int(ontask_prefs.MAX_UPLOAD_SIZE), content_types=json.loads(str(ontask_prefs.CONTENT_TYPES)), allow_empty_file=False, label=_('File'), help_text=_('File containing a previously exported workflow')) def __init__(self, data, *args, **kwargs): """Store the user that prompted the request.""" self.user = kwargs.pop('user', None) super().__init__(data, *args, **kwargs) def clean(self): """Check that the name is unique and form multipart.""" form_data = super().clean() if not self.is_multipart(): self.add_error( None, _('Incorrect form request (it is not multipart)'), ) # Check if the name already exists name_exists = Workflow.objects.filter( user=self.user, name=self.cleaned_data['name']).exists() if name_exists: self.add_error( 'name', _('A workflow with this name already exists'), ) return form_data
class UploadExcelFileForm(UploadBasic): """ Form to read an Excel file. """ file = RestrictedFileField( max_upload_size=int(ontask.ontask_prefs.MAX_UPLOAD_SIZE), content_types=[ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ], allow_empty_file=False, label="", help_text=_('File in Excel format (.xls or .xlsx)')) sheet = forms.CharField( max_length=512, required=True, initial='', help_text=_('Sheet within the excelsheet to upload')) def clean(self): """ Function to check that the integers are positive. :return: The cleaned data """ data = super(UploadExcelFileForm, self).clean() # # Process Excel file using pandas read_excel try: self.data_frame = pandas_db.load_df_from_excelfile( self.files['file'], data['sheet'] ) except Exception as e: self.add_error('file', _('File could not be processed ({0})').format(e)) return data # Check the conditions in the data frame self.clean_data_frame() return data
class UploadExcelFileForm(UploadBasic): """Form to read an Excel file.""" data_file = RestrictedFileField( max_upload_size=int(ontask_prefs.MAX_UPLOAD_SIZE), content_types=[ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.' + 'spreadsheetml.sheet', ], allow_empty_file=False, label='', help_text=_('File in Excel format (.xls or .xlsx)')) sheet = forms.CharField( max_length=CHAR_FIELD_LENGTH, required=True, initial='', help_text=_('Sheet within the excelsheet to upload')) def clean(self): """Check that the data can be loaded from the file. :return: The cleaned data """ form_data = super().clean() # # Process Excel file using pandas read_excel try: self.data_frame = load_df_from_excelfile(self.files['data_file'], form_data['sheet']) except Exception as exc: self.add_error( 'text_file', _('File could not be processed: {0}').format(str(exc))) return form_data # Check the validity of the data frame self.validate_data_frame() return form_data
class ActionImportForm(forms.Form): """Form to edit information to import an action.""" # Action name name = forms.CharField( max_length=ACTION_NAME_LENGTH, strip=True, required=True, label='Name', ) upload_file = RestrictedFileField( max_upload_size=int(ontask_prefs.MAX_UPLOAD_SIZE), content_types=json.loads(str(ontask_prefs.CONTENT_TYPES)), allow_empty_file=False, label=_('File'), help_text=_('File containing a previously exported action'), ) def __init__(self, form_data, *args, **kwargs): """Store workflow and user parameters.""" self.workflow = kwargs.pop('workflow') self.user = kwargs.pop('user') super().__init__(form_data, *args, **kwargs) def clean(self): """Verify that the name of the action is not present already.""" form_data = super().clean() if self.workflow.actions.filter(name=form_data['name']).exists(): # There is an action with this name. Return error. self.add_error('name', _('An action with this name already exists')) return form_data
class UploadCSVFileForm(UploadBasic): """Form to read a csv file. It also allows to specify the number of lines to skip at the top and the bottom of the file. This functionality is offered by the underlyng function read_csv in Pandas """ data_file = RestrictedFileField( max_upload_size=int(ontask_prefs.MAX_UPLOAD_SIZE), content_types=json.loads(str(ontask_prefs.CONTENT_TYPES)), allow_empty_file=False, label='', help_text=_('File in CSV format (typically produced by a statistics' + ' package or Excel)')) skip_lines_at_top = forms.IntegerField( label=_('Lines to skip at the top'), help_text=_( 'Number of lines to skip at the top when reading the file'), initial=0, required=False) skip_lines_at_bottom = forms.IntegerField( label=_('Lines to skip at the bottom'), help_text=_( 'Number of lines to skip at the bottom when reading the file'), initial=0, required=False) def clean(self): """Check that the integers are positive. :return: The cleaned data """ form_data = super().clean() if form_data['skip_lines_at_top'] < 0: self.add_error( 'skip_lines_at_top', _('This number has to be zero or positive'), ) return form_data if form_data['skip_lines_at_bottom'] < 0: self.add_error( 'skip_lines_at_bottom', _('This number has to be zero or positive'), ) return form_data # Process CSV file using pandas read_csv try: self.data_frame = load_df_from_csvfile( TextIOWrapper(self.files['data_file'].file, encoding=self.data.encoding), self.cleaned_data['skip_lines_at_top'], self.cleaned_data['skip_lines_at_bottom']) except Exception as exc: self.add_error( 'data_file', _('File could not be processed ({0})').format(str(exc))) return form_data # Check the validity of the data frame self.validate_data_frame() return form_data