def search(request): details = {} if request.method == 'GET': auth = request.COOKIES.get('auth') #check whether user is logged in if not auth: logged_in = False else: resp = checkauth_exp_api(auth) if (resp["ok"] == True): user_id = resp["resp"]["user_id"] logged_in = True details["logged_in"] = logged_in if 'searchquery' in request.GET: searchquery = request.GET['searchquery'] details['searchquery'] = searchquery #search_form = SearchForm(request.POST) #if not search_form.is_valid(): # return render(request, "search.html", {'state': "Invalid Input"}) #searchquery = search_form.cleaned_data['search'] params = urllib.parse.urlencode({'searchquery': searchquery}) url = 'http://' + settings.EXP_API + ':8000/searchResults/?%s' % params req = urllib.request.Request(url) resp_json = urllib.request.urlopen(req).read().decode('utf-8') resp = json.loads(resp_json) if (resp["ok"] == False): # search unsucessful return render(request, 'search.html', {"state": "No searchable entrees exist", "searchquery": searchquery}) if (resp["ok"] == True): # login worked and returned retuslts results = resp["resp"]["results"] details["results"] = results return render(request, "search.html", details)
def register(request): if request.method == 'GET': #redirect to home if already logged in auth = request.COOKIES.get('auth') #check whether user is logged in if not auth: logged_in = False else: resp = checkauth_exp_api(auth) if (resp["ok"] == True): user_id = resp["resp"]["user_id"] logged_in = True return HttpResponseRedirect(reverse("home")) vendor_form = VendorForm() user_form = UserForm() next = request.GET.get('next') or reverse('home') return render(request, 'register.html', {'next':next, 'user_form': user_form, 'vendor_form': vendor_form}) user_form = UserForm(request.POST) vendor_form = VendorForm(request.POST) if not user_form.is_valid(): return render(request, "register.html", {'state': "Invalid Input", 'user_form': user_form, "vendor_form": vendor_form}) username = user_form.cleaned_data['username'] email = user_form.cleaned_data['email'] f_name = user_form.cleaned_data['first_name'] l_name = user_form.cleaned_data['last_name'] password = user_form.cleaned_data['password'] is_vendor = user_form.cleaned_data['is_vendor'] vendor_name = "" if (is_vendor == True): if not vendor_form.is_valid(): expire_view_cache(request, 'register') return render(request, "register.html", \ {'state': "Vendors must fill out company name", \ 'user_form': user_form, "vendor_form": vendor_form}) vendor_name = vendor_form.cleaned_data['vendor_name'] post_data = {'username': username, \ 'email': email, \ 'f_name': f_name, \ 'l_name': l_name, \ 'password': password, \ 'is_vendor': is_vendor, \ 'vendor_name': vendor_name \ } post_encoded = urllib.parse.urlencode(post_data).encode('utf-8') req = urllib.request.Request('http://' + settings.EXP_API + ':8000/api/register/', data=post_encoded, method='POST') resp_json = urllib.request.urlopen(req).read().decode('utf-8') resp = json.loads(resp_json) if (resp['ok'] == False): expire_view_cache(request, 'register') return render(request, "register.html", {'state': "Unsucessful registration", 'user_form': user_form, 'vendor_form':vendor_form}) expire_view_cache(request, 'login') if (is_vendor == True): expire_view_cache(request, 'home') return HttpResponseRedirect(reverse('login'), {'status': 'Registration sucessful. Please login.'})
def login(request): if request.method == 'GET': auth = request.COOKIES.get('auth') #check whether user is logged in if not auth: logged_in = False else: resp = checkauth_exp_api(auth) if (resp["ok"] == True): user_id = resp["resp"]["user_id"] logged_in = True return HttpResponseRedirect(reverse("home")) login_form = LoginForm() next = request.GET.get('next') or reverse('home') return render(request, 'login.html', {"state": "", 'next':next, 'login_form': login_form}) login_form = LoginForm(request.POST) if not login_form.is_valid(): return render(request, "login.html", {'state': "Invalid Input", 'login_form': login_form}) username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] #hashed_password = hashers.make_password(password) next = login_form.cleaned_data.get('next') or reverse('home') # get response from login experience service params = urllib.parse.urlencode({'username': username, 'password': password}) url = 'http://' + settings.EXP_API + ':8000/login/?%s' % params req = urllib.request.Request(url) resp_json = urllib.request.urlopen(req).read().decode('utf-8') resp = json.loads(resp_json) if (resp["ok"] == False): # login unsucessful, reutrn error on login page key = get_cache_key(request, key_prefix=None) expire_view_cache(request, "login") return render(request, 'login.html', {"state": "Invalid username and/or password", 'login_form': login_form}) if (resp["ok"] == True): # login worked and returned authenticator user_id = resp["resp"]["user_id"] authenticator = resp["resp"]["authenticator"] # if the user is a vendor, update their corresponding vendor page resp = isuservendor_exp_api(user_id) if (resp["ok"] == True): is_vendor = resp["resp"]["is_vendor"] if (is_vendor == True): users_vendor_id = resp["resp"]["vendor_id"] expire_view_cache(request, "vendor", [users_vendor_id]) expire_view_cache(request, "home") response = HttpResponseRedirect(reverse('home')) response.set_cookie("auth", authenticator) return response
def logout(request): response = HttpResponseRedirect(reverse('home')) auth = request.COOKIES.get('auth') #if a user is a vendor, invalidate their vendor page before they logout if auth: resp = checkauth_exp_api(auth) if (resp["ok"] == True): user_id = resp["resp"]["user_id"] resp = isuservendor_exp_api(user_id) is_vendor = resp["resp"]["is_vendor"] if (resp["ok"] == True): if (is_vendor == True): vendor_id = resp["resp"]["vendor_id"] expire_view_cache(request, 'vendor', [vendor_id]) response.delete_cookie("auth") expire_view_cache(request, 'home') return response
def home(request): auth = request.COOKIES.get('auth') #initialize dictionary details = {} #check whether user is logged in if not auth: logged_in = False else: resp = checkauth_exp_api(auth) if (resp["ok"] == False): #authenticator is not a match logout(request) if (resp["ok"] == True): user_id = resp["resp"]["user_id"] details["user_id"] = user_id logged_in = True resp = isuservendor_exp_api(user_id) if (resp["ok"] == True): is_vendor = resp["resp"]["is_vendor"] details["is_vendor"] = is_vendor if (is_vendor == True): details["vendor_id"] = resp["resp"]["vendor_id"] details["vendor_name"] = resp["resp"]["vendor_name"] else: return HttpResponseRedirect(reverse("home")) vendors_req = urllib.request.Request('http://' + settings.EXP_API + ':8000/allVendors/') vendors_resp_json = urllib.request.urlopen(vendors_req).read().decode('utf-8') resp = json.loads(vendors_resp_json) vendors = [] if (resp["ok"] == True): for v in resp["resp"]["vendors"]: vendors.append({"id": v[0], "name": v[1]}) details["logged_in"] = logged_in details["vendors"] = vendors return render(request, "home.html", details)
def vendor(request, vendor_id): auth = request.COOKIES.get('auth') details_req = urllib.request.Request('http://' + settings.EXP_API + ':8000/getVendorDetails/' + str(vendor_id)) details_resp_json = urllib.request.urlopen(details_req).read().decode('utf-8') resp = json.loads(details_resp_json) # will add locations in here as well, but first have to figure out how to serialize ManyToMany field if (resp["ok"] == True): details = resp["resp"] vendor_id = details["vendor_id"] vendor_name = details["vendor_name"] vendor_owner = details["vendor_owner"] menu = details["menu"] entrees = details["entrees"] vendor_locations = details["vendor_locations"] #check whether user is logged in if not auth: logged_in = False else: resp = checkauth_exp_api(auth) if (resp["ok"] == False): #authenticator is not a match logout(request) if (resp["ok"] == True): user_id = resp["resp"]["user_id"] details["user_id"] = user_id logged_in = True resp = isuservendor_exp_api(user_id) if (resp["ok"] == True): is_vendor = resp["resp"]["is_vendor"] details["is_vendor"] = is_vendor if (is_vendor == True): details["users_vendor_id"] = resp["resp"]["vendor_id"] details["logged_in"] = logged_in return render(request, "vendor.html", details)
def newEntree(request): # Check if user is logged in, if not, then redirect to login page auth = request.COOKIES.get("auth") # initialize dictionary details = {} # check whether user is logged in if not auth: logged_in = False return HttpResponseRedirect(reverse("home")) else: resp = checkauth_exp_api(auth) if resp["ok"] == False: # authenticator is not a match logout(request) if resp["ok"] == True: user_id = resp["resp"]["user_id"] logged_in = True resp = isuservendor_exp_api(user_id) if resp["ok"] == True: is_vendor = resp["resp"]["is_vendor"] details["is_vendor"] = is_vendor if is_vendor == True: details["vendor_id"] = resp["resp"]["vendor_id"] details["vendor_name"] = resp["resp"]["vendor_name"] # Return to home page if user is not a vendor else: return HttpResponseRedirect(reverse("home")) else: return HttpResponseRedirect(reverse("home")) # Generate empty form if first time to page if request.method != "POST": newEntreeForm = NewEntreeForm() next = request.GET.get("next") or reverse("newEntree") return render( request, "new_entree.html", {"next": next, "newEntreeForm": newEntreeForm, "logged_in": logged_in} ) # Get the vendor associated with the currently logged in user vendor_id = details["vendor_id"] newEntreeForm = NewEntreeForm(request.POST) if not newEntreeForm.is_valid(): return render( request, "new_entree.html", {"state": "Invalid Input", "newEntreeForm": newEntreeForm, "logged_in": logged_in}, ) # Get the entree data from form entree_name = newEntreeForm.cleaned_data["name"] entree_description = newEntreeForm.cleaned_data["description"] entree_price = newEntreeForm.cleaned_data["price"] post_data = { "entree_name": entree_name, "entree_description": entree_description, "entree_price": entree_price, "vendor_id": vendor_id, } post_encoded = urllib.parse.urlencode(post_data).encode("utf-8") req = urllib.request.Request("http://" + settings.EXP_API + ":8000/api/addEntree", data=post_encoded, method="POST") resp_json = urllib.request.urlopen(req).read().decode("utf-8") resp = json.loads(resp_json) if resp["ok"] == False: return render( request, "new_entree.html", { "state": "Could not add entree to database or Kafka topic does not yet exist. Please try again.", "newEntreeForm": newEntreeForm, "logged_in": logged_in, }, ) # else: # return render(request, "new_entree.html", {'state': "You've succesfully added an entree", 'newEntreeForm': newEntreeForm}) # reset cache page for vendor complete = expire_view_cache(request, "vendor", [vendor_id]) if complete or not complete: # this line is so that the cache is reset before the page is returned return HttpResponseRedirect(reverse("vendor", args=[vendor_id]))