Little cleanup on courses
authorCharles Connell <charles@connells.org>
Tue, 17 Dec 2013 16:08:19 +0000 (11:08 -0500)
committerCharles Connell <charles@connells.org>
Wed, 18 Dec 2013 03:49:03 +0000 (22:49 -0500)
karmaworld/apps/courses/test/test.py
karmaworld/apps/courses/test/test_selenium.py
karmaworld/apps/courses/views.py

index 2d7417693cfec02ef1276d5d441ccba949c07f96..cd577a0f08d849b383375fd506b6f26101a4b1f3 100644 (file)
@@ -15,8 +15,14 @@ class CoursesTests(TestCase):
     def testCourseUniqueness(self):
         """Make sure we can't create multiple courses with the same
         school + course name + instructor name combination."""
-        #with self.assertRaises(Exception):
-        #    Course.objects.create(name="Underwater Basketweaving", instructor_name="Alice", school=self.harvard)
+        with self.assertRaises(Exception):
+            Course.objects.create(name="Underwater Basketweaving", instructor_name="Alice Janney", school=self.harvard)
+
+    def testSchoolSlug(self):
+        self.assertEqual(self.harvard.slug, defaultfilters.slugify(self.harvard.name))
+
+    def testCourseSlug(self):
+        self.assertEqual(self.course1.slug, defaultfilters.slugify("%s %s" % (self.course1.name, self.course1.id)))
 
     def testSearchForSchool(self):
         """Test searching for a school by partial name"""
index 6c81d3638f35a6f65ff5a784ca860a204f28c56b..3130704f8eb2d59a4bcde87303eb273f92754fbb 100644 (file)
@@ -55,7 +55,7 @@ class AddCourseTest(LiveServerTestCase):
         self.assertEqual(schoolInput.get_attribute("value"), "Harvard University")
 
         schoolId = self.driver.find_element_by_id("id_school")
-        self.assertEqual(schoolId.get_attribute("value"), self.harvard.id)
+        self.assertEqual(schoolId.get_attribute("value"), str(self.harvard.id))
 
     def testCreateCourse(self):
         self.driver.get(self.live_server_url)
index 39c32ad09c0b35ba2e85a69c89031092646817d7..48cfa2be156dc4f548f624fcb970dd533a0a1ce6 100644 (file)
@@ -20,6 +20,7 @@ from karmaworld.apps.courses.models import Course
 from karmaworld.apps.courses.models import School
 from karmaworld.apps.notes.models import Note
 
+
 class CourseListView(ListView, ModelFormMixin, ProcessFormView):
     """ Simple ListView for the front page that includes the CourseForm """
     model = Course
@@ -47,8 +48,7 @@ class CourseListView(ListView, ModelFormMixin, ProcessFormView):
         """ On form submission success, redirect to what url """
         return u'/{school_slug}/{course_slug}'.format(
                 school_slug=self.object.school.slug,
-                course_slug=self.object.slug
-            )
+                course_slug=self.object.slug)
 
     def form_invalid(self, form, **kwargs):
         """ override form_invalid to populate object_list on redirect """
@@ -82,102 +82,101 @@ class AboutView(TemplateView):
 
     def get_context_data(self, **kwargs):
         """ get the list of schools with the most files for leaderboard """
-        context = { 'params': kwargs, \
+        context = { 'params': kwargs,
                     'schools': School.objects.all()[:3] }
         return context
 
 
 def school_list(request):
     """ Return JSON describing Schools that match q query on name """
-    if request.method == 'POST' and request.is_ajax() \
-                        and request.POST.has_key('q'):
-        # if an ajax get request with a 'q' name query
-        # get the schools as a id name dict,
-        _query = request.POST['q']
-        matching_school_aliases = list(School.objects.filter(alias__icontains=_query))
-        matching_school_names = list(School.objects.filter(name__icontains=_query)[:20])
-        _schools = matching_school_aliases[:2] + matching_school_names
-        schools = [{'id': s.id, 'name': s.name} for s in _schools]
-
-        # return as json
-        return HttpResponse(json.dumps({'status':'success', 'schools': schools}), mimetype="application/json")
-    else:
-        # else return that the api call failed
+    if not (request.method == 'POST' and request.is_ajax()
+                        and request.POST.has_key('q')):
+        #return that the api call failed
         return HttpResponseBadRequest(json.dumps({'status':'fail'}), mimetype="application/json")
 
+    # if an ajax get request with a 'q' name query
+    # get the schools as a id name dict,
+    _query = request.POST['q']
+    matching_school_aliases = list(School.objects.filter(alias__icontains=_query))
+    matching_school_names = list(School.objects.filter(name__icontains=_query)[:20])
+    _schools = matching_school_aliases[:2] + matching_school_names
+    schools = [{'id': s.id, 'name': s.name} for s in _schools]
+
+    # return as json
+    return HttpResponse(json.dumps({'status':'success', 'schools': schools}), mimetype="application/json")
+
 
 def school_course_list(request):
     """Return JSON describing courses we know of at the given school
      that match the query """
-    if request.method == 'POST' and request.is_ajax() \
-                        and request.POST.has_key('q')\
-                        and request.POST.has_key('school_id'):
-
-        _query = request.POST['q']
-        try:
-          _school_id = int(request.POST['school_id'])
-        except:
-          return HttpResponseBadRequest(json.dumps({'status': 'fail',
-                                                  'message': 'could not convert school id to integer'}),
-                                      mimetype="application/json")
-
-        # Look up the school
-        try:
-            school = School.objects.get(id__exact=_school_id)
-        except (MultipleObjectsReturned, ObjectDoesNotExist):
-            return HttpResponseBadRequest(json.dumps({'status': 'fail',
-                                                    'message': 'school id did not match exactly one school'}),
-                                        mimetype="application/json")
-
-        # Look up matching courses
-        _courses = Course.objects.filter(school__exact=school.id, name__icontains=_query)
-        courses = [{'name': c.name} for c in _courses]
-
-        # return as json
-        return HttpResponse(json.dumps({'status':'success', 'courses': courses}),
-                            mimetype="application/json")
-    else:
-        # else return that the api call failed
+    if not (request.method == 'POST' and request.is_ajax()
+                        and request.POST.has_key('q')
+                        and request.POST.has_key('school_id')):
+        # return that the api call failed
         return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'query parameters missing'}),
                                     mimetype="application/json")
 
+    _query = request.POST['q']
+    try:
+      _school_id = int(request.POST['school_id'])
+    except:
+      return HttpResponseBadRequest(json.dumps({'status': 'fail',
+                                              'message': 'could not convert school id to integer'}),
+                                  mimetype="application/json")
+
+    # Look up the school
+    try:
+        school = School.objects.get(id__exact=_school_id)
+    except (MultipleObjectsReturned, ObjectDoesNotExist):
+        return HttpResponseBadRequest(json.dumps({'status': 'fail',
+                                                'message': 'school id did not match exactly one school'}),
+                                    mimetype="application/json")
+
+    # Look up matching courses
+    _courses = Course.objects.filter(school__exact=school.id, name__icontains=_query)
+    courses = [{'name': c.name} for c in _courses]
+
+    # return as json
+    return HttpResponse(json.dumps({'status':'success', 'courses': courses}),
+                        mimetype="application/json")
+
+
 def school_course_instructor_list(request):
     """Return JSON describing instructors we know of at the given school
        teaching the given course
        that match the query """
-    if request.method == 'POST' and request.is_ajax() \
-                        and request.POST.has_key('q')\
-                        and request.POST.has_key('course_name')\
-                        and request.POST.has_key('school_id'):
-
-        _query = request.POST['q']
-        _course_name = request.POST['course_name']
-        try:
-          _school_id = int(request.POST['school_id'])
-        except:
-          return HttpResponseBadRequest(json.dumps({'status': 'fail',
-                                                  'message':'could not convert school id to integer'}),
-                                      mimetype="application/json")
-
-        # Look up the school
-        try:
-            school = School.objects.get(id__exact=_school_id)
-        except (MultipleObjectsReturned, ObjectDoesNotExist):
-            return HttpResponseBadRequest(json.dumps({'status': 'fail',
-                                                    'message': 'school id did not match exactly one school'}),
-                                        mimetype="application/json")
-
-        # Look up matching courses
-        _courses = Course.objects.filter(school__exact=school.id,
-                                         name__exact=_course_name,
-                                         instructor_name__icontains=_query)
-        instructors = [{'name': c.instructor_name, 'url': c.get_absolute_url()} for c in _courses]
-
-        # return as json
-        return HttpResponse(json.dumps({'status':'success', 'instructors': instructors}),
-                            mimetype="application/json")
-    else:
-        # else return that the api call failed
+    if not(request.method == 'POST' and request.is_ajax()
+                        and request.POST.has_key('q')
+                        and request.POST.has_key('course_name')
+                        and request.POST.has_key('school_id')):
+        # return that the api call failed
         return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'query parameters missing'}),
                                     mimetype="application/json")
 
+    _query = request.POST['q']
+    _course_name = request.POST['course_name']
+    try:
+      _school_id = int(request.POST['school_id'])
+    except:
+      return HttpResponseBadRequest(json.dumps({'status': 'fail',
+                                              'message':'could not convert school id to integer'}),
+                                  mimetype="application/json")
+
+    # Look up the school
+    try:
+        school = School.objects.get(id__exact=_school_id)
+    except (MultipleObjectsReturned, ObjectDoesNotExist):
+        return HttpResponseBadRequest(json.dumps({'status': 'fail',
+                                                'message': 'school id did not match exactly one school'}),
+                                    mimetype="application/json")
+
+    # Look up matching courses
+    _courses = Course.objects.filter(school__exact=school.id,
+                                     name__exact=_course_name,
+                                     instructor_name__icontains=_query)
+    instructors = [{'name': c.instructor_name, 'url': c.get_absolute_url()} for c in _courses]
+
+    # return as json
+    return HttpResponse(json.dumps({'status':'success', 'instructors': instructors}),
+                        mimetype="application/json")
+