Skip to content

Instantly share code, notes, and snippets.

@alimp5
Forked from chelseatroy/models.py
Created March 18, 2021 04:32
Show Gist options
  • Save alimp5/873d8dae56a600fe7f0816d5d265c455 to your computer and use it in GitHub Desktop.
Save alimp5/873d8dae56a600fe7f0816d5d265c455 to your computer and use it in GitHub Desktop.
Example of Giant Conditional in Django Model
from __future__ import unicode_literals
from django.conf import settings
from django.db import models
from django.utils import timezone
import numpy as np
from calendar import Calendar
from charts import HeatMap, PieChart
from exceptions import IncompleteDataException
class Visualization(models.Model):
name = models.CharField(primary_key=True, max_length=200)
description = models.TextField()
position = models.IntegerField(default=200)
def __repr__ (self):
return self.name
def __str__ (self):
return self.name
def from_data(name, csvfile):
if name == "infection_rates":
department_instances = []
infection_type = []
with open csvfile as file:
if file["department"] and file["type"]:
department_instances = np.array(file["department"])
infection_type = np.array(file["type"])
else:
return IncompleteDataException("Please include a department and a type column in your CSV.")
map = HeatMap(list(zip(department_instances, infection_type)))
return map.to_svg()
if name == "family_visits":
#...similar rigamarole
if name == "food_requests":
#...similar rigamarole
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment