Exemple #1
0
def _write_uploaded_file(file, uploaded_file):
    buffer = StringIO()
    for chunk in uploaded_file.chunks():
        buffer.write(chunk)

    buffer.seek(0)
    file.file.put(buffer, content_type=uploaded_file.content_type)
    if uploaded_file.name and uploaded_file.name.find('.') != -1:
        file.extension = uploaded_file.name.split('.')[-1]
Exemple #2
0
def _make_swf(file):
    buffer = StringIO()
    for chunk in file.chunks():
        buffer.write(chunk)

    buffer.reset()

    file = File(type='application_swf')
    file.file.put(buffer, content_type='application/x-shockwave-flash')
    file.transformation = 'main.swf'
    file.save()
    return file
Exemple #3
0
    def audio_file(name, description):
        buffer = StringIO()
        for chunk in request.FILES['file'].chunks():
            buffer.write(chunk)

        buffer.reset()

        file = File(type='library_audio')
        file.file.put(buffer, content_type='audio/mpeg')
        file.transformation = 'main.mp3'
        file.name = name
        file.description = description
        return file.save()
    def test_batch_transformation_with_single_transformation(self):
        file = File(type='text')
        buffer = StringIO()
        buffer.write('1')
        buffer.reset()
        file.file.put(buffer, content_type='text/plain')
        file.save()
        file.reload()

        transformation = BatchFileTransformation('batch',
                    TextTransformation('test')
                )

        transformation.apply(file, file)

        file.reload()
        self.failUnlessEqual('2', file.file.read())
Exemple #5
0
    def _apply(self, source, destination):
        parser = ImageFileParser()
        parser.feed(source.file.read())
        source_image = parser.close()
        crop_box = ImageResize.cropbox(source_image.size, (self.width, self.height))
        image = source_image.crop(crop_box)
        image = image.resize((self.width, self.height), Image.BICUBIC)
        buffer = StringIO()
        image.save(buffer, self.format)
        buffer.reset()
        if destination.file:
            write = destination.file.replace
        else:
            write = destination.file.put

        write(buffer, content_type="image/%s" % self.format)

        destination.save()
        return destination
Exemple #6
0
    def _apply(self, source, destination):
        parser = ImageFileParser()
        parser.feed(source.file.read())
        source_image = parser.close()
        crop_box = self._get_crop_box(source_image.size,
                                      (self.width, self.height))
        image = source_image.crop(crop_box)
        image = image.resize((self.width, self.height), Image.ANTIALIAS)
        buffer = StringIO()
        image.save(buffer, self.format)
        buffer.reset()
        if destination.file:
            write = destination.file.replace
        else:
            write = destination.file.put

        write(buffer, content_type = 'image/%s' % self.format)

        destination.save()
        return destination
    def test_apply_transformation_into_source(self):
        file = File(type='text')
        buffer = StringIO()
        buffer.write('1')
        buffer.reset()
        file.file.put(buffer, content_type='text/plain')
        file.save()
        file.reload()
        self.failUnlessEqual('1', file.file.read())


        transformation = TextTransformation('test')
        transformation.apply(file, file)

        file.reload()
        self.failUnlessEqual('2', file.file.read())

        transformation.apply(file, file)
        file.reload()
        self.failUnlessEqual('4', file.file.read())
    def test_system_command_transformations(self):
        class TestSystemCommandFileTransformation(SystemCommandFileTransformation):
            SYSTEM_COMMAND = 'cp %(source)s %(destination)s'
            def _get_derivative_content_type(self):
                return 'text/plain'


        DATA = 'xxxxXXXXxxxx'
        file = File(type='text')
        buffer = StringIO()
        buffer.write(DATA)
        buffer.reset()
        file.file.put(buffer, content_type='text/plain')
        file.save()
        file.reload()

        transformation = TestSystemCommandFileTransformation('test_trans')
        transformation.apply(file)
        derivative = file.get_derivative('test_trans')
        self.failUnlessEqual(DATA, derivative.file.read())
        self.failUnlessEqual('text/plain', derivative.file.content_type)
Exemple #9
0
    def from_zip(cls, zip_file_name):
        if not os.path.exists(zip_file_name):
            raise Theme.SourceFileNotExists("File %s not exists" % zip_file_name)

        file = zipfile.ZipFile(zip_file_name, 'r')
        theme = Theme._get_or_create(file.read('theme.xml').decode('utf-8'))

        for file_name in file.namelist():
            if cls._is_valid_theme_subfile(file_name):
                theme.add_file(file_name, file.read(file_name))
            elif file_name.startswith('templates') and not file_name.endswith('/'):
                theme.add_template_file(file_name.replace('templates/', ''),
                                        StringIO(file.read(file_name)))

        return theme
Exemple #10
0
    def test_batch_transformation_with_single_transformation(self):
        file = File(type='text')
        buffer = StringIO()
        buffer.write('1')
        buffer.reset()
        file.file.put(buffer, content_type='text/plain')
        file.save()
        file.reload()

        transformation = BatchFileTransformation('batch',
                                                 TextTransformation('test'))

        transformation.apply(file, file)

        file.reload()
        self.failUnlessEqual('2', file.file.read())
Exemple #11
0
    def to_python(self, data):
        """
        Checks that the file-upload field data contains a valid video file
        """
        f = super(VideoField, self).to_python(data)
        if f is None:
            return None

        buffer = StringIO()
        for chunk in data.chunks():
            buffer.write(chunk)

        buffer.reset()
        try:
            validator = VideoFileValidator()
            validator.validate(buffer)
        except FileValidator.Exception:
            raise ValidationError(self.error_messages['invalid_video'])

        buffer.reset()
        self.buffer = buffer
        self.content_type = f.content_type
        return data
Exemple #12
0
    def test_apply_transformation_into_source(self):
        file = File(type='text')
        buffer = StringIO()
        buffer.write('1')
        buffer.reset()
        file.file.put(buffer, content_type='text/plain')
        file.save()
        file.reload()
        self.failUnlessEqual('1', file.file.read())

        transformation = TextTransformation('test')
        transformation.apply(file, file)

        file.reload()
        self.failUnlessEqual('2', file.file.read())

        transformation.apply(file, file)
        file.reload()
        self.failUnlessEqual('4', file.file.read())
Exemple #13
0
    def to_python(self, data):
        """
        Checks that the file-upload field data contains a valid video file
        """
        f = super(VideoField, self).to_python(data)
        if f is None:
            return None

        buffer = StringIO()
        for chunk in data.chunks():
            buffer.write(chunk)

        buffer.reset()
        try:
            validator = VideoFileValidator()
            validator.validate(buffer)
        except FileValidator.Exception:
            raise ValidationError(self.error_messages["invalid_video"])

        buffer.reset()
        self.buffer = buffer
        self.content_type = f.content_type
        return data
Exemple #14
0
    def to_python(self, data):
        """
        Checks that the file-upload field data contains a valid image (GIF, JPG,
        PNG, possibly others -- whatever the Python Imaging Library supports).
        """
        f = super(ImageField, self).to_python(data)
        if f is None:
            return None

        buffer = StringIO()
        for chunk in data.chunks():
            buffer.write(chunk)

        buffer.reset()
        try:
            parser = ImageFileParser()
            parser.feed(buffer.read())
            parser.close()
        except Exception:
            raise ValidationError(self.error_messages['invalid_image'])
        self.buffer = buffer
        self.content_type = f.content_type
        return data
Exemple #15
0
    def test_system_command_transformations(self):
        class TestSystemCommandFileTransformation(
                SystemCommandFileTransformation):
            SYSTEM_COMMAND = 'cp %(source)s %(destination)s'

            def _get_derivative_content_type(self):
                return 'text/plain'

        DATA = 'xxxxXXXXxxxx'
        file = File(type='text')
        buffer = StringIO()
        buffer.write(DATA)
        buffer.reset()
        file.file.put(buffer, content_type='text/plain')
        file.save()
        file.reload()

        transformation = TestSystemCommandFileTransformation('test_trans')
        transformation.apply(file)
        derivative = file.get_derivative('test_trans')
        self.failUnlessEqual(DATA, derivative.file.read())
        self.failUnlessEqual('text/plain', derivative.file.content_type)
Exemple #16
0
    def to_python(self, data):
        """
        Checks that the file-upload field data contains a valid image (GIF, JPG,
        PNG, possibly others -- whatever the Python Imaging Library supports).
        """
        f = super(ImageField, self).to_python(data)
        if f is None:
            return None

        buffer = StringIO()
        for chunk in data.chunks():
            buffer.write(chunk)

        buffer.reset()
        try:
            parser = ImageFileParser()
            parser.feed(buffer.read())
            parser.close()
        except Exception:
            raise ValidationError(self.error_messages["invalid_image"])
        self.buffer = buffer
        self.content_type = f.content_type
        return data
Exemple #17
0
def send(request):

    send_form = InviteForm(request.POST or None)
    import_form = ImportInviteForm(request.POST or None, request.FILES)

    sent = []

    if send_form.is_valid():
        sent.append(_send_invite(request.user,
                        send_form.cleaned_data['email'],
                        send_form.cleaned_data['name']))


    if import_form.is_valid():
        buffer = StringIO()
        for chunk in request.FILES['file']:
            buffer.write(chunk)

        buffer.reset()
        str = buffer.read()
        if re.match(r'BEGIN\s*:\s*VCARD', str, re.IGNORECASE | re.MULTILINE):
            file_type = 'vcard'
        else:
            file_type = 'csv'

        buffer.reset()
        contacts = []

        if file_type == 'csv':
            try:
                encoding = chardet.detect(buffer.read())['encoding']
                buffer.reset()
                reader = codecs.getreader(encoding)(buffer)
                buffer = StringIO()
                buffer.write('\n'.join(reader.readlines()).encode('utf-8'))
                buffer.reset()

                reader = csv.DictReader(buffer)
                for row in reader:
                    try:
                        email = row['E-mail Address']
                        first_name = row['First Name']
                        last_name = row['Last Name']
                        contacts.append((email, ('%s %s' %
                                            (first_name, last_name)).decode('utf-8')))
                    except:
                        pass

            except:
                pass    

        elif file_type == 'vcard':
            vobjects = vobject.readComponents(buffer)

            while 1:
                try:
                    vcf = vobjects.next()
                    if vcf.contents.has_key('email'):
                        contacts.append((vcf.email.value,
                                         vcf.fn.value))

                except Exception, e:
                    print e
                    break


        for (email, name) in contacts:
            sent.append(_send_invite(request.user, email, name))