Exemple #1
0
    def save(self, user=None, force_insert=False, force_update=False, commit=True):
        m = super(ResourceForm, self).save(commit=False)

        if commit:


            cleaned_data = self.cleaned_data
            file = None
            language = cleaned_data['source_language']
            if cleaned_data.has_key('sourcefile'):
                file = cleaned_data['sourcefile']

            if file:
                sf = StorageFile()
                sf.uuid = str(uuid4())
                sf.name = file.name
                fh = open(sf.get_storage_path(), 'wb')
                file.seek(0)
                fh.write(file.read())
                fh.flush()
                fh.close()

                sf.size = file.size
                sf.language = language

                sf.update_props()
                sf.save()

                parser = sf.find_parser()

                try:
                    # Try to do an actual parsing to see if file is valid
                    fhandler = parser(filename=sf.get_storage_path())
                    fhandler.set_language(language)
                    fhandler.bind_resource(self.instance)
                    fhandler.contents_check(fhandler.filename)
                    fhandler.parse_file(is_source=True)
                    fhandler.save2db(is_source=True, user=user)
                except:
                    raise

                # Try to set the i18n type. Problem is that we only check
                # filename instead of mime type. we should probably update the
                # function to use python magic as well
                try:
                    m.i18n_type = get_i18n_type_from_file(file.name)
                except:
                    pass

                m.save()

        return m
Exemple #2
0
    def clean(self):
        """
        Check if provided file is a valid file and can be handled by Transifex
        """
        cleaned_data = self.cleaned_data
        file = None
        language = cleaned_data['source_language']
        if cleaned_data.has_key('sourcefile'):
            file = cleaned_data['sourcefile']

        if file:
            # Check if we can handle the file with an existing parser
            sf = StorageFile()
            sf.uuid = str(uuid4())
            sf.name = file.name
            fh = open(sf.get_storage_path(), 'wb')
            file.seek(0)
            fh.write(file.read())
            fh.flush()
            fh.close()

            sf.size = file.size
            sf.language = language

            try:
                sf.update_props()
            except (FileCheckError, ParseError), e:
                raise forms.ValidationError(e.message)
            sf.save()

            parser = sf.find_parser()
            if not parser:
                raise forms.ValidationError("File doesn't seem to be in a"
                    " valid format.")
            try:
                # Try to do an actual parsing to see if file is valid
                fhandler = parser(filename=sf.get_storage_path())
                fhandler.set_language(language)
                fhandler.contents_check(fhandler.filename)
                fhandler.parse_file(is_source=True)
            except Exception,e:
                sf.delete()
                raise forms.ValidationError("Could not import file: %s" % str(e))