REST API
The REST API usage documentation can be found at https://www.rcvis.com/api/ This documentation only covers the internal modules used to support the REST API.
Serializers
Data serializers - used for the REST API
- class visualizer.serializers.BallotpediaSerializer(*args, **kwargs)
Bases:
BaseVisualizationSerializer
A serializer for specifying the Ballotpedia data.
Instead of specifying jsonFile (which is confusing because the sidecar is also JSON), this serializer uses “resultsSummaryFile” and “candidateSidecarFile”, along with the other options bp wants: 1. dataSourceURL (URL) 2. areResultsCertified (bool) 3. isPrimary (bool)
- class Meta
Bases:
Meta
- bp_fields = ('jsonFile', 'textForWinner', 'candidateSidecarFile', 'dataSourceURL', 'areResultsCertified', 'owner')
- fields = ('slug', 'id', 'movieHorizontal', 'movieVertical', 'movieGenerationStatus', 'numRounds', 'numCandidates', 'title', 'jsonFile', 'textForWinner', 'candidateSidecarFile', 'dataSourceURL', 'areResultsCertified', 'owner')
- resultsSummaryFile = SerializerMethodField()
- to_internal_value(data)
Instead of jsonFile, this endpoint uses the more-descriptive “resultsSummaryFile”. Rename resultsSummaryFile to jsonFile here.
- class visualizer.serializers.BaseVisualizationSerializer(*args, **kwargs)
Bases:
HyperlinkedModelSerializer
The rest_framework serializer for a JsonConfig Model. DRF expects a fixed set of options, so this uses the model defaults and nothing more.
- class Meta
Bases:
object
The meta class to simplify construction of the serializer
- fields = ('slug', 'id', 'movieHorizontal', 'movieVertical', 'movieGenerationStatus', 'numRounds', 'numCandidates', 'title')
- model
alias of
JsonConfig
- owner = ReadOnlyField(source='owner.username')
- read_only_but_validate_fields = ('numRounds', 'numCandidates', 'title')
- read_only_fields = ('slug', 'id', 'movieHorizontal', 'movieVertical', 'movieGenerationStatus')
- check_for_superfluous_fields_before_modification(data)
Raises a ValidationError if the data does not have superfluous fields.
- classmethod load_graph_or_errors(jsonFile, candidateSidecarFile)
Returns the graph, or raises an error if it cannot.
- classmethod populate_dict_with_json_data(model, graph)
Adds title, num rounds, num candidates to the model. Does not save the model.
- classmethod populate_model_with_json_data(model, graph)
Same as populate_dict_with_json_data, but using dot notation instead of bracket
- to_internal_value(data)
Before saving from the REST API, populates all required data
- to_representation(instance)
Object instance -> Dict of primitive datatypes.
- class visualizer.serializers.JsonOnlySerializer(*args, **kwargs)
Bases:
BaseVisualizationSerializer
A serializer for specifying just the JSON file. All other settings are set to their default values.
- class visualizer.serializers.UserSerializer(*args, **kwargs)
Bases:
ModelSerializer
The rest_framework serializer for a User Model
Validators
Data validation - to be used across REST and HTTP access
- visualizer.validators.ensure_file_is_under_2_mb(jsonFileObj)
Limit file size to 2mb
- visualizer.validators.ensure_title_is_under_256_chars(graph)
Limit title length 256 chars
- visualizer.validators.try_to_load_jsons(jsonFileObj, sidecarJsonFileObj)
Checks that the JSON can be loaded and is under 2mb. Raises: - BadJSONError: Summary JSON cannot be loaded - BadSidecarError: Sidecar JSON cannot be loaded - ValidationError: 2mb limit is reached - Anything else: unknown error Returns: - Loaded graph