示例#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]
示例#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
示例#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()
示例#4
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())
    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())
示例#6
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())
    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)
示例#9
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)
示例#10
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
示例#11
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
示例#12
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
示例#13
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
示例#14
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))