def prod_review(request, pid): ret_mail = "*****@*****.**" ret = retailer.objects(email=ret_mail).get() mail = request.session['username'] farm = farmer.objects(email=mail).get() name = farm.l_name for i in ret.products: if i.product_id == pid: current = i if request.method == 'POST': title = request.POST.get('title') content = request.POST.get('content') rating = request.POST.get('rate') rating = int(rating) date = datetime.now() print(content, rating) comment = Comment(title=title, content=content, rating=rating, name=name, date=date) current.reviews.append(comment) print(current.reviews) if current.rating != None: current.rating = (current.rating + rating) / len( current.reviews) + 1 else: current.rating = rating ret.save() return prod_page(request, pid) else: content = {'status': 2} return render(request, 'farmer/review_prod.html', content)
def search(request): if request.session.has_key('username'): u = consumer.objects.get(email=request.session['username']) if request.method == 'GET': return render(request, 'consumer/search.html') elif request.method == 'POST': product = request.POST.get('product') distance = request.POST.get('distance') quantity = request.POST.get('quantity') farmers = farmer.objects(stock__crop=product, location__geo_within_center=[ u.location['coordinates'], float(distance) ]) vlist = [] for i in farmers: a = {} for j in i.stock: if j.crop == product: if j.qty > int(quantity): a['name'] = i.f_name a['product'] = product a['stock'] = j.qty a['enc'] = product + '-' + urlsafe_base64_encode( force_bytes(i.email)) vlist.append(a) print(vlist) return render(request, 'consumer/search.html', {'vlist': vlist}) else: return redirect('user_auth:login')
def subtract(request, farmer, crop, qty): farmer = farmer.objects(farmer_id=100).get() for i in farmer.stock: if i.crop == crop: i.qty = i.qty - qty farmer.save()
def invview(request): mail = request.session['username'] farm = farmer.objects(email=mail).get() ilist = [] for i in farm.stock: lmg = i["photo"].grid_id col = db.images.chunks.find({"files_id": lmg}) my_string = base64.b64encode(col[0]["data"]) l = my_string.decode('utf-8') ilist.append(l) print(l) map = zip(farm.stock, ilist) content = {'stocks': farm.stock, 'status': 2, 'map': map} return render(request, 'farmer/inv.html', content)
def reviewtext(request): mail = request.session['username'] farm = farmer.objects(email=mail).get() #cons = consumer.objects(email = mail).get() name = farm.name if request.method == 'POST': content = request.POST.get('content') rating = request.POST.get('rating') date = datetime.now() print(content, rating) comment = Comment(content=content, rating=rating, name=name, date=date) farm.reviews.append(comment) farm.save() return viewreview(request) return render(request, 'farmer/writereview.html')
def edit(request): mail = request.session['username'] farm = farmer.objects(email=mail).get() if request.method == 'POST': empty = '' first_name = request.POST['f_name'] last_name = request.POST['l_name'] phone_no = request.POST['phone_no'] address = request.POST['address'] farm.f_name = first_name farm.l_name = last_name farm.phone_no = phone_no farm.address = address farm.save() message = "Your profile has been edited" #messages.info(request, 'Your password has been changed successfully!') content = {'p': farm, 'messages': message, 'no': 0, 'status': 2} return render(request, 'farmer/profile.html', content) content = {'p': farm, 'no': 1, 'status': 2} return render(request, 'farmer/profile.html', content)
def add(request): mail = request.session['username'] farm = farmer.objects(email=mail).get() already = 0 if request.method == 'POST': crop = request.POST.get('crop') qty = request.POST.get('qty') img = request.FILES["img"] # bmg =InMemoryUploadedFile(img) # print(bmg) # with open(bmg, "rb") as image_file: # encoded_string = base64.b64encode(image_file.read()) # print(encoded_string) qty = int(qty) w = crop[0] if w.isupper(): w = w.lower() else: w = w.upper() worp = w + crop[1:] for i in farm.stock: if i.crop == crop or i.crop == worp: already = 1 i.qty = qty + i.qty farm.save() if already == 0: inv = Inventory(crop=crop, qty=qty, photo=img) #inv.photo.put(img, content_type = 'image/jpeg') #inv.save() farm.stock.append(inv) farm.save() return invview(request) content = {'status': 2} return render(request, 'farmer/add.html', content)
def viewreview(request): mail = request.session['username'] farm = farmer.objects(email=mail).get() content = {'reviews': farm.reviews, 'status': 2} return render(request, 'farmer/view_reviews.html', content)
def history(request): mail = request.session['username'] farm = farmer.objects(email=mail).get() content = {'posts': history, 'status': 2} return render(request, 'farmer/historydone.html', content)