Last active
September 19, 2016 23:51
-
-
Save batemapf/2d677b8f2d90b713ae0ef3dd2d3284ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import os | |
import json | |
from projects.models import Project | |
from django.contrib.auth.models import User | |
from django.core.exceptions import ValidationError | |
""" | |
For data structure: | |
[ | |
{ | |
"id1": 77, | |
"id2": "", | |
"id3": "", | |
"id4": "", | |
"id5": "", | |
"id7": "", | |
"id8": "", | |
"18F Project POC Email": "john.smith" | |
}, | |
{ | |
"id1": 44, | |
"id2": "", | |
"id3": "", | |
"id4": "", | |
"id5": "", | |
"id7": "", | |
"id8": "", | |
"18F Project POC Email": "christopher.smith" | |
}, | |
] | |
""" | |
data = json.loads(open('agreement_poc.json', 'r').read()) | |
fields = 'id4', 'id7', 'id3', 'id1', 'id2', 'id8','id5' | |
field_list = list() | |
for i in fields: | |
field_list.append(i) | |
for obj in data: | |
for field in field_list: | |
if obj[field]: | |
try: | |
project = Project.objects.get(id=obj[field]) | |
except Project.DoesNotExist: | |
ValidationError("Project does not exist.") | |
if project: | |
try: | |
project_lead = User.objects.get(username=obj['18F Project POC Email'].strip()) | |
project.project_lead = project_lead | |
project.save() | |
except User.DoesNotExist: | |
raise ValidationError("{0} does not exist.".format(obj['18F Project POC Email'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment