def create_new_project_from_history(user_id, history, title=None): record_User = User.objects.get(id=user_id) username = record_User.username if title is None: title = "新規のプロジェクト" the_key = username + str(title) path_local, pickle_basename = generate_pickle_path_local(key=the_key, get_basename=True) # pickleをコピー copy_initial_pickle_local(path_local) # concat_movie_pathを「生成 concat_movie_basename = generate_basename(key=the_key + "concatmoviebasename", ext="mp4") # プロジェクトを保存 record_Project = Project( user=record_User, title=title, pickle_basename=pickle_basename, concat_movie_path=fname_cloud(concat_movie_basename)) record_Project.save() # 盤面を保存 result = {"history": history} with open(path_local, mode="wb") as f: pickle.dump(result, f)
def create_scene_from_preview_request(request): ''' 新たなチャプターを作成する ''' # POSTを受け取る payload = json.loads(request.body.decode("utf-8")) image_payload = payload["image"] text = payload["text"] image_prefix, image_base64 = image_payload.split(",") print(image_prefix) dec_file = base64.b64decode(image_base64) user_id = int(request.user.id) # 画像のパスを生成 ext = "jpg" image_basename = generate_basename(key=str(user_id) + "newscene", ext=ext) image_url = fname_cloud(image_basename) print(image_url) # 画像をアップロード obj = bucket.Object(image_basename) response = obj.put(Body=dec_file, ContentType="image/jpeg") print("uploaded image") # セッション情報を取得 activeSection_index = request.session["activeSection_index"] activeSection_id = request.session["activeSection_id"] activeSlide_index = request.session["activeSlide_index"] activeSlide_id = request.session["activeSlide_id"] is_create_next = request.session["is_create_next"] insert_scene(chapter_id=activeSection_id, index=activeSlide_index - 1, is_create_next=is_create_next, new_scene_info={ "text": text, "image_url": image_url }) return JsonResponse({"code": 200})
def save_initial_board_request(request): data = json.loads(request.body.decode("utf-8")) title = data["title"] history = data["history"] # プロジェクトを新規作成 user_id = int(request.user.id) record_User = User.objects.get(id=user_id) username = record_User.username the_key = username + str(title) path_local, pickle_basename = generate_pickle_path_local(key=the_key, get_basename=True) # pickleをコピー copy_initial_pickle_local(path_local) # concat_movie_pathを「生成 concat_movie_basename = generate_basename(key=the_key + "concatmoviebasename", ext="mp4") # プロジェクトを保存 record_Project = Project( user=record_User, title=title, pickle_basename=pickle_basename, concat_movie_path=fname_cloud(concat_movie_basename)) record_Project.save() # 盤面を保存 # なぜかmoveの「+」が抜けてしまうので、対策 history = modify(history) result = {"history": history} with open(path_local, mode="wb") as f: pickle.dump(result, f) print("success : save_pickle") return JsonResponse({"code": 200})
def change_image_from_post(image, image_url, bucket): ''' image : request.FILES["key"] image_url : fname_cloud ''' content_type = image.content_type if content_type not in ["image/jpeg", "image/png"]: content_type = "image/jpeg" if content_type == "image/png": ext = "png" else: ext = "jpg" temporal_image_path = None try: temporal_image_path = image.temporary_file_path() print("temporal path exists") print(temporal_image_path) except: print("temporal path not exist") image_basename = os.path.basename(image_url) assert fname_cloud(image_basename) == image_url # もしオンメモリデータだったら、画像にして保存 if temporal_image_path is None: temporal_image_path = generate_temporal_path(image_basename) with open(temporal_image_path, 'wb') as f: f.write(image.read()) # アップロード bucket.upload_file(temporal_image_path, image_basename, ExtraArgs={"ContentType": content_type}) if os.path.exists(temporal_image_path): os.remove(temporal_image_path) return image_url
def create_new_project(record_User, title): username = record_User.username n_project = len(get_projects(record_User)) the_key = username + str(title) + str(n_project) pickle_path_local, pickle_basename = generate_pickle_path_local( key=the_key, get_basename=True) the_key_movie = the_key + "concatmovie" concat_movie_basename = generate_basename(key=the_key_movie, ext="mp4") # pickleをコピー copy_initial_pickle_local(pickle_path_local) # プロジェクトを保存 record_Project = Project( user=record_User, title=title, pickle_basename=pickle_basename, concat_movie_path=fname_cloud(concat_movie_basename)) record_Project.save() print("success : create new project")
def create_book_request(request): ''' 新たなブックを作成する ''' # POSTを受け取る payload = request.POST text = payload["text"] opening_sente = str(payload["opening_sente"]) opening_gote = str(payload["opening_gote"]) cropping_x = float(payload["cropping_x"]) cropping_y = float(payload["cropping_y"]) cropping_w = float(payload["cropping_w"]) cropping_h = float(payload["cropping_h"]) print(payload) image = request.FILES["original_image"] content_type = image.content_type # セッション情報の取得 if "user_id" in payload.keys(): user_id = int(payload["user_id"]) else: user_id = int(request.user.id) # 画像の保存先を決める if content_type not in ["image/jpeg", "image/png"]: content_type = "image/jpeg" if content_type == "image/png": ext = "png" else: ext = "jpg" temporal_image_path = None try: temporal_image_path = image.temporary_file_path() print("temporal path exists") print(temporal_image_path) except: print("temporal path not exist") image_basename = generate_basename(key=str(user_id) + "newchapter", ext=ext) image_url = fname_cloud(image_basename) # 画像を読み込み if temporal_image_path is None: # もしオンメモリデータだったら、画像にして保存 temporal_image_path = generate_temporal_path(image_basename) with open(temporal_image_path, 'wb') as f: f.write(image.read()) im = Image.open(temporal_image_path) # exifに基づいて修正 im_modify = modify_by_EXIF(im) if im_modify is not None: im = im_modify im_crop = im.crop((cropping_x, cropping_y, cropping_x + cropping_w, cropping_y + cropping_h)) # スマホから送信すると"**.upload"という拡張子になることがあるので対策 if get_normalized_ext(temporal_image_path.split(".")[-1], restriction="image") is None: temporal_image_path = ".".join( temporal_image_path.split(".")[:-1] + [ext]) # 画像をトリミング if ext == "jpg": im_crop.save(temporal_image_path, quality=100) else: im_crop.save(temporal_image_path) # アップロード bucket.upload_file(temporal_image_path, image_basename, ExtraArgs={"ContentType": content_type}) if os.path.exists(temporal_image_path): os.remove(temporal_image_path) print("uploaded image") # レコードを保存 record_User = User.objects.get(id=user_id) record_Book = Book(user=record_User, title=text, thumb_url=image_url, is_public=False, opening_sente=opening_sente, opening_gote=opening_gote) record_Book.save() print("saved book") return JsonResponse({"code": 200})
def create_book_request(request): ''' 新たなブックを作成する ''' print("create new book request") print() payload = request.POST # payload = json.loads(request.body.decode("utf-8")) # print(payload) title = payload["title"] opening_sente = payload["opening_sente"] opening_gote = payload["opening_gote"] print(payload) print("") print("title : {}".format(title)) print("opening_sente : {}".format(opening_sente)) print("opening_gote : {}".format(opening_gote)) print("") print(request.FILES) thumb = request.FILES["thumb"] fname = thumb._name content_type = thumb.content_type # print(thumb) # print(thumb.__dict__) print(fname) print(content_type) temporal_path = None try: temporal_path = thumb.temporary_file_path() print("temporal path exists") print(temporal_path) except: print("temporal path not exist") if "user_id" in payload.keys(): user_id = int(payload["user_id"]) else: user_id = int(request.user.id) print("user_id : {}".format(str(user_id))) # 画像のパスを生成 ext_original = fname.split(".")[-1] ext = get_normalized_ext(ext=ext_original, restriction="image") assert ext is not None thumb_basename = generate_basename(key=str(user_id)+"bookthumb", ext=ext) thumb_url = fname_cloud(thumb_basename) print(thumb_url) # 画像をアップロード if temporal_path is None: obj = bucket.Object(thumb_basename) response = obj.put( Body = thumb.read(), ContentType = content_type ) else: bucket.upload_file( temporal_path, thumb_basename, ExtraArgs={"ContentType": content_type} ) print("uploaded thumb image") # レコードを保存 record_User = User.objects.get(id=user_id) record_Book = Book( user = record_User, title = title, thumb_url = thumb_url, is_public = False, opening_sente = opening_sente, opening_gote = opening_gote ) record_Book.save() print("saved book") # return JsonResponse({"code" : 200}) return redirect("/ShareShogi/books/mypage")
def create_chapter_request(request): ''' 新たなチャプターを作成する ''' print("create new chapter request") print() payload = request.POST title = payload["title"] thumb = request.FILES["thumb"] fname = thumb._name content_type = thumb.content_type # print(thumb) # print(thumb.__dict__) print(fname) print(content_type) temporal_path = None try: temporal_path = thumb.temporary_file_path() print("temporal path exists") print(temporal_path) except: print("temporal path not exist") # ユーザの情報を取得 if "user_id" in payload.keys(): user_id = int(payload["user_id"]) else: user_id = int(request.user.id) # print("user_id : {}".format(str(user_id))) # book_idを取得 if "mybook_id" in request.session: book_id = int(request.session["mybook_id"]) else: return JsonResponse({"code": 400, "comment": "book_idが見つかりません"}) # 画像のパスを生成 ext_original = fname.split(".")[-1] ext = get_normalized_ext(ext=ext_original, restriction="image") assert ext is not None thumb_basename = generate_basename(key=str(user_id) + "chapterthumb", ext=ext) thumb_url = fname_cloud(thumb_basename) print(thumb_url) # 画像をアップロード if temporal_path is None: obj = bucket.Object(thumb_basename) response = obj.put(Body=thumb.read(), ContentType=content_type) else: bucket.upload_file(temporal_path, thumb_basename, ExtraArgs={"ContentType": content_type}) print("uploaded thumb image") # レコードを保存 record_Book = Book.objects.get(id=book_id) record_Chapter = Chapter( book=record_Book, title=title, thumb_url=thumb_url, ) record_Chapter.save() print("saved chapter") return redirect("/ShareShogi/chapters/mypage")
def create_scene_from_preview_request(request): ''' 新たなシーンを作成する ''' # POSTを受け取る ######################### 1 # payload = json.loads(request.body.decode("utf-8")) # image_payload = payload["image"] # text = payload["text"] # image_prefix, image_base64 = image_payload.split(",") # print(image_prefix) # dec_file = base64.b64decode(image_base64) ####################### 2 payload = request.POST text = payload["text"] cropping_x = float(payload["cropping_x"]) cropping_y = float(payload["cropping_y"]) cropping_w = float(payload["cropping_w"]) cropping_h = float(payload["cropping_h"]) print(payload) image = request.FILES["original_image"] print(image) print(image.__dict__) temporal_image_path = None try: temporal_image_path = image.temporary_file_path() print("temporal path exists") print(temporal_image_path) except: print("temporal path not exist") ####################### user_id = int(request.user.id) # 画像のパスを生成 # ext = "jpg" # image_basename = generate_basename(key=str(user_id)+"newscene", ext=ext) # image_url = fname_cloud(image_basename) # temporal_image_path = generate_temporal_path(image_basename) # print(image_url) # print(temporal_image_path) ####################### 1 # # 画像をデコードして保存 # with open(temporal_image_path, "wb") as f: # f.write(base64.b64decode(image_base64)) # # 画像をアップロード # upload_file(bucket, temporal_image_path, image_basename) # # 画像を削除 # os.remove(temporal_image_path) ######################### 2 # 画像をアップロード content_type = image.content_type print(content_type) if content_type is None: content_type = "image/jpeg" ext = get_normalized_ext(content_type.split("/")[-1], restriction="image") if ext is None: ext = "jpg" image_basename = generate_basename(key=str(user_id) + "newscene", ext=ext) image_url = fname_cloud(image_basename) # if temporal_image_path is None: # obj = bucket.Object(image_basename) # response = obj.put( # Body = image.read(), # ContentType = content_type # ) # else: # bucket.upload_file( # temporal_image_path, # image_basename, # ExtraArgs={"ContentType": content_type} # ) # 画像をクロッピング if temporal_image_path is None: # 画像にして保存 temporal_image_path = generate_temporal_path(image_basename) with open(temporal_image_path, 'wb') as f: f.write(image.read()) im = Image.open(temporal_image_path) # exifに基づいて修正 im_modify = modify_by_EXIF(im) if im_modify is not None: im = im_modify im_crop = im.crop((cropping_x, cropping_y, cropping_x + cropping_w, cropping_y + cropping_h)) # スマホからアップロードすると、".upload"になることがあるので対策 if get_normalized_ext(temporal_image_path.split(".")[-1], restriction="image") is None: temporal_image_path = ".".join( temporal_image_path.split(".")[:-1] + [ext]) if ext == "jpg": im_crop.save(temporal_image_path, quality=100) else: im_crop.save(temporal_image_path) bucket.upload_file(temporal_image_path, image_basename, ExtraArgs={"ContentType": content_type}) if os.path.exists(temporal_image_path): os.remove(temporal_image_path) print("uploaded image") ########################### # セッション情報を取得 activeSection_index = request.session["activeSection_index"] activeSection_id = request.session["activeSection_id"] activeSlide_index = request.session["activeSlide_index"] activeSlide_id = request.session["activeSlide_id"] is_create_next = request.session["is_create_next"] insert_scene(chapter_id=activeSection_id, index=activeSlide_index - 1, is_create_next=is_create_next, new_scene_info={ "text": text, "image_url": image_url }) return JsonResponse({"code": 200})