当前位置: 首页>>代码示例>>Python>>正文


Python VoterManager.create_voter方法代码示例

本文整理汇总了Python中voter.models.VoterManager.create_voter方法的典型用法代码示例。如果您正苦于以下问题:Python VoterManager.create_voter方法的具体用法?Python VoterManager.create_voter怎么用?Python VoterManager.create_voter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在voter.models.VoterManager的用法示例。


在下文中一共展示了VoterManager.create_voter方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: voter_create

# 需要导入模块: from voter.models import VoterManager [as 别名]
# 或者: from voter.models.VoterManager import create_voter [as 别名]
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')
开发者ID:JesseAldridge,项目名称:WeVoteServer,代码行数:51,代码来源:controllers.py

示例2: my_ballot_view

# 需要导入模块: from voter.models import VoterManager [as 别名]
# 或者: from voter.models.VoterManager import create_voter [as 别名]
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
开发者ID:ondrae,项目名称:WeVoteBase,代码行数:44,代码来源:views.py

示例3: test_count_with_no_cookie

# 需要导入模块: from voter.models import VoterManager [as 别名]
# 或者: from voter.models.VoterManager import create_voter [as 别名]
    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 = "[email protected]"
        voter_manager.create_voter(
            email=email1,
            password="password123",
        )
        email2 = "[email protected]"
        voter_manager.create_voter(
            email=email2,
            password="password123",
        )
        email3 = "[email protected]"
        voter_manager.create_voter(
            email=email3,
            password="password123",
        )

        #######################################
        # 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']))
开发者ID:JesseAldridge,项目名称:WeVoteServer,代码行数:75,代码来源:test_views_voter_count.py

示例4: test_count_with_cookie

# 需要导入模块: from voter.models import VoterManager [as 别名]
# 或者: from voter.models.VoterManager import create_voter [as 别名]
    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 = "[email protected]"
        voter_manager.create_voter(
            email=email1,
            password="password123",
        )
        email2 = "[email protected]"
        voter_manager.create_voter(
            email=email2,
            password="password123",
        )
        email3 = "[email protected]"
        voter_manager.create_voter(
            email=email3,
            password="password123",
        )

        #######################################
        # 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']))
开发者ID:JesseAldridge,项目名称:WeVoteServer,代码行数:82,代码来源:test_views_voter_count.py

示例5: voter_create_for_api

# 需要导入模块: from voter.models import VoterManager [as 别名]
# 或者: from voter.models.VoterManager import create_voter [as 别名]
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')
开发者ID:sammyds,项目名称:WeVoteServer,代码行数:68,代码来源:controllers.py


注:本文中的voter.models.VoterManager.create_voter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。