Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active March 18, 2021 04:57
Show Gist options
  • Save chelseatroy/6aa9e4a79be93a867447f9ba8ff21fcf to your computer and use it in GitHub Desktop.
Save chelseatroy/6aa9e4a79be93a867447f9ba8ff21fcf to your computer and use it in GitHub Desktop.
Example of Proxy Model Dependency Injection
from __future__ import unicode_literals
from django.shortcuts import render
from models import Visualization
from exceptions import IncompleteDataException
from .forms import VisualizationForm
from django.contrib.auth.decorators import login_required
def _inject_visualization(name):
if name == "infection_rate_visualization":
return InfectionRateVisualization.objects.get(pk=name)
if name == "friend_family_visit_visualization":
return FriendFamilyVisitVisualization.objects.get(pk=name)
if name == "food_request_visualization":
return FoodRequestVisualization.objects.get(pk=name)
@login_required
def transform(request):
name = request.GET['script']
if request.POST and request.FILES:
form = VisualizaionForm(request.POST, request.FILES)
if form.is_valid():
input_file = request.FILES['file']
image = None
try:
vizualization = _inject_visualization(pk=name)
image = vizualization.from_data(name, input_file)
except IncompleteDataException as e:
return render(request, 'visualizations/form.html', {'error': e.message()})
return render(request, 'visualizations/results.html', {'image': image})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment