closes #253 by removing field, adds backend for #236, and nearly completes #68
authorBryan <btbonval@gmail.com>
Mon, 6 Jan 2014 23:56:23 +0000 (18:56 -0500)
committerBryan <btbonval@gmail.com>
Mon, 6 Jan 2014 23:56:23 +0000 (18:56 -0500)
karmaworld/apps/courses/forms.py
karmaworld/apps/courses/migrations/0005_auto__add_department__del_field_course_academic_year__add_field_course.py [new file with mode: 0644]
karmaworld/apps/courses/models.py
karmaworld/apps/notes/management/commands/import_ocw_json.py
karmaworld/templates/courses/course_detail.html
karmaworld/templates/courses/course_list_entry.html

index 7b18fe1d64e4d7730b6557758fdfffd3fb70ec20..bde2cfccd7821b944adb5c6aa250f65719d9ad04 100644 (file)
@@ -9,6 +9,6 @@ from karmaworld.apps.courses.models import Course
 class CourseForm(ModelForm):
     class Meta:
         model = Course
-        fields = ('name', 'school', 'desc', 'url', 'instructor_name', \
-                'instructor_email')
+        fields = ('name', 'school', 'url', 'instructor_name', \
+                  'instructor_email')
 
diff --git a/karmaworld/apps/courses/migrations/0005_auto__add_department__del_field_course_academic_year__add_field_course.py b/karmaworld/apps/courses/migrations/0005_auto__add_department__del_field_course_academic_year__add_field_course.py
new file mode 100644 (file)
index 0000000..433b562
--- /dev/null
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        # Adding model 'Department'
+        db.create_table('courses_department', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('name', self.gf('django.db.models.fields.CharField')(max_length=255)),
+            ('school', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['courses.School'])),
+            ('slug', self.gf('django.db.models.fields.SlugField')(max_length=150, null=True)),
+            ('url', self.gf('django.db.models.fields.URLField')(max_length=511, null=True, blank=True)),
+        ))
+        db.send_create_signal('courses', ['Department'])
+
+        # Deleting field 'Course.academic_year'
+        db.delete_column('courses_course', 'academic_year')
+
+        # Adding field 'Course.department'
+        db.add_column('courses_course', 'department',
+                      self.gf('django.db.models.fields.related.ForeignKey')(to=orm['courses.Department'], null=True, blank=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting model 'Department'
+        db.delete_table('courses_department')
+
+        # Adding field 'Course.academic_year'
+        db.add_column('courses_course', 'academic_year',
+                      self.gf('django.db.models.fields.IntegerField')(default=2014, null=True, blank=True),
+                      keep_default=False)
+
+        # Deleting field 'Course.department'
+        db.delete_column('courses_course', 'department_id')
+
+
+    models = {
+        'courses.course': {
+            'Meta': {'ordering': "['-file_count', 'school', 'name']", 'unique_together': "(('school', 'name', 'instructor_name'),)", 'object_name': 'Course'},
+            'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'department': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['courses.Department']", 'null': 'True', 'blank': 'True'}),
+            'desc': ('django.db.models.fields.TextField', [], {'max_length': '511', 'null': 'True', 'blank': 'True'}),
+            'file_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'flags': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'instructor_email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'null': 'True', 'blank': 'True'}),
+            'instructor_name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'school': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['courses.School']"}),
+            'slug': ('django.db.models.fields.SlugField', [], {'max_length': '150', 'null': 'True'}),
+            'updated_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.utcnow'}),
+            'url': ('django.db.models.fields.URLField', [], {'max_length': '511', 'null': 'True', 'blank': 'True'})
+        },
+        'courses.department': {
+            'Meta': {'object_name': 'Department'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'school': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['courses.School']"}),
+            'slug': ('django.db.models.fields.SlugField', [], {'max_length': '150', 'null': 'True'}),
+            'url': ('django.db.models.fields.URLField', [], {'max_length': '511', 'null': 'True', 'blank': 'True'})
+        },
+        'courses.school': {
+            'Meta': {'ordering': "['-file_count', '-priority', 'name']", 'object_name': 'School'},
+            'alias': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'facebook_id': ('django.db.models.fields.BigIntegerField', [], {'null': 'True', 'blank': 'True'}),
+            'file_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'hashtag': ('django.db.models.fields.CharField', [], {'max_length': '16', 'null': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'location': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'priority': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'slug': ('django.db.models.fields.SlugField', [], {'max_length': '150', 'null': 'True'}),
+            'url': ('django.db.models.fields.URLField', [], {'max_length': '511', 'blank': 'True'}),
+            'usde_id': ('django.db.models.fields.BigIntegerField', [], {'null': 'True', 'blank': 'True'})
+        }
+    }
+
+    complete_apps = ['courses']
\ No newline at end of file
index d18e7d65a8ee86572568f560727ea28d3e6a7a59..f13ddb35fe4bb56839d080d8d406f0f9ae69ecdd 100644 (file)
@@ -56,18 +56,37 @@ class School(models.Model):
         self.save()
 
 
+class Department(models.Model):
+    """ Department within a School. """
+    name        = models.CharField(max_length=255)
+    school      = models.ForeignKey(School) # Should this be optional ever?
+    slug        = models.SlugField(max_length=150, null=True)
+    url         = models.URLField(max_length=511, blank=True, null=True)
+
+    def __unicode__(self):
+        return self.name
+
+    def save(self, *args, **kwargs):
+        """ Save department and generate a slug if one doesn't exist """
+        if not self.slug:
+            self.slug = defaultfilters.slugify(self.name)
+        super(Department, self).save(*args, **kwargs)
+
+
 class Course(models.Model):
     """ First class object that contains many notes.Note objects """
     # Core metadata
     name        = models.CharField(max_length=255)
     slug        = models.SlugField(max_length=150, null=True)
-    school      = models.ForeignKey(School) # Should this be optional ever?
+    # department should remove nullable when school gets yoinked
+    department  = models.ForeignKey(Department, blank=True, null=True)
+    # school is an appendix: the kind that gets swollen and should be removed
+    # (vistigial)
+    school      = models.ForeignKey(School) 
     file_count  = models.IntegerField(default=0)
 
     desc        = models.TextField(max_length=511, blank=True, null=True)
     url         = models.URLField(max_length=511, blank=True, null=True)
-    academic_year   = models.IntegerField(blank=True, null=True, 
-                        default=datetime.datetime.now().year)
 
     instructor_name     = models.CharField(max_length=255, blank=True, null=True)
     instructor_email    = models.EmailField(blank=True, null=True)
index 3113c727585e0071af00f8ceda4bc3320c57d139..89f30a4edf4d8e00387377504a8a7488df75a4ad 100644 (file)
@@ -10,6 +10,7 @@ from apps.notes.models import Note
 from apps.notes.gdrive import convert_raw_document
 from apps.courses.models import Course
 from apps.courses.models import School
+from apps.courses.models import Department
 from apps.licenses.models import License
 from apps.document_upload.models import RawDocument
 from django.core.management.base import BaseCommand
@@ -63,22 +64,33 @@ class Command(BaseCommand):
             with open(filename, 'r') as jsondata:
                 # parse JSON into python
                 parsed = json.load(jsondata)
+
+                # find the department or create one.
+                dept_info = {
+                    'name': parsed['subject'],
+                    'school': dbschool,
+                    'url': parsed['departmentLink'],
+                }
+                dbdept = Department.objects.get_or_create(**dept_info)[0]
+
                 # process courses
                 for course in parsed['courses']:
-
                     # Extract the course info
                     course_info = {
                       'name': course['courseTitle'],
                       'instructor_name': course['professor'],
                       'school': dbschool,
-                      # courseLink is "course-number-name-semester-year"
-                      'academic_year': \
-                        int(course['courseLink'].split('-')[-1])
                     }
                     # Create or Find the Course object.
                     dbcourse = Course.objects.get_or_create(**course_info)[0]
+                    dbcourse.department = dbdept;
+                    dbcourse.save()
                     print "Course is in the database: {0}".format(dbcourse.name)
 
+                    if 'noteLinks' not in course:
+                        print "No Notes in course."
+                        continue
+
                     # process notes for each course
                     for note in course['noteLinks']:
                         # Check to see if the Note is already uploaded.
index 47226557581a839a2fb3b76e7a621afc2931daaa..c5a1443000b7b988029a3a0a337372b56744f3fe 100644 (file)
       <div class="row">
         <div id="course_meta" class="twelve columns">
           <div class="activity_details_context">
-            {{ course.instructor_name }} // {{ course.school.name }}
+            {{ course.instructor_name }} {% if course.department %}// {{ course.department.name }}{% endif %}
+          </div><!-- /activity_details_context -->
+        </div><!-- /course_meta -->
+      </div>
+
+      <div class="row">
+        <div id="course_meta" class="twelve columns">
+          <div class="activity_details_context">
+            {{ course.school.name }}
           </div><!-- /activity_details_context -->
         </div><!-- /course_meta -->
       </div>
index 5668ebb892ff415aaa619f0b939790dbec4aa51b..36d2ed4eeab63cb80b913c6596ddcbf9e33ff239 100644 (file)
@@ -4,7 +4,7 @@
   <td class="note-count small-4 columns large-2 large-offset-2 slim">{{ course.file_count }}</td>
 
   <td class="course-name small-12 large-5 columns">
-    <a class="" href="{{ course.get_absolute_url }}">{{ course.name }}</a> 
+    <a class="" href="{{ course.get_absolute_url }}">{% if course.department %}{{course.department.name}}: {% endif %}{{ course.name }}</a> 
   </td>
   <td class="upload hide-for-small large-1 column">
     <div class="upload">