def current_money(request): username = request.user.username user_obj = get_or_none(User, username=username) response = assert_not_found(user_obj, "User not found") if not response['success']: return JsonResponse(response, safe=False) current_user_holding_object = get_or_none(CurrentUserHolding, user=user_obj) response = assert_not_found(current_user_holding_object, "User holding not found") if not response['success']: return JsonResponse(response, safe=False) money = current_user_holding_object.current_holding response = { "success": True, "message": "Your current money is %s" % (str(money)), "money": round(money, 3) } return JsonResponse(response, safe=False)
def dashboard(request): if request.user.is_authenticated(): shares = Share.objects.all() name = get_or_none(UserDetail, username=request.user.username) return render(request, 'SellBuy/dashboard.html', { 'shares': shares, 'name': name }) else: return HttpResponseRedirect('/auth/login/')
def generate_share_price(): time.sleep(1) share_obj = get_or_none(Share, id=1) last_price = share_obj.current_price share_obj.current_price = 17364.14 share_obj.previous_price = last_price share_obj.save() share_price_obj = SharePrice.objects.create(share=share_obj, price=last_price) share_price_obj.save() print("Price changed") return True
def register_post(request): username = request.POST.get('username') user = get_or_none(User, username=username) response = assert_found(user, "User with this username already exists") if not response['success']: return response password = request.POST.get('password') name = request.POST.get('name') email = request.POST.get('email') college = request.POST.get('college') branch = request.POST.get('branch') contact = request.POST.get('contact') user = User.objects.create_user(username=username, password=password) user.save() user_detail = UserDetail.objects.create(username=username, name=name, email=email, college=college, branch=branch, contact=contact) user_detail.save() user_holding = UserHolding.objects.create(user=user) user_holding.save() current_user_holding = CurrentUserHolding.objects.create(user=user) current_user_holding.save() shares = Share.objects.all() for share in shares: user_share_quantity = UserShareQuantity.objects.create(share=share, user=user, quantity=0) user_share_quantity.save() response = { 'success': True, 'message': "User registered", 'type': 'register_post', "user": { "id": user.id, "username": user.username } } return response
def share_graph(request, id): share = get_or_none(Share, id=id) response = assert_not_found(share, "Share not found") if not response['success']: return JsonResponse(response, safe=False) share_price = SharePrice.objects.filter(share=share) x = [] y = [] for obj in share_price: time = obj.time time = time.strftime("%H:%M:%S") x.append(time) y.append(obj.price) data = { "success": True, "share_name": share.name, "share_price": y, "share_time": x } return JsonResponse(data, safe=False)
def transaction(request): if request.method == "POST": share_id = request.POST.get('dropdown') quantity = int(request.POST.get('quantity')) button = request.POST.get('button') username = request.user.username user = User.objects.get(username=username) current_user_holding = get_or_none(CurrentUserHolding, user=user) response = assert_not_found(current_user_holding, "User holding not found") if not response['success']: return JsonResponse(response, safe=False) current_money = current_user_holding.current_holding share_obj = get_or_none(Share, id=share_id) response = assert_not_found(share_obj, "Share not found") if not response['success']: return JsonResponse(response, safe=False) share_price = share_obj.current_price share_name = share_obj.name try: user_share_quantity = UserShareQuantity.objects.get( user=user, share=share_obj) except Exception as e: print(str(e)) JsonResponse({"error": "Some error occured"}, safe=False) previous_quantity = user_share_quantity.quantity response = {} if quantity > int(0): if button == "BUY": if (share_price) * (quantity) <= current_money: user_share_quantity.quantity = previous_quantity + quantity user_share_quantity.save() current_user_holding.current_holding = (current_money) - ( share_price * quantity) current_user_holding.save() response['success'] = True response['message'] = ("Bought %s share of %s") % ( str(quantity), share_name) return JsonResponse(response, safe=False) response['success'] = False response['message'] = ( "Money is less only %s share can be bought") % (str( int(current_money / share_price))) return JsonResponse(response, safe=False) if button == "SELL": if previous_quantity == int(0): response['success'] = False response['message'] = ( "You haven't bought shares of %s") % (share_name) return JsonResponse(response, safe=False) elif previous_quantity >= quantity: user_share_quantity.quantity = previous_quantity - quantity user_share_quantity.save() current_user_holding.current_holding = (current_money) + ( share_price * quantity) current_user_holding.save() response['success'] = True response['message'] = ("Sold %s share of %s") % ( str(quantity), share_name) return JsonResponse(response, safe=False) response['success'] = False response['message'] = ("You have only %s share of %s to sell" ) % (str(previous_quantity), share_name) return JsonResponse(response, safe=False) else: response['success'] = False response['message'] = "Be smart and enter a valid quantity" return JsonResponse(response, safe=False) else: return JsonResponse({"error": "Method not allowed"}, safe=False)