def get(self, request): return render(request, "dashboard/index.html", { 'usersCount': len(Transaction.objects.distinct('user')), 'transactionsCount': Transaction.objects.count(), 'alertsCount': Transaction.objects(alert=True).count(), 'latestTransactions': Transaction.objects.order_by('-id').limit(20), 'now': datetime.now() })
def post(self, request): if not request.POST.get('account') or\ not request.POST.get('buyer_id') or\ not request.POST.get('buyer_name') or\ not request.POST.get('items'): return JsonResponse( { 'errors': ['Invalid data'], 'data': request.POST }, status=400) account = request.POST.get('account') buyer_id = request.POST.get('buyer_id') buyer_name = request.POST.get('buyer_name') items = json.loads(request.POST.get('items')) try: transaction = Transaction(account=account, buyer_id=buyer_id, buyer_name=buyer_name) transaction.full_clean() transaction.save() products = [] purchases = [] for item in items: product = Product.objects.get(pk=item['productId']) products.append(product) purchase = Purchase(transaction=transaction, product=product, quantity=item['quantity']) purchase.full_clean() purchases.append(purchase) for purchase in purchases: purchase.save() except ValidationError as e: transaction.delete() return JsonResponse({'errors': e.message_dict}, status=400) except: transaction.delete() return JsonResponse({'errors': ['Invalid data']}, status=400) return JsonResponse({'success': 'Transaction Created'}, status=201)
def transfer_money_view(request): if not request.user.is_authenticated: return redirect('login') friends = (Friend.objects.filter(user_1=request.user) | Friend.objects.filter( user_2=request.user)) & Friend.objects.filter(status=True) if request.method == "POST": receiver_id = request.POST.get('u_id') amount = request.POST.get('amount') account_balance = Wallet.objects.filter(user=request.user)[0] transactions = Transaction.objects.filter( user_1=request.user, status=True) | Transaction.objects.filter( user_2=request.user, status=True) transactions_count = len(transactions) if request.user.is_casual_user and not request.user.is_premium_user and not request.user.is_commercial_user and transactions_count > 15: return redirect('wallet') if request.user.is_premium_user and not request.user.is_commercial_user and transactions_count > 30: return redirect('wallet') if int(account_balance.balance) - int(amount) >= 0: account_balance.balance = int( account_balance.balance) - int(amount) account_balance.save() user_2 = Account.objects.filter(id=receiver_id)[0] t = Transaction(user_1=request.user, user_2=user_2, status=False, payment_method='paytm', amount=amount) t.save() return redirect('wallet') args = {'friends': friends} return render(request, 'transfer_money.html', args)
def consumerTask(data): user_id = int(data['id']) date = datetime.strptime(data['date'], '%d%b%Y') amount = float( data['amount']) if data['type'] == u'D' else -float(data['amount']) # Maintain a total balance, balance_amount = Transaction.objects(user=user_id).sum('amount') # average transaction amount, avg_txn_amount = cal_avg_txn_amount(user_id) # standard deviation of transaction amount standard_deviation = calculate_standard_deviation(user_id) # Average monthly balance. avg_monthly_balance = cal_avg_monthly_bal_amount(user_id) alert = abs(amount) > 2 * standard_deviation # Maintain transaction log for each and every user. Transaction(user=user_id, date=date, amount=amount, alert=alert).save() print(user_id, balance_amount, avg_txn_amount, standard_deviation, avg_monthly_balance, alert)
def process_item(self, item, spider): if item['table_type'] == 'customer_order': try: cust = Customer.objects.get(source_id=item['source_id'], source_type=item['source_type']) except Customer.DoesNotExist: cust = Customer() cust.source_type = item['source_type'] cust.source_id = item['source_id'] cust.name = item['name'] cust.save() logger.info(u'Added {}'.format(str(cust))) cust_ord = CustomerOrder() cust_ord.customer = cust cust_ord.order_id = item['order_id'] if item['member'].lower().strip() == 'yes': cust_ord.member = True elif item['member'].lower().strip() == 'no': cust_ord.member = False cust_ord.caregiver = item['caregiver'] cust_ord.date = datetime.strptime(item['date'], '%m/%d/%y').date() cust_ord.order_total = item['order_total'].lstrip('$') cust_ord.save() logger.info(u'Added {}'.format(str(cust_ord))) elif item['table_type'] == 'transaction': try: cust_ord = CustomerOrder.objects.get( order_id=item['order_id'], customer__source_type=item['source_type']) except Customer.DoesNotExist: logger.error(u'There is no customer order #{}!'.format( item['order_id'])) else: trans = Transaction() trans.order = cust_ord trans.description = item['description'] trans.qty_dispensed = item['qty_dispensed'] trans.qty_sold = item['qty_sold'] trans.price = item['price'].lstrip('$') trans.subtotal = item['subtotal'].lstrip('$') trans.discount = item['discount'] trans.tax = item['tax'] trans.cashier = item['cashier'] trans.save() logger.info(u'Added {}'.format(str(trans))) return item
def calculate_standard_deviation(user_id): x = [i.amount for i in Transaction.objects(user=user_id).only('amount')] try: return statistics.stdev(x) except: return float('inf')
def get(self, request): return render(request, "dashboard/alerts.html", { 'transactions': Transaction.objects(alert=True).order_by('-id') })
def userTransactions(self, request, id): return render(request, "dashboard/transactions.html", { 'transactions': Transaction.objects(user=id) })