From b6f336c6a13b2a41b2c29b35884277aea9daca50 Mon Sep 17 00:00:00 2001 From: Bryan Date: Tue, 7 Jan 2014 15:51:20 -0500 Subject: [PATCH] Removing School and Department from Course model to prevent circular dependencies between Course, Department, and Professor. --- karmaworld/apps/courses/admin.py | 2 - karmaworld/apps/courses/models.py | 60 +--------------- karmaworld/apps/courses/views.py | 2 +- karmaworld/apps/document_upload/tests.py | 3 +- .../management/commands/import_ocw_json.py | 4 +- karmaworld/apps/notes/tests.py | 2 +- karmaworld/apps/schools/__init__.py | 0 karmaworld/apps/schools/admin.py | 12 ++++ .../apps/schools/management/__init__.py | 1 + .../schools/management/commands/__init__.py | 0 .../management/commands/fetch_usde_csv.py | 2 +- .../management/commands/import_usde_csv.py | 2 +- .../commands/sanitize_usde_schools.py | 2 +- karmaworld/apps/schools/models.py | 70 +++++++++++++++++++ 14 files changed, 95 insertions(+), 67 deletions(-) create mode 100644 karmaworld/apps/schools/__init__.py create mode 100644 karmaworld/apps/schools/admin.py create mode 100644 karmaworld/apps/schools/management/__init__.py create mode 100644 karmaworld/apps/schools/management/commands/__init__.py rename karmaworld/apps/{courses => schools}/management/commands/fetch_usde_csv.py (97%) rename karmaworld/apps/{courses => schools}/management/commands/import_usde_csv.py (97%) rename karmaworld/apps/{courses => schools}/management/commands/sanitize_usde_schools.py (98%) create mode 100644 karmaworld/apps/schools/models.py diff --git a/karmaworld/apps/courses/admin.py b/karmaworld/apps/courses/admin.py index 6984901..b0eb89f 100644 --- a/karmaworld/apps/courses/admin.py +++ b/karmaworld/apps/courses/admin.py @@ -5,7 +5,6 @@ from django.contrib import admin -from karmaworld.apps.courses.models import School from karmaworld.apps.courses.models import Course class CourseAdmin(admin.ModelAdmin): @@ -15,5 +14,4 @@ class CourseAdmin(admin.ModelAdmin): 'fk': ['school'] } -admin.site.register(School) admin.site.register(Course, CourseAdmin) diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index 1d162b6..56fb2d8 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -14,63 +14,9 @@ from django.db import models from django.template import defaultfilters from karmaworld.settings.manual_unique_together import auto_add_check_unique_together - -class School(models.Model): - """ A grouping that contains many courses """ - name = models.CharField(max_length=255) - slug = models.SlugField(max_length=150, null=True) - location = models.CharField(max_length=255, blank=True, null=True) - url = models.URLField(max_length=511, blank=True) - # Facebook keeps a unique identifier for all schools - facebook_id = models.BigIntegerField(blank=True, null=True) - # United States Department of Education institution_id - usde_id = models.BigIntegerField(blank=True, null=True) - file_count = models.IntegerField(default=0) - priority = models.BooleanField(default=0) - alias = models.CharField(max_length=255, null=True, blank=True) - hashtag = models.CharField(max_length=16, null=True, blank=True, unique=True, help_text='School abbreviation without #') - - class Meta: - """ Sort School by file_count descending, name abc=> """ - ordering = ['-file_count','-priority', 'name'] - - - def __unicode__(self): - return self.name - - def save(self, *args, **kwargs): - """ Save school and generate a slug if one doesn't exist """ - if not self.slug: - self.slug = defaultfilters.slugify(self.name) - super(School, self).save(*args, **kwargs) - - @staticmethod - def autocomplete_search_fields(): - return ("name__icontains",) - - def update_note_count(self): - """ Update the School.file_count by summing the - contained course.file_count - """ - self.file_count = sum([course.file_count for course in self.course_set.all()]) - 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) +from karmaworld.apps.schools.models import School +from karmaworld.apps.schools.models import Department +from karmaworld.apps.professors.models import Professor class Course(models.Model): diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index 657d756..2023dec 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -17,7 +17,7 @@ from django.views.generic.list import ListView from karmaworld.apps.courses.forms import CourseForm from karmaworld.apps.courses.models import Course -from karmaworld.apps.courses.models import School +from karmaworld.apps.schools.models import School from karmaworld.apps.notes.models import Note diff --git a/karmaworld/apps/document_upload/tests.py b/karmaworld/apps/document_upload/tests.py index 29a6128..2097f59 100644 --- a/karmaworld/apps/document_upload/tests.py +++ b/karmaworld/apps/document_upload/tests.py @@ -6,7 +6,8 @@ Replace this with more appropriate tests for your application. """ from django.test import TestCase, Client -from karmaworld.apps.courses.models import School, Course +from karmaworld.apps.courses.models import Course +from karmaworld.apps.schools.models import School from karmaworld.apps.document_upload.forms import RawDocumentForm from karmaworld.apps.notes.gdrive import * from karmaworld.apps.notes.models import Note diff --git a/karmaworld/apps/notes/management/commands/import_ocw_json.py b/karmaworld/apps/notes/management/commands/import_ocw_json.py index 89f30a4..660be7f 100644 --- a/karmaworld/apps/notes/management/commands/import_ocw_json.py +++ b/karmaworld/apps/notes/management/commands/import_ocw_json.py @@ -9,8 +9,8 @@ import requests 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.schools.models import School +from apps.schools.models import Department from apps.licenses.models import License from apps.document_upload.models import RawDocument from django.core.management.base import BaseCommand diff --git a/karmaworld/apps/notes/tests.py b/karmaworld/apps/notes/tests.py index 8e6646d..8949fb6 100644 --- a/karmaworld/apps/notes/tests.py +++ b/karmaworld/apps/notes/tests.py @@ -17,7 +17,7 @@ from karmaworld.apps.notes.search import SearchIndex from karmaworld.apps.notes.models import Note from karmaworld.apps.courses.models import Course -from karmaworld.apps.courses.models import School +from karmaworld.apps.schools.models import School import indextank.client as itc class TestNoes(TestCase): diff --git a/karmaworld/apps/schools/__init__.py b/karmaworld/apps/schools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/karmaworld/apps/schools/admin.py b/karmaworld/apps/schools/admin.py new file mode 100644 index 0000000..70271b5 --- /dev/null +++ b/karmaworld/apps/schools/admin.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +# Copyright (C) 2012 FinalsClub Foundation +""" Administration configuration for notes """ + +from django.contrib import admin + +from karmaworld.apps.schools.models import School +from karmaworld.apps.schools.models import Department + +admin.site.register(School) +admin.site.register(Department) diff --git a/karmaworld/apps/schools/management/__init__.py b/karmaworld/apps/schools/management/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/karmaworld/apps/schools/management/__init__.py @@ -0,0 +1 @@ + diff --git a/karmaworld/apps/schools/management/commands/__init__.py b/karmaworld/apps/schools/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/karmaworld/apps/courses/management/commands/fetch_usde_csv.py b/karmaworld/apps/schools/management/commands/fetch_usde_csv.py similarity index 97% rename from karmaworld/apps/courses/management/commands/fetch_usde_csv.py rename to karmaworld/apps/schools/management/commands/fetch_usde_csv.py index 57f6415..6d627f8 100644 --- a/karmaworld/apps/courses/management/commands/fetch_usde_csv.py +++ b/karmaworld/apps/schools/management/commands/fetch_usde_csv.py @@ -7,7 +7,7 @@ from urlparse import urljoin from subprocess import call from django.core.management.base import BaseCommand -from karmaworld.apps.courses.models import School +from karmaworld.apps.schools.models import School class Command(BaseCommand): args = '' diff --git a/karmaworld/apps/courses/management/commands/import_usde_csv.py b/karmaworld/apps/schools/management/commands/import_usde_csv.py similarity index 97% rename from karmaworld/apps/courses/management/commands/import_usde_csv.py rename to karmaworld/apps/schools/management/commands/import_usde_csv.py index 8422624..87f398c 100644 --- a/karmaworld/apps/courses/management/commands/import_usde_csv.py +++ b/karmaworld/apps/schools/management/commands/import_usde_csv.py @@ -2,7 +2,7 @@ import csv from itertools import izip from django.core.management.base import BaseCommand -from karmaworld.apps.courses.models import School +from karmaworld.apps.schools.models import School class Command(BaseCommand): diff --git a/karmaworld/apps/courses/management/commands/sanitize_usde_schools.py b/karmaworld/apps/schools/management/commands/sanitize_usde_schools.py similarity index 98% rename from karmaworld/apps/courses/management/commands/sanitize_usde_schools.py rename to karmaworld/apps/schools/management/commands/sanitize_usde_schools.py index 119e420..8639832 100644 --- a/karmaworld/apps/courses/management/commands/sanitize_usde_schools.py +++ b/karmaworld/apps/schools/management/commands/sanitize_usde_schools.py @@ -8,7 +8,7 @@ from django.core.management.base import BaseCommand from django.db.models import Q -from karmaworld.apps.courses.models import School +from karmaworld.apps.schools.models import School RESTRICTED_WORDS = [ 'internship', diff --git a/karmaworld/apps/schools/models.py b/karmaworld/apps/schools/models.py new file mode 100644 index 0000000..c7b13af --- /dev/null +++ b/karmaworld/apps/schools/models.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +# Copyright (C) 2012 FinalsClub Foundation + +""" + Models for schools. + Handles schools and departments. +""" +import datetime + +from django.db import models +from django.template import defaultfilters + + +class School(models.Model): + """ A grouping that contains many courses """ + name = models.CharField(max_length=255) + slug = models.SlugField(max_length=150, null=True) + location = models.CharField(max_length=255, blank=True, null=True) + url = models.URLField(max_length=511, blank=True) + # Facebook keeps a unique identifier for all schools + facebook_id = models.BigIntegerField(blank=True, null=True) + # United States Department of Education institution_id + usde_id = models.BigIntegerField(blank=True, null=True) + file_count = models.IntegerField(default=0) + priority = models.BooleanField(default=0) + alias = models.CharField(max_length=255, null=True, blank=True) + hashtag = models.CharField(max_length=16, null=True, blank=True, unique=True, help_text='School abbreviation without #') + + class Meta: + """ Sort School by file_count descending, name abc=> """ + ordering = ['-file_count','-priority', 'name'] + + + def __unicode__(self): + return self.name + + def save(self, *args, **kwargs): + """ Save school and generate a slug if one doesn't exist """ + if not self.slug: + self.slug = defaultfilters.slugify(self.name) + super(School, self).save(*args, **kwargs) + + @staticmethod + def autocomplete_search_fields(): + return ("name__icontains",) + + def update_note_count(self): + """ Update the School.file_count by summing the + contained course.file_count + """ + self.file_count = sum([course.file_count for course in self.course_set.all()]) + 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) -- 2.25.1