def test_clean_exception(self): success, clean = validate_required( {'required_field': '123abc'}, {'required_field': { 'clean': lambda a: int(a) } } ) self.assertFalse(success) self.assertEqual(clean['required_field'], 'Validation Failed')
def test_required_custom_message(self): success, clean = validate_required( {}, {'required_field': {}}, {'missing_field': 'test_message'} ) self.assertFalse(success) self.assertEqual(clean['required_field'], 'test_message')
def test_clean_exception_custom_message(self): success, clean = validate_required( {'required_field': '123abc'}, {'required_field': { 'clean': lambda a: int(a) } }, {'clean_failed': 'test_message'} ) self.assertFalse(success) self.assertEqual(clean['required_field'], 'test_message')
def test_validation(self): success, clean = validate_required( {'required_field': 'abc'}, {'required_field': { 'validation': lambda a: a.isdigit() } } ) self.assertFalse(success) self.assertEqual(clean['required_field'], 'Validation Failed')
def test_clean(self): success, clean = validate_required( {'required_field': '123'}, {'required_field': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a) } } ) self.assertTrue(success) self.assertEqual(clean['required_field'], 123)
def test_validation_custom_message(self): success, clean = validate_required( {'required_field': 'abc'}, {'required_field': { 'validation': lambda a: a.isdigit(), } }, {'validation_failed': 'test_message' } ) self.assertFalse(success) self.assertEqual(clean['required_field'], 'test_message')
def add_company(request): required_fields = { 'company_name': { 'validation': lambda a: len(a.strip()) >= 3, 'clean': lambda a: a.strip()}, 'password': { 'validation': lambda a: len(a.strip()) >= 3, 'clean': lambda a: a.strip()}, 'email': { 'validation': lambda a: len(a.strip()) >= 3 and a.find('@') > -1, 'clean': lambda a: a.strip() } } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: # get one field out of validation response field_name = validate_result[1].keys()[0] return json_response( False, error_message="Invalid Field: %s" % (field_name.title()) ) clean = validate_result[1] company_name = clean['company_name'] password = clean['password'] email = clean['email'] # check if company with the same name already exists if Company.objects.filter(short_name = company_name).exists(): return json_response( False, error_message="A company with that name already exists.") # create company company = Company.objects.create(short_name=company_name, name=company_name) # create user account user = User.objects.create_user(company_name, email, password) user.profile.company = company user.save() # log the user account in user = authenticate(username=company_name, password=password) login(request, user) # return success return json_response( True, redirect = reverse( "account" ) )
def add_match(request, company_name): required_fields = { 'winner': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a) }, 'loser': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a) } } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: return json_response(False, error_message="Invalid Field: %s " % str(validate_result[1])) clean = validate_result[1] winner_id, loser_id = clean['winner'], clean['loser'] try: company = Company.objects.filter(short_name=company_name).get() except ObjectDoesNotExist: # todo: show signup form raise Http404() # get winner and loser players try: winner = company.player_set.get(pk=winner_id) except ObjectDoesNotExist: return json_response(False, error_message="Invalid Winner") try: loser = company.player_set.get(pk=loser_id) except ObjectDoesNotExist: return json_response(False, error_message="Invalid Loser") # check permission if not company.check_permission(request.user): # todo: show permission denied raise Http404() # create a new match match = company.match_set.create(winner=winner, loser=loser, played_time=datetime.datetime.now()) # save company to update the last changed time company.save() # return success return json_response(True, match_id=match.id)
def test_return_only_failures(self): success, clean = validate_required( {'required_field': 'abc', 'other_field': '123'}, { 'required_field': { 'validation': lambda a: a.isdigit(), 'validation_failed_message': 'invalid field {field}' }, 'other_field': {} } ) self.assertFalse(success) self.assertEqual(clean['required_field'], 'invalid field required_field') self.assertFalse('other_field' in clean)
def add_company(request): required_fields = { 'company_name': { 'validation': lambda a: len(a.strip()) >= 3, 'clean': lambda a: a.strip() }, 'password': { 'validation': lambda a: len(a.strip()) >= 3, 'clean': lambda a: a.strip() }, 'email': { 'validation': lambda a: len(a.strip()) >= 3 and a.find('@') > -1, 'clean': lambda a: a.strip() } } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: # get one field out of validation response field_name = validate_result[1].keys()[0] return json_response(False, error_message="Invalid Field: %s" % (field_name.title())) clean = validate_result[1] company_name = clean['company_name'] password = clean['password'] email = clean['email'] # check if company with the same name already exists if Company.objects.filter(short_name=company_name).exists(): return json_response( False, error_message="A company with that name already exists.") # create company company = Company.objects.create(short_name=company_name, name=company_name) # create user account user = User.objects.create_user(company_name, email, password) user.profile.company = company user.save() # log the user account in user = authenticate(username=company_name, password=password) login(request, user) # return success return json_response(True, redirect=reverse("account"))
def add_match(request, company_name): required_fields = { 'winner': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a)}, 'loser': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a)} } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: return json_response( False, error_message="Invalid Field: %s " % str(validate_result[1])) clean = validate_result[1] winner_id, loser_id = clean['winner'], clean['loser'] try: company = Company.objects.filter(short_name = company_name).get() except ObjectDoesNotExist: # todo: show signup form raise Http404() # get winner and loser players try: winner = company.player_set.get(pk=winner_id) except ObjectDoesNotExist: return json_response(False, error_message="Invalid Winner") try: loser = company.player_set.get(pk=loser_id) except ObjectDoesNotExist: return json_response(False, error_message="Invalid Loser") # check permission if not company.check_permission(request.user): # todo: show permission denied raise Http404() # create a new match match = company.match_set.create( winner = winner, loser = loser, played_time = datetime.datetime.now() ) # save company to update the last changed time company.save() # return success return json_response(True, match_id=match.id)
def add_player(request, company_name): required_fields = { 'name': { 'validation': lambda a: a.replace(' ', '').isalnum() and len(a) > 0, 'clean': lambda a: a.strip() } } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: return json_response(False, error_message="Invalid Field: " + str(validate_result[1])) clean = validate_result[1] name = clean['name'] try: company = Company.objects.filter(short_name=company_name).get() except ObjectDoesNotExist: # todo: show signup form raise Http404() # validate no other players with this name matching_players = company.player_set.filter(name=name).count() if matching_players > 0: return json_response( False, error_message="Player already exists with this name") # check permission if not company.check_permission(request.user): # todo: show permission denied raise Http404() # create a new player player = company.player_set.create( name=name, rank=company.player_set.count() + 1, rating=500, # todo: company can specify the starting rating ) # save company to update the last changed time company.save() # return success return json_response(True, player_id=player.id)
def test_multi_field(self): success, clean = validate_required( {'required_field': 'abc', 'other_field': '123'}, { 'required_field': { 'validation': lambda a: a.isalnum(), 'validation_failed_message': 'invalid field {field}', 'clean': lambda a: a.upper() }, 'other_field': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a) } } ) self.assertTrue(success) self.assertEqual(clean['required_field'], 'ABC') self.assertEqual(clean['other_field'], 123)
def add_player(request, company_name): required_fields = { 'name': { 'validation': lambda a: a.replace(' ', '').isalnum() and len(a) > 0, 'clean': lambda a: a.strip()} } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: return json_response( False, error_message="Invalid Field: " + str(validate_result[1])) clean = validate_result[1] name = clean['name'] try: company = Company.objects.filter(short_name = company_name).get() except ObjectDoesNotExist: # todo: show signup form raise Http404() # validate no other players with this name matching_players = company.player_set.filter(name = name).count() if matching_players > 0: return json_response(False, error_message="Player already exists with this name") # check permission if not company.check_permission(request.user): # todo: show permission denied raise Http404() # create a new player player = company.player_set.create( name = name, rank = company.player_set.count() + 1, rating = 500, # todo: company can specify the starting rating ) # save company to update the last changed time company.save() # return success return json_response(True, player_id = player.id)
def delete_match(request, company_name): required_fields = { 'match_id': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a) } } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: return json_response(False, error_message="Invalid Field: %s " % str(validate_result[1].keys()[0])) clean = validate_result[1] match_id = clean['match_id'] try: company = Company.objects.filter(short_name=company_name).get() except ObjectDoesNotExist: # todo: show signup form raise Http404() # get match try: match = company.match_set.get(pk=match_id) except ObjectDoesNotExist: return json_response(False, error_message="Invalid Match") # check permission if not company.check_permission(request.user): # todo: show permission denied raise Http404() # delete the match match.delete() # recalculate everything to fix the user caches company.recache_matches() # return success return json_response(True)
def delete_match(request, company_name): required_fields = { 'match_id': { 'validation': lambda a: a.isdigit(), 'clean': lambda a: int(a)} } validate_result = validate_required(request.POST, required_fields) if not validate_result[0]: return json_response( False, error_message="Invalid Field: %s " % str(validate_result[1].keys()[0])) clean = validate_result[1] match_id = clean['match_id'] try: company = Company.objects.filter(short_name = company_name).get() except ObjectDoesNotExist: # todo: show signup form raise Http404() # get match try: match = company.match_set.get(pk=match_id) except ObjectDoesNotExist: return json_response(False, error_message="Invalid Match") # check permission if not company.check_permission(request.user): # todo: show permission denied raise Http404() # delete the match match.delete() # recalculate everything to fix the user caches company.recache_matches() # return success return json_response(True)
def test_required(self): success, clean = validate_required({}, {'required_field': {}}) self.assertFalse(success)