示例#1
0
文件: forms.py 项目: cstroie/tranpy
 def save(self, commit = True, user = None):
     """ Store the new catalog file """
     if self.catalogfile is None:
         # New Catalog file, create it
         self.catalogfile = CatalogFile()
     # Set the properties
     if 'language' in self.clean_data:
         self.catalogfile.language = self.clean_data['language']
     if 'project' in self.clean_data:
         self.catalogfile.project = self.clean_data['project']
     if 'catalog' in self.clean_data:
         self.catalogfile.catalog = self.clean_data['catalog']
     if 'status' in self.clean_data:
         self.catalogfile.status = self.clean_data['status']
     # Check if there is any file uploaded
     if self.clean_data['catfile'] is not None:
         # Save the catalog file content and original file name
         self.catalogfile.origfile = self.clean_data['catfile']['filename']
         (_nothing, extension) = self.catalogfile.origfile.rsplit('.', 1)
         self.catalogfile.filetype = CFT.get_type(extension)
         # Save the file
         self.catalogfile.save_file(self.clean_data['catfile']['content'])
         # Set the user
         if user is not None:
             self.catalogfile.user = user
     # Save the catalogfile model if requested
     if commit == True:
         self.catalogfile.save()
     # Return the model
     return self.catalogfile
示例#2
0
文件: forms.py 项目: cstroie/tranpy
class CatalogFileUploadForm(Form):
    """ Upload a catalog file """

    # Form fields
    language = forms.ModelChoiceField(Language.objects.active().select_related(), empty_label = _('All languages (select the project and the catalog)'), required = False, label = _('Language'))
    project  = forms.ModelChoiceField(Project.objects.active().select_related(), empty_label = _('No project (compendium)'), required = False, label = _('Project'))
    catalog  = forms.ModelChoiceField(Catalog.objects.active().select_related(), empty_label = _('No catalog (create auto name)'), required = False, label = _('Catalog'))
    status   = forms.ChoiceField(choices = CATALOGFILE_STATUS_CHOICES, initial = 'Q', label = _('Status'))
    catfile  = forms.Field(widget = forms.FileInput(), label = _('Catalog file'))
    catalog_upload_form = forms.CharField(widget = forms.HiddenInput, required = False)

    def __init__(self, *args, **kwargs):
        """ Init the form """
        # Keep the catalog file
        self.catalogfile = kwargs.pop('catalogfile', None)
        # Init the parent
        super(CatalogFileUploadForm, self).__init__(*args, **kwargs)
        # Populate the form
        if self.catalogfile is not None:
            if self.catalogfile.language is not None:
                self.fields['language'].initial = self.catalogfile.language.id
            if self.catalogfile.project is not None:
                self.fields['project'].initial = self.catalogfile.project.id
            if self.catalogfile.catalog is not None:
                self.fields['catalog'].initial = self.catalogfile.catalog.id
            self.fields['status'].initial = self.catalogfile.status
            self.fields['catfile'].required = False

    def clean_catfile(self):
        """ This field keeps the file content, not the file name """
        if 'catfile' in self.clean_data:
            if self.clean_data['catfile'] is not None:
                # Get the file extension
                (_nothing, extension) = self.clean_data['catfile']['filename'].rsplit('.', 1)
                # Check the extension
                if CFT.check_extension(extension):
                    return self.clean_data['catfile']
                else:
                    raise forms.ValidationError(_('Unsupported file type'))
        return None

    def clean(self):
        """ Make sure that one language or one project are specified at least """
        if not 'language' in self.clean_data and not 'project' in self.clean_data:
            raise forms.ValidationError(_('You need to specify at least the language or the project'))
        # Return the clean data
        return self.clean_data

    def save(self, commit = True, user = None):
        """ Store the new catalog file """
        if self.catalogfile is None:
            # New Catalog file, create it
            self.catalogfile = CatalogFile()
        # Set the properties
        if 'language' in self.clean_data:
            self.catalogfile.language = self.clean_data['language']
        if 'project' in self.clean_data:
            self.catalogfile.project = self.clean_data['project']
        if 'catalog' in self.clean_data:
            self.catalogfile.catalog = self.clean_data['catalog']
        if 'status' in self.clean_data:
            self.catalogfile.status = self.clean_data['status']
        # Check if there is any file uploaded
        if self.clean_data['catfile'] is not None:
            # Save the catalog file content and original file name
            self.catalogfile.origfile = self.clean_data['catfile']['filename']
            (_nothing, extension) = self.catalogfile.origfile.rsplit('.', 1)
            self.catalogfile.filetype = CFT.get_type(extension)
            # Save the file
            self.catalogfile.save_file(self.clean_data['catfile']['content'])
            # Set the user
            if user is not None:
                self.catalogfile.user = user
        # Save the catalogfile model if requested
        if commit == True:
            self.catalogfile.save()
        # Return the model
        return self.catalogfile