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 test_serializes_two_accounts(self): ''' An serialize multiple accounts ''' user = User.objects.create_user( 'john', '*****@*****.**', 'johnpassword' ) account = Account.objects.create( user=user, balance=1000, name="John's private account", address="New York", phone_number="9176910399", creator=user ) account2 = Account.objects.create( user=user, balance=1000, name="John's public account", address="New York", phone_number="9176910399", creator=user ) self.assertEqual( len(serialize_accounts([account, account2])), 2 )
def manage_index(request): """ Returns a list of all accounts """ accounts = Account.objects.extra(select={ 'lower_name': 'lower(name)' }).order_by('lower_name').all() serialized_q = serialize_accounts(accounts) return JsonResponse(serialized_q, status=200, safe=False)
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 test_serializes_an_user(self): ''' Serializers users ''' user = User.objects.create_user( 'john', '*****@*****.**', 'johnpassword' ) account = Account.objects.create( user=user, balance=1000, name="John's private account", address="New York", phone_number="9176910399", creator=user ) result = serialize_user(user) self.assertEqual( result['accounts'], serialize_accounts([account]) )