Store Module#
The Store module is part of the Decide application, handling aspects related to data storage and management. Below is the Sphinx documentation for its components.
models.py#
- class store.models.Vote[source]#
- Bases: - Model- A model representing a vote in a voting system. - voting_id#
- The ID of the voting session this vote is associated with. - Type:
- PositiveIntegerField 
 
 - voter_id#
- The ID of the voter who cast this vote. - Type:
- PositiveIntegerField 
 
 - a#
- Encrypted data part A representing the vote. - Type:
- BigBigField 
 
 - b#
- Encrypted data part B representing the vote. - Type:
- BigBigField 
 
 - voted#
- The timestamp of when the vote was cast. - Type:
- DateTimeField 
 
 - exception DoesNotExist#
- Bases: - ObjectDoesNotExist
 - exception MultipleObjectsReturned#
- Bases: - MultipleObjectsReturned
 - a#
- A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. 
 - b#
- A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. 
 - get_next_by_voted(*, field=<django.db.models.fields.DateTimeField: voted>, is_next=True, **kwargs)#
 - get_previous_by_voted(*, field=<django.db.models.fields.DateTimeField: voted>, is_next=False, **kwargs)#
 - id#
- A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. 
 - objects = <django.db.models.manager.Manager object>#
 - voted#
- A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. 
 - voter_id#
- A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. 
 - voting_id#
- A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. 
 
utils.py#
- store.utils.choices_store(request)[source]#
- Processes and stores votes for a voting of type ‘choices’. - Parameters:
- request – Django HttpRequest object containing votes data. 
 - The request should contain:
- voting (int): The ID of the voting. 
- voter (int): The ID of the voter. 
- votes (list): A list of dictionaries, each containing the ‘a’ and ‘b’ keys representing individual encrypted votes. 
- voting_type (str): A string representing the type of voting (‘choices’). 
 
 - Returns:
- HTTP status code indicating the result of the operation. 
- Raises:
- HTTP_401_UNAUTHORIZED – If the voting is not found, or has not started, or is closed. 
- HTTP_400_BAD_REQUEST – If the voting ID, voter ID, or votes data is missing or invalid. 
 
 
- store.utils.classic_store(request)[source]#
- Processes and stores a classic type vote. - Parameters:
- request – Django HttpRequest object containing vote data. 
 - The request should contain:
- voting (int): The ID of the voting. 
- voter (int): The ID of the voter. 
- vote (dict): A dictionary containing the ‘a’ and ‘b’ keys representing the encrypted vote. 
- voting_type (str): A string representing the type of voting (‘yesno’, ‘classic’, ‘comment’, ‘preference’). 
 
 - Returns:
- HTTP status code indicating the result of the operation. 
- Raises:
- HTTP_401_UNAUTHORIZED – If the voting is not found, or has not started, or is closed. 
- HTTP_400_BAD_REQUEST – If the voting ID, voter ID, or vote data is missing or invalid. 
 
 
views.py#
- class store.views.StoreView[source]#
- Bases: - ListAPIView- API view for listing and creating votes. - Inherits from Django Rest Framework’s ListAPIView to provide a method for listing existing votes and a method for creating new votes based on the voting type. - queryset#
- A queryset that includes all Vote objects. 
 - serializer_class#
- Serializer class used to serialize Vote objects. 
 - filter_backends#
- Set of filter backends to be used for the queryset. 
 - filterset_fields#
- Fields of the Vote model that can be filtered. 
 - filter_backends = (<class 'django_filters.rest_framework.backends.DjangoFilterBackend'>,)#
 - filterset_fields = ('voting_id', 'voter_id')#
 - get(request)[source]#
- Handles GET requests to retrieve a list of votes. - Parameters:
- request – HttpRequest object. 
- Returns:
- Response object containing serialized Vote data. 
- Raises:
- PermissionDenied – If the user is not staff. 
 
 - post(request)[source]#
- Handles POST requests to create a new vote based on the voting type. - Parameters:
- request – HttpRequest object containing voting_type and vote data. 
- Returns:
- Response object with an HTTP status code. 
- Raises:
- HTTP_400_BAD_REQUEST – If voting_type is not in the predefined voting types. 
 
 - queryset#
 - serializer_class#
- alias of - VoteSerializer
 
tests.py#
- class store.tests.StoreChoiceCase[source]#
- Bases: - BaseTestCase- Test case for storing votes with choices. - This class tests the functionality of storing votes for different types of voting, including classic, choices, text, yes/no, and preference types. - gen_votes()[source]#
- Generate random votes for testing. - Returns:
- A tuple containing lists of generated voting IDs and user IDs. 
- Return type:
- tuple 
 
 - gen_voting(pk)[source]#
- Generate a voting object for testing. - Parameters:
- pk (int) – Primary key for the voting object. 
 - Creates a Voting object with specified primary key and saves it to the database. 
 - get_or_create_user(pk)[source]#
- Get an existing user or create a new one for testing. - Parameters:
- pk (int) – Primary key of the user. 
- Returns:
- The fetched or created User object. 
- Return type:
- User 
 
 - setUp()[source]#
- Set up necessary objects for testing. - Creates Question and Voting objects for both classic and multiple choices type voting. 
 - test_filter()[source]#
- Test case for filtering votes. - This method generates votes and then tests filtering them by voting_id and voter_id via GET requests, verifying the response status codes and content. 
 - test_gen_vote_invalid()[source]#
- Test case for generating an invalid vote. - This method sends a POST request with invalid vote data and expects to receive a 401 Unauthorized status code. - data#
- A dictionary containing the invalid vote data. - Type:
- dict 
 
 
 - test_hasvote()[source]#
- Test case for checking if a specific vote exists. - This method tests the existence of a specific vote by voter_id and voting_id, verifying the response status codes and content. 
 - test_store_vote()[source]#
- Test case for storing a valid vote. - This method creates a voting instance and a user, then sends a POST request to store a vote and expects to receive a 200 OK status code. It also checks the count and content of the Vote objects in the database to ensure the vote is stored correctly. - VOTING_PK#
- Primary key for the voting instance. - Type:
- int 
 
 - CTE_A#
- Test constant for ‘a’ field in vote. - Type:
- int 
 
 - CTE_B#
- Test constant for ‘b’ field in vote. - Type:
- int 
 
 - data#
- A dictionary containing the vote data. - Type:
- dict 
 
 
 - test_store_vote_choices()[source]#
- Test case for storing multiple choice votes. - This method tests the storage of multiple choice votes by creating relevant data, sending a POST request, and verifying the response and database records. - CTE_A#
- Test constant for ‘a’ field in vote. - Type:
- int 
 
 - CTE_B#
- Test constant for ‘b’ field in vote. - Type:
- int 
 
 - data#
- A dictionary containing multiple votes data. - Type:
- dict 
 
 
 - test_store_vote_text()[source]#
- Test case for storing a text vote. - This method tests storing a text-based vote by creating relevant data, sending a POST request, and verifying the response and database records. - CTE_A#
- Test constant for ‘a’ field in vote. - Type:
- int 
 
 - CTE_B#
- Test constant for ‘b’ field in vote. - Type:
- int 
 
 - data#
- A dictionary containing the text vote data. - Type:
- dict 
 
 
 - test_vote()[source]#
- Test case for retrieving stored votes. - This method generates votes and then tests retrieving them via GET requests, verifying the response status codes and content. 
 
- class store.tests.StorePreferenceCase[source]#
- Bases: - BaseTestCase- Test case for storing preference-type votes. 
- class store.tests.StoreTextCase[source]#
- Bases: - BaseTestCase- Test case for storing text-type votes.