Skip to content

Commit

Permalink
CollaborationPolicyRecord - instantiate policy rules from the policy_…
Browse files Browse the repository at this point in the history
…record

* Remove AppConfig, CollaborationPolicy registration/caching messiness
* Data migration ensures all valid policies are in the CollaborationPolicyRecord
* Instantiate the CollaborationPolicy directly from CollaborationPolicyRecord
(note: removes a layer of caching. possible speed hit)
  • Loading branch information
sdreher committed Oct 16, 2015
1 parent 7b667e9 commit 0214372
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 91 deletions.
14 changes: 0 additions & 14 deletions mediathread/main/fixtures/sample_assignment.json
Expand Up @@ -5,20 +5,6 @@
"fields": {
"body": "<p>The Columbia Center for New Media Teaching and Learning (CCNMTL) was founded at Columbia University in 1999 to enhance teaching and learning through the purposeful use of new media and technology. In partnership with faculty, the Center supports efforts ranging from basic course website management to advanced project development.&#160;<span class=\"caps\">CCNMTL&#160;</span>is committed to remaining a leader in the field of new media teaching and learning, engaging faculty, educators, librarians, partner institutions, and the community in the reinvention of education for the digital age.</p>\r\n<p>We are committed to ongoing evaluation of the efficacy of our work within the University.</p>\r\n<p>&#160;</p>\r\n<p>The Armory - Home to CCNMTL's CUMC Office</p>\r\n<p><a class=\"materialCitation asset-image asset-whole\" href=\"/asset/3/annotations/7/\">Left Corner</a></p>\r\n<p>&#160;</p>\r\n<p>Some of our awards. Maurice &amp; Frank</p>\r\n<p><a class=\"materialCitation asset-image asset-whole\" href=\"/asset/2/annotations/5/\">Our esteemed leaders</a></p>", "project_type": "assignment", "author": 2, "title": "Sample Assignment", "modified": "2012-05-26 15:11:25", "date_submitted": "2012-05-26 15:11:25", "course": 1, "participants": [2]}
},
{
"pk": 1,
"model": "structuredcollaboration.collaborationpolicyrecord",
"fields": {
"policy_name": "PrivateEditorsAreOwners"
}
},
{
"pk": 2,
"model": "structuredcollaboration.collaborationpolicyrecord",
"fields": {
"policy_name": "CourseProtected"
}
},
{
"pk": 2,
"model": "structuredcollaboration.collaboration",
Expand Down
@@ -1 +1 @@
[{"pk": 1, "model": "projects.project", "fields": {"body": "<p>The Columbia Center for New Media Teaching and Learning (CCNMTL) was founded at Columbia University in 1999 to enhance teaching and learning through the purposeful use of new media and technology. In partnership with faculty, the Center supports efforts ranging from basic course website management to advanced project development.&#160;<span class=\"caps\">CCNMTL&#160;</span>is committed to remaining a leader in the field of new media teaching and learning, engaging faculty, educators, librarians, partner institutions, and the community in the reinvention of education for the digital age.</p>\r\n<p>We are committed to ongoing evaluation of the efficacy of our work within the University.</p>\r\n<p>&#160;</p>\r\n<p>The Armory - Home to CCNMTL's CUMC Office</p>\r\n<p><a class=\"materialCitation asset-image asset-whole\" href=\"/asset/3/annotations/7/\">Left Corner</a></p>\r\n<p>&#160;</p>\r\n<p>Some of our awards. Maurice &amp; Frank</p>\r\n<p><a class=\"materialCitation asset-image asset-whole\" href=\"/asset/2/annotations/5/\">Our esteemed leaders</a></p>", "project_type": "assignment", "author": 2, "title": "Sample Assignment", "modified": "2012-05-26 15:11:25", "date_submitted": "2012-05-26 15:11:25", "course": 1, "participants": [2]}}, {"pk": 2, "model": "projects.project", "fields": {"body": "<p>The Columbia Center for New Teaching and Learning was (CCNMTL)</p>\r\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; was founded at Columbia University in 1999 to enhance teaching and</p>\r\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; learning through the purposeful use of new media and technology</p>", "author": 3, "title": "Sample Assignment Response", "modified": "2012-05-26 15:17:06", "date_submitted": "2012-05-26 15:11:25", "course": 1, "participants": [3]}}, {"pk": 1, "model": "structuredcollaboration.collaborationpolicyrecord", "fields": {"policy_name": "PrivateEditorsAreOwners"}}, {"pk": 2, "model": "structuredcollaboration.collaborationpolicyrecord", "fields": {"policy_name": "CourseProtected"}}, {"pk": 3, "model": "structuredcollaboration.collaborationpolicyrecord", "fields": {"policy_name": "InstructorShared"}}, {"pk": 2, "model": "structuredcollaboration.collaboration", "fields": {"group": null, "context": 1, "title": "Sample Assignment", "policy_record": 2, "object_pk": "1", "_parent": null, "user": 2, "content_type": 21, "slug": null}}, {"pk": 3, "model": "structuredcollaboration.collaboration", "fields": {"group": null, "context": 1, "title": "Sample Assignment Response", "policy_record": 3, "object_pk": "2", "_parent": 2, "user": 3, "content_type": 21, "slug": null}}, {"pk": 1, "model": "structuredcollaboration.collaboration", "fields": {"group": 2, "context": null, "title": "Sample Course", "policy_record": null, "object_pk": "1", "_parent": null, "user": null, "content_type": 8, "slug": "Sample_Course"}}]
[{"pk": 1, "model": "projects.project", "fields": {"body": "<p>The Columbia Center for New Media Teaching and Learning (CCNMTL) was founded at Columbia University in 1999 to enhance teaching and learning through the purposeful use of new media and technology. In partnership with faculty, the Center supports efforts ranging from basic course website management to advanced project development.&#160;<span class=\"caps\">CCNMTL&#160;</span>is committed to remaining a leader in the field of new media teaching and learning, engaging faculty, educators, librarians, partner institutions, and the community in the reinvention of education for the digital age.</p>\r\n<p>We are committed to ongoing evaluation of the efficacy of our work within the University.</p>\r\n<p>&#160;</p>\r\n<p>The Armory - Home to CCNMTL's CUMC Office</p>\r\n<p><a class=\"materialCitation asset-image asset-whole\" href=\"/asset/3/annotations/7/\">Left Corner</a></p>\r\n<p>&#160;</p>\r\n<p>Some of our awards. Maurice &amp; Frank</p>\r\n<p><a class=\"materialCitation asset-image asset-whole\" href=\"/asset/2/annotations/5/\">Our esteemed leaders</a></p>", "project_type": "assignment", "author": 2, "title": "Sample Assignment", "modified": "2012-05-26 15:11:25", "date_submitted": "2012-05-26 15:11:25", "course": 1, "participants": [2]}}, {"pk": 2, "model": "projects.project", "fields": {"body": "<p>The Columbia Center for New Teaching and Learning was (CCNMTL)</p>\r\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; was founded at Columbia University in 1999 to enhance teaching and</p>\r\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; learning through the purposeful use of new media and technology</p>", "author": 3, "title": "Sample Assignment Response", "modified": "2012-05-26 15:17:06", "date_submitted": "2012-05-26 15:11:25", "course": 1, "participants": [3]}}, {"pk": 2, "model": "structuredcollaboration.collaboration", "fields": {"group": null, "context": 1, "title": "Sample Assignment", "policy_record": 2, "object_pk": "1", "_parent": null, "user": 2, "content_type": 21, "slug": null}}, {"pk": 3, "model": "structuredcollaboration.collaboration", "fields": {"group": null, "context": 1, "title": "Sample Assignment Response", "policy_record": 3, "object_pk": "2", "_parent": 2, "user": 3, "content_type": 21, "slug": null}}, {"pk": 1, "model": "structuredcollaboration.collaboration", "fields": {"group": 2, "context": null, "title": "Sample Course", "policy_record": null, "object_pk": "1", "_parent": null, "user": null, "content_type": 8, "slug": "Sample_Course"}}]
Binary file modified scripts/lettuce_base.db
Binary file not shown.
1 change: 0 additions & 1 deletion structuredcollaboration/__init__.py
@@ -1 +0,0 @@
default_app_config = 'structuredcollaboration.apps.CollaborationPolicyConfig'
48 changes: 0 additions & 48 deletions structuredcollaboration/apps.py

This file was deleted.

56 changes: 56 additions & 0 deletions structuredcollaboration/migrations/0004_auto_20151016_1401.py
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations


def get_or_create_policies(apps, schema_editor):
CollaborationPolicyRecord = apps.get_model('structuredcollaboration',
'CollaborationPolicyRecord')

valid_policies = [
'PrivateEditorsAreOwners',
'PublicEditorsAreOwners',
'PrivateStudentAndFaculty',
'CourseProtected',
'InstructorShared',
'InstructorManaged',
'CourseCollaboration'
]

for p in valid_policies:
CollaborationPolicyRecord.objects.get_or_create(policy_name=p)


def deprecate_policies(apps, schema_editor):
invalid_policies = [
'Assignment',
'PublicAssignment'
]

CollaborationPolicyRecord = apps.get_model('structuredcollaboration',
'CollaborationPolicyRecord')
Collaboration = apps.get_model('structuredcollaboration',
'Collaboration')

course_protected = CollaborationPolicyRecord.objects.get(
policy_name='CourseProtected')

for p in invalid_policies:
policies = Collaboration.objects.filter(policy_record__policy_name=p)
policies.update(policy_record=course_protected)

CollaborationPolicyRecord.objects.filter(
policy_name__in=invalid_policies).delete()


class Migration(migrations.Migration):

dependencies = [
('structuredcollaboration', '0003_auto_20150509_1914'),
]

operations = [
migrations.RunPython(get_or_create_policies),
migrations.RunPython(deprecate_policies)
]
46 changes: 19 additions & 27 deletions structuredcollaboration/models.py
@@ -1,3 +1,5 @@
import importlib

from django.conf import settings
from django.contrib.auth.models import User, Group
from django.contrib.contenttypes import generic
Expand All @@ -7,27 +9,7 @@
from django.utils.translation import ugettext_lazy as _


DEFAULT_POLICY = getattr(settings, 'DEFAULT_COLLABORATION_POLICY',
'PublicEditorsAreOwners')


class CollaborationPolicyRecordManager(models.Manager):
'''
@todo - consider pulling this whole registration approach.
feels overcomplicated & unnecessary. The primary aim here
seems to be caching instances of the policies.
'''
registered_policies = dict()

def policy_instance(self, record):
return self.registered_policies[record.policy_name]

def register_policy(self, policy_class, policy_key, policy_title):
self.registered_policies[policy_key] = policy_class()


class CollaborationPolicyRecord(models.Model):
objects = CollaborationPolicyRecordManager()
policy_name = models.CharField(max_length=512)

def __unicode__(self):
Expand All @@ -36,6 +18,17 @@ def __unicode__(self):
def __eq__(self, other):
return self.policy_name is other or self is other

@classmethod
def class_for_name(cls, class_name):
# load the module, will raise ImportError if module cannot be loaded
m = importlib.import_module('structuredcollaboration.policies')
# get the class, will raise AttributeError if class cannot be found
c = getattr(m, class_name)
return c

def policy_instance(self):
return self.class_for_name(self.policy_name)()


class CollaborationManager(models.Manager):
def get_for_object_list(self, object_list):
Expand Down Expand Up @@ -142,13 +135,12 @@ def append_child(self, obj=None):
return coll

def get_policy(self):
if self.policy_record:
return CollaborationPolicyRecord.objects.policy_instance(
self.policy_record)
else:
record, created = CollaborationPolicyRecord.objects.get_or_create(
policy_name=DEFAULT_POLICY)
return CollaborationPolicyRecord.objects.policy_instance(record)
if not self.policy_record:
policy_name = getattr(settings, 'DEFAULT_COLLABORATION_POLICY',
'PublicEditorsAreOwners')
self.set_policy(policy_name)
self.save()
return self.policy_record.policy_instance()

def set_policy(self, policy_name):
if policy_name is None:
Expand Down

0 comments on commit 0214372

Please sign in to comment.