generated from dhmit/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated TCPD Elections 1951-62 dataset
- Loading branch information
1 parent
790ceaa
commit 069f7f2
Showing
8 changed files
with
30,573 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
|
||
from .models import ( | ||
TCPDElection | ||
) | ||
|
||
from .serializers import ( | ||
TCPDElectionSerializer | ||
) | ||
|
||
|
||
@api_view(['GET']) | ||
def all_elections(request): | ||
""" | ||
API endpoint to get all elections in the database | ||
""" | ||
elections = TCPDElection.objects.all() | ||
serializer = TCPDElectionSerializer(elections, many=True) | ||
return Response(serializer.data) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
Django management command load_dataset | ||
Updates local db with values from base csv dataset | ||
""" | ||
import os | ||
import pandas | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from app.models import TCPDElection | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Custom django-admin command used to run an analysis from the app/analysis folder | ||
""" | ||
help = '' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'dataset_name', | ||
type=str, | ||
action='store', | ||
help='Name of dataset in app/data folder (with extension)', | ||
) | ||
|
||
def handle(self, *args, **options): | ||
# pylint: disable=too-many-locals | ||
file_name = options.get('dataset_name') | ||
file_path = os.path.join(settings.DATASET_DIR, file_name) | ||
df = pandas.read_csv(file_path) | ||
|
||
# TODO: Generalize this to update correct model(s) and columns based on dataset | ||
for election_type, number_of_seats in zip(df["Election_Type"], df["NumberOfSeats"]): | ||
election = TCPDElection( | ||
election_type=election_type, | ||
number_of_seats=number_of_seats | ||
) | ||
election.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 5.0.2 on 2024-03-01 08:49 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='TCPDElection', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('election_type', models.CharField(choices=[('AE', 'Ae'), ('GE', 'Ge')], max_length=2)), | ||
('number_of_seats', models.IntegerField()), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
""" | ||
Models for the the data-driven-democracy web app. | ||
""" | ||
|
||
from django.db import models | ||
|
||
|
||
class TCPDElection(models.Model): | ||
# https://docs.djangoproject.com/en/5.0/ref/models/fields/#enumeration-types | ||
class ElectionType(models.TextChoices): | ||
AE = "AE" | ||
GE = "GE" | ||
|
||
election_type = models.CharField( | ||
max_length=2, | ||
choices=ElectionType | ||
) | ||
|
||
number_of_seats = models.IntegerField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters