def create_item_data(category_obj, title=None, price=None, description=None, adm0=None, adm1=None, adm2=None): """機能 高階関数の引数として使うことを予定している category_objの引数としてpickUp_category_obj_for_testを使うことができる 変数countはItemオブジェクトをobjects.getする際にtitleを使って取得するが、単一の値を担保するために使う。 """ data = {} count = str(Item.objects.all().count()) data['category'] = category_obj.id if title is not None: data["title"] = title else: data["title"] = "テストアイテム" + count if price is not None and type(price) == int: data["price"] = price else: data["price"] = 900 if description is not None: data["description"] = description else: data["description"] = "テストアイテム1の説明" if adm0 is not None: data["adm0"] = adm0 else: data["adm0"] = "GUATEMALA" if adm1 is not None: data["adm1"] = adm1 else: data["adm1"] = "Quetzaltenango" if adm2 is not None: data["adm2"] = adm2 else: data["adm2"] = "Quetzaltenango" # フォームのチェック form = ItemModelForm(data) if form.is_valid() is False: for ele in form.errors: print(ele) return data
def post(self, request, *args, **kwargs): """ 収集したデータを送信する endpoint:"listing/shuppin/" name: "" """ """テスト項目 """ user = User.objects.get(email="*****@*****.**") new_dict = request.data.copy() category = request.data["category_"] cate_obj = Category.objects.get(number=category) new_dict["category"] = cate_obj.id new_dict["point"] = GEOSGeometry(request.data["point_"]) new_dict["user"] = user # print(request.data) # print(new_dict) title = request.data["title"] price = request.data["price"] if len(Item.objects.filter(title=title).filter(price=price)) >= 1: # print("既に登録済み") return HttpResponse("登録済みなので登録しない") # print(request.FILES) form = ItemModelForm(new_dict) # print(form.is_valid()) # print([ele for ele in form.errors]) if form.is_valid(): obj = form.save(commit=False) adm1 = request.POST["adm1"] adm2 = request.POST["adm2"] obj.adm1 = adm1 obj.adm2 = adm2 obj.point = new_dict["point"] obj.radius = new_dict["radius"] obj.user = user try: obj.image1 = request.FILES["file0"] except: pass try: obj.image2 = request.FILES["file1"] except: pass try: obj.image3 = request.FILES["file2"] except: pass obj.save() return HttpResponse("Response")
def create_item_data_for_android_test( category_obj, title=None, price=None, description=None, adm0=None, adm1=None, adm2=None, point=None, radius=None, ): """機能 高階関数の引数として使うことを予定している category_objの引数としてpickUp_category_obj_for_testを使うことができる 変数countはItemオブジェクトをobjects.getする際にtitleを使って取得するが、単一の値を担保するために使う。 androidが送信するデータはCategoryオブジェクトでnumber属性は分かっているがidが不明のデータである。 したがってそのようなデータを作成しなければならない。 webappの場合はFormがカテゴリーオブジェクトを受け取る。 これ違う。。。ModelFormの仕様上、category_objを利用してItemオブジェクト生成する場合には、category_obj.id値を渡さなければならない。 androidappの場合にはcategoy_objがjson形式に変換されている。したがってcategory_objはjson形式に変更する。またid値は削除する。 """ item_data = {} count = str(Item.objects.all().count()) item_data['category'] = {"number": category_obj.number} if title is not None: item_data["title"] = title else: item_data["title"] = "テストアイテム" + count if price is not None and type(price) == int: item_data["price"] = price else: item_data["price"] = 900 if description is not None: item_data["description"] = description else: item_data["description"] = "テストアイテム1の説明" if adm0 is not None: item_data["adm0"] = adm0 else: item_data["adm0"] = "GUATEMALA" if adm1 is not None: item_data["adm1"] = adm1 else: item_data["adm1"] = "Quetzaltenango" if adm2 is not None: item_data["adm2"] = adm2 else: item_data["adm2"] = "Quetzaltenango" if point is not None: item_data["point"] = point else: item_data["point"] = "SRID=4326;POINT (-91.5910384 14.8390624)" if radius is not None: item_data["radius"] = radius else: item_data["radius"] = 100 # フォームのチェック form = ItemModelForm(item_data) if form.is_valid() is False: for ele in form.errors: print(ele) data = {"jsonData": json.dumps(item_data, ensure_ascii=False)} return data
def post(self, request, *args, **kwargs): """ endpoint:"items/create2/" name: "items:item_create2" """ """テスト項目 categoryを選択しない場合にはItemオブジェクトが作られない """ form = ItemModelForm(request.POST, request.FILES) if form.is_valid(): obj = form.save(commit=False) adm1 = request.POST["adm1"] adm2 = request.POST["adm2"] obj.adm1 = adm1 obj.adm2 = adm2 obj.save() try: wkt = request.POST["point"] result = is_in_Guatemala(wkt) if result is True: point = GEOSGeometry(wkt) obj.point = point elif result is False: messages.warning(request, no_match_guatemala_message) except: pass try: obj.radius = int(request.POST["radius"]) except: pass try: obj.image1 = request.FILES['image1'] except: pass try: obj.image2 = request.FILES['image2'] except: pass try: obj.image3 = request.FILES['image3'] except: pass # 現状では1つの記事につき画像は3枚まで使う仕様とする # try: # obj.image4 = request.FILES['image4'] # except: # pass # try: # obj.image5 = request.FILES['image5'] # except: # pass # try: # obj.image6 = request.FILES['image6'] # except: # pass obj.user = request.user obj.save() context = {} context["item_obj"] = obj return render(request, 'items/created.html', context) else: for error in form.errors: print(error) context = {} context["form"] = form return render(request, 'items/create_item_k.html', context)
def post(self, request, *args, **kwargs): """ 画像が追加された場合と追加されない場合で対応できないとこまる """ context = {} pk = self.kwargs["pk"] item_obj = Item.objects.get(id=pk) form = ItemModelForm(request.POST, request.FILES) print(request.POST) if form.is_valid() is True: title = form.cleaned_data["title"] # user = form.cleaned_data["user"] description = form.cleaned_data["description"] price = form.cleaned_data['price'] category = form.cleaned_data['category'] print(category) # item_obj.user = request.user item_obj.title = title item_obj.description = description item_obj.price = price item_obj.category = category adm1 = request.POST["adm1"] adm2 = request.POST["adm2"] item_obj.adm1 = adm1 item_obj.adm2 = adm2 try: wkt = request.POST["point"] result = is_in_Guatemala(wkt) if result is True: point = GEOSGeometry(wkt) item_obj.point = point elif result is False: messages.warning(request, no_match_guatemala_message) except: pass try: item_obj.radius = int(request.POST["radius"]) except: pass try: item_obj.image1 = request.FILES['image1'] except: pass try: item_obj.image2 = request.FILES['image2'] except: pass try: item_obj.image3 = request.FILES['image3'] except: pass item_obj.save() context["item_obj"] = item_obj print("IS_VALID == TRUE") return render(request, "items/edited.html", context) elif form.is_valid() is False: print("IS_VALID == FALSE") print(form.errors) return redirect('items:item_edit', item_obj.id)