def view_index(request): form = ShopForm() if request.method == 'POST': form = ShopForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('shops_index') shops = Shop.objects.all() context = {"form": form, "shops": shops} return render(request, 'shops/index.html', context)
def create_shop(request): if request.method == "POST": form = ShopForm(request.POST) if form.is_valid(): #generate auto id shop_id = 1 shop_obj = Shop.objects.all().order_by("-date_added")[:1] if shop_obj: for shop in shop_obj: shop_id = shop.shop_id + 1 #create shop data = form.save(commit=False) data.creator = request.user data.updater = request.user data.shop_id = shop_id data.save() profiles = Profile.objects.filter(user_type='admin') for profile in profiles: profile.shops.add(data.pk) request.session['message'] = 'Form Submitted successfully' return HttpResponseRedirect( reverse('shops:view_shop', kwargs={'pk': data.pk})) else: errors = generate_form_errors(form, formset=False) context = { "form": form, "title": "Create Shop", "errors": errors, "shops_active": "active" } return render(request, 'shops/entry_shop.html', context) else: form = ShopForm() context = { "form": form, "title": "Create Shop", "url": reverse('shops:create_shop'), "shops_active": "active" } return render(request, 'shops/entry_shop.html', context)
def update_shop(request, slug): owner = request.user shop = get_object_or_404(Shop, slug=slug) web_info = ShopWebInfo.objects.get(shop=shop) address_info = ShopAddressInfo.objects.get(shop=shop) if owner != shop.owner: return redirect('dashboard:view_shops') if request.method == "POST": main_form = ShopForm(request.POST, instance=shop) web_form = ShopWebInfoForm(request.POST, instance=web_info) address_form = ShopAddressInfoForm(request.POST, instance=address_info) if request.POST['form_id'] == 'main-info': if main_form.is_valid(): main_form.save() messages.success(request, 'Your changes have been saved!') else: messages.error(request, 'There was an error with your main info!') elif request.POST['form_id'] == 'web-info': if web_form.is_valid(): web_form.save() messages.success(request, 'Your changes have been saved!') else: messages.error(request, 'There was an error with your web info!') elif request.POST['form_id'] == 'address-info': if address_form.is_valid(): address_form.save() messages.success(request, 'Your changes have been saved!') else: messages.error(request, 'There was an error with your address info!') return redirect('dashboard:update_shop', shop.slug) else: main_form = ShopForm(instance=shop) web_form = ShopWebInfoForm(instance=web_info) address_form = ShopAddressInfoForm(instance=address_info) context = { "shop": shop, "main_form": main_form, "web_form": web_form, "address_form": address_form } return render(request, 'dashboard/shops/update.html', context)
def create_shop(request): owner = request.user if request.method == "POST": form = ShopForm(request.POST, initial={'owner': owner}) if form.is_valid(): form.save() messages.success(request, 'You have created a new shop!') return redirect('dashboard:view_shops') else: messages.error(request, 'There was an error!') else: form = ShopForm(initial={'owner': owner}) context = {"form": form} return render(request, 'dashboard/shops/create.html', context)
def shops(request): if request.method == "POST": form = ShopForm(request.POST) if (form.is_valid()): form.save() return redirect('/shops/') else: results = Charity.objects.all() results = json.loads(serializers.serialize('geojson', results)) if ("application/json" in request.META.get('HTTP_ACCEPT')): return JsonResponse({"data": results}) else: return render(request, 'map.html', {"shops": json.dumps(results)})
def edit_shop(request, pk): instance = get_object_or_404(Shop.objects.filter(pk=pk, is_deleted=False)) if request.method == "POST": form = ShopForm(request.POST, instance=instance) if form.is_valid(): #edit shop data = form.save(commit=False) data.updater = request.user data.date_updated = datetime.datetime.now() data.save() request.session['message'] = 'Form Edited successfully' return HttpResponseRedirect( reverse('shops:view_shop', kwargs={'pk': data.pk})) else: errors = generate_form_errors(form, formset=False) context = { "form": form, "title": "Edit Shop", "errors": errors, "shops_active": "active" } return render(request, 'shops/entry_shop.html', context) else: form = ShopForm(instance=instance) context = { "form": form, "title": "Edit Shop : " + instance.name, "url": reverse('shops:edit_shop', kwargs={'pk': instance.pk}), "shops_active": "active" } return render(request, 'shops/entry_shop.html', context)
def view_show(request, pk): try: shop = Shop.objects.get(pk=pk) except Shop.DoesNotExist: return redirect('shops_index') if request.GET.get('action') == 'del': shop.delete() return redirect('shops_index') if request.method == 'POST' and request.GET['action'] == 'edit': print("i am here") form = ShopForm(request.POST, request.FILES, instance=shop) if form.is_valid(): form.save() return redirect('shop_show', shop.id) if request.GET.get('action') == 'edit': form = ShopForm(instance=shop) context = {"shop": shop, "edit": True, "form": form} return render(request, 'shops/show.html', context) context = {"shop": shop, "edit": False} return render(request, 'shops/show.html', context)
def shops_create_form(request): form = ShopForm() return render(request, 'shopcreate.html', {'form': form})
def get_context_data(self, **kwargs): context = super(ShopEdit, self).get_context_data(**kwargs) form = ShopForm(self.request.POST or None, instance=get_object_or_404(Shop, pk=self.kwargs['pk'])) context["form"] = form return context