Esempio n. 1
0
def voter_create(voter_device_id):
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        return HttpResponse(json.dumps(results['json_data']),
                            content_type='application/json')

    voter_id = 0
    # Make sure a voter record hasn't already been created for this
    existing_voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)
    if existing_voter_id:
        json_data = {
            'status': "VOTER_ALREADY_EXISTS",
            'success': False,
            'voter_device_id': voter_device_id,
        }
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')

    # Create a new voter and return the id
    voter_manager = VoterManager()
    results = voter_manager.create_voter()

    if results['voter_created']:
        voter = results['voter']

        # Now save the voter_device_link
        voter_device_link_manager = VoterDeviceLinkManager()
        results = voter_device_link_manager.save_new_voter_device_link(
            voter_device_id, voter.id)

        if results['voter_device_link_created']:
            voter_device_link = results['voter_device_link']
            voter_id_found = True if voter_device_link.voter_id > 0 else False

            if voter_id_found:
                voter_id = voter_device_link.voter_id

    if voter_id:
        json_data = {
            'status': "VOTER_CREATED",
            'success': False,
            'voter_device_id': voter_device_id,
            'voter_id':
            voter_id,  # We may want to remove this after initial testing
        }
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')
    else:
        json_data = {
            'status': "VOTER_NOT_CREATED",
            'success': False,
            'voter_device_id': voter_device_id,
        }
        return HttpResponse(json.dumps(json_data),
                            content_type='application/json')
Esempio n. 2
0
def voter_create(voter_device_id):
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        return HttpResponse(json.dumps(results['json_data']), content_type='application/json')

    voter_id = 0
    # Make sure a voter record hasn't already been created for this
    existing_voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)
    if existing_voter_id:
        json_data = {
            'status': "VOTER_ALREADY_EXISTS",
            'success': False,
            'voter_device_id': voter_device_id,
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')

    # Create a new voter and return the id
    voter_manager = VoterManager()
    results = voter_manager.create_voter()

    if results['voter_created']:
        voter = results['voter']

        # Now save the voter_device_link
        voter_device_link_manager = VoterDeviceLinkManager()
        results = voter_device_link_manager.save_new_voter_device_link(voter_device_id, voter.id)

        if results['voter_device_link_created']:
            voter_device_link = results['voter_device_link']
            voter_id_found = True if voter_device_link.voter_id > 0 else False

            if voter_id_found:
                voter_id = voter_device_link.voter_id

    if voter_id:
        json_data = {
            'status': "VOTER_CREATED",
            'success': False,
            'voter_device_id': voter_device_id,
            'voter_id': voter_id,  # We may want to remove this after initial testing
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')
    else:
        json_data = {
            'status': "VOTER_NOT_CREATED",
            'success': False,
            'voter_device_id': voter_device_id,
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')
Esempio n. 3
0
def my_ballot_view(request):
    generate_voter_device_id_if_needed = True
    voter_device_id = get_voter_device_id(request,
                                          generate_voter_device_id_if_needed)
    voter_id_found = False

    voter_device_link_manager = VoterDeviceLinkManager()
    results = voter_device_link_manager.retrieve_voter_device_link_from_voter_device_id(
        voter_device_id)
    if results['voter_device_link_found']:
        voter_device_link = results['voter_device_link']
        voter_id_found = True if voter_device_link.voter_id > 0 else False

    # If existing voter not found, create a new voter
    if not voter_id_found:
        # Create a new voter and return the id
        voter_manager = VoterManager()
        results = voter_manager.create_voter()

        if results['voter_created']:
            voter = results['voter']

            # Now save the voter_device_link
            results = voter_device_link_manager.save_new_voter_device_link(
                voter_device_id, voter.id)

            if results['voter_device_link_created']:
                voter_device_link = results['voter_device_link']
                voter_id_found = True if voter_device_link.voter_id > 0 else False

    if not voter_id_found:
        print "Voter ID not found, nor generated. This should not be possible. (Cookies may be turned off?)"
        ballot_item_list = []
    else:
        ballot_item_manager = BallotItemManager()
        results = ballot_item_manager.retrieve_all_ballot_items_for_voter(
            voter_device_link.voter_id)
        ballot_item_list = results['ballot_item_list']

    template_values = {
        'ballot_item_list': ballot_item_list,
    }
    response = render(request, 'ux_oak/my_ballot.html', template_values)

    set_voter_device_id(request, response, voter_device_id)
    return response
Esempio n. 4
0
def my_ballot_view(request):
    generate_voter_device_id_if_needed = True
    voter_device_id = get_voter_device_id(request, generate_voter_device_id_if_needed)
    voter_id_found = False

    voter_device_link_manager = VoterDeviceLinkManager()
    results = voter_device_link_manager.retrieve_voter_device_link_from_voter_device_id(voter_device_id)
    if results['voter_device_link_found']:
        voter_device_link = results['voter_device_link']
        voter_id_found = True if voter_device_link.voter_id > 0 else False

    # If existing voter not found, create a new voter
    if not voter_id_found:
        # Create a new voter and return the id
        voter_manager = VoterManager()
        results = voter_manager.create_voter()

        if results['voter_created']:
            voter = results['voter']

            # Now save the voter_device_link
            results = voter_device_link_manager.save_new_voter_device_link(voter_device_id, voter.id)

            if results['voter_device_link_created']:
                voter_device_link = results['voter_device_link']
                voter_id_found = True if voter_device_link.voter_id > 0 else False

    if not voter_id_found:
        print "Voter ID not found, nor generated. This should not be possible. (Cookies may be turned off?)"
        ballot_item_list = []
    else:
        ballot_item_manager = BallotItemManager()
        results = ballot_item_manager.retrieve_all_ballot_items_for_voter(voter_device_link.voter_id)
        ballot_item_list = results['ballot_item_list']

    template_values = {
        'ballot_item_list':     ballot_item_list,
    }
    response = render(request, 'ux_birch/my_ballot.html', template_values)

    set_voter_device_id(request, response, voter_device_id)
    return response
    def test_count_with_cookie(self):
        """
        Test the various cookie states
        :return:
        """

        #######################################
        # Generate the voter_device_id cookie
        response0 = self.client.get(self.generate_voter_device_id_url)
        json_data0 = json.loads(response0.content.decode())

        # Make sure we got back a voter_device_id we can use
        self.assertEqual('voter_device_id' in json_data0, True,
                         "voter_device_id expected in the deviceIdGenerateView json response")

        # Now save the retrieved voter_device_id in a mock cookie
        cookies = SimpleCookie()
        cookies["voter_device_id"] = json_data0['voter_device_id']
        self.client = Client(HTTP_COOKIE=cookies.output(header='', sep='; '))

        #######################################
        # Test for status: VOTER_CREATED
        response02 = self.client.get(self.voter_create_url)
        json_data02 = json.loads(response02.content.decode())

        self.assertEqual('status' in json_data02, True,
                         "status expected in the voterCreateView json response but not found")
        self.assertEqual('voter_device_id' in json_data02, True,
                         "voter_device_id expected in the voterCreateView json response but not found")

        # With a brand new voter_device_id, a new voter record should be created
        self.assertEqual(
            json_data02['status'], 'VOTER_CREATED',
            "status: {status} (VOTER_CREATED expected), voter_device_id: {voter_device_id}".format(
                status=json_data02['status'], voter_device_id=json_data02['voter_device_id']))

        #######################################
        # Check to see if there is 1 voter - i.e., the viewer
        response11 = self.client.get(self.voter_count_url)
        json_data11 = json.loads(response11.content.decode())

        self.assertEqual('success' in json_data11, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data11, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data11['voter_count'], 1,
            "success: {success} (voter_count '1' expected), voter_count: {voter_count}".format(
                success=json_data11['success'], voter_count=json_data11['voter_count']))

        #######################################
        # Add 3 voters so we can check count again
        voter_manager = VoterManager()
        email1 = "*****@*****.**"
        voter_manager.create_voter(
            email=email1,
            password="******",
        )
        email2 = "*****@*****.**"
        voter_manager.create_voter(
            email=email2,
            password="******",
        )
        email3 = "*****@*****.**"
        voter_manager.create_voter(
            email=email3,
            password="******",
        )

        #######################################
        # Check to see if there are 4 voters
        response12 = self.client.get(self.voter_count_url)
        json_data12 = json.loads(response12.content.decode())

        self.assertEqual('success' in json_data12, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data12, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data12['voter_count'], 4,
            "success: {success} (voter_count '4' expected), voter_count: {voter_count}".format(
                success=json_data12['success'], voter_count=json_data12['voter_count']))
    def test_count_with_no_cookie(self):
        """
        This API should work even if person isn't signed in
        :return:
        """
        #######################################
        # Check to see if there are 0 voters
        response = self.client.get(self.voter_count_url)
        json_data = json.loads(response.content.decode())

        # Python3 solution? Problem is refused connection
        # req = Request(self.voter_count_url)
        # response = urlopen(req)
        # json_data = response.read()

        self.assertEqual('success' in json_data, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data['voter_count'], 0,
            "success: {success} (voter_count '0' expected), voter_count: {voter_count}".format(
                success=json_data['success'], voter_count=json_data['voter_count']))

        #######################################
        # Add 3 voters so we can check count again
        voter_manager = VoterManager()
        email1 = "*****@*****.**"
        voter_manager.create_voter(
            email=email1,
            password="******",
        )
        email2 = "*****@*****.**"
        voter_manager.create_voter(
            email=email2,
            password="******",
        )
        email3 = "*****@*****.**"
        voter_manager.create_voter(
            email=email3,
            password="******",
        )

        #######################################
        # Check to see if there are 3 voters
        response2 = self.client.get(self.voter_count_url)
        json_data2 = json.loads(response2.content.decode())

        self.assertEqual('success' in json_data2, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data2, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data2['voter_count'], 3,
            "success: {success} (voter_count '3' expected), voter_count: {voter_count}".format(
                success=json_data2['success'], voter_count=json_data2['voter_count']))

        #######################################
        # Remove data for 3 voters
        voter_manager.delete_voter(email1)
        voter_manager.delete_voter(email2)
        voter_manager.delete_voter(email3)

        #######################################
        # Check to see if there are 0 voters
        response3 = self.client.get(self.voter_count_url)
        json_data3 = json.loads(response3.content.decode())

        self.assertEqual('success' in json_data, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data3, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data3['voter_count'], 0,
            "success: {success} (voter_count '0' expected - 2nd pass), voter_count: {voter_count}".format(
                success=json_data3['success'], voter_count=json_data3['voter_count']))
Esempio n. 7
0
def voter_create_for_api(voter_device_id):  # voterCreate
    # If a voter_device_id isn't passed in, automatically create a new voter_device_id
    if not positive_value_exists(voter_device_id):
        voter_device_id = generate_voter_device_id()
    else:
        # If a voter_device_id is passed in that isn't valid, we want to throw an error
        results = is_voter_device_id_valid(voter_device_id)
        if not results['success']:
            return HttpResponse(json.dumps(results['json_data']), content_type='application/json')

    voter_id = 0
    voter_we_vote_id = ''
    # Make sure a voter record hasn't already been created for this
    voter_manager = VoterManager()
    results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id)
    if results['voter_found']:
        voter = results['voter']
        voter_id = voter.id
        voter_we_vote_id = voter.we_vote_id
        json_data = {
            'status': "VOTER_ALREADY_EXISTS",
            'success': True,
            'voter_device_id': voter_device_id,
            'voter_id':         voter_id,
            'voter_we_vote_id': voter_we_vote_id,
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')

    # Create a new voter and return the voter_device_id
    voter_manager = VoterManager()
    results = voter_manager.create_voter()

    if results['voter_created']:
        voter = results['voter']

        # Now save the voter_device_link
        voter_device_link_manager = VoterDeviceLinkManager()
        results = voter_device_link_manager.save_new_voter_device_link(voter_device_id, voter.id)

        if results['voter_device_link_created']:
            voter_device_link = results['voter_device_link']
            voter_id_found = True if voter_device_link.voter_id > 0 else False

            if voter_id_found:
                voter_id = voter.id
                voter_we_vote_id = voter.we_vote_id

    if voter_id:
        json_data = {
            'status':           "VOTER_CREATED",
            'success':          True,
            'voter_device_id':  voter_device_id,
            'voter_id':         voter_id,
            'voter_we_vote_id': voter_we_vote_id,

        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')
    else:
        json_data = {
            'status':           "VOTER_NOT_CREATED",
            'success':          False,
            'voter_device_id':  voter_device_id,
            'voter_id':         0,
            'voter_we_vote_id': '',
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')
    def test_count_with_voter_device_id(self):
        """
        Test the various cookie states
        :return:
        """

        #######################################
        # Generate the voter_device_id cookie
        response0 = self.client.get(self.generate_voter_device_id_url)
        json_data0 = json.loads(response0.content.decode())

        # Make sure we got back a voter_device_id we can use
        self.assertEqual('voter_device_id' in json_data0, True,
                         "voter_device_id expected in the deviceIdGenerateView json response")

        # Now put the voter_device_id in a variable we can use below
        voter_device_id = json_data0['voter_device_id'] if 'voter_device_id' in json_data0 else ''

        #######################################
        # Test for status: VOTER_CREATED
        response02 = self.client.get(self.voter_create_url)
        json_data02 = json.loads(response02.content.decode())

        self.assertEqual('status' in json_data02, True,
                         "status expected in the voterCreateView json response but not found")
        self.assertEqual('voter_device_id' in json_data02, True,
                         "voter_device_id expected in the voterCreateView json response but not found")

        # With a brand new voter_device_id, a new voter record should be created
        self.assertEqual(
            json_data02['status'], 'VOTER_CREATED',
            "status: {status} (VOTER_CREATED expected), voter_device_id: {voter_device_id}".format(
                status=json_data02['status'], voter_device_id=json_data02['voter_device_id']))

        #######################################
        # Check to see if there is 1 voter - i.e., the viewer
        response11 = self.client.get(self.voter_count_url)
        json_data11 = json.loads(response11.content.decode())

        self.assertEqual('success' in json_data11, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data11, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data11['voter_count'], 1,
            "success: {success} (voter_count '1' expected), voter_count: {voter_count}".format(
                success=json_data11['success'], voter_count=json_data11['voter_count']))

        #######################################
        # Add 3 voters so we can check count again
        voter_manager = VoterManager()
        email1 = "*****@*****.**"
        voter_manager.create_voter(
            email=email1,
            password="******",
        )
        email2 = "*****@*****.**"
        voter_manager.create_voter(
            email=email2,
            password="******",
        )
        email3 = "*****@*****.**"
        voter_manager.create_voter(
            email=email3,
            password="******",
        )

        #######################################
        # Check to see if there are 4 voters
        response12 = self.client.get(self.voter_count_url)
        json_data12 = json.loads(response12.content.decode())

        self.assertEqual('success' in json_data12, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data12, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data12['voter_count'], 4,
            "success: {success} (voter_count '4' expected), voter_count: {voter_count}".format(
                success=json_data12['success'], voter_count=json_data12['voter_count']))
    def test_count_with_no_voter_device_id(self):
        """
        This API should work even if person isn't signed in
        :return:
        """
        #######################################
        # Check to see if there are 0 voters
        response = self.client.get(self.voter_count_url)
        json_data = json.loads(response.content.decode())

        # Python3 solution? Problem is refused connection
        # req = Request(self.voter_count_url)
        # response = urlopen(req)
        # json_data = response.read()

        self.assertEqual('success' in json_data, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data['voter_count'], 0,
            "success: {success} (voter_count '0' expected), voter_count: {voter_count}".format(
                success=json_data['success'], voter_count=json_data['voter_count']))

        #######################################
        # Add 3 voters so we can check count again
        voter_manager = VoterManager()
        email1 = "*****@*****.**"
        voter_manager.create_voter(
            email=email1,
            password="******",
        )
        email2 = "*****@*****.**"
        voter_manager.create_voter(
            email=email2,
            password="******",
        )
        email3 = "*****@*****.**"
        voter_manager.create_voter(
            email=email3,
            password="******",
        )

        #######################################
        # Check to see if there are 3 voters
        response2 = self.client.get(self.voter_count_url)
        json_data2 = json.loads(response2.content.decode())

        self.assertEqual('success' in json_data2, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data2, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data2['voter_count'], 3,
            "success: {success} (voter_count '3' expected), voter_count: {voter_count}".format(
                success=json_data2['success'], voter_count=json_data2['voter_count']))

        #######################################
        # Remove data for 3 voters
        voter_manager.delete_voter(email1)
        voter_manager.delete_voter(email2)
        voter_manager.delete_voter(email3)

        #######################################
        # Check to see if there are 0 voters
        response3 = self.client.get(self.voter_count_url)
        json_data3 = json.loads(response3.content.decode())

        self.assertEqual('success' in json_data, True, "'success' expected in the json response, and not found")
        self.assertEqual('voter_count' in json_data3, True,
                         "'voter_count' expected in the voterCount json response")
        self.assertEqual(
            json_data3['voter_count'], 0,
            "success: {success} (voter_count '0' expected - 2nd pass), voter_count: {voter_count}".format(
                success=json_data3['success'], voter_count=json_data3['voter_count']))