def createNewLocalStore()

in shop/views.py [0:0]


def createNewLocalStore(request):
    """ view for creating a local store """
    if request.method == "POST":
        form = StoreCreationForm(request.POST)
        if form.is_valid():
            try:
                store = Store.objects.get(
                    name=form.cleaned_data["business_name"], merchant=request.user
                )
                print("Store found")
                # cannot use context on redirect so use messages framework
                messages.warning(
                    request,
                    "You have been redirected to an existing shop with the same name (new shop not created).",
                )
            except Exception:
                print("Store not found.  Creating...")
                unique_business_id = form.cleaned_data["unique_business_id"]
                store = createStore(
                    form.cleaned_data["business_name"],
                    request.user,
                    unique_business_id=unique_business_id or None,
                )

                # Creates the initial metadata object in the DB
                metadata_obj = FacebookMetadata(
                    store=store,
                    fbe_timezone=form.cleaned_data["timezone"],
                    fbe_currency=form.cleaned_data["currency"],
                    fbe_business_vertical="ECOMMERCE",
                    fbe_domain=settings.DOMAIN,
                    fbe_channel="COMMERCE",
                )
                metadata_obj.save()
                print("Created metadata for store")
            return redirect("viewStore", store.id)
        else:  # form is not valid
            return HttpResponseBadRequest()
    else:  # GET request
        form = StoreCreationForm()
        context = {
            "form": form,
        }
    return render(request, "shop/create_store.html", context)