def startTransaction(request): data = json.loads(request.body) user_from_name = data['username'] try: user_from = User.objects.get(username = user_from_name ) except: return NotFound("No such user " + user_from_name) if checkToken(request): user_to_name = data['Transaction']['recipient'] user_to = User.objects.get(username = user_to_name ) transfer_amount = Decimal(data['Transaction']['amount']) print(transfer_amount) if validateTransactionAmount(user_from,transfer_amount): user_from_wallet= Wallet.objects.get(user = user_from) user_to_wallet = Wallet.objects.get(user = user_to) # if wallets are the same if user_from_wallet == user_to_wallet: return Forbidden("wallets cant be the same") #changing wallets balance user_from_wallet.balance = float(Decimal(user_from_wallet.balance) - transfer_amount) user_to_wallet.balance = float(Decimal(user_to_wallet.balance) + transfer_amount) user_to_wallet.save() user_from_wallet.save() #adding Transaction transaction = Transaction.objects.create(user_to = user_to , user_from =user_from , amount = float(transfer_amount)) return JsonResponse({"Transaction id": transaction.id} , status =201) else: return Forbidden("Not enough money in the account") else: return Forbidden("Invalid token")
def buyTender(request, tender_id): tender = validateTender(tender_id) if tender is not None: data = json.loads(request.body) user_name = data['username'] if checkToken(request): user = validateUser(user_name) if user is not None: commodity = validateCommodity(tender.wish_commodity.name) asset = validateAsset(user, commodity) if asset is not None: if ((asset.amount > tender.wish_commodity_ammount) or (asset.amount == tender.wish_commodity_ammount)): # decrease buyer amount asset.amount = asset.amount - tender.wish_commodity_ammount asset.save() asset = createOrSumAsset(tender.user, commodity, tender.wish_commodity_ammount) asset = createOrSumAsset(user, tender.own_commodity, tender.own_commodity_ammount) tender.delete() return JsonResponse({"Sucess": "trade done"}) else: return Forbidden( "You have not such asset in this amount") else: return Forbidden("You have not such asset") else: return NotFound("No such user " + user_name) else: return Forbidden("Invalid token") else: return NotFound("No such tender")
def createTender(request): data = json.loads(request.body) user_name = data['username'] if checkToken(request): try: user = User.objects.get(username=user_name) except: return NotFound("No such user " + user_name) own_asset_commodity_name = data["Tender"]["OwnAsset"]["Commodity"][ "сommodity_name"] wish_asset_commodity_name = data["Tender"]["WishAsset"]["Commodity"][ "сommodity_name"] if own_asset_commodity_name == wish_asset_commodity_name: return Forbidden("You cant buy same commodity") own_asset_commodity_amount = Decimal( data["Tender"]["OwnAsset"]["amount"]) wish_asset_commodity_amount = Decimal( data["Tender"]["WishAsset"]["amount"]) commodity = validateCommodity(own_asset_commodity_name) if commodity is not None: own_asset = Asset.objects.filter(commodity=commodity, user=user) if len(own_asset) > 0: own_asset = own_asset[0] if ((Decimal(own_asset.amount) > own_asset_commodity_amount) or (Decimal(own_asset.amount) == own_asset_commodity_amount)): wish_commodity = validateCommodity( wish_asset_commodity_name) if wish_commodity is not None: own_asset.amount = own_asset.amount - own_asset_commodity_amount own_asset.save() tender = Tender.objects.create( user=user, own_commodity=commodity, own_commodity_ammount=own_asset_commodity_amount, wish_commodity=wish_commodity, wish_commodity_ammount=wish_asset_commodity_amount, status="OnCreate") tender.save() return JsonResponse({"tender_id": str(tender.pk)}) else: return Forbidden("No such commodity " + wish_asset_commodity_name) else: return Forbidden("You have not such asset in this amount") else: return Forbidden("You have not such asset") else: return NotFound("No such commodity " + own_asset_commodity_name) else: return Forbidden("Invalid token")
def getAssets(request): data = json.loads(request.body) user_name = data['username'] if checkToken(request): user = validateUser(user_name) if user is not None: assets = Asset.objects.filter(user=user) if len(assets) > 0: data = serializers.serialize("json", assets) data = json.loads(data) return JsonResponse(data, status=200, safe=False) else: return Forbidden("You have no assets") else: return NotFound("No such user " + user_name) else: return Forbidden("Invalid token")
def getToken(request): data = json.loads(request.body) username = data["username"] password = data['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) try: token = Token.objects.get(user=user) Token.objects.filter(user=user).update( key=token.generate_key()) token = Token.objects.get(user=user) return JsonResponse({"token": token.key}) except: token = Token.objects.create(user=user) return JsonResponse({"token": token.key}) else: return Forbidden("Disabled account") else: return Forbidden("Invalid login or password")
def getAsset(request, asset_id): data = json.loads(request.body) user_name = data['username'] if checkToken(request): user = validateUser(user_name) if user is not None: asset = Asset.objects.filter(pk=asset_id) if len(asset) > 0: if asset[0].user == user: data = serializers.serialize("json", [ asset[0], ]) data = json.loads(data) return JsonResponse(data, status=200, safe=False) else: return Forbidden("You have no rigths for this asset") else: return NotFound("No such asset with id: " + str(asset_id)) else: return NotFound("No such user " + user_name) else: return Forbidden("Invalid token")
def createAsset(request): data = json.loads(request.body) user_name = data['username'] if checkToken(request): try: user = User.objects.get(username=user_name) except: return NotFound("No such user " + user_name) сommodity_name = data["Asset"]["Commodity"]["сommodity_name"] try: commodity = Commodity.objects.get(name=сommodity_name) except: return NotFound("No such commodity " + сommodity_name) amount = Decimal(data["Asset"]["ammount"]) asset = createOrSumAsset(user, commodity, amount) return JsonResponse({"Success": "added", "amount": asset.amount}) else: return Forbidden("Invalid token")
def getTender(request, tender_id): data = json.loads(request.body) user_name = data['username'] if checkToken(request): user = validateUser(user_name) if user is not None: tender = Tender.objects.filter(pk=tender_id) if len(tender) > 0: data = serializers.serialize("json", [ tender[0], ]) data = json.loads(data) return JsonResponse(data, status=200, safe=False) else: return NotFound("No such tender with id: " + str(tender_id)) else: return NotFound("No such user " + user_name) else: return Forbidden("Invalid token")
def getTime(request): if checkToken(request): return JsonResponse({'time': str(datetime.now())}) else: return Forbidden("Invalid token ")