Skip to content

Commit

Permalink
Integrated TCPD Elections 1951-62 dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
JusticeV452 committed Mar 1, 2024
1 parent 790ceaa commit 069f7f2
Show file tree
Hide file tree
Showing 8 changed files with 30,573 additions and 5 deletions.
20 changes: 20 additions & 0 deletions backend/app/api_views.py
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)
30,440 changes: 30,440 additions & 0 deletions backend/app/data/TCPD_IED_1951-62.csv

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions backend/app/management/commands/load_dataset.py
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()
22 changes: 22 additions & 0 deletions backend/app/migrations/0001_initial.py
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()),
],
),
]
16 changes: 16 additions & 0 deletions backend/app/models.py
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()
24 changes: 24 additions & 0 deletions backend/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,27 @@
in ways that can be transported across the backend/frontend divide, or
allow the frontend to suggest changes to the backend/database.
"""

from rest_framework import serializers
from .models import (
TCPDElection
)


class TCPDElectionSerializer(serializers.ModelSerializer):
"""
Serializes a photo
"""
number_of_seats = serializers.SerializerMethodField()

## TODO: Remove
# Not necessary, done to show how instance variables can be modified upon serialization
@staticmethod
def get_number_of_seats(instance):
return instance.number_of_seats

class Meta:
model = TCPDElection
fields = [
"election_type", "number_of_seats"
]
1 change: 1 addition & 0 deletions backend/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SETTINGS_DIR = os.path.join(CONFIG_DIR, 'settings')
DB_PATH = os.path.join(BACKEND_DIR, 'db.sqlite3')
PROJECT_ROOT = os.path.dirname(BACKEND_DIR)
DATASET_DIR = os.path.join(BACKEND_DIR, 'app', 'data')

# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

Expand Down
15 changes: 10 additions & 5 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
from django.contrib import admin
from django.urls import path

try:
from ..app import views
except (ImportError, ModuleNotFoundError):
from app import views
from app import views, api_views

urlpatterns = [
# Django admin page
path('admin/', admin.site.urls),
# API endpoints

################################################################################
# View Pages
################################################################################
path('', views.index),
path('example/', views.example),
path('example/<example_id>', views.example),

################################################################################
# API endpoints
################################################################################
path('1951-1962elections/', api_views.all_elections),
]

0 comments on commit 069f7f2

Please sign in to comment.