def index(request): ''' Supports the index of a users account and the post for a single account ''' user = request.user if request.method == "POST": if teller_permission(user): data = decode_json_content(request.body) data = {camel_case_to_snake(key): data[key] for key in data} data["user_id"] = data["user"] del data["user"] data['creator'] = user data['balance'] = float(data.get('balance', 0)) Account.objects.create(**data) return JsonResponse({}, status=201) else: return JsonResponse({}, status=403) if user.is_anonymous: return JsonResponse([], status=200, safe=False) serialized_q = serialize_accounts( user.account_set.extra(select={ 'lower_name': 'lower(name)' }).order_by('lower_name').all()) return JsonResponse(serialized_q, status=200, safe=False)
def post(request): user = request.user if user.is_anonymous: account_ids = [] else: account_ids = user.account_set.values_list('id', flat=True) data = decode_json_content(request.body) data = { camel_case_to_snake(key): data[key] for key in data } data['creator'] = user data['amount'] = float(data['amount']) if "account_to" in data: data["account_to_id"] = data["account_to"] del data["account_to"] if "account_from" in data: data["account_from_id"] = data["account_from"] del data["account_from"] if ( teller_permission(user) or ( data.get("transaction_type") == "transfer" and int(data.get("account_to_id")) in account_ids and int(data.get("account_from_id")) in account_ids ) ): Transaction.objects.create(**data) return JsonResponse({}, status=201) else: return JsonResponse({}, status=403)
def test_determines_superuser(self): ''' Returns true is superuser ''' user = User.objects.create_user('john', '*****@*****.**', 'johnpassword', is_superuser=True) self.assertTrue(teller_permission(user))
def test_determines_no_permission(self): ''' Returns false if no permissions ''' user = User.objects.create_user( 'john', '*****@*****.**', 'johnpassword', ) self.assertFalse(teller_permission(user))
def test_determines_is_teller(self): ''' Returns True if someone is a teller ''' user = User.objects.create_user( 'john', '*****@*****.**', 'johnpassword', ) user.user_permissions.add(self.teller) self.assertTrue(teller_permission(user))
def test_knows_if_someone_is_a_manager(self): ''' Tells if a someone is a manager ''' user = User.objects.create_user( 'john', '*****@*****.**', 'johnpassword', ) user.user_permissions.add(self.manager) self.assertTrue(teller_permission(user))
def data(request): """ Get Data on a user. """ user = request.user return JsonResponse( { "loggedIn": not user.is_anonymous(), "username": user.username, "isManager": manager_permission(user), "isTeller": teller_permission(user) }, status=200)
def serialize_user(user): ''' Serializes user data ''' return { 'id': user.id, 'username': user.username, 'firstName': user.first_name, 'lastName': user.last_name, 'accounts': serialize_accounts(user.account_set.all()), "isManager": manager_permission(user), "isTeller": teller_permission(user) }
def detail(request, account_id): """ Returns the details of the account """ if request.user.is_anonymous: return JsonResponse([], status=404, safe=False) try: if teller_permission(request.user): account = Account.objects.get(pk=account_id) else: account = Account.objects.get(pk=account_id, user=request.user) return JsonResponse(serialize_account(account), status=200, safe=False) except Account.DoesNotExist: return JsonResponse([], status=404, safe=False)