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
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
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))