def create_placement(request): u"""プレースメント新規作成""" if request.method == "POST": create_form = app.forms.CreatePlacementForm(request.POST, request.FILES) if create_form.is_valid(): placement = Placement() placement.label = create_form.cleaned_data["label"] placement.information = create_form.cleaned_data["information"] placement.floor = create_form.cleaned_data["floor"] placement.top = create_form.cleaned_data["top"] placement.left = create_form.cleaned_data["left"] placement.width = create_form.cleaned_data["width"] placement.height = create_form.cleaned_data["height"] placement.save() _BASE_DIR = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(_BASE_DIR, "static", "image", "placement") file_name = "{0:0>6}.png".format(placement.pk) im = Image.open(BytesIO(request.FILES["image"].read())) im.save(os.path.join(file_path, file_name), format="PNG") placement.image = "/static/image/placement/{0}".format(file_name) placement.save() if "message" in request.session: msg = request.session["message"] else: msg = u"" msg += u"{0}を作成しました".format(placement.label) request.session["message"] = msg return redirect("/floor/{0}".format(placement.floor.pk)) else: return render_to_response( "create_placement.html", {"title": u"floormap / 新規作成", "form": create_form}, context_instance=RequestContext(request), ) elif request.method == "GET": create_form = app.forms.CreatePlacementForm(instance=Placement()) return render_to_response( "create_placement.html", {"title": u"floormap / 新規作成", "form": create_form}, context_instance=RequestContext(request), )
def handle(self, *args, **options): with open(options["filename"], "rb") as fp: data = json.load(fp) if "floor" in data: with transaction.atomic(): for f in data["floor"]: if "id" not in f: # idが指定されていない場合は新規作成 floor = Floor( **f ) floor.save() else: # idが指定されていた場合は更新 if Floor.objects.filter(pk=f["id"]).exists(): floor = Floor.objects.get(pk=f["id"]) if "label" in f: floor.label = f["label"] if "image" in f: floor.image = f["image"] if "information" in f: floor.information = f["information"] if "width" in f: floor.width = f["width"] if "height" in f: floor.height = f["height"] floor.save() else: # 存在しないので新規作成 floor = Floor( **f ) floor.save() if "placement" in data: with transaction.atomic(): for p in data["placement"]: if "id" not in p: # idが指定されていない場合は新規作成 placement = Placement( **p ) placement.save() else: # idが指定されていた場合は更新 if Placement.objects.filter(pk=p["id"]).exists(): placement = Placement.objects.get(pk=p["id"]) if "label" in p: placement.label = p["label"] if "image" in p: placement.image = p["image"] if "information" in p: placement.information = p["information"] if "floor_id" in p: placement.floor_id = p["floor_id"] if "top" in p: placement.top = p["top"] if "left" in p: placement.left = p["left"] if "width" in p: placement.width = p["width"] if "height" in p: placement.height = p["height"] placement.save() else: # 存在しないので新規作成 placement = Placement( **p ) placement.save()