from django.shortcuts import render
from django.http import JsonResponse, HttpResponse

import urllib.request
import urllib.parse
import json


# Create your views here.
def index(request):
    return HttpResponse('Success!')


def course_all(request):
    req = urllib.request.Request('http://models-api:8000/api/course/all/')
    resp_json = urllib.request.urlopen(req).read().decode('utf-8')
    resp = json.loads(resp_json)

    new_data = {}
    new_data['status_code'] = resp['status_code']

    course_data = []
    for course_dict in resp['all_courses']:
        new_dict = {}
        new_dict['href'] = course_dict['id'] + '/'
        new_dict['course_name'] = course_dict['mnemonic'] + " " + course_dict['number']
        if course_dict['section'] != '':
            new_dict['course_name'] += " - " + course_dict['section']
        if course_dict['title'] != '':
            new_dict['course_name'] += ": " + course_dict['title']
        new_dict['instructor'] = course_dict['instructor']
        course_data.append(new_dict)
    new_data['all_courses'] = course_data

    return JsonResponse(new_data)


def course_detail(request, sisid):
    req = urllib.request.Request('http://models-api:8000/api/course/' + sisid + '/')
    resp_json = urllib.request.urlopen(req).read().decode('utf-8')
    resp = json.loads(resp_json)

    for key in resp.keys:
        if resp[key] == '':
            resp.pop(key)

    return JsonResponse(resp)

@@ -1325,24 +1325,58 @@
 (resp_json)%0A
+    %0A    new_dict = resp%5B'course'%5D
 %0A    for key
@@ -1383,17 +1383,22 @@
  in 
-resp.keys
+list(new_dict)
 :%0A  
@@ -1406,20 +1406,24 @@
      if 
-resp
+new_dict
 %5Bkey%5D ==
@@ -1443,21 +1443,61 @@
     
-resp.pop(key)
+new_dict.pop(key, None)%0A    resp%5B'course'%5D = new_dict
 %0A%0A  

fix bug with dicts