def create_profile_data(strategy, user, response, details, is_new=False, *args, **kwargs): if is_new: if 'fullname' in details: full_name = details['fullname'] else: full_name = user.username new_profile = Profile(user=user, name=full_name) new_profile.user_id = user.id new_profile.name = user.username new_profile.user.email = details[ 'email'] if 'email' in details else None url = 'https://graph.facebook.com/{0}/picture'.format(response['id']) try: img_content = request('GET', url, params={'type': 'large'}) file_path = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'uploads', 'facebook', '{0}__social.jpg'.format(user.id)) with open(file_path, 'wb+') as destination: img_file = ContentFile(img_content.content) for chunk in img_file.chunks(): destination.write(chunk) new_profile.photo = 'facebook/{0}__social.jpg'.format(user.id) new_profile.save() profile_settings = Settings.objects.get(profile_id=new_profile.id) profile_settings.email = details[ 'email'] if 'email' in details else None profile_settings.feed_radius = -1 profile_settings.save() return except Exception as e: raise e elif user.date_joined >= (datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(hours=1)): existing_profile = Profile.objects.get(user_id=user.id) url = 'https://graph.facebook.com/{0}/picture'.format(response['id']) try: img_content = request('GET', url, params={'type': 'large'}) file_path = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'uploads', 'facebook', '{0}__social.jpg'.format(user.id)) with open(file_path, 'wb+') as destination: img_file = ContentFile(img_content.content) for chunk in img_file.chunks(): destination.write(chunk) existing_profile.photo = 'facebook/{0}__social.jpg'.format(user.id) existing_profile.save() except Exception as e: raise e
def post(self, request): written_filename = None for filename, file in request.FILES.iteritems(): BASE_DIR = os.path.dirname(os.path.dirname(__file__)) output_folder = "%s/static/avatars" % (BASE_DIR) name, extension = os.path.splitext(file.name) output_filename = "%s%s" % (request.user.pk, extension) destination = "%s/%s" % (output_folder, output_filename) file_content = ContentFile(request.FILES[filename].read()) # TODO - Need to parse the name and give the output file the right extension. fout = open(destination, 'wb+') for chunk in file_content.chunks(): fout.write(chunk) fout.close() makeThumb(destination) written_filename = destination data = destination.replace(BASE_DIR, "") # Replace user avatar user = CustomUser.objects.get(pk=request.user.pk) user.avatar = data user.save() output = {} output["data"] = data output["statusText"] = "File uploaded successfully" return Response(output, status=201)
def storeConfigFile(file): configFileRelativePath = "storage/config/" + file.name configFilePath = getFullFilePath(configFileRelativePath) md5sum = hashlib.md5() fout = open(configFilePath, 'wb+') file_content = ContentFile(file.read()) for chunk in file_content.chunks(): fout.write(chunk) md5sum.update(chunk) fout.close() configJsonFileRelativePath = "storage/config/configFile.json" configJsonFilePath = getFullFilePath(configJsonFileRelativePath) configFile = { "fileName": file.name, "md5sum": md5sum.hexdigest(), "updatedAt": datetime.datetime.now().strftime("%d %b, %Y %I:%M %P") } deleteConfigFile() json_data = json.dumps(configFile) with open(configJsonFilePath, "wb") as f: f.write(bytes(json_data, 'utf-8'))
def storeConfigFile(file): configFileRelativePath = "storage/config/"+file.name configFilePath = getFullFilePath(configFileRelativePath) md5sum = hashlib.md5() fout = open(configFilePath, 'wb+') file_content = ContentFile( file.read() ) for chunk in file_content.chunks(): fout.write(chunk) md5sum.update(chunk) fout.close() configJsonFileRelativePath = "storage/config/configFile.json" configJsonFilePath = getFullFilePath(configJsonFileRelativePath) configFile = { "fileName": file.name, "md5sum": md5sum.hexdigest(), "updatedAt" : datetime.datetime.now().strftime("%d %b, %Y %I:%M %P") } deleteConfigFile() json_data = json.dumps(configFile) with open(configJsonFilePath, "wb") as f: f.write(bytes(json_data, 'utf-8'))
def test_to_internal_value(self): base64_header = "data:image/jpeg;base64," base64_data = ( "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAKBueIx4ZKCMgoy0qqC+8P//8Nzc8P/////////////////////" "/////////////////////////////////////2wBDAaq0tPDS8P///////////////////////////////////////////////////////" "///////////////////////wgARCADIAMgDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAECA//EABYBAQEBAAAAAAAAAAAAAAA" "AAAABAv/aAAwDAQACEAMQAAABwADU6ZMtZFujDQzNwzbowtMNCZ68gAAADTI1kLci3I1INMjTI0yNZAAAAAAAAA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KUY26dv3KUeXQ6RGx7Hb+uCBXn+Ihuir7fM" "//Z") base64_full = base64_header + base64_data expected = ContentFile(base64.b64decode(base64_data), name='tmp.jpeg') assert list(StdImageSerializerField().to_internal_value( base64_full).chunks()) == list(expected.chunks())
def test_file_greater_than_5MB(self): name = 'test_storage_save.txt' content = ContentFile('0' * 10 * 1024 * 1024) # Set the encryption flag used for multipart uploads self.storage.encryption = True # Set the ACL header used when creating/writing data. self.storage.bucket.connection.provider.acl_header = 'x-amz-acl' # Set the mocked key's bucket self.storage.bucket.get_key.return_value.bucket = self.storage.bucket # Set the name of the mock object self.storage.bucket.get_key.return_value.name = name def get_upload_file_size(fp): pos = fp.tell() fp.seek(0, os.SEEK_END) length = fp.tell() - pos fp.seek(pos) return length def upload_part_from_file(fp, part_num, *args, **kwargs): if len(file_part_size) != part_num: file_part_size.append(get_upload_file_size(fp)) file_part_size = [] f = self.storage.open(name, 'w') # initiate the multipart upload f.write('') f._multipart.upload_part_from_file = upload_part_from_file for chunk in content.chunks(): f.write(chunk) f.close() assert content.size == sum(file_part_size)
def send_video_blob(req): BASE_PATH = 'huangsite/static/' folder = 'videos' file = req.FILES['videoFile'].read() hash_name = hashlib.md5(file).hexdigest() try: os.mkdir(os.path.join(BASE_PATH, folder)) except: pass full_name = os.path.join(BASE_PATH, folder, hash_name) file_content = ContentFile(file) fout = open(full_name + '.webm', 'wb+') try: # Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() add_video_url({ 'url': '/static/videos/' + hash_name + '.webm', 'update_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S') }) return Rejson.success( {'content': { 'url': 'static/videos/' + hash_name + '.webm' }}) except: return Rejson.error({'message': 'CANNOT SAVE THE FILE'})
def upload_code(request): if request.method == 'POST': obj = request.FILES.get('file') project_name = request.POST.get('project_name') print project_name code_path = request.POST.get('code_path') print code_path # print(obj.name) code = ContentFile(obj.read()) code_content = "" for chunk in code.chunks(): code_content += chunk print code_content file.objects.create(code_content=code_content, code_path=code_path, project_name=project_name, code_owner=request.session['user_name'], update_time=datetime.now()) return HttpResponseRedirect('/code/code_timeline') else: projects = user.objects.filter(user_name=request.session['user_name']) return render_to_response( 'pages/code/code_upload.html', { 'projects': projects, 'request': request, 'username': request.session['user_name'] })
def myadsadd(request): if request.user.is_authenticated: if request.method == 'POST': form = MyAdsAddForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] t = form.cleaned_data['tags'] d = form.cleaned_data['description'] o = request.user.profile s = Stuff(tags=t, description=d, owner=o, name=name) #Check if picture has been loaded. if 'myfile' in request.FILES: pic1 = request.FILES['myfile'] image_path = 'Storage/%s/' % ( request.user.username) + name + str(pic1)[:-4] save_path = './stuffsharing/static/' + image_path print('path', save_path) directory = os.path.dirname(save_path) if not os.path.exists(directory): os.makedirs(directory) fout = open(save_path, 'wb+') file_content = ContentFile(pic1.read()) for chunk in file_content.chunks(): fout.write(chunk) s.image = image_path s.save() form = MyAdsAddForm() return render(request, 'stuffsharing/myadsadd.html', {'form': form})
def identifikasi_wajah(request): imgData = request.POST['hasil'] format, imgstr = imgData.split(";base64,") dataDecode = ContentFile(base64.b64decode(imgstr)) imgRandom = get_random_string(10) nama_gambar = imgRandom + ".png" with open("ladun/pic_identifikasi/" + nama_gambar, "wb+") as f: for chunk in dataDecode.chunks(): f.write(chunk) alamat_pic = "http://127.0.0.1:7001/ladun/pic_upload/rUZ4sdKVw3.png" url = "https://api.luxand.cloud/photo/search" payload = {} headers = {'token': "0c5e5b2cd47c480fbfa6066c3aee9970"} files = {"photo": open("ladun/pic_identifikasi/" + nama_gambar, "rb")} # payload["photo"] = alamat_pic response = requests.request("POST", url, data=payload, headers=headers, files=files) # result = client.recognize(photo = alamat_pic) hasil = response.text context = {'status': 'sukses', 'hasil': hasil, 'imgData': imgData} return JsonResponse(context, safe=False)
def post(self,request,format=None): folder = 'predic_images/' #request.path.replace("/", "_") uploaded_filename = request.FILES['file'].name BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # create the folder if it doesn't exist. try: os.mkdir(os.path.join(BASE_PATH, folder)) except: pass # save the uploaded file inside that folder. full_filename = os.path.join(BASE_PATH, folder, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile( request.FILES['file'].read() ) try: # Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() allpreds, veg_index, veg_name = get_pred(full_filename) os.remove(full_filename) return Response({'key': allpreds, 'veg_index' : veg_index, 'veg_name' : veg_name}, status=status.HTTP_201_CREATED) except Exception as inst: raise inst return Response({'key': 'NOT SAVED'}, status=status.HTTP_201_CREATED) return Response({'key': 'value'}, status=status.HTTP_201_CREATED)
def handle_uploaded_file(request): #Initializing files = [] folder = 'zendesk_user_uploads' BASE_PATH = '/tmp/' attachment_list = [] #Enter the loop only if attachments are present if request.method == 'POST' and request.FILES['attachment']: attachment_list = request.FILES.getlist('attachment') for file in attachment_list: uploaded_filename = file.name if file.size > 1000: raise Exception( '%s is too large. Please attach files that are less than 1MB.' % uploaded_filename) #break full_filename = os.path.join(BASE_PATH, folder, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile(file.read()) #Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() files.append(str(full_filename)) return files
def post(self, request): emotions = ["angry", "happy", "sad", "neutral"] img = request.FILES['image'].name #edited here #image = cv2.imread(img, 0) print(img) print(str(request.FILES['image'].size)) BASE_PATH = 'Images/' # f= open("guru.jpg","w+") # f.close() print("shape") # print(image.shape) # cv2.imwrite(''+img,image) # shutil.copy(img,"guru.jpg") folder = "" try: print("try") os.mkdir(os.path.join(BASE_PATH, folder)) except: print("except") pass # save the uploaded file inside that folder. full_filename = os.path.join(BASE_PATH, folder, img) fout = open(full_filename, 'wb+') # Iterate through the chunks. file_content = ContentFile( request.FILES['image'].read() ) for chunk in file_content.chunks(): fout.write(chunk) fout.close() image = cv2.imread("Images/"+img, 0) height, width = image.shape[:2] print(str(height)+"-"+str(width)) #edit complete facecascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") fishface = cv2.face.FisherFaceRecognizer_create() # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) image = clahe.apply(image) face = facecascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=15, minSize=(10, 10), flags=cv2.CASCADE_SCALE_IMAGE) for (x, y, w, h) in face: faceslice = image[y:y+h, x:x+w] faceslice = cv2.resize(faceslice, (350, 350)) try: fishface.read("trained_emoclassifier.xml") # print("loaded") except: return Response('NOT DONE', status=status.HTTP_201_CREATED) # print("no xml found. Using --update will create one.") pred, conf = fishface.predict(faceslice) os.remove("Images/"+img) return Response(emotions[pred], status=status.HTTP_201_CREATED)
def photoupload(request): if request.user.is_authenticated(): uname = str(request.user) uploaded_filename = request.FILES['file'].name base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/ProfilePicApp/static/users/' destination = 'users/' + uname +"/"+ uploaded_filename # save the uploaded file inside that folder. full_filename = os.path.join(base_path, uname, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile( request.FILES['file'].read() ) try: # Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() try: savepic = ProfilePics.objects.get(username=uname) savepic.profilepic = destination savepic.save() except: savepic = ProfilePics(username=uname, profilepic=destination) savepic.save() return HttpResponseRedirect('/home') except: return HttpResponseRedirect('/home')
def nuevo_turismo(request): if request.method == 'POST' and request.FILES['image']: form = NuevoTurismoForm(request.POST) if form.is_valid(): folder = '/static/images/turismo/' uploaded_filename = request.FILES['image'].name BASE_PATH = os.path.abspath(os.path.dirname(__file__)) full_filename = BASE_PATH + folder + uploaded_filename fout = open(full_filename, 'wb+') file_content = ContentFile(request.FILES['image'].read()) try: # Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() turismo = Turismo( nombreSitio=form.cleaned_data["nombreSitio"], direccion=form.cleaned_data["direccion"], descripcion=form.cleaned_data["descripcion"], imagen="/images/turismo/" + uploaded_filename, categoria=form.cleaned_data["categoria"], fechaAlta=timezone.now(), ) turismo.save() mensaje = "Nuevo sitio de turismo creado correctamente" return render(request, 'web/administrador/index.html', {'mensaje': mensaje}) except: mensaje = "No se ha podido crear" return render(request, 'web/administrador/index.html', {'mensaje': mensaje}) form = NuevoTurismoForm() return render(request, 'web/administrador/forms/nuevo_turismo.html', {'form': form})
def test(request): video_filename = MEDIA_ROOT + "/" + "121212" + request.FILES["track[video]"].name file_content = ContentFile(request.FILES["track[video]"].read()) with open(video_filename, "wb") as fp: for chunk in file_content.chunks(): fp.write(chunk) return HttpResponse()
def post(self, request): folder = 'chapter_pdf' uploaded_filename = request.FILES['upload'].name try: os.mkdir(os.path.join( BASE_DIR+"/static/", folder)) except Exception as e: logging.debug(e) pass # save the uploaded file inside that folder. uploaded_filename = datetime.datetime.now().strftime("%y_%m_%d_%H_%M_%S_%f_file.pdf") full_filename = os.path.join( BASE_DIR+"/static/", folder, uploaded_filename) store_path = folder+'/'+uploaded_filename fout = open(full_filename, 'wb+') file_content = ContentFile(request.FILES['upload'].read()) try: # Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() data = Chapterdetails(chapter_name = request.POST['chaptername']) data.cat = request.POST['cat'] data.chapter_path = store_path data.save() html = "<html><body>SAVED</body></html>" return HttpResponse(json.dumps({'status':200})) except Exception as e: print e logging.debug(e) html = "<html><body>NOT SAVED</body></html>" return HttpResponse(json.dumps({'status':400}))
def save_uploaded_file(f, path): full_uploading_filename = os.path.join(path, 'uploading.csv') fout = open(full_uploading_filename, 'wb+') file_content = ContentFile(f.read()) for chunk in file_content.chunks(): fout.write(chunk) fout.close() return full_uploading_filename
def post(self, request): try: print request.POST['exam_name'] print request.FILES['upload'] exam_list_data = Examlist.objects(cat= request.POST['cat']) if exam_list_data: Examlist.objects(cat=request.POST['cat']).update_one(push__exam_list=request.POST['exam_name']) else: cdata = Examlist(cat= request.POST['cat']) cdata.exam_list = [request.POST['exam_name']] cdata.save() folder = 'all_csv_files' uploaded_filename = request.FILES['upload'].name try: os.mkdir(os.path.join(BASE_DIR, folder)) except: pass # save the uploaded file inside that folder. uploaded_filename = datetime.datetime.now().strftime("%y_%m_%d_%H_%M_%S_%f_file.csv") full_filename = os.path.join(BASE_DIR, folder, uploaded_filename) store_path = folder+'/'+uploaded_filename fout = open(full_filename, 'wb+') file_content = ContentFile(request.FILES['upload'].read()) # Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() with open(full_filename, 'rb') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') counter = 0 for row in spamreader: print '-------------------------' data = ', '.join(row) details = data.split(",") if counter > 0: exam_details_insert = Examdetails(exam_name = request.POST['exam_name'], cat = request.POST['cat']) exam_details_insert.cat = request.POST['cat'] exam_details_insert.question =details[0] exam_details_insert.a = details[1] exam_details_insert.b = details[2] exam_details_insert.c = details[3] exam_details_insert.d = details[4] exam_details_insert.correct = details[5] exam_details_insert.time = request.POST['exam_time'] exam_details_insert.pass_mark = request.POST['exam_pass_mark'] exam_details_insert.save() counter += 1 return HttpResponse(json.dumps({'status':200})) except Exception as e: logging.debug(e) return HttpResponse(json.dumps({'status':400}))
def test_file_chunks_error(self): """ Test behaviour when file.chunks() is raising an error """ f1 = ContentFile('chunks fails') def failing_chunks(): raise IOError f1.chunks = failing_chunks with self.assertRaises(IOError): self.storage.save('error.file', f1)
def post(self, request, folder): upload_file = request.FILES['document'].name full_filename = os.path.join(self.absolute_path, upload_file) file_content = ContentFile(request.FILES['document'].read()) fout = open(full_filename, 'wb+') # Iterate through the chunks. for chunk in file_content.chunks(): fout.write(chunk) fout.close() return redirect('contein_dir', folder=os.path.basename(self.absolute_path))
def test_file_chunks_error(self): """ Test behavior when file.chunks() is raising an error """ f1 = ContentFile("chunks fails") def failing_chunks(): raise OSError f1.chunks = failing_chunks with self.assertRaises(OSError): self.storage.save("error.file", f1)
def post(self,request): user_id = request.session.get('userid') user = Users.objects.get(id = user_id) f_name = request.POST.get('firstname') l_name = request.POST.get('lastname') mobile = request.POST.get('mobile') organization_name = request.POST.get('Organization_name') profile_image = request.FILES.get('Image') print(profile_image,'>>>>>>>>>>>') if profile_image: image_name = (profile_image.name).strip() try: Users.objects.get(profile_pic = image_name) random_string = str(uuid.uuid4())[:8] image_name = random_string + '_' + image_name except Users.DoesNotExist: pass profile_pic = settings.MEDIA_URL + image_name try: print(profile_pic) full_path = settings.MEDIA_DIR + '/' + image_name print(full_path) print(profile_pic) fout = open(full_path, 'wb+') file_content = ContentFile(profile_image.read()) for chunk in file_content.chunks(): fout.write(chunk) fout.close() user.firstname = f_name user.lastname = l_name user.mobile = mobile user.organization_name = organization_name user.profile_pic = profile_pic user.save() except Exception as e: raise e else: img = user.profile_pic user.firstname = f_name user.lastname = l_name user.mobile = mobile user.organization_name = organization_name user.profile_pic = img user.save() return HttpResponseRedirect('/live/dashboard/update_info')
def proses_tambah_pegawai(request): url = "https://api.luxand.cloud/subject/v2" headers = {"token": "0c5e5b2cd47c480fbfa6066c3aee9970"} # 'dataImg': dataImg, 'nama':nama, 'alamat':alamat, 'password':password imgData = request.POST['dataImg'] nama = request.POST['nama'] alamat = request.POST['alamat'] jk = request.POST['jk'] password = request.POST['password'] username = request.POST['username'] pass_hash = hashlib.md5(password.encode("utf-8")).hexdigest() format, imgstr = imgData.split(";base64,") dataDecode = ContentFile(base64.b64decode(imgstr)) imgRandom = get_random_string(10) nama_gambar = imgRandom + ".png" with open("ladun/pic_upload/" + nama_gambar, "wb+") as f: for chunk in dataDecode.chunks(): f.write(chunk) # start upload to facesoft name = username store = '1' alamat_pic = "http://127.0.0.1:7001/ladun/pic_upload/" + nama_gambar payload = {"name": name, "store": store} files = {"photo": open("ladun/pic_upload/" + nama_gambar, "rb")} payload["photo"] = alamat_pic response = requests.request("POST", url, data=payload, headers=headers, files=files) save_akses_login = Akses_Login.objects.create(kd_pegawai=imgRandom, username=username, kata_sandi=pass_hash, mac_x="-", mac_y="-", secret_key="NURUL") save_akses_login.save() save_pegawai = Pegawai.objects.create(kd_pegawai=imgRandom, nama_pegawai=nama, jenis_kelamin=jk, alamat=alamat, akses="administrator") save_pegawai.save() context = { 'status': 'sukses', 'respons': response.text, 'lokasi': alamat_pic, 'nama': nama } return JsonResponse(context, safe=False)
def combine_video(video_param, audio_param, prefix_num, request): video_filename = MEDIA_ROOT + "/" + str(prefix_num) + request.FILES[video_param].name file_content = ContentFile(request.FILES[video_param].read()) with open(video_filename, "wb") as fp: for chunk in file_content.chunks(): fp.write(chunk) audio_file_content = ContentFile(request.FILES[audio_param].read()) audio_filename = MEDIA_ROOT + "/" + str(prefix_num) + request.FILES[audio_param].name with open(audio_filename, "wb") as fp: for chunk in audio_file_content.chunks(): fp.write(chunk) audio_convert_file_name = ( MEDIA_ROOT + "/" + str(prefix_num) + request.FILES[audio_param].name.split(".")[0] + ".wav" ) encoded_file_name = MEDIA_ROOT + "/" + "new" + str(prefix_num) + request.FILES[video_param].name mp4_file_name = encoded_file_name.split(".")[0] + ".mp4" video_filename_without_path = "new" + str(prefix_num) + request.FILES[video_param].name sox_command = "sox -t ul -U -r 16000 -c 1 " + audio_filename + " " + audio_convert_file_name avconv_command = ( "avconv -i " + audio_convert_file_name + " -i " + video_filename + " -acodec copy -vcodec copy " + encoded_file_name ) print "sox, avconv commands" print sox_command os.system(sox_command) print avconv_command os.system(avconv_command) transcoding(video_filename_without_path) if os.path.exists(audio_filename): os.remove(audio_filename) if os.path.exists(video_filename): os.remove(video_filename) return mp4_file_name, video_filename_without_path
def post(self, request, jobId, taskId): #, *args, **kwargs): timelog = self.getTimeLog(request.POST['timelog']) if not timelog: return errorResponse('Time log does not exist.') elif not isJobAssigned(request.user.id, jobId): return errorResponse('Job not accessible.') else: file = request.FILES['file'] if file and self.allowed_image_file(file.name): filename = file.name name, ext = os.path.splitext(filename) filename = str(uuid.uuid4()) + ext full_filename = os.path.join(UPLOAD_DIR, filename) fout = open(full_filename, 'wb+') file_content = ContentFile( request.FILES['file'].read() ) try: for chunk in file_content.chunks(): fout.write(chunk) fout.close() data = { "image": settings.SERVER_HOST + settings.STATIC_URL + 'uploads/' + filename, "timelog": request.POST['timelog'] } serializer = TimeLogMediaSerializer(data=data) if serializer.is_valid(): serializer.save() timelog.end_date = datetime.datetime.now() timelog.save() result = { 'success': True } except: result = { 'success': False } else: result = { 'success': False, 'message': 'Invalid file' } return JsonResponse(result, safe=False)
def send_form_ajax_file(request): if request.is_ajax and request.method == "POST": clicked = str(request.POST.get('current_clicked')) file_name = request.POST.get('file' + clicked + '_name') file = request.FILES.get('file' + str(clicked)) folder = 'wszystkie_pliki/pliki/' + str(timezone.now().year) + '/' BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) try: os.mkdir(os.path.join(BASE_PATH, folder)) except: pass try: os.mkdir(os.path.join(BASE_PATH, folder, file_name)) except: pass name, extension = os.path.splitext(file.name) currentYear = timezone.now().year uploaded_filename = file_name + '_' + str(clicked) + '_' + str( currentYear) + '_' + get_random_string(length=10) + extension full_filename = os.path.join(BASE_PATH, folder, file_name, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile(file.read()) for chunk in file_content.chunks(): fout.write(chunk) fout.close() save = Files(file_name=file_name, file=file, author=get_author(request.user)) save.save() if file is not None and file_name is not None: return JsonResponse({ 'clicked': clicked, 'error': 'false', }, status=200) user = get_author(request.user) if user.rank.create_files: pass else: raise Http404 return redirect(reverse("add_file"))
def upload_file(request, ses_id): try: if request.method == 'POST' and request.FILES['file']: myfile = request.FILES['file'] ran = randomString(10) support_ext = ['jpg', 'jpeg', 'pdf', 'cpp'] folder = "templates/media/upload_attachment/" SITE_ROOT = os.path.dirname(os.path.realpath(__name__)) full_filename = os.path.join(SITE_ROOT, folder, ran + myfile.name) ext = myfile.name.split(".")[-1] if ext.lower() in support_ext: fout = open(full_filename, 'wb+') file_content = ContentFile(myfile.read()) for chunk in file_content.chunks(): fout.write(chunk) fout.close() filesize = os.path.getsize(full_filename) if filesize <= 5242880: insert = post_att.objects.create( status="Active", creation_session=ses_id, filename=ran + myfile.name, upload=ran + myfile.name, actual_filename=myfile.name, extension=ext) if insert: status = 200 else: status = 499 else: status = 487 else: status = 488 else: status = 499 except: status = 499 return HttpResponse(request, "", status)
def post(self, request, *args, **kwargs): """Handles POST request Notes ----- 1. Saves uploaded test file to directory and name it 2. Fetch parameters of most accurate experiment 3. Run test script with test image name and parameters from last step 4. Map test experiment result to keys and return response """ request_data = request.data.dict() image_file = request_data['test_image'] test_images_dir = settings.TEST_IMAGES_DIR_PATH if not os.path.exists(test_images_dir): os.makedirs(test_images_dir) test_filename = os.path.join(test_images_dir, image_file.name) with open(test_filename, 'wb+') as file_obj: file_content = ContentFile(image_file.read()) for chunk in file_content.chunks(): file_obj.write(chunk) # Fetch parameters of most accurate experiment best_experiment = Experiment.objects.order_by('-accuracy').first() if not best_experiment: raise Exception('No experiment has been performed yet') learning_rate = str(best_experiment.learning_rate) layers_count = str(best_experiment.layers_count) steps_count = str(best_experiment.steps_count) # Execute test script script_path = os.path.abspath('model_scripts/test.py') test = Popen([ "python", script_path, "--i", learning_rate, "--j", layers_count, "--k", steps_count, "--image", test_filename ], stdout=PIPE, stderr=STDOUT) exp_result = test.stdout.read() test_dict = ast.literal_eval(exp_result.decode('utf-8')) result_dict = { 'learning_rate': test_dict['i'], 'layers_count': test_dict['j'], 'steps_count': test_dict['k'], 'accuracy': test_dict['accuracy'] } return Response(result_dict, status=status.HTTP_201_CREATED)
def _save_to_storage(self, given_file, file_format, submission_id): destination = (f'/teams/{self._trial.team_task.team.name}/' f'trial_{self.trial_id}/qs_{submission_id}/') uploaded_filename = 'f_' + uuid.uuid4().hex[:16] + '.' + file_format try: os.makedirs(settings.MEDIA_ROOT + destination, exist_ok=True) except OSError: print('oops') full_filename = settings.MEDIA_ROOT + destination + uploaded_filename copied_file = open(full_filename, 'wb+') file_content = ContentFile(given_file.read()) for chunk in file_content.chunks(): copied_file.write(chunk) copied_file.close() return destination + uploaded_filename
def upload(request, format=None): uploaded_filename = request.FILES['file'].name # save the uploaded file inside that folder. full_filename = './back_end/crops/uploads/' + uploaded_filename fout = open(full_filename, 'wb+') # Iterate through the chunks. file_content = ContentFile(request.FILES['file'].read()) for chunk in file_content.chunks(): fout.write(chunk) fout.close() base_name = uploaded_filename.split('.')[0] elements = loadElements('./back_end/crops/uploads/' + base_name + '.json') processDocument(full_filename, elements) return Response({'uuid': "Suss"})
def add_mult_image(request): user = get_author(request.user) if user.rank.adding_images: pass else: raise Http404 folder = 'wszystkie_pliki/zdjecia/' + str(timezone.now().year) + '/' BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) try: os.mkdir(os.path.join(BASE_PATH, folder)) except: pass if request.is_ajax and request.method == "POST": images_name = request.POST.get('images_name') try: os.mkdir(os.path.join(BASE_PATH, folder, images_name)) except: pass pomocnicza = 0 for f in request.FILES.getlist('images_mult'): name, extension = os.path.splitext(f.name) currentYear = timezone.now().year uploaded_filename = images_name + '_' + str( pomocnicza) + '_' + str(currentYear) + '_' + get_random_string( length=10) + extension full_filename = os.path.join(BASE_PATH, folder, images_name, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile(f.read()) for chunk in file_content.chunks(): fout.write(chunk) fout.close() pomocnicza = pomocnicza + 1 images = Pictures(picture_title=images_name, picture=f, author=user) images.save() return JsonResponse({'error': 'false'}, status=200) return redirect(reverse("add_image"))
def post(self, request): # Save source image for image crop widget max_upload_size = settings.PRODUCT_MAX_UPLOAD_SIZE if request.POST.get('is_company', None): max_upload_size = settings.COMPANY_MAX_UPLOAD_SIZE uploaded_filename = request.FILES['file'].name full_filename = os.path.join(settings.MEDIA_ROOT, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile(request.FILES['file'].read()) if file_content._size > max_upload_size: return JsonResponse({'error': "File too big."}) for chunk in file_content.chunks(): fout.write(chunk) fout.close() return JsonResponse({'url': '/media/{}'.format(uploaded_filename)})
def put(self, request, id, format=None): file_obj = request.data['file'] folder = request.path.replace("/", "_") full_filename = os.path.join(settings.MEDIA_ROOT, id) fout = open(full_filename, 'wb+') file_content = ContentFile(file_obj.read()) try: contacto = Contacto.objects.get(id=id) except ObjectDoesNotExist: return Response(status=204) contacto.imagen = full_filename contacto.save() for chunk in file_content.chunks(): fout.write(chunk) fout.close() return Response(status=204)
def Home(request): import os # output = script_function() # return HttpResponse(output) if request.method == 'GET': obj=get_object_or_404(TorrentDownload, id=1) obj.percentage='0' obj.peers='0' print obj obj.save() return render(request,'home/home.html') elif request.method == 'POST': # print "POST Successfull" # torrent_download = threading.Thread(target=start_downoading) # torrent_download.daemon = True # torrent_download.start() folder = 'Scripts/' #request.path.replace("/", "_") uploaded_filename = request.FILES['file'].name BASE_PATH = '/home/bhavi/py_torrent_client/BittorentGui/' full_filename = os.path.join(BASE_PATH, folder, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile( request.FILES['file'].read() ) # print "File name", uploaded_filename try: for chunk in file_content.chunks(): fout.write(chunk) fout.close() # html = "<html><body>SAVED</body></html>" # return HttpResponse(html) except: html = "<html><body>FILE NOT SAVED</body></html>" return HttpResponse(html) global torrent_download torrent_download = Process(target=start_downoading, kwargs={"file_name":uploaded_filename}) torrent_download.daemon = True torrent_download.start() return render(request,'home/downloading.html') # output = script_function() return HttpResponse("Downloading")
def picture(request): print("in picture") if request.method == 'POST': print("in post") form = ImageForm(request.POST, request.FILES) if form.is_valid(): folder = 'profile_pics' uploaded_filename = request.session['username'] + '.' + request.FILES['image'].name # create the folder if it doesn't exist. try: os.makedirs(os.path.join(PIC, folder)) except: pass # save the uploaded file inside that folder. db_path = folder + '/' + uploaded_filename full_filename = os.path.join(PIC, folder, uploaded_filename) fout = open(full_filename, 'wb+') file_content = ContentFile(request.FILES['image'].read()) try: for chunk in file_content.chunks(): fout.write(chunk) fout.close() try: picture = Picture.objects.get(user_id=request.session['user_id']) picture.user_id = request.session['user_id'] picture.data = db_path picture.save() print("in try") except Picture.DoesNotExist: Picture.create(user_id=request.session['user_id'], data=db_path) print("does not exist") except: return redirect('/picture') return redirect('/profile') else: form = ImageForm() return render(request, 'uploadimage.html', {'form': form})
def test(request): BASE_PATH="/home/ubuntu/files" name = request.FILES['code'].name file_content = ContentFile( request.FILES['code'].read() ) now = "temp-" + datetime.now().strftime("%Y-%m-%d-%H-%M-%S") try: os.mkdir(os.path.join(BASE_PATH, str(now))) filename = os.path.join(BASE_PATH, str(now), name) fout = open(filename, 'wb+') for chunk in file_content.chunks(): fout.write(chunk) fout.close() arg_string = "cp " + os.path.join(settings.BASE_DIR,"3240HW4Grader.py") + " " + os.path.join(BASE_PATH,str(now)) arg_string += ";cp " + os.path.join(settings.BASE_DIR,"3240test1.txt") + " " + os.path.join(BASE_PATH,str(now)) arg_string += ";cp " + os.path.join(settings.BASE_DIR,"3240test2.jpg") + " " + os.path.join(BASE_PATH,str(now)) arg_string += ";cp " + os.path.join(settings.BASE_DIR,"3240test3.txt") + " " + os.path.join(BASE_PATH,str(now)) arg_string += ";python3 " +os.path.join(BASE_PATH,str(now),"3240HW4Grader.py -d") results = RunCmd(arg_string, 60).Run() #subprocess.call("copy " + os.path.join(settings.BASE_DIR,"3240HW4Grader.py") + " " + os.path.join(BASE_PATH,str(now)), shell=True) #results = subprocess.check_output("py -3 " +os.path.join(BASE_PATH,str(now),"3240HW4Grader.py -d"), shell=True) results = results.replace(b'\n',b'<br />') except: results = "Sorry something went wrong. Try again or email David." return HttpResponse(results)
def fleet_track1(request): import os if request.method == "POST": base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) prefix_num = TrackPrefix().prefix rear_img = None front_img = None try: rear_img = MEDIA_ROOT + "/" + str(prefix_num) + request.FILES["track[rear_img]"].name rear_file_content = ContentFile(request.FILES["track[rear_img]"].read()) with open(rear_img, "wb") as fp: for chunk in rear_file_content.chunks(): fp.write(chunk) front_img = MEDIA_ROOT + "/" + str(prefix_num) + request.FILES["track[front_img]"].name front_file_content = ContentFile(request.FILES["track[front_img]"].read()) with open(front_img, "wb") as fp: for chunk in front_file_content.chunks(): fp.write(chunk) image_cmd_string = ( ' --form "track[rear_img]=@' + rear_img + ';type=image/jpeg" ' + ' --form "track[front_img]=@' + front_img + ';type=image/jpeg" ' ) except MultiValueDictKeyError as e: image_cmd_string = "" result_file_path = base_dir + "/" + str(prefix_num) + "track1_result" print result_file_path # --form 'track[front_img]=@$FRONT_JPEG;type=image/jpeg' --form 'track[rear_img]=@$REAR_JPEG;type=image/jpeg' cmd = ( 'curl -H "Cookie: _trackvue_session=' + request.POST["cookie"] + '"' + ' --form "track[driver_id]=' + request.POST["track[driver_id]"] + '"' + ' --form "track[start_time]=' + request.POST["track[start_time]"] + '"' + ' --form "track[end_time]=' + request.POST["track[end_time]"] + '"' + ' --form "track[speed]=' + request.POST["track[speed]"] + '"' + ' --form "track[speed_max]=' + request.POST["track[speed_max]"] + '"' + ' --form "track[speed_avg]=' + request.POST["track[speed_avg]"] + '"' + ' --form "track[status]=' + request.POST["track[status]"] + '"' + ' --form "track[from_lat]=' + request.POST["track[from_lat]"] + '"' + ' --form "track[from_lng]=' + request.POST["track[from_lng]"] + '"' + ' --form "track[to_lat]=' + request.POST["track[to_lat]"] + '"' + ' --form "track[to_lng]=' + request.POST["track[to_lng]"] + '"' + ' --form "track[elapsed]=' + request.POST["track[elapsed]"] + '"' + ' --form "track[distance]=' + request.POST["track[distance]"] + '"' + ' --form "track[count_off]=' + request.POST["track[count_off]"] + '"' + ' --form "track[count_idle]=' + request.POST["track[count_idle]"] + '"' + ' --form "track[count_slow]=' + request.POST["track[count_slow]"] + '"' + ' --form "track[count_normal]=' + request.POST["track[count_normal]"] + '"' + ' --form "track[count_fast]=' + request.POST["track[count_fast]"] + '"' + ' --form "track[count_speeding]=' + request.POST["track[count_speeding]"] + '"' + image_cmd_string + " http://104.236.199.54/tracks.json >> " + result_file_path ) os.system(cmd) print "track command : " + cmd with open(result_file_path) as result_file: data = result_file.readlines() if os.path.exists(result_file_path): os.remove(result_file_path) if image_cmd_string != "": delete_file(rear_img) delete_file(front_img) return HttpResponse(data)
def concatenate_and_hash(paths, comment_key, format, storage=None, fail_loudly=False): if not storage: storage = staticfiles_storage storage._should_hash = False css_abs = CssAbsolute(storage=storage) try: content = u"/*!" + comment_key + u"*/" for path in paths: content += u"\n /*!" + path + u"*/ \n" if format == "css": processed = css_abs.get_and_process(path) if "@import" in processed: raise ValueError("%s: convoy cannot safely concatenate css files that use the @import statement" % path) content += processed elif format == "js": with storage.open(path) as f: f_content = f.read().decode(settings.FILE_CHARSET) content += f_content content = ContentFile(content) # Use the storage's file_hash if possible if hasattr(storage, 'file_hash'): hashed_name = storage.file_hash(comment_key, content) elif content is not None: hashed_name = hashlib.md5() for chunk in content.chunks(): hashed_name.update(chunk) file_name = CARPOOL_PATH_FRAGMENT + u"/" + hashed_name + u"." + format #Save it if storage.exists(file_name): storage.delete(file_name) stored_name = storage.save(file_name, content) #Store it in the cache if hasattr(storage, "hashed_files"): hashed_files = OrderedDict() hashed_files[storage.hash_key(comment_key)] = stored_name storage.hashed_files.update(hashed_files) #Post process it if hasattr(storage, "post_process"): found_files = OrderedDict() found_files[stored_name] = (storage, stored_name) processor = storage.post_process(found_files, False) for orig_path, processed_path, processed in processor: if isinstance(processed, Exception): print "Post-processing '%s' failed!" % orig_path raise processed if processed: print "Post-processed '%s' as '%s'" % (orig_path, processed_path) else: print("Skipped post-processing '%s'" % orig_path) return stored_name except Exception as e: if settings.DEBUG or fail_loudly: raise else: # Fall back to including each file individually # if there is any error -- concatenation simply isn't important # enough to bring down the site return False