def encrypt_image(obj): ''' Enrypte an impage ''' file_split = os.path.splitext(obj.name) file_name = file_split[0] file_extension = file_split[1] img = PILImage.open(obj) if img.mode in ['RGBA']: background = PILImage.new( img.mode[:-1], img.size, (255,255,255) ) background.paste(img, img.split()[-1]) img = background width, height = compress_image(img) img.thumbnail( (width, height), PILImage.ANTIALIAS ) output = BytesIO() img.save(output, format='JPEG', quality=70) # Reduce a quality by 70% output.seek(0) fernet = encrypt_algorithm() encrypted = fernet.encrypt(output.read()) content = ContentFile(encrypted) img.close() content.close() output.close() return InMemoryUploadedFile(content, 'ImageField', '{0}.jpg'.format(file_name), 'image/jpeg', sys.getsizeof(content), None)
def process(self, verbosity, save=True): dirname, filename = os.path.split( os.path.join(os.path.abspath(os.path.dirname(__file__)), '../media/utils/temp_files/') + self.ref) prefix, suffix = os.path.splitext(filename) fd, filename = tempfile.mkstemp(suffix, prefix + "_", dirname) file_url = self.source_url try: try: # python >= 2.7 import requests r = requests.get(file_url) f = StringIO(r.content) except ImportError: # python <= 2.6 import urllib2 r = urllib2.urlopen(file_url) f = r file = ContentFile(f.read(), filename) file.close() try: tree = objectify.parse(file) parser_cls = parsers[tree.getroot().tag] try: parser_cls(tree, True, verbosity).parse() os.remove(file.name) except Exception, e: os.remove(file.name) raise Exception, e #TODO log error except KeyError: raise ImportError(u"Undefined document structure") except Exception, e: pass #TODO log error
def make_thumbnail(self): import os from PIL import Image, ImageOps from io import BytesIO from django.core.files.base import ContentFile from django.core.files.storage import default_storage size = (300, 300) f = default_storage.open(self.img) image = Image.open(f) ftype = image.format image = ImageOps.fit(image, size, Image.ANTIALIAS) path, ext = os.path.splitext(self.img.name) name = os.path.basename(path) thumbnail_name = '%s_thumb%s' % (name, ext) temp_file = BytesIO() image.save(temp_file, ftype) temp_file.seek(0) content_file = ContentFile(temp_file.read()) self.img_thumbnail.save(thumbnail_name, content_file) temp_file.close() content_file.close() f.close() return True
def request_image(self, file_url): try: # Check for existing demo d = DemoResults.objects.get(ml_model=model, data=file_url) return d.file.path, d.pk except: suffix_list = [ 'jpg', 'gif', 'png', 'tif', 'svg', ] file_name = urlsplit(file_url)[2].split('/')[-1].strip('/') file_suffix = file_name.split('.')[1].lower() r = requests.get(file_url) if file_suffix in suffix_list and r.status_code == requests.codes.ok: file = ContentFile(r.content, file_name) demo = DemoResults(ml_model=self.model) demo.data = file_url demo.file = file demo.save() file.close() path = demo.file.path demo.delete() return path, demo.pk else: return False
def make_thumbnail(self): from PIL import Image, ImageOps #파일을 만들기 싫어서 파일인 척 하는 것 from io import BytesIO from django.core.files.base import ContentFile #로컬이 아닌 다른 스테이지를 가져올 수 있도록 해주는 것 from django.core.files.storage import default_storage import os size = (300, 300) f = default_storage.open(self.img) image = Image.open(f) ftype = image.format image = ImageOps.fit(image, size, Image.ANTIALIAS) #안티앨리어스는 사진의 계단 현상을 없애준다. path, ext = os.path.splitext(self.img.name) name = os.path.basename(path) thumbnail_name = '%s_thumb%s' % (name, ext) temp_file = BytesIO() image.save(temp_file, ftype) temp_file.seek(0) content_file = ContentFile(temp_file.read()) self.img_thumbnail.save(thumbnail_name, content_file) temp_file.close() content_file.close() f.close()
def make_thumbnail(self): size = (300, 300) f = default_storage.open(self.img.name) image = Image.open(f) ftype = image.format image = ImageOps.fit(image, size, Image.ANTIALIAS) path, ext = os.path.splitext(self.img.name) name = os.path.basename(path) thumbnail_name = '%s_thumb%s' % (name, ext) temp_file = BytesIO() image.save(temp_file, ftype) temp_file.seek(0) content_file = ContentFile(temp_file.read()) self.img_thumbnail.save(thumbnail_name, content_file) temp_file.close() content_file.close() f.close()
def process(self, verbosity): dirname, filename = os.path.split(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../media/utils/temp_files/')+self.ref) prefix, suffix = os.path.splitext(filename) fd, filename = tempfile.mkstemp(suffix, prefix+"_", dirname) file_url = self.source_url try: try: # python >= 2.7 import requests r = requests.get(file_url) f = StringIO(r.content) except ImportError: # python <= 2.6 import urllib2 r = urllib2.urlopen(file_url) f = r file = ContentFile(f.read(), filename) file.close() try: tree = objectify.parse(file) parser_cls = parsers[tree.getroot().tag] try: parser_cls(tree, True, verbosity).parse() os.remove(file.name) except Exception, e: os.remove(file.name) raise Exception, e #TODO log error except KeyError: raise ImportError(u"Undefined document structure") except Exception, e: pass #TODO log error
def savemodel(request,mno): train_df=request.session['train_df'] colnos=request.session['colnos'] y_col=request.session['y_col'] f1_list=request.session['f1'] train_name=request.session['train_name'] regnames=request.session['regnames'] colList=list(train_df.columns) colnames="" for i in colnos: colnames+=colList[i]+',' colnames+=','+colList[y_col] regList=request.session['regList'] index=int(mno) modelname=train_name filename=train_name filename+='_'+str(len(colnos))+'-columns'+'_'+str(datetime.datetime.now())[:-7]+'.pkl' modelname+=',,'+colnames+',,'+regnames[index]+',,'+str(datetime.datetime.now())[:-7]+',,'+str(f1_list[index]) model_to_save=regList[index] data_entry = saved_models(name=modelname,user1=request.user) content = pickle.dumps(model_to_save) fid = ContentFile(content) data_entry.model.save(filename, fid) fid.close() return JsonResponse({'stat':'ok'},status=200)
def persist(self): """a private method that persists an estimator object to the filesystem""" if self.object_hash: data = dill.dumps(self.object_property) f = ContentFile(data) self.object_file.save(self.object_hash, f) f.close() return True return False
def test_email_file_direct(self): downloader = FakeDownloader([self.item.id], self.editor.id, {}) f = ContentFile('MyFile') size = f.size f.close() downloader.email_file(f, size, 'https://example.com/file.txt') self.assertEqual(len(mail.outbox), 1) message = mail.outbox[0] self.assertEqual(len(message.attachments), 1) content = message.attachments[0][1] self.assertEqual(content, 'MyFile')
def import_mp_photos(): print 'A associar fotos dos deputados...' from django.core.files.base import ContentFile for mp in MP.objects.all(): imgfilename = os.path.abspath(os.path.join(PHOTO_DIR, '%d.jpg' % mp.id)) if os.path.exists(imgfilename): file_content = ContentFile(open(imgfilename, 'rb').read()) mp.photo.save(imgfilename, file_content) file_content.close() else: pass
def editProfile(request): try: username = request.data.get('username',None) except MultiValueDictKeyError: return HttpResponse("Username is required") try: photo=request.data.get('image',None) except MultiValueDictKeyError: return JsonResponse({"Error": "Image Required"}) try: firstname = request.data.get('firstname',None) except MultiValueDictKeyError: return JsonResponse({"Error": "First name Required"}) try: lastname=request.data.get('lastname',None) except MultiValueDictKeyError: return JsonResponse({"Error": "Last name Required"}) try: email=request.data.get('email',None) except MultiValueDictKeyError: return JsonResponse({"Error": "Email Required"}) try: City = request.data.get('city',None) except MultiValueDictKeyError: return JsonResponse({"Error":"City is required"},status=500) try: State = request.data.get('state',None) except MultiValueDictKeyError: return JsonResponse({"Error":"State is required"},status=500) try: PhoneNumber = request.data.get('phoneNumber',None) except MultiValueDictKeyError: return JsonResponse({"Error":"Phone Number is required"},status=500) user = User.objects.get(username=username) client = Client.objects.get(user=user) Clientid = client.id if photo.find('https://vlawyer-backend.herokuapp.com') == -1: data = photo.split(',')[1] imgdata = base64.b64decode(data) filename = username + '.jpg' file = ContentFile(imgdata,name=filename) client.image = file file.close() user.first_name = firstname user.last_name = lastname client.city = City client.state = State client.phone_number = PhoneNumber user.email= email user.save() client.save() return JsonResponse({"Success": "Image Saved"})
def bake(imageFile, assertion_json_string): reader = png.Reader(file=imageFile) filename = '%s.png' % hashlib.md5(str(assertion_json_string)).hexdigest() newfile = ContentFile("", name=filename) newfile.open() chunkheader = 'openbadges\x00\x00\x00\x00\x00' badge_chunk = ('iTXt', bytes(chunkheader + assertion_json_string)) png.write_chunks(newfile, baked_chunks(reader.chunks(), badge_chunk)) newfile.close() return newfile
def post(self, request, *args, **kwargs): user = request.auth.user try: user_img = request.FILES["user_img"] except MultiValueDictKeyError: return Response({"error": "이미지를 선택하지 않았습니다."}, status=status.HTTP_400_BAD_REQUEST) else: temp_img, img_name = create_thumbnail(user_img) content_file = ContentFile(temp_img.read()) user.user_img.save(img_name+".jpg", content_file) temp_img.close() content_file.close() return Response({"info": "프로필 이미지를 등록하였습니다."}, status=status.HTTP_201_CREATED)
def test_email_file(self): downloader = FakeDownloader([self.item.id], self.editor.id, {}) f = ContentFile('MyFile') size = f.size f.close() downloader.email_file(f, size, 'https://example.com/file.txt') self.assertEqual(len(mail.outbox), 1) message = mail.outbox[0] self.assertEqual(len(message.attachments), 0) self.assertTrue('https://example.com/file.txt' in message.body) expected_regen_url = reverse('aristotle:download_options', args=['fake'])\ + '?items=' + str(self.item.id) self.assertTrue(expected_regen_url in message.body)
def image_to_contentfile(image, filename, quality): """ Returns a named bytestream of the input image :param image: the image to be stored :param filename: the name of the outputfile :param quality: the quality of the image """ named_content = NamedBytesIO(name=filename) image.save(named_content, quality=quality) out_file = ContentFile(content=named_content.getvalue(), name=filename) out_file.close() return out_file
def upload_pic(pic, code, up_type='ava'): #pdb.set_trace() try: ext_name = '' if up_type == 'ava': conn = UpYun('one9inava', 'one9', 'one9in1234') elif up_type == 'vid': conn = UpYun('vid19', 'one9', 'one9in1234') ext_name = '_cpp.mp4' elif up_type == 'avd': conn = UpYun('vid19', 'one9', 'one9in1234') ext_name = '_ava.mp4' else: conn = UpYun('cpp19', 'one9', 'one9in1234') if 'http' in pic.name: data = ContentFile(urllib2.urlopen(pic.name).read()) data.seek(0) elif 'default' in pic.name: return True else: try: pic.seek(0) data = pic except: print 'no pic' return False #conn.setContentMD5(md5file(pic)) #result = conn.writeFile('%d' % code, pic) #data.write(pic.read()) #data.seek(0) conn.setContentMD5(md5file(data)) #result = conn.deleteFile('%d' % code) result = conn.writeFile('%s%s' % (code, ext_name), data) print result data.close() if result: print 'ok!!' return True else: print 'nok' return False except Exception, ex: print Exception, ":", ex return False
def upload_pic(pic, code, up_type='ava'): #pdb.set_trace() try: ext_name = '' if up_type == 'ava': conn = UpYun('one9inava', 'one9', 'one9in1234') elif up_type == 'vid': conn = UpYun('vid19', 'one9', 'one9in1234') ext_name = '_cpp.mp4' elif up_type == 'avd': conn = UpYun('vid19', 'one9', 'one9in1234') ext_name = '_ava.mp4' else: conn = UpYun('cpp19', 'one9', 'one9in1234') if 'http' in pic.name: data = ContentFile(urllib2.urlopen(pic.name).read()) data.seek(0) elif 'default' in pic.name: return True else: try: pic.seek(0) data = pic except: print 'no pic' return False #conn.setContentMD5(md5file(pic)) #result = conn.writeFile('%d' % code, pic) #data.write(pic.read()) #data.seek(0) conn.setContentMD5(md5file(data)) #result = conn.deleteFile('%d' % code) result = conn.writeFile('%s%s' % (code, ext_name), data) print result data.close() if result: print 'ok!!' return True else: print 'nok' return False except Exception, ex: print Exception,":",ex return False
def read_all(): df = pd.read_excel('eye.xlsx') for tup in df.iterrows(): row = tup[1] number = row['شماره'] title = row['نام فایل'] drive_link = row['لینک'] download_url = get_download_link(drive_link) response = requests.get(download_url) f = ContentFile(response.content) vid = Video.objects.create(number=number, title=title, price=10) vid.video_file.save(vid.title + '.m4v', f) vid.save() f.close() response.close()
def edit_arquivo(request, id): usuario = Usuario.objects.get(email=request.session['email']) arquivo = Arquivo.objects.get(id=id) if request.method == 'GET': file = arquivo.arquivo file.open(mode='rb') content = file.readlines() file.close() return render_to_response('edit_file.html', { 'usuario': usuario, 'arquivo': arquivo, 'content': content, 'usuarios': Usuario.objects.all() }, context_instance=RequestContext(request)) elif request.method == 'POST': myfile = ContentFile(request.POST['content']) nome_arquivo = request.POST['nome'] tipo_arquivo = request.POST['tipo'] if 'pasta' in request.POST: pasta_arquivo = Pasta.objects.get(id=request.POST['pasta']) else: pasta_arquivo = None try: if nome_arquivo != arquivo.nome: arq_temp = Arquivo.objects.get(nome=nome_arquivo) messages.error(request, 'Ja existe arquivo com este nome') return redirect('/app') else: arq_temp = Arquivo.objects.get(id=nome_arquivo.id) except: arquivo.arquivo.save( str(nome_arquivo) + '.' + tipo_arquivo, myfile) arquivo.nome = nome_arquivo arquivo.pasta = pasta_arquivo arquivo.tipo = tipo_arquivo arquivo.save() myfile.open(mode='rb') content = myfile.readlines() myfile.close() messages.success(request, 'Alterado com sucesso') return render_to_response('edit_file.html', { 'arquivo': arquivo, 'content': content, 'usuarios': Usuario.objects.all() }, context_instance=RequestContext(request))
def bake(imageFile, assertion_json_string): """ Embeds a serialized representation of a badge instance in a PNG image file. """ reader = png.Reader(file=imageFile) output_filename = '%s.png' % hashlib.md5(str(assertion_json_string)).hexdigest() newfile = ContentFile("", name=output_filename) newfile.open() chunkheader = 'openbadges\x00\x00\x00\x00\x00' badge_chunk = ('iTXt', bytes(chunkheader + assertion_json_string)) png.write_chunks(newfile, baked_chunks(reader.chunks(), badge_chunk)) newfile.close() return newfile
def bake(imageFile, assertion_json_string): """ Embeds a serialized representation of a badge instance in a PNG image file. """ reader = png.Reader(file=imageFile) output_filename = "%s.png" % hashlib.md5(str(assertion_json_string)).hexdigest() newfile = ContentFile("", name=output_filename) newfile.open() chunkheader = "openbadges\x00\x00\x00\x00\x00" badge_chunk = ("iTXt", bytes(chunkheader + assertion_json_string)) png.write_chunks(newfile, baked_chunks(reader.chunks(), badge_chunk)) newfile.close() return newfile
def export_aids(aids_id_list, author_id, file_format): queryset = Aid.objects.filter(id__in=aids_id_list) exported_data = AidResource().export(queryset) if file_format == 'csv': content_file = ContentFile(exported_data.csv) if file_format == 'xlsx': content_file = ContentFile(exported_data.xlsx) file_name = 'export-aides-' file_name += dateformat.format(timezone.now(), 'Y-m-d_H-i-s') file_name += f'.{file_format}' file_object = files.File(content_file, name=file_name) DataExport.objects.create( author_id=author_id, exported_file=file_object, ) file_object.close() content_file.close()
def edit_arquivo(request, id): arquivo = Arquivo.objects.get(id=id) if request.method == 'GET': file = arquivo.arquivo file.open(mode='rb') content = file.readlines() file.close() return render_to_response('view_file.html', {'arquivo':arquivo, 'content': content}, context_instance=RequestContext(request)) elif request.method == 'POST': myfile = ContentFile(request.POST['content']) arquivo.arquivo.save(str(arquivo.nome)+'.'+arquivo.tipo, myfile) arquivo.save() myfile.open(mode='rb') content = myfile.readlines() myfile.close() messages.success(request, 'Alterado com sucesso') return render_to_response('view_file.html', {'arquivo':arquivo, 'content': content}, context_instance=RequestContext(request))
def handle_page_upload(self, page, response, key): has_changed = False temp_file = ContentFile(response.content) local_md5, b64 = key.compute_md5(temp_file) etag = key.etag or '' # If key is new, there's no etag yet remote_md5 = etag.strip('"') # for some weird reason, etags are quoted # force publish if page is marked as changed if not remote_md5 == local_md5 or page.has_changed: has_changed = True key.set_contents_from_file(temp_file, policy='public-read') message = _('The content was successfully published.') self.log_success(page, message) temp_file.close() return page, has_changed
def make_thumbnail(self): import os from PIL import Image, ImageOps from io import BytesIO, StringIO, FileIO from django.core.files.base import ContentFile from django.core.files.storage import default_storage size = (300, 300) # Default storage에서 FileField내용 읽어오기 f = default_storage.open(self.img) print('f : %s' % f) # Image.open으로 파일을 Image인스턴스화 (image) image = Image.open(f) # Image.format은 JPEG, PNG, BMP등 포맷정보를 나타냄 ftype = image.format print('ftype : %s' % ftype) # ImageOps.fit메서드를 이용해서 썸네일이미지 생성 image = ImageOps.fit(image, size, Image.ANTIALIAS) # 기존에 있던 img의 경로와 확장자를 가져옴 path, ext = os.path.splitext(self.img.name) name = os.path.basename(path) # 기존파일명_thumb.확장자 형태가 됨 thumbnail_name = '%s_thumb%s' % (name, ext) # 임시 파일로 취급되는 객체 생성 temp_file = BytesIO() image.save(temp_file, ftype) temp_file.seek(0) # img_thumbnail필드에 해당 파일내용을 저장 # Django의 FileField에 내용을 저장할때는 ContentFile형식이어야 함 content_file = ContentFile(temp_file.read()) self.img_thumbnail.save(thumbnail_name, content_file) # 열었던 파일 닫아줌 temp_file.close() content_file.close() f.close() return True
def print_instance(request, instance_id): instance = Instance.objects.get(id=instance_id) tag_image = instance.generate_tag() tag_io = StringIO.StringIO() tag_image.save(tag_io, format='PNG') tag_file = ContentFile(tag_io.getvalue()) #Django is automatically prepending the MEDIA_ROOT to the 'upload_to' value during the default file upload workflow, here we have to do it by hand. name = os.path.join( conf.settings.MEDIA_ROOT, name_tag(instance), ) tagdir = os.path.join( conf.settings.MEDIA_ROOT, get_tagdir(instance), ) for previoustag in glob.glob(os.path.join(tagdir, 'tag*')): #some basic concurrency protection try: os.remove(previoustag) except: pass #Delete the tag image if it already exists (django default behavior being to rename the file if there is a collision) : does not apply anymore #try: # os.remove(name) #except: # pass instance.tag.save(name, tag_file) #no clue if we have to close it by hand, better safe... tag_file.close() # replace() in an attempt to support Windows host and still return a valid url #\todo : test the tag_url under Windows host tag_url = os.path.join(conf.settings.MEDIA_URL, name_tag(instance)).replace('\\', '/') return render(request, 'view_tag.html', { 'tag_url': tag_url, 'form_action': '/' + settings.URL_TAG_REDIRECT })
def save_file(query, instance, solution, filename, save=False): if save: content = pickle.dumps(instance) fid = ContentFile(content) query.instance.save(filename, fid) fid.close() content = pickle.dumps(solution) fid = ContentFile(content) query.solution.save(filename, fid) fid.close() else: instance_loc = f"{settings.MEDIA_ROOT}/temp/{filename}.instance" query.instance = instance_loc with open(instance_loc, "wb") as file: pickle.dump(instance, file) solution_loc = f"{settings.MEDIA_ROOT}/temp/{filename}.solution" query.solution = solution_loc with open(solution_loc, "wb") as file: pickle.dump(solution, file)
def make_thumbnail(self): import os from PIL import Image, ImageOps from io import BytesIO from django.core.files.storage import default_storage size = (300, 300) # 이미지의 경로를 가져옴 f = default_storage.open(self.img) # 파일 Image 인스턴스화, file type 별도 저장 image = Image.open(f) ftype = image.format # 리사이즈+크롭한 이미지를 저장 image = ImageOps.fit(image, size, Image.ANTIALIAS) # 기존파일 이름과 파일형식 추출 # slpitext: 마지막 . 을 기준으로 slpit path, ext = os.path.splitext(self.img.name) name = os.path.basename(self.img.name) # 기존 파일 이름과 파일형식을 기반으로 thumbnail 이름 추출 thumbnail_name = '%s_thumb%s' % (name, ext) # BytesIO로 임시 파일에 image 저장 temp_file = BytesIO() image.save(temp_file, ftype) # read 위치 초기화 temp_file.seek(0) # 임시파일과 썸네일 이름을 불러와서 썸네일 저장 content_file = ContentFile(temp_file.read()) self.img_thumbnail.save(thumbnail_name, content_file) # 모든 파일 종료 temp_file.close() content_file.close() f.close()
def save_pilimage(image, path, storage): """Save a pil image using django storage system. As a PIL image does not inherit from django file class it can not be saved using django storage system. So we have to load the image content to a django file and the save it using django storage. Args: image: a PIL image to be saved in the host. path: a string that indicates the file path where the image should be saved. storage: a django storage instance. Returns: A string with the path of the stored file. """ content = ContentFile(b'') save_image(image, outfile=content, format=get_format(path)) name = storage.save(path, content) content.close() return name
def edit_arquivo(request, id): usuario = Usuario.objects.get(email=request.session['email']) arquivo = Arquivo.objects.get(id=id) if request.method == 'GET': file = arquivo.arquivo file.open(mode='rb') content = file.readlines() file.close() return render_to_response('edit_file.html', {'usuario': usuario, 'arquivo': arquivo, 'content': content, 'usuarios': Usuario.objects.all()}, context_instance=RequestContext(request)) elif request.method == 'POST': myfile = ContentFile(request.POST['content']) nome_arquivo = request.POST['nome'] tipo_arquivo = request.POST['tipo'] if 'pasta' in request.POST: pasta_arquivo = Pasta.objects.get(id=request.POST['pasta']) else: pasta_arquivo = None try: if nome_arquivo != arquivo.nome: arq_temp = Arquivo.objects.get(nome=nome_arquivo) messages.error(request, 'Ja existe arquivo com este nome') return redirect('/app') else: arq_temp = Arquivo.objects.get(id=nome_arquivo.id) except: arquivo.arquivo.save(str(nome_arquivo) + '.' + tipo_arquivo, myfile) arquivo.nome = nome_arquivo arquivo.pasta = pasta_arquivo arquivo.tipo = tipo_arquivo arquivo.save() myfile.open(mode='rb') content = myfile.readlines() myfile.close() messages.success(request, 'Alterado com sucesso') return render_to_response('edit_file.html', {'arquivo': arquivo, 'content': content, 'usuarios': Usuario.objects.all()}, context_instance=RequestContext(request))
def append_records_to_file(records: List[Dict[str, str]], new_file): header = records[0].keys() values = [list(v) for v in [record.values() for record in records]] new_file_records = new_file.read() new_file_header = new_file_records.decode('utf-8').split('\n')[0] new_file_records = r'\n'.join( new_file_records.decode('utf-8').split('\n')[1:]) records_str = '' for record in values: records_str += repr(str(','.join(record)) + '\n').replace('\'', '') aux = records_str records_str = str( ','.join(header)) + r'\n' + aux + str(new_file_records).replace( '\'', '') if new_file_header != str(','.join(header)): raise InvalidFileStructure() buff = io.BytesIO( records_str.encode('utf-8').decode('unicode_escape').encode('utf-8')) content_file = ContentFile(buff.read()) content_file.close() return content_file
def _save(self, name, content, write_mode, mute=True): """ Given the name of the file and the content (django File object or its subclasses) overwrite the file if it exists If the incoming stream was a file, it is closed when the method returns """ # Create a content file if the incoming data is not that if not hasattr(content, 'open'): content = ContentFile(content) content.open() if content.size <= self.CHUNK_SIZE: self.client.files_upload(content.read(), self._full_path(name), write_mode, mute=mute) else: self._chunked_upload(content, self._full_path, write_mode, mute) content.close() return name
def handle_noargs(self, **options): profiles = Profile.objects.filter(user__id=301) for profile in profiles: conn = UpYun('ava19', 'one9', 'one9in1234') data = None if 'http' in profile.avatar.name: #pdb.set_trace() #data_img = cStringIO.StringIO(urllib2.urlopen(profile.avatar.name).read()) data = ContentFile(urllib2.urlopen(profile.avatar.name).read()) #data = tempfile.TemporaryFile() #data.write(urllib2.urlopen(profile.avatar.name).read()) data.seek(0) elif 'default' in profile.avatar.name: continue else: try: profile.avatar.seek(0) data = profile.avatar except: print 'Warning -- Profile : %d avatar lost...' % profile.user.id continue conn.setContentMD5(md5file(data)) result = conn.writeFile('%d' % profile.user.id, data) if 'http' in profile.avatar.name: data.close() if result: print '-- Profile : %d success...' % profile.user.id else: print 'Error -- Profile : %d ...' % profile.user.id print 'Finish.'
def export_eligibility_tests_stats_as_csv(eligibility_tests_id_list, author_id, background=True): # noqa """ Method to write to csv and export all of the stats of an Eligibility Test. """ eligibility_test = EligibilityTest.objects.get( id=eligibility_tests_id_list[0]) # noqa eligibility_test_questions = eligibility_test.eligibilitytestquestion_set \ .select_related('question') \ .all() eligibility_test_stats = AidEligibilityTestEvent.objects \ .select_related('aid') \ .filter(eligibility_test=eligibility_test) csv_buffer = StringIO() response = HttpResponse(content_type='text/csv') response.write(codecs.BOM_UTF8) csv_writer = csv.writer(csv_buffer if background else response, delimiter=',', dialect='excel') # noqa # write header header = ['eligibility_test_name', 'aid_name'] header_questions = [] question_id_list = [] for eligibility_question in eligibility_test_questions: question_id_list.append(eligibility_question.question_id) header_questions.append(eligibility_question.question) header_meta = ['answer_success', 'querystring', 'source', 'date_created'] csv_writer.writerow(header + header_questions + header_meta) for eligibility_test_event in eligibility_test_stats: eligibility_test_event_row = [ eligibility_test.name, eligibility_test_event.aid ] # noqa # we need to map the answers to the right questions eligibility_test_event_row_questions = [] for question_id in question_id_list: answer = next((a for a in eligibility_test_event.answer_details if a['id'] == question_id), None) # noqa answer_cleaned = answer['answer'] if answer else '' eligibility_test_event_row_questions.append(answer_cleaned) eligibility_test_event_row_meta = [ getattr(eligibility_test_event, key) for key in header_meta ] # noqa csv_writer.writerow(eligibility_test_event_row + eligibility_test_event_row_questions + eligibility_test_event_row_meta) # noqa file_name = 'export-test-eligibilite-{eligibility_test_id}-stats-{timestamp}'.format( # noqa eligibility_test_id=eligibility_test.id, timestamp=dateformat.format(timezone.now(), 'Y-m-d_H-i-s')) if background: file_content = ContentFile(csv_buffer.getvalue().encode('utf-8')) file_object = files.File(file_content, name=f'{file_name}.csv') DataExport.objects.create( author_id=author_id, exported_file=file_object, ) file_object.close() file_content.close() else: response['Content-Disposition'] = 'attachment; filename={}.csv'.format( file_name) # noqa return response
def post(self, request): """""" f = zipfile.ZipFile(request.FILES['file']) album_name = str(request.FILES['file']).split('.zip')[0] # ImageDescription: 0x010e # DateTimeOriginal: 0x9003 # GPSLongitude: 0x0004 # GPSLatitude: 0x0002 # Orientation: 1 == Horizontal, 6 === Vertical # 4000x3000 , 2664 x 4000 photo_album = PhotoAlbum(name=album_name) photo_album.save() photos = [] day_photos_map = {} conn = S3Connection(self.S3_ACCESS_KEY_ID, self.S3_SECRET_ACCESS_KEY, host='s3-us-west-1.amazonaws.com') bucket = conn.get_bucket('deepic') k = Key(bucket) for filename in f.namelist(): full_image_fp = ContentFile(f.read(filename)) full_image = Image.open(full_image_fp) exif_data = get_exif_data(full_image) horizontal_image = exif_data['Orientation'] == 1 # 2 tuple of (width, height) full_image_size = full_image.size full_image_width = full_image_size[0] full_image_height = full_image_size[1] # So this is something like 4000 x 3000, or 4000 x 2664 # The decent resize is simply 1/4th of the full res # The thumbnail needs to be a square. Based on the dimensions of the original, crop an image # Decent resize is 1/4 of the the original decent_width = full_image_size[0] / 2 decent_height = full_image_size[1] / 2 smaller_dimension = min(full_image_size[0], full_image_size[1]) left_crop = (full_image_width - full_image_height) / 2 # L, U, R, Lower crop_box = (left_crop, 0, full_image_width - left_crop, full_image_height) crop_resize = (smaller_dimension / 2, smaller_dimension / 2) decent_resize = (decent_width, decent_height) full_copy = full_image.copy() decent_copy = full_copy.resize(decent_resize) cropped_image = full_copy.crop(crop_box) thumbnail_copy = cropped_image.resize(crop_resize) if not horizontal_image: thumbnail_copy = thumbnail_copy.rotate(270) decent_copy = decent_copy.rotate(270) thumbnail_io = StringIO.StringIO() thumbnail_copy.save(thumbnail_io, format='JPEG') thumbnail_image_fp = ContentFile(thumbnail_io.getvalue()) decent_io = StringIO.StringIO() decent_copy.save(decent_io, format='JPEG') decent_image_fp = ContentFile(decent_io.getvalue()) datetime_str = exif_data['DateTimeOriginal'] photo_datetime = datetime.datetime.strptime(datetime_str, '%Y:%m:%d %H:%M:%S') photo_date = photo_datetime.date() day_results = Day.objects.filter(date=photo_date) if day_results: if len(day_results) == 1: d = day_results.get() if d.photoalbum_set.all(): # If the photoalbum_set exists, it's part of a different album, so create a new Date day = Day(date=photo_date) day.save() else: day = d else: found = False for d in day_results: if not d.photoalbum_set.all(): day = d found = True break elif d.photoalbum_set.all()[0].id == photo_album.id: day = d found = True break if not found: day = Day(date=photo_date) day.save() else: day = Day(date=photo_date) day.save() if day.id not in day_photos_map: day_photos_map[day.id] = [] photo = Photo(date=photo_datetime) photo.save() # 3 to save: the thumbnail, the decent, and the full k.key = '{0}/{1}/{2}.full.JPG'.format(photo_album.id, day.id, photo.id) full_image_fp.seek(0) k.set_contents_from_file(full_image_fp, cb=self.percent_cb, num_cb=10) full_image_fp.close() k.key = '{0}/{1}/{2}.decent.JPG'.format(photo_album.id, day.id, photo.id) k.set_contents_from_file(decent_image_fp, cb=self.percent_cb, num_cb=10) # decent_image_fp.close() k.key = '{0}/{1}/{2}.thumbnail.JPG'.format(photo_album.id, day.id, photo.id) k.set_contents_from_file(thumbnail_image_fp, cb=self.percent_cb, num_cb=10) thumbnail_image_fp.close() logger.info('Uploaded photos w/ID: \"{0}\"'.format(photo.id)) photos.append(photo) day_photos_map[day.id].append(photo) # Now map photos to days, and days to an Album days = [] for day_id, photos in day_photos_map.iteritems(): day = Day.objects.get(id=day_id) day.photos.add(*photos) day.save() days.append(day) photo_album.days.add(*days) photo_album.save() return HttpResponse(json.dumps({'photo_album': photo_album.id}), content_type='application/json')
def editProfile(request): try: categories = request.data.get('categories', None) except MultiValueDictKeyError: return HttpResponse("Categories is required") try: username = request.data.get('username', None) except MultiValueDictKeyError: return HttpResponse("Username is required") try: photo = request.data.get('image', None) except MultiValueDictKeyError: return JsonResponse({"Error": "Image Required"}) try: about = request.data.get('about', None) except MultiValueDictKeyError: return JsonResponse({"Error": "about Required"}) try: contact = request.data.get('contact', None) except MultiValueDictKeyError: return JsonResponse({"Error": "contact Required"}) try: firstname = request.data.get('firstname', None) except MultiValueDictKeyError: return JsonResponse({"Error": "First name Required"}) try: lastname = request.data.get('firstname', None) except MultiValueDictKeyError: return JsonResponse({"Error": "Last name Required"}) try: email = request.data.get('email', None) except MultiValueDictKeyError: return JsonResponse({"Error": "Email Required"}) try: City = request.data.get('city', None) except MultiValueDictKeyError: return JsonResponse({"Error": "City is required"}, status=500) try: State = request.data.get('state', None) except MultiValueDictKeyError: return JsonResponse({"Error": "State is required"}, status=500) try: PhoneNumber = request.data.get('phoneNumber', None) except MultiValueDictKeyError: return JsonResponse({"Error": "Phone Number is required"}, status=500) try: LicenseIDNumber = request.data.get('LicenseIDNumber', None) except MultiValueDictKeyError: return JsonResponse({"Error": "LicenseIDNumber is required"}, status=500) try: BusinessAddress = request.data.get('buisness_address', None) except MultiValueDictKeyError: return JsonResponse({"Error": "BusinessAddress is required"}, status=500) try: HCRNo = request.data.get('hcr_number', None) except MultiValueDictKeyError: return JsonResponse({"Error": "hcr_number is required"}, status=500) user = User.objects.get(username=username) lawyer = Lawyer.objects.get(user=user) allcategories = json.loads(categories) lawyerid = lawyer.id for i in allcategories: if bool(i['checked']) == False: try: cat = Category.objects.get(id=i['id']) obj = LawyerCategory.objects.get(lawyerid=lawyer, categoryid=cat) obj.delete() except LawyerCategory.DoesNotExist: pass else: try: cat = Category.objects.get(id=i['id']) LawyerCategory.objects.get(lawyerid=lawyer, categoryid=cat) except LawyerCategory.DoesNotExist: obj = LawyerCategory(lawyerid=lawyer, categoryid=cat) obj.save() if photo.find('https://leaid.herokuapp.com') == -1: data = photo.split(',')[1] imgdata = base64.b64decode(data) filename = username + '.jpg' file = ContentFile(imgdata, name=filename) lawyer.image = file file.close() lawyer.about = about lawyer.contact = contact user.first_name = firstname user.last_name = lastname lawyer.city = City lawyer.state = State lawyer.phone_number = PhoneNumber lawyer.liscence_number = LicenseIDNumber lawyer.hcr_number = HCRNo user.email = email user.save() lawyer.save() return JsonResponse({"Success": "Image Saved"})
def handle(self, *args, **options): if already_downloaded(): self.stdout.write('Menu already downloaded\n') if options['force']: MenuOfTheDay.objects.get(day=date.today()).delete() else: return r = check_menu(get_pdf_url2) if r is None: r = check_menu(get_pdf_url) if r is None: r = parse_pdf_url() if r is not None: try: if not options['force']: MenuOfTheDay.objects.get(downloaded_from=r.url) self.stdout.write('Menu already downloaded\n') send_notification('Menu already donwloaded', r.url) return except MenuOfTheDay.DoesNotExist: pass m = MenuOfTheDay() m.day = date.today() f = ContentFile(r.content) m.pdf.save('temp', f) m.published = False m.downloaded_from = r.url m.save() try: courses = pdf_to_courses(f) f.close() for course in courses: if not course['name']: continue try: c = Course.objects.get(name=course['name']) if c.description != course['description']: c.description = course['description'] c.save() except Course.DoesNotExist: c = Course(name=course['name'], description=course['description'], type=course['type']) c.save() m.courses.add(c) m.published = True m.save() if options['send_mail']: body = render_to_string('menu/mail_menu.mdown', { 'menu': m, 'courses': m.courses.all(), }) mail_managers(u'[Unisa Menu] Menu caricato', body) send_notification('Done', 'Menu downloaded') self.stdout.write('Done.\n') except PDFParsingError: mail_managers(u'[Unisa Menu] Problema PDF', 'Mi scoccio') self.stdout.write('Problem while parsing the PDF.\n') else: send_notification('Not Found', 'Menu does not exist.') self.stdout.write('Menu does not exists.\n')
def write_second_pdf(self, pdf_second: ContentFile): pdf_second_reader = PyPDF2.PdfFileReader(pdf_second) self._write(pdf_second_reader) pdf_second.close()