Census Module#

The Census module is a vital component of the Decide application, managing census-related functionalities. Below is the Sphinx documentation for its components.

admin.py#

class census.admin.CensusAdmin[source]#

Bases: ModelAdmin

A Django admin interface class for the Census model.

Configures how the Census model is displayed and interacted with in the Django admin interface. This includes settings for how the list of Census records is displayed, which fields are used for filtering and searching.

list_display#

Specifies the fields of the Census model to be displayed in the list view.

list_filter#

Specifies the fields of the Census model to be used for filtering in the list view.

search_fields#

Specifies the fields of the Census model to be used for searching in the list view.

list_display = ('voting_id', 'voter_id')#
list_filter = ('voting_id',)#
property media#
search_fields = ('voter_id',)#

models.py#

class census.models.Census[source]#

Bases: Model

Model representing a voting census, associating voters with votings.

voting_id#

The ID of the voting to which the voter belongs.

Type:

models.IntegerField

voter_id#

The ID of the user who is voting.

Type:

models.IntegerField

clean()[source]#

Validates that both the Voting and User instances exist.

save()[source]#

Saves the instance after validation.

exception DoesNotExist#

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned#

Bases: MultipleObjectsReturned

clean()[source]#

Validates that the referenced Voting and User objects exist.

Raises a ValidationError if either the voting or the user do not exist.

Raises:

ValidationError – If the referenced voting or user do not exist.

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>#
save(*args, **kwargs)[source]#

Saves the Census object after validation.

First calls clean to validate the data, then calls the save method of the superclass.

Parameters:
  • args – Variable arguments.

  • kwargs – Variable keyword arguments.

Returns:

The result of the save method from the superclass.

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.

forms.py#

class census.forms.CreationCensusForm[source]#

Bases: Form

A Django form for creating Census instances.

This form allows for the creation of new Census records, associating a voter with a voting.

voting_id#

Field for the ID of the voting.

Type:

forms.IntegerField

voter_id#

Field for the ID of the voter.

Type:

forms.IntegerField

Inner class:

Meta: Provides metadata for the form, including model association and fields.

class Meta[source]#

Bases: object

Meta class for CreationCensusForm.

Specifies the model and fields associated with this form.

model#

The model associated with this form.

fields#

The fields of the model to include in this form.

fields = ('voting_id', 'voter_id')#
model#

alias of Census

base_fields = {'voter_id': <django.forms.fields.IntegerField object>, 'voting_id': <django.forms.fields.IntegerField object>}#
declared_fields = {'voter_id': <django.forms.fields.IntegerField object>, 'voting_id': <django.forms.fields.IntegerField object>}#
property media#

Return all media required to render the widgets on this form.

save(commit=True)[source]#

Saves the form instance as a Census model instance.

Overrides the save method to handle the saving of the Census instance with the provided ‘voting_id’ and ‘voter_id’.

Parameters:

commit (bool) – Whether to save the instance to the database. Defaults to True.

Returns:

The saved or unsaved Census instance, depending on the commit parameter.

Return type:

Census

views.py#

class census.views.CensusCreate[source]#

Bases: ListCreateAPIView

API view for listing and creating census records.

Inherits from ListCreateAPIView to provide handling for GET and POST requests for Census model.

create(request, *args, **kwargs)[source]#

Creates new census records from the provided data.

Parameters:
  • request – The HTTP request object containing voting and voter data.

  • args – Variable arguments.

  • kwargs – Keyword arguments.

Returns:

A Response object with creation status.

list(request, *args, **kwargs)[source]#

Lists voters in a census for a given voting.

Parameters:
  • request – The HTTP request object containing the voting_id parameter.

  • args – Variable arguments.

  • kwargs – Keyword arguments.

Returns:

A Response object with the list of voters.

permission_classes = (<class 'base.perms.UserIsStaff'>,)#
class census.views.CensusDetail[source]#

Bases: RetrieveDestroyAPIView

API view for retrieving and deleting census records.

Inherits from RetrieveDestroyAPIView to provide handling for GET and DELETE requests for the Census model.

destroy(request, voting_id, *args, **kwargs)[source]#

Deletes voters from a census based on provided voting_id and voter ids.

Parameters:
  • request – The HTTP request object containing voter data.

  • voting_id – The ID of the voting.

  • args – Variable arguments.

  • kwargs – Keyword arguments.

Returns:

A Response object confirming deletion.

retrieve(request, voting_id, *args, **kwargs)[source]#

Retrieves a specific voter in a census based on provided voting_id and voter id.

Parameters:
  • request – The HTTP request object containing the voter_id parameter.

  • voting_id – The ID of the voting.

  • args – Variable arguments.

  • kwargs – Keyword arguments.

Returns:

A Response object indicating if the voter is valid.

class census.views.CensusExportView[source]#

Bases: TemplateView

View for exporting census data to an Excel file.

Inherits from TemplateView to display a page for selecting the voting to export census data.

dispatch(request, *args, **kwargs)[source]#

Handles the request before it reaches the get or post method. Redirects to home if the user is not an admin.

Parameters:
  • request – The HTTP request object.

  • args – Variable arguments.

  • kwargs – Keyword arguments.

Returns:

HttpResponse or redirection.

get_context_data(**kwargs)[source]#

Provides the context data for rendering the template.

Parameters:

kwargs – Keyword arguments.

Returns:

Context data dictionary.

template_name = 'census/export_census.html'#
class census.views.CensusImportView[source]#

Bases: TemplateView

View for importing census data from an Excel file.

Inherits from TemplateView to display a page for selecting the voting and uploading the Excel file for census data.

dispatch(request, *args, **kwargs)[source]#

Handles the request before it reaches the get or post method. Redirects to home if the user is not an admin.

Parameters:
  • request – The HTTP request object.

  • args – Variable arguments.

  • kwargs – Keyword arguments.

Returns:

HttpResponse or redirection.

get_context_data(**kwargs)[source]#

Provides the context data for rendering the template.

Parameters:

kwargs – Keyword arguments.

Returns:

Context data dictionary.

post(request, *args, **kwargs)[source]#

Handles the POST request for importing census data from an uploaded Excel file.

Parameters:
  • request – The HTTP request object.

  • args – Variable arguments.

  • kwargs – Keyword arguments.

Returns:

Redirects to the import page with a success or error message.

template_name = 'census/import_census.html'#
census.views.GetId(request)[source]#

Retrieves and displays the census details for a given voting ID.

Parameters:

request – The HTTP request object containing the voting ID.

Returns:

Rendered web page with census details or an error message.

census.views.census(request)[source]#

Renders the census page.

Parameters:

request – The HTTP request object.

Returns:

Rendered web page for the census.

census.views.censusList(request)[source]#

Displays a list of all census records.

Parameters:

request – The HTTP request object.

Returns:

Rendered web page displaying a list of all census records.

census.views.createCensus(request)[source]#

Handles the creation of a census record through a form submission.

Parameters:

request – The HTTP request object.

Returns:

Redirects to the census page on success or renders the creation form with errors.

census.views.deleteCensus(request)[source]#

Deletes a specific census record based on the voting and voter ID provided in the request.

Parameters:

request – The HTTP request object containing the voting and voter IDs.

Returns:

Redirects to the census page on successful deletion or renders the census page with an error message.

census.views.export_census(request, voting_id)[source]#

Exports the census data for a given voting to an Excel file.

Parameters:
  • request – The HTTP request object.

  • voting_id – The ID of the voting whose census data is to be exported.

Returns:

HttpResponse with the Excel file.

census.views.is_admin(user)[source]#

Determines if the given user is an admin.

Parameters:

user (User) – The user object to be checked.

Returns:

True if the user is authenticated and is a staff member, False otherwise.

Return type:

bool

tests.py#

class census.tests.CensusExportViewTest[source]#

Bases: BaseTestCase

Test suite for the CensusExportView functionality.

create_voting()[source]#

Creates a Voting instance with associated questions and options.

Returns:

The Voting instance that was created.

Return type:

Voting

setUp()[source]#

Set up the test environment for each test method.

test_census_export_view_for_admin()[source]#

Test the CensusExportView for admin users.

Ensures that admin users can access the export census view and that the context contains the correct data.

test_census_export_view_for_non_admin()[source]#

Test the CensusExportView for non-admin users.

Ensures that non-admin users are redirected when trying to access the export census view.

class census.tests.CensusImportViewTest[source]#

Bases: BaseTestCase

Test suite for the CensusImportView functionality.

create_voting()[source]#

Creates a Voting instance with associated questions and options.

Returns:

The Voting instance that was created.

Return type:

Voting

setUp()[source]#

Set up the test environment for each test method by creating a user and initializing the base test case.

test_census_import_view_fail()[source]#

Test the failure case of importing census data from an Excel file with incorrect data.

Ensures that an error message is displayed when incorrect data is imported.

test_census_import_view_no_file()[source]#

Test the case where no file is provided for importing census data.

Ensures that an appropriate error message is displayed when no file is selected.

test_census_import_view_not_admin()[source]#

Test the access control for the import census view to ensure only admins can access it.

Ensures that non-admin users receive an appropriate error message when trying to access the import view.

test_census_import_view_success()[source]#

Test the successful import of census data from an Excel file.

Ensures that census data is correctly imported into the database and appropriate success message is displayed.

test_census_import_view_voting_ended()[source]#

Test the case where an attempt is made to import census data for a voting that has already ended.

Ensures that an appropriate error message is displayed when trying to import data for an ended voting.

test_census_import_view_voting_not_started()[source]#

Test the case where an attempt is made to import census data for a voting that has not started yet.

Ensures that an appropriate error message is displayed when trying to import data for a not-started voting.

class census.tests.CensusTest[source]#

Bases: StaticLiveServerTestCase

Test case for census creation using Selenium and a live server.

This class inherits from StaticLiveServerTestCase to provide a test environment for web-based interactions using Selenium WebDriver.

createCensusEmptyError()[source]#

Test the behavior when attempting to create a census entry with empty fields.

This method checks if the correct error message is displayed when trying to save a census entry without filling out the required fields.

createCensusSuccess()[source]#

Test the successful creation of a census entry in the admin interface.

This method simulates the process of logging into the admin site and creating a new census entry with valid data.

createCensusValueError()[source]#

Test the behavior when attempting to create a census entry with invalid values.

This method simulates the creation of a census entry with invalid values and checks if the appropriate error message is displayed.

setUp()[source]#

Set up the test environment before each test method.

Initializes the WebDriver and sets up the base test environment.

tearDown()[source]#

Tear down the test environment after each test method.

Closes the WebDriver and tears down the base test environment.

class census.tests.CensusTestCase[source]#

Bases: BaseTestCase

Test case for census-related operations.

This class inherits from TestCase and provides test methods for creating, validating, and deleting census records.

setUp()[source]#

Set up the test environment before each test method.

Creates a voting instance, a user, and a census record.

tearDown()[source]#

Tear down the test environment after each test method.

Cleans up any created test data.

test_add_new_voters()[source]#

Test adding new voters to a voting.

Validates that new voters can be added to a voting successfully.

test_add_new_voters_conflict()[source]#

Test adding new voters to a voting when there’s a conflict.

Validates the behavior when adding a voter who no longer exists.

test_check_vote_permissions()[source]#

Test to check voting permissions for a user.

Validates that a user has permission to vote in a specific voting.

test_create_census()[source]#

Test the creation of a census record.

Validates that the census record is correctly created and its attributes match the expected values.

test_create_census_invalid_voter_id()[source]#

Test the creation of a census record with an invalid voter ID.

Expects a ValueError when trying to create a census with an invalid voter ID.

test_create_census_invalid_voting_id()[source]#

Test the creation of a census record with an invalid voting ID.

Expects a ValueError when trying to create a census with an invalid voting ID.

test_create_census_invalid_voting_id_and_voter_id()[source]#

Test the creation of a census record with both invalid voting and voter IDs.

Expects a ValueError when trying to create a census with invalid voting and voter IDs.

test_delete_census()[source]#

Test the deletion of a census record.

Validates that the census record is successfully deleted and no longer exists in the database.

test_delete_census_invalid_voter_id()[source]#

Test the deletion of a census record with an invalid voter ID.

Expects a ValueError when trying to delete a census with an invalid voter ID.

test_delete_census_invalid_voting_id()[source]#

Test the deletion of a census record with an invalid voting ID.

Expects a ValueError when trying to delete a census with an invalid voting ID.

test_delete_census_invalid_voting_id_and_voter_id()[source]#

Test the deletion of a census record with both invalid voting and voter IDs.

Expects a ValueError when trying to delete a census with invalid voting and voter IDs.

test_destroy_voter()[source]#

Test destroying a voter in a voting.

Validates that a voter can be removed from a voting successfully.

test_get_census()[source]#

Test retrieval of a specific census record.

Validates that a specific census record can be retrieved and its attributes match the expected values.

test_get_census_invalid_voter_id()[source]#

Test retrieval of a census record with an invalid voter ID.

Expects a ValueError when trying to retrieve a census record with an invalid voter ID.

test_get_census_invalid_voting_id()[source]#

Test retrieval of a census record with an invalid voting ID.

Expects a ValueError when trying to retrieve a census record with an invalid voting ID.

test_get_census_invalid_voting_id_and_voter_id()[source]#

Test retrieval of a census record with both invalid voting and voter IDs.

Expects a ValueError when trying to retrieve a census record with invalid voting and voter IDs.

test_list_census()[source]#

Test listing of all census records.

Validates that the census records can be retrieved and their attributes match the expected values.

test_list_census_invalid_voter_id()[source]#

Test listing of census records with an invalid voter ID.

Expects a ValueError when trying to list census records with an invalid voter ID.

test_list_census_invalid_voting_id()[source]#

Test listing of census records with an invalid voting ID.

Expects a ValueError when trying to list census records with an invalid voting ID.

test_list_census_invalid_voting_id_and_voter_id()[source]#

Test listing of census records with both invalid voting and voter IDs.

Expects a ValueError when trying to list census records with invalid voting and voter IDs.

test_list_voting()[source]#

Test listing all votings.

Validates that all votings can be listed and their attributes match the expected values.

class census.tests.ExportCensusTest[source]#

Bases: BaseTestCase

Test suite for exporting census data.

create_voters(v)[source]#

Creates voters and adds them to the census of a given voting.

Parameters:

v (Voting) – The Voting instance to which the voters will be added.

create_voting()[source]#

Creates a Voting instance with associated questions and options.

Returns:

The Voting instance that was created.

Return type:

Voting

setUp()[source]#

Set up the test environment for each test method.

test_export_census()[source]#

Test the functionality of exporting census data to an Excel file.

Ensures that the response contains the correct content type and disposition, and the Excel file contains the correct data.