Commit 58b81472 by Calen Pennington

Cleaning up pep8 violations

parent 9f237d2e
...@@ -19,10 +19,12 @@ def user(email): ...@@ -19,10 +19,12 @@ def user(email):
'''look up a user by email''' '''look up a user by email'''
return User.objects.get(email=email) return User.objects.get(email=email)
def registration(email): def registration(email):
'''look up registration object by email''' '''look up registration object by email'''
return Registration.objects.get(user__email=email) return Registration.objects.get(user__email=email)
class AuthTestCase(TestCase): class AuthTestCase(TestCase):
"""Check that various permissions-related things work""" """Check that various permissions-related things work"""
...@@ -36,7 +38,7 @@ class AuthTestCase(TestCase): ...@@ -36,7 +38,7 @@ class AuthTestCase(TestCase):
resp = self.client.get(url) resp = self.client.get(url)
self.assertEqual(resp.status_code, expected) self.assertEqual(resp.status_code, expected)
return resp return resp
def test_public_pages_load(self): def test_public_pages_load(self):
"""Make sure pages that don't require login load without error.""" """Make sure pages that don't require login load without error."""
pages = ( pages = (
...@@ -60,11 +62,11 @@ class AuthTestCase(TestCase): ...@@ -60,11 +62,11 @@ class AuthTestCase(TestCase):
'username': username, 'username': username,
'email': email, 'email': email,
'password': pw, 'password': pw,
'location' : 'home', 'location': 'home',
'language' : 'Franglish', 'language': 'Franglish',
'name' : 'Fred Weasley', 'name': 'Fred Weasley',
'terms_of_service' : 'true', 'terms_of_service': 'true',
'honor_code' : 'true', 'honor_code': 'true',
}) })
return resp return resp
...@@ -99,7 +101,6 @@ class AuthTestCase(TestCase): ...@@ -99,7 +101,6 @@ class AuthTestCase(TestCase):
self.create_account(self.username, self.email, self.pw) self.create_account(self.username, self.email, self.pw)
self.activate_user(self.email) self.activate_user(self.email)
def _login(self, email, pw): def _login(self, email, pw):
'''Login. View should always return 200. The success/fail is in the '''Login. View should always return 200. The success/fail is in the
returned json''' returned json'''
...@@ -108,7 +109,6 @@ class AuthTestCase(TestCase): ...@@ -108,7 +109,6 @@ class AuthTestCase(TestCase):
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
return resp return resp
def login(self, email, pw): def login(self, email, pw):
'''Login, check that it worked.''' '''Login, check that it worked.'''
resp = self._login(self.email, self.pw) resp = self._login(self.email, self.pw)
...@@ -162,7 +162,6 @@ class AuthTestCase(TestCase): ...@@ -162,7 +162,6 @@ class AuthTestCase(TestCase):
for page in simple_auth_pages: for page in simple_auth_pages:
print "Checking '{0}'".format(page) print "Checking '{0}'".format(page)
self.check_page_get(page, expected=200) self.check_page_get(page, expected=200)
def test_index_auth(self): def test_index_auth(self):
......
...@@ -58,7 +58,7 @@ def export_to_github(course, commit_message, author_str=None): ...@@ -58,7 +58,7 @@ def export_to_github(course, commit_message, author_str=None):
git_repo.git.commit(m=commit_message, author=author_str) git_repo.git.commit(m=commit_message, author=author_str)
else: else:
git_repo.git.commit(m=commit_message) git_repo.git.commit(m=commit_message)
origin = git_repo.remotes.origin origin = git_repo.remotes.origin
if settings.MITX_FEATURES['GITHUB_PUSH']: if settings.MITX_FEATURES['GITHUB_PUSH']:
push_infos = origin.push() push_infos = origin.push()
......
...@@ -50,4 +50,3 @@ class PostReceiveTestCase(TestCase): ...@@ -50,4 +50,3 @@ class PostReceiveTestCase(TestCase):
import_from_github.assert_called_with(settings.REPOS['repo']) import_from_github.assert_called_with(settings.REPOS['repo'])
mock_revision, mock_course = import_from_github.return_value mock_revision, mock_course = import_from_github.return_value
export_to_github.assert_called_with(mock_course, 'path', "Changes from cms import of revision %s" % mock_revision) export_to_github.assert_called_with(mock_course, 'path', "Changes from cms import of revision %s" % mock_revision)
...@@ -13,6 +13,7 @@ from django.db import DEFAULT_DB_ALIAS ...@@ -13,6 +13,7 @@ from django.db import DEFAULT_DB_ALIAS
from . import app_settings from . import app_settings
def get_instance(model, instance_or_pk, timeout=None, using=None): def get_instance(model, instance_or_pk, timeout=None, using=None):
""" """
Returns the ``model`` instance with a primary key of ``instance_or_pk``. Returns the ``model`` instance with a primary key of ``instance_or_pk``.
...@@ -87,6 +88,7 @@ def get_instance(model, instance_or_pk, timeout=None, using=None): ...@@ -87,6 +88,7 @@ def get_instance(model, instance_or_pk, timeout=None, using=None):
return instance return instance
def delete_instance(model, *instance_or_pk): def delete_instance(model, *instance_or_pk):
""" """
Purges the cache keys for the instances of this model. Purges the cache keys for the instances of this model.
...@@ -94,6 +96,7 @@ def delete_instance(model, *instance_or_pk): ...@@ -94,6 +96,7 @@ def delete_instance(model, *instance_or_pk):
cache.delete_many([instance_key(model, x) for x in instance_or_pk]) cache.delete_many([instance_key(model, x) for x in instance_or_pk])
def instance_key(model, instance_or_pk): def instance_key(model, instance_or_pk):
""" """
Returns the cache key for this (model, instance) pair. Returns the cache key for this (model, instance) pair.
......
...@@ -84,6 +84,7 @@ from django.contrib.auth.middleware import AuthenticationMiddleware ...@@ -84,6 +84,7 @@ from django.contrib.auth.middleware import AuthenticationMiddleware
from .model import cache_model from .model import cache_model
class CacheBackedAuthenticationMiddleware(AuthenticationMiddleware): class CacheBackedAuthenticationMiddleware(AuthenticationMiddleware):
def __init__(self): def __init__(self):
cache_model(User) cache_model(User)
......
...@@ -58,6 +58,7 @@ from django.db.models.signals import post_save, post_delete ...@@ -58,6 +58,7 @@ from django.db.models.signals import post_save, post_delete
from .core import get_instance, delete_instance from .core import get_instance, delete_instance
def cache_model(model, timeout=None): def cache_model(model, timeout=None):
if hasattr(model, 'get_cached'): if hasattr(model, 'get_cached'):
# Already patched # Already patched
......
...@@ -74,6 +74,7 @@ from django.db.models.signals import post_save, post_delete ...@@ -74,6 +74,7 @@ from django.db.models.signals import post_save, post_delete
from .core import get_instance, delete_instance from .core import get_instance, delete_instance
def cache_relation(descriptor, timeout=None): def cache_relation(descriptor, timeout=None):
rel = descriptor.related rel = descriptor.related
related_name = '%s_cache' % rel.field.related_query_name() related_name = '%s_cache' % rel.field.related_query_name()
......
...@@ -5,6 +5,7 @@ from django.template import resolve_variable ...@@ -5,6 +5,7 @@ from django.template import resolve_variable
register = template.Library() register = template.Library()
class CacheNode(Node): class CacheNode(Node):
def __init__(self, nodelist, expire_time, key): def __init__(self, nodelist, expire_time, key):
self.nodelist = nodelist self.nodelist = nodelist
...@@ -21,6 +22,7 @@ class CacheNode(Node): ...@@ -21,6 +22,7 @@ class CacheNode(Node):
cache.set(key, value, expire_time) cache.set(key, value, expire_time)
return value return value
@register.tag @register.tag
def cachedeterministic(parser, token): def cachedeterministic(parser, token):
""" """
...@@ -42,6 +44,7 @@ def cachedeterministic(parser, token): ...@@ -42,6 +44,7 @@ def cachedeterministic(parser, token):
raise TemplateSyntaxError(u"'%r' tag requires 2 arguments." % tokens[0]) raise TemplateSyntaxError(u"'%r' tag requires 2 arguments." % tokens[0])
return CacheNode(nodelist, tokens[1], tokens[2]) return CacheNode(nodelist, tokens[1], tokens[2])
class ShowIfCachedNode(Node): class ShowIfCachedNode(Node):
def __init__(self, key): def __init__(self, key):
self.key = key self.key = key
...@@ -50,6 +53,7 @@ class ShowIfCachedNode(Node): ...@@ -50,6 +53,7 @@ class ShowIfCachedNode(Node):
key = resolve_variable(self.key, context) key = resolve_variable(self.key, context)
return cache.get(key) or '' return cache.get(key) or ''
@register.tag @register.tag
def showifcached(parser, token): def showifcached(parser, token):
""" """
......
...@@ -60,6 +60,7 @@ def csrf_response_exempt(view_func): ...@@ -60,6 +60,7 @@ def csrf_response_exempt(view_func):
PendingDeprecationWarning) PendingDeprecationWarning)
return view_func return view_func
def csrf_view_exempt(view_func): def csrf_view_exempt(view_func):
""" """
Marks a view function as being exempt from CSRF view protection. Marks a view function as being exempt from CSRF view protection.
...@@ -68,6 +69,7 @@ def csrf_view_exempt(view_func): ...@@ -68,6 +69,7 @@ def csrf_view_exempt(view_func):
PendingDeprecationWarning) PendingDeprecationWarning)
return csrf_exempt(view_func) return csrf_exempt(view_func)
def csrf_exempt(view_func): def csrf_exempt(view_func):
""" """
Marks a view function as being exempt from the CSRF view protection. Marks a view function as being exempt from the CSRF view protection.
......
...@@ -6,6 +6,7 @@ from pipeline.conf import settings ...@@ -6,6 +6,7 @@ from pipeline.conf import settings
from pipeline.packager import Packager from pipeline.packager import Packager
from pipeline.utils import guess_type from pipeline.utils import guess_type
def compressed_css(package_name): def compressed_css(package_name):
package = settings.PIPELINE_CSS.get(package_name, {}) package = settings.PIPELINE_CSS.get(package_name, {})
if package: if package:
...@@ -20,6 +21,7 @@ def compressed_css(package_name): ...@@ -20,6 +21,7 @@ def compressed_css(package_name):
paths = packager.compile(package.paths) paths = packager.compile(package.paths)
return render_individual_css(package, paths) return render_individual_css(package, paths)
def render_css(package, path): def render_css(package, path):
template_name = package.template_name or "mako/css.html" template_name = package.template_name or "mako/css.html"
context = package.extra_context context = package.extra_context
...@@ -29,6 +31,7 @@ def render_css(package, path): ...@@ -29,6 +31,7 @@ def render_css(package, path):
}) })
return render_to_string(template_name, context) return render_to_string(template_name, context)
def render_individual_css(package, paths): def render_individual_css(package, paths):
tags = [render_css(package, path) for path in paths] tags = [render_css(package, path) for path in paths]
return '\n'.join(tags) return '\n'.join(tags)
...@@ -49,6 +52,7 @@ def compressed_js(package_name): ...@@ -49,6 +52,7 @@ def compressed_js(package_name):
templates = packager.pack_templates(package) templates = packager.pack_templates(package)
return render_individual_js(package, paths, templates) return render_individual_js(package, paths, templates)
def render_js(package, path): def render_js(package, path):
template_name = package.template_name or "mako/js.html" template_name = package.template_name or "mako/js.html"
context = package.extra_context context = package.extra_context
...@@ -58,6 +62,7 @@ def render_js(package, path): ...@@ -58,6 +62,7 @@ def render_js(package, path):
}) })
return render_to_string(template_name, context) return render_to_string(template_name, context)
def render_inline_js(package, js): def render_inline_js(package, js):
context = package.extra_context context = package.extra_context
context.update({ context.update({
...@@ -65,6 +70,7 @@ def render_inline_js(package, js): ...@@ -65,6 +70,7 @@ def render_inline_js(package, js):
}) })
return render_to_string("mako/inline_js.html", context) return render_to_string("mako/inline_js.html", context)
def render_individual_js(package, paths, templates=None): def render_individual_js(package, paths, templates=None):
tags = [render_js(package, js) for js in paths] tags = [render_js(package, js) for js in paths]
if templates: if templates:
......
...@@ -15,4 +15,3 @@ admin.site.register(CourseEnrollment) ...@@ -15,4 +15,3 @@ admin.site.register(CourseEnrollment)
admin.site.register(Registration) admin.site.register(Registration)
admin.site.register(PendingNameChange) admin.site.register(PendingNameChange)
...@@ -25,27 +25,29 @@ import mitxmako.middleware as middleware ...@@ -25,27 +25,29 @@ import mitxmako.middleware as middleware
middleware.MakoMiddleware() middleware.MakoMiddleware()
class Command(BaseCommand): class Command(BaseCommand):
help = \ help = \
'''Exports all users and user profiles. '''Exports all users and user profiles.
Caveat: Should be looked over before any run Caveat: Should be looked over before any run
for schema changes. for schema changes.
Current version grabs user_keys from Current version grabs user_keys from
django.contrib.auth.models.User and up_keys django.contrib.auth.models.User and up_keys
from student.userprofile. ''' from student.userprofile. '''
def handle(self, *args, **options): def handle(self, *args, **options):
users = list(User.objects.all()) users = list(User.objects.all())
user_profiles = list(UserProfile.objects.all()) user_profiles = list(UserProfile.objects.all())
user_profile_dict = dict([(up.user_id, up) for up in user_profiles]) user_profile_dict = dict([(up.user_id, up) for up in user_profiles])
user_tuples = [(user_profile_dict[u.id], u) for u in users if u.id in user_profile_dict] user_tuples = [(user_profile_dict[u.id], u) for u in users if u.id in user_profile_dict]
user_keys = ['id', 'username', 'email', 'password', 'is_staff', user_keys = ['id', 'username', 'email', 'password', 'is_staff',
'is_active', 'is_superuser', 'last_login', 'date_joined', 'is_active', 'is_superuser', 'last_login', 'date_joined',
'password'] 'password']
up_keys = ['language', 'location','meta','name', 'id','user_id'] up_keys = ['language', 'location', 'meta', 'name', 'id', 'user_id']
def extract_dict(keys, object): def extract_dict(keys, object):
d = {} d = {}
for key in keys: for key in keys:
......
...@@ -22,6 +22,7 @@ import mitxmako.middleware as middleware ...@@ -22,6 +22,7 @@ import mitxmako.middleware as middleware
middleware.MakoMiddleware() middleware.MakoMiddleware()
def import_user(u): def import_user(u):
user_info = u['u'] user_info = u['u']
up_info = u['up'] up_info = u['up']
...@@ -30,11 +31,10 @@ def import_user(u): ...@@ -30,11 +31,10 @@ def import_user(u):
user_info['last_login'] = dateutil.parser.parse(user_info['last_login']) user_info['last_login'] = dateutil.parser.parse(user_info['last_login'])
user_info['date_joined'] = dateutil.parser.parse(user_info['date_joined']) user_info['date_joined'] = dateutil.parser.parse(user_info['date_joined'])
user_keys = ['id', 'username', 'email', 'password', 'is_staff', user_keys = ['id', 'username', 'email', 'password', 'is_staff',
'is_active', 'is_superuser', 'last_login', 'date_joined', 'is_active', 'is_superuser', 'last_login', 'date_joined',
'password'] 'password']
up_keys = ['language', 'location','meta','name', 'id','user_id'] up_keys = ['language', 'location', 'meta', 'name', 'id', 'user_id']
u = User() u = User()
for key in user_keys: for key in user_keys:
...@@ -47,20 +47,22 @@ def import_user(u): ...@@ -47,20 +47,22 @@ def import_user(u):
up.__setattr__(key, up_info[key]) up.__setattr__(key, up_info[key])
up.save() up.save()
class Command(BaseCommand): class Command(BaseCommand):
help = \ help = \
'''Exports all users and user profiles. '''Exports all users and user profiles.
Caveat: Should be looked over before any run Caveat: Should be looked over before any run
for schema changes. for schema changes.
Current version grabs user_keys from Current version grabs user_keys from
django.contrib.auth.models.User and up_keys django.contrib.auth.models.User and up_keys
from student.userprofile. ''' from student.userprofile. '''
def handle(self, *args, **options): def handle(self, *args, **options):
extracted = json.load(open('transfer_users.txt')) extracted = json.load(open('transfer_users.txt'))
n=0 n = 0
for u in extracted: for u in extracted:
import_user(u) import_user(u)
if n%100 == 0: if n % 100 == 0:
print n print n
n = n+1 n = n + 1
...@@ -17,29 +17,32 @@ import json ...@@ -17,29 +17,32 @@ import json
middleware.MakoMiddleware() middleware.MakoMiddleware()
def group_from_value(groups, v): def group_from_value(groups, v):
''' Given group: (('a',0.3),('b',0.4),('c',0.3)) And random value ''' Given group: (('a',0.3),('b',0.4),('c',0.3)) And random value
in [0,1], return the associated group (in the above case, return in [0,1], return the associated group (in the above case, return
'a' if v<0.3, 'b' if 0.3<=v<0.7, and 'c' if v>0.7 'a' if v<0.3, 'b' if 0.3<=v<0.7, and 'c' if v>0.7
''' '''
sum = 0 sum = 0
for (g,p) in groups: for (g, p) in groups:
sum = sum + p sum = sum + p
if sum > v: if sum > v:
return g return g
return g # For round-off errors return g # For round-off errors
class Command(BaseCommand): class Command(BaseCommand):
help = \ help = \
''' Assign users to test groups. Takes a list ''' Assign users to test groups. Takes a list
of groups: of groups:
a:0.3,b:0.4,c:0.3 file.txt "Testing something" a:0.3,b:0.4,c:0.3 file.txt "Testing something"
Will assign each user to group a, b, or c with Will assign each user to group a, b, or c with
probability 0.3, 0.4, 0.3. Probabilities must probability 0.3, 0.4, 0.3. Probabilities must
add up to 1. add up to 1.
Will log what happened to file.txt. Will log what happened to file.txt.
''' '''
def handle(self, *args, **options): def handle(self, *args, **options):
if len(args) != 3: if len(args) != 3:
print "Invalid number of options" print "Invalid number of options"
...@@ -47,13 +50,13 @@ Will log what happened to file.txt. ...@@ -47,13 +50,13 @@ Will log what happened to file.txt.
# Extract groups from string # Extract groups from string
group_strs = [x.split(':') for x in args[0].split(',')] group_strs = [x.split(':') for x in args[0].split(',')]
groups = [(group,float(value)) for group,value in group_strs] groups = [(group, float(value)) for group, value in group_strs]
print "Groups", groups print "Groups", groups
## Confirm group probabilities add up to 1 ## Confirm group probabilities add up to 1
total = sum(zip(*groups)[1]) total = sum(zip(*groups)[1])
print "Total:", total print "Total:", total
if abs(total-1)>0.01: if abs(total - 1) > 0.01:
print "Total not 1" print "Total not 1"
sys.exit(-1) sys.exit(-1)
...@@ -65,15 +68,15 @@ Will log what happened to file.txt. ...@@ -65,15 +68,15 @@ Will log what happened to file.txt.
group_objects = {} group_objects = {}
f = open(args[1],"a+") f = open(args[1], "a+")
## Create groups ## Create groups
for group in dict(groups): for group in dict(groups):
utg = UserTestGroup() utg = UserTestGroup()
utg.name=group utg.name = group
utg.description = json.dumps({"description":args[2]}, utg.description = json.dumps({"description": args[2]},
{"time":datetime.datetime.utcnow().isoformat()}) {"time": datetime.datetime.utcnow().isoformat()})
group_objects[group]=utg group_objects[group] = utg
group_objects[group].save() group_objects[group].save()
## Assign groups ## Assign groups
...@@ -83,11 +86,11 @@ Will log what happened to file.txt. ...@@ -83,11 +86,11 @@ Will log what happened to file.txt.
if count % 1000 == 0: if count % 1000 == 0:
print count print count
count = count + 1 count = count + 1
v = random.uniform(0,1) v = random.uniform(0, 1)
group = group_from_value(groups,v) group = group_from_value(groups, v)
group_objects[group].users.add(user) group_objects[group].users.add(user)
f.write("Assigned user {name} ({id}) to {group}\n".format(name=user.username, f.write("Assigned user {name} ({id}) to {group}\n".format(name=user.username,
id=user.id, id=user.id,
group=group)) group=group))
## Save groups ## Save groups
......
...@@ -10,9 +10,11 @@ import mitxmako.middleware as middleware ...@@ -10,9 +10,11 @@ import mitxmako.middleware as middleware
middleware.MakoMiddleware() middleware.MakoMiddleware()
class Command(BaseCommand): class Command(BaseCommand):
help = \ help = \
''' Extract an e-mail list of all active students. ''' ''' Extract an e-mail list of all active students. '''
def handle(self, *args, **options): def handle(self, *args, **options):
#text = open(args[0]).read() #text = open(args[0]).read()
#subject = open(args[1]).read() #subject = open(args[1]).read()
......
...@@ -10,18 +10,20 @@ import mitxmako.middleware as middleware ...@@ -10,18 +10,20 @@ import mitxmako.middleware as middleware
middleware.MakoMiddleware() middleware.MakoMiddleware()
class Command(BaseCommand): class Command(BaseCommand):
help = \ help = \
'''Sends an e-mail to all users. Takes a single '''Sends an e-mail to all users. Takes a single
parameter -- name of e-mail template -- located parameter -- name of e-mail template -- located
in templates/email. Adds a .txt for the message in templates/email. Adds a .txt for the message
body, and an _subject.txt for the subject. ''' body, and an _subject.txt for the subject. '''
def handle(self, *args, **options): def handle(self, *args, **options):
#text = open(args[0]).read() #text = open(args[0]).read()
#subject = open(args[1]).read() #subject = open(args[1]).read()
users = User.objects.all() users = User.objects.all()
text = middleware.lookup['main'].get_template('email/'+args[0]+".txt").render() text = middleware.lookup['main'].get_template('email/' + args[0] + ".txt").render()
subject = middleware.lookup['main'].get_template('email/'+args[0]+"_subject.txt").render().strip() subject = middleware.lookup['main'].get_template('email/' + args[0] + "_subject.txt").render().strip()
for user in users: for user in users:
if user.is_active: if user.is_active:
user.email_user(subject, text) user.email_user(subject, text)
...@@ -16,16 +16,18 @@ import datetime ...@@ -16,16 +16,18 @@ import datetime
middleware.MakoMiddleware() middleware.MakoMiddleware()
def chunks(l, n): def chunks(l, n):
""" Yield successive n-sized chunks from l. """ Yield successive n-sized chunks from l.
""" """
for i in xrange(0, len(l), n): for i in xrange(0, len(l), n):
yield l[i:i+n] yield l[i:i + n]
class Command(BaseCommand): class Command(BaseCommand):
help = \ help = \
'''Sends an e-mail to all users in a text file. '''Sends an e-mail to all users in a text file.
E.g. E.g.
manage.py userlist.txt message logfile.txt rate manage.py userlist.txt message logfile.txt rate
userlist.txt -- list of all users userlist.txt -- list of all users
message -- prefix for template with message message -- prefix for template with message
...@@ -35,28 +37,28 @@ rate -- messages per second ...@@ -35,28 +37,28 @@ rate -- messages per second
log_file = None log_file = None
def hard_log(self, text): def hard_log(self, text):
self.log_file.write(datetime.datetime.utcnow().isoformat()+' -- '+text+'\n') self.log_file.write(datetime.datetime.utcnow().isoformat() + ' -- ' + text + '\n')
def handle(self, *args, **options): def handle(self, *args, **options):
global log_file global log_file
(user_file, message_base, logfilename, ratestr) = args (user_file, message_base, logfilename, ratestr) = args
users = [u.strip() for u in open(user_file).readlines()] users = [u.strip() for u in open(user_file).readlines()]
message = middleware.lookup['main'].get_template('emails/'+message_base+"_body.txt").render() message = middleware.lookup['main'].get_template('emails/' + message_base + "_body.txt").render()
subject = middleware.lookup['main'].get_template('emails/'+message_base+"_subject.txt").render().strip() subject = middleware.lookup['main'].get_template('emails/' + message_base + "_subject.txt").render().strip()
rate = int(ratestr) rate = int(ratestr)
self.log_file = open(logfilename, "a+", buffering = 0)
i=0 self.log_file = open(logfilename, "a+", buffering=0)
i = 0
for users in chunks(users, rate): for users in chunks(users, rate):
emails = [ (subject, message, settings.DEFAULT_FROM_EMAIL, [u]) for u in users ] emails = [(subject, message, settings.DEFAULT_FROM_EMAIL, [u]) for u in users]
self.hard_log(" ".join(users)) self.hard_log(" ".join(users))
send_mass_mail( emails, fail_silently = False ) send_mass_mail(emails, fail_silently=False)
time.sleep(1) time.sleep(1)
print datetime.datetime.utcnow().isoformat(), i print datetime.datetime.utcnow().isoformat(), i
i = i+len(users) i = i + len(users)
# Emergency interruptor # Emergency interruptor
if os.path.exists("/tmp/stopemails.txt"): if os.path.exists("/tmp/stopemails.txt"):
self.log_file.close() self.log_file.close()
......
...@@ -13,26 +13,28 @@ from student.models import UserProfile ...@@ -13,26 +13,28 @@ from student.models import UserProfile
middleware.MakoMiddleware() middleware.MakoMiddleware()
class Command(BaseCommand): class Command(BaseCommand):
help = \ help = \
''' Extract full user information into a JSON file. ''' Extract full user information into a JSON file.
Pass a single filename.''' Pass a single filename.'''
def handle(self, *args, **options): def handle(self, *args, **options):
f = open(args[0],'w') f = open(args[0], 'w')
#text = open(args[0]).read() #text = open(args[0]).read()
#subject = open(args[1]).read() #subject = open(args[1]).read()
users = User.objects.all() users = User.objects.all()
l = [] l = []
for user in users: for user in users:
up = UserProfile.objects.get(user = user) up = UserProfile.objects.get(user=user)
d = { 'username':user.username, d = {'username': user.username,
'email':user.email, 'email': user.email,
'is_active':user.is_active, 'is_active': user.is_active,
'joined':user.date_joined.isoformat(), 'joined': user.date_joined.isoformat(),
'name':up.name, 'name': up.name,
'language':up.language, 'language': up.language,
'location':up.location} 'location': up.location}
l.append(d) l.append(d)
json.dump(l,f) json.dump(l, f)
f.close() f.close()
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding model 'UserProfile' # Adding model 'UserProfile'
db.create_table('auth_userprofile', ( db.create_table('auth_userprofile', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
...@@ -28,16 +29,14 @@ class Migration(SchemaMigration): ...@@ -28,16 +29,14 @@ class Migration(SchemaMigration):
)) ))
db.send_create_signal('student', ['Registration']) db.send_create_signal('student', ['Registration'])
def backwards(self, orm): def backwards(self, orm):
# Deleting model 'UserProfile' # Deleting model 'UserProfile'
db.delete_table('auth_userprofile') db.delete_table('auth_userprofile')
# Deleting model 'Registration' # Deleting model 'Registration'
db.delete_table('auth_registration') db.delete_table('auth_registration')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Changing field 'UserProfile.name' # Changing field 'UserProfile.name'
db.alter_column('auth_userprofile', 'name', self.gf('django.db.models.fields.CharField')(max_length=255)) db.alter_column('auth_userprofile', 'name', self.gf('django.db.models.fields.CharField')(max_length=255))
...@@ -32,9 +33,8 @@ class Migration(SchemaMigration): ...@@ -32,9 +33,8 @@ class Migration(SchemaMigration):
# Adding index on 'UserProfile', fields ['location'] # Adding index on 'UserProfile', fields ['location']
db.create_index('auth_userprofile', ['location']) db.create_index('auth_userprofile', ['location'])
def backwards(self, orm): def backwards(self, orm):
# Removing index on 'UserProfile', fields ['location'] # Removing index on 'UserProfile', fields ['location']
db.delete_index('auth_userprofile', ['location']) db.delete_index('auth_userprofile', ['location'])
...@@ -59,7 +59,6 @@ class Migration(SchemaMigration): ...@@ -59,7 +59,6 @@ class Migration(SchemaMigration):
# Changing field 'UserProfile.location' # Changing field 'UserProfile.location'
db.alter_column('auth_userprofile', 'location', self.gf('django.db.models.fields.TextField')()) db.alter_column('auth_userprofile', 'location', self.gf('django.db.models.fields.TextField')())
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding model 'UserTestGroup' # Adding model 'UserTestGroup'
db.create_table('student_usertestgroup', ( db.create_table('student_usertestgroup', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
...@@ -24,16 +25,14 @@ class Migration(SchemaMigration): ...@@ -24,16 +25,14 @@ class Migration(SchemaMigration):
)) ))
db.create_unique('student_usertestgroup_users', ['usertestgroup_id', 'user_id']) db.create_unique('student_usertestgroup_users', ['usertestgroup_id', 'user_id'])
def backwards(self, orm): def backwards(self, orm):
# Deleting model 'UserTestGroup' # Deleting model 'UserTestGroup'
db.delete_table('student_usertestgroup') db.delete_table('student_usertestgroup')
# Removing M2M table for field users on 'UserTestGroup' # Removing M2M table for field users on 'UserTestGroup'
db.delete_table('student_usertestgroup_users') db.delete_table('student_usertestgroup_users')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,18 +4,17 @@ from south.db import db ...@@ -4,18 +4,17 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
db.execute("create unique index email on auth_user (email)") db.execute("create unique index email on auth_user (email)")
pass pass
def backwards(self, orm): def backwards(self, orm):
db.execute("drop index email on auth_user") db.execute("drop index email on auth_user")
pass pass
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding model 'PendingEmailChange' # Adding model 'PendingEmailChange'
db.create_table('student_pendingemailchange', ( db.create_table('student_pendingemailchange', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
...@@ -29,9 +30,8 @@ class Migration(SchemaMigration): ...@@ -29,9 +30,8 @@ class Migration(SchemaMigration):
# Changing field 'UserProfile.user' # Changing field 'UserProfile.user'
db.alter_column('auth_userprofile', 'user_id', self.gf('django.db.models.fields.related.OneToOneField')(unique=True, to=orm['auth.User'])) db.alter_column('auth_userprofile', 'user_id', self.gf('django.db.models.fields.related.OneToOneField')(unique=True, to=orm['auth.User']))
def backwards(self, orm): def backwards(self, orm):
# Deleting model 'PendingEmailChange' # Deleting model 'PendingEmailChange'
db.delete_table('student_pendingemailchange') db.delete_table('student_pendingemailchange')
...@@ -41,7 +41,6 @@ class Migration(SchemaMigration): ...@@ -41,7 +41,6 @@ class Migration(SchemaMigration):
# Changing field 'UserProfile.user' # Changing field 'UserProfile.user'
db.alter_column('auth_userprofile', 'user_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], unique=True)) db.alter_column('auth_userprofile', 'user_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], unique=True))
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,20 +4,19 @@ from south.db import db ...@@ -4,20 +4,19 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Changing field 'UserProfile.meta' # Changing field 'UserProfile.meta'
db.alter_column('auth_userprofile', 'meta', self.gf('django.db.models.fields.TextField')()) db.alter_column('auth_userprofile', 'meta', self.gf('django.db.models.fields.TextField')())
def backwards(self, orm): def backwards(self, orm):
# Changing field 'UserProfile.meta' # Changing field 'UserProfile.meta'
db.alter_column('auth_userprofile', 'meta', self.gf('django.db.models.fields.CharField')(max_length=255)) db.alter_column('auth_userprofile', 'meta', self.gf('django.db.models.fields.CharField')(max_length=255))
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,6 +4,7 @@ from south.db import db ...@@ -4,6 +4,7 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
...@@ -16,12 +17,10 @@ class Migration(SchemaMigration): ...@@ -16,12 +17,10 @@ class Migration(SchemaMigration):
ALTER TABLE student_usertestgroup_users CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE student_usertestgroup_users CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
""") """)
def backwards(self, orm): def backwards(self, orm):
# Although this migration can't be undone, it is okay for it to be run backwards because it doesn't add/remove any fields # Although this migration can't be undone, it is okay for it to be run backwards because it doesn't add/remove any fields
pass pass
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -16,12 +16,10 @@ class Migration(SchemaMigration): ...@@ -16,12 +16,10 @@ class Migration(SchemaMigration):
)) ))
db.send_create_signal('student', ['CourseRegistration']) db.send_create_signal('student', ['CourseRegistration'])
def backwards(self, orm): def backwards(self, orm):
# Deleting model 'CourseRegistration' # Deleting model 'CourseRegistration'
db.delete_table('student_courseregistration') db.delete_table('student_courseregistration')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -129,4 +127,4 @@ class Migration(SchemaMigration): ...@@ -129,4 +127,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -19,7 +19,6 @@ class Migration(SchemaMigration): ...@@ -19,7 +19,6 @@ class Migration(SchemaMigration):
)) ))
db.send_create_signal('student', ['CourseEnrollment']) db.send_create_signal('student', ['CourseEnrollment'])
def backwards(self, orm): def backwards(self, orm):
# Adding model 'CourseRegistration' # Adding model 'CourseRegistration'
db.create_table('student_courseregistration', ( db.create_table('student_courseregistration', (
...@@ -32,7 +31,6 @@ class Migration(SchemaMigration): ...@@ -32,7 +31,6 @@ class Migration(SchemaMigration):
# Deleting model 'CourseEnrollment' # Deleting model 'CourseEnrollment'
db.delete_table('student_courseenrollment') db.delete_table('student_courseenrollment')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -140,4 +138,4 @@ class Migration(SchemaMigration): ...@@ -140,4 +138,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -124,4 +124,4 @@ class Migration(SchemaMigration): ...@@ -124,4 +124,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -13,8 +13,8 @@ class Migration(SchemaMigration): ...@@ -13,8 +13,8 @@ class Migration(SchemaMigration):
pass pass
# # Removing unique constraint on 'CourseEnrollment', fields ['user'] # # Removing unique constraint on 'CourseEnrollment', fields ['user']
# db.delete_unique('student_courseenrollment', ['user_id']) # db.delete_unique('student_courseenrollment', ['user_id'])
# #
# #
# # Changing field 'CourseEnrollment.user' # # Changing field 'CourseEnrollment.user'
# db.alter_column('student_courseenrollment', 'user_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])) # db.alter_column('student_courseenrollment', 'user_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User']))
...@@ -25,7 +25,6 @@ class Migration(SchemaMigration): ...@@ -25,7 +25,6 @@ class Migration(SchemaMigration):
# # Adding unique constraint on 'CourseEnrollment', fields ['user'] # # Adding unique constraint on 'CourseEnrollment', fields ['user']
# db.create_unique('student_courseenrollment', ['user_id']) # db.create_unique('student_courseenrollment', ['user_id'])
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -133,4 +132,4 @@ class Migration(SchemaMigration): ...@@ -133,4 +132,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -38,7 +38,6 @@ class Migration(SchemaMigration): ...@@ -38,7 +38,6 @@ class Migration(SchemaMigration):
self.gf('django.db.models.fields.CharField')(max_length=255, null=True, blank=True), self.gf('django.db.models.fields.CharField')(max_length=255, null=True, blank=True),
keep_default=False) keep_default=False)
def backwards(self, orm): def backwards(self, orm):
# Deleting field 'UserProfile.gender' # Deleting field 'UserProfile.gender'
db.delete_column('auth_userprofile', 'gender') db.delete_column('auth_userprofile', 'gender')
...@@ -58,7 +57,6 @@ class Migration(SchemaMigration): ...@@ -58,7 +57,6 @@ class Migration(SchemaMigration):
# Deleting field 'UserProfile.occupation' # Deleting field 'UserProfile.occupation'
db.delete_column('auth_userprofile', 'occupation') db.delete_column('auth_userprofile', 'occupation')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -172,4 +170,4 @@ class Migration(SchemaMigration): ...@@ -172,4 +170,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -130,4 +130,4 @@ class Migration(SchemaMigration): ...@@ -130,4 +130,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -11,7 +11,6 @@ class Migration(SchemaMigration): ...@@ -11,7 +11,6 @@ class Migration(SchemaMigration):
# Deleting model 'CourseEnrollment' # Deleting model 'CourseEnrollment'
db.delete_table('student_courseenrollment') db.delete_table('student_courseenrollment')
def backwards(self, orm): def backwards(self, orm):
# Adding model 'CourseEnrollment' # Adding model 'CourseEnrollment'
db.create_table('student_courseenrollment', ( db.create_table('student_courseenrollment', (
...@@ -21,7 +20,6 @@ class Migration(SchemaMigration): ...@@ -21,7 +20,6 @@ class Migration(SchemaMigration):
)) ))
db.send_create_signal('student', ['CourseEnrollment']) db.send_create_signal('student', ['CourseEnrollment'])
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -129,4 +127,4 @@ class Migration(SchemaMigration): ...@@ -129,4 +127,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -19,7 +19,6 @@ class Migration(SchemaMigration): ...@@ -19,7 +19,6 @@ class Migration(SchemaMigration):
# Adding unique constraint on 'CourseEnrollment', fields ['user', 'course_id'] # Adding unique constraint on 'CourseEnrollment', fields ['user', 'course_id']
db.create_unique('student_courseenrollment', ['user_id', 'course_id']) db.create_unique('student_courseenrollment', ['user_id', 'course_id'])
def backwards(self, orm): def backwards(self, orm):
# Removing unique constraint on 'CourseEnrollment', fields ['user', 'course_id'] # Removing unique constraint on 'CourseEnrollment', fields ['user', 'course_id']
db.delete_unique('student_courseenrollment', ['user_id', 'course_id']) db.delete_unique('student_courseenrollment', ['user_id', 'course_id'])
...@@ -27,7 +26,6 @@ class Migration(SchemaMigration): ...@@ -27,7 +26,6 @@ class Migration(SchemaMigration):
# Deleting model 'CourseEnrollment' # Deleting model 'CourseEnrollment'
db.delete_table('student_courseenrollment') db.delete_table('student_courseenrollment')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -141,4 +139,4 @@ class Migration(SchemaMigration): ...@@ -141,4 +139,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -13,7 +13,6 @@ class Migration(SchemaMigration): ...@@ -13,7 +13,6 @@ class Migration(SchemaMigration):
self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, null=True, blank=True), self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, null=True, blank=True),
keep_default=False) keep_default=False)
# Changing field 'UserProfile.country' # Changing field 'UserProfile.country'
db.alter_column('auth_userprofile', 'country', self.gf('django_countries.fields.CountryField')(max_length=2, null=True)) db.alter_column('auth_userprofile', 'country', self.gf('django_countries.fields.CountryField')(max_length=2, null=True))
...@@ -21,7 +20,6 @@ class Migration(SchemaMigration): ...@@ -21,7 +20,6 @@ class Migration(SchemaMigration):
# Deleting field 'CourseEnrollment.date' # Deleting field 'CourseEnrollment.date'
db.delete_column('student_courseenrollment', 'date') db.delete_column('student_courseenrollment', 'date')
# Changing field 'UserProfile.country' # Changing field 'UserProfile.country'
db.alter_column('auth_userprofile', 'country', self.gf('django.db.models.fields.CharField')(max_length=255, null=True)) db.alter_column('auth_userprofile', 'country', self.gf('django.db.models.fields.CharField')(max_length=255, null=True))
...@@ -139,4 +137,4 @@ class Migration(SchemaMigration): ...@@ -139,4 +137,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -15,7 +15,6 @@ class Migration(SchemaMigration): ...@@ -15,7 +15,6 @@ class Migration(SchemaMigration):
# Rename 'created' field to 'date' # Rename 'created' field to 'date'
db.rename_column('student_courseenrollment', 'created', 'date') db.rename_column('student_courseenrollment', 'created', 'date')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -130,4 +129,4 @@ class Migration(SchemaMigration): ...@@ -130,4 +129,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -11,12 +11,10 @@ class Migration(SchemaMigration): ...@@ -11,12 +11,10 @@ class Migration(SchemaMigration):
# Adding index on 'CourseEnrollment', fields ['created'] # Adding index on 'CourseEnrollment', fields ['created']
db.create_index('student_courseenrollment', ['created']) db.create_index('student_courseenrollment', ['created'])
def backwards(self, orm): def backwards(self, orm):
# Removing index on 'CourseEnrollment', fields ['created'] # Removing index on 'CourseEnrollment', fields ['created']
db.delete_index('student_courseenrollment', ['created']) db.delete_index('student_courseenrollment', ['created'])
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -131,4 +129,4 @@ class Migration(SchemaMigration): ...@@ -131,4 +129,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -38,7 +38,6 @@ class Migration(SchemaMigration): ...@@ -38,7 +38,6 @@ class Migration(SchemaMigration):
# Adding index on 'UserProfile', fields ['gender'] # Adding index on 'UserProfile', fields ['gender']
db.create_index('auth_userprofile', ['gender']) db.create_index('auth_userprofile', ['gender'])
def backwards(self, orm): def backwards(self, orm):
# Removing index on 'UserProfile', fields ['gender'] # Removing index on 'UserProfile', fields ['gender']
db.delete_index('auth_userprofile', ['gender']) db.delete_index('auth_userprofile', ['gender'])
...@@ -72,7 +71,6 @@ class Migration(SchemaMigration): ...@@ -72,7 +71,6 @@ class Migration(SchemaMigration):
# Deleting field 'UserProfile.goals' # Deleting field 'UserProfile.goals'
db.delete_column('auth_userprofile', 'goals') db.delete_column('auth_userprofile', 'goals')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
...@@ -186,4 +184,4 @@ class Migration(SchemaMigration): ...@@ -186,4 +184,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['student'] complete_apps = ['student']
\ No newline at end of file
...@@ -18,6 +18,7 @@ from django_countries import CountryField ...@@ -18,6 +18,7 @@ from django_countries import CountryField
#from cache_toolbox import cache_model, cache_relation #from cache_toolbox import cache_model, cache_relation
class UserProfile(models.Model): class UserProfile(models.Model):
class Meta: class Meta:
db_table = "auth_userprofile" db_table = "auth_userprofile"
...@@ -28,7 +29,7 @@ class UserProfile(models.Model): ...@@ -28,7 +29,7 @@ class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True, db_index=True, related_name='profile') user = models.OneToOneField(User, unique=True, db_index=True, related_name='profile')
name = models.CharField(blank=True, max_length=255, db_index=True) name = models.CharField(blank=True, max_length=255, db_index=True)
meta = models.TextField(blank=True) # JSON dictionary for future expansion meta = models.TextField(blank=True) # JSON dictionary for future expansion
courseware = models.CharField(blank=True, max_length=255, default='course.xml') courseware = models.CharField(blank=True, max_length=255, default='course.xml')
# Location is no longer used, but is held here for backwards compatibility # Location is no longer used, but is held here for backwards compatibility
...@@ -59,7 +60,6 @@ class UserProfile(models.Model): ...@@ -59,7 +60,6 @@ class UserProfile(models.Model):
mailing_address = models.TextField(blank=True, null=True) mailing_address = models.TextField(blank=True, null=True)
goals = models.TextField(blank=True, null=True) goals = models.TextField(blank=True, null=True)
def get_meta(self): def get_meta(self):
js_str = self.meta js_str = self.meta
if not js_str: if not js_str:
...@@ -69,9 +69,10 @@ class UserProfile(models.Model): ...@@ -69,9 +69,10 @@ class UserProfile(models.Model):
return js_str return js_str
def set_meta(self,js): def set_meta(self, js):
self.meta = json.dumps(js) self.meta = json.dumps(js)
## TODO: Should be renamed to generic UserGroup, and possibly ## TODO: Should be renamed to generic UserGroup, and possibly
# Given an optional field for type of group # Given an optional field for type of group
class UserTestGroup(models.Model): class UserTestGroup(models.Model):
...@@ -79,6 +80,7 @@ class UserTestGroup(models.Model): ...@@ -79,6 +80,7 @@ class UserTestGroup(models.Model):
name = models.CharField(blank=False, max_length=32, db_index=True) name = models.CharField(blank=False, max_length=32, db_index=True)
description = models.TextField(blank=True) description = models.TextField(blank=True)
class Registration(models.Model): class Registration(models.Model):
''' Allows us to wait for e-mail before user is registered. A ''' Allows us to wait for e-mail before user is registered. A
registration profile is created when the user creates an registration profile is created when the user creates an
...@@ -92,8 +94,8 @@ class Registration(models.Model): ...@@ -92,8 +94,8 @@ class Registration(models.Model):
def register(self, user): def register(self, user):
# MINOR TODO: Switch to crypto-secure key # MINOR TODO: Switch to crypto-secure key
self.activation_key=uuid.uuid4().hex self.activation_key = uuid.uuid4().hex
self.user=user self.user = user
self.save() self.save()
def activate(self): def activate(self):
...@@ -101,22 +103,25 @@ class Registration(models.Model): ...@@ -101,22 +103,25 @@ class Registration(models.Model):
self.user.save() self.user.save()
#self.delete() #self.delete()
class PendingNameChange(models.Model): class PendingNameChange(models.Model):
user = models.OneToOneField(User, unique=True, db_index=True) user = models.OneToOneField(User, unique=True, db_index=True)
new_name = models.CharField(blank=True, max_length=255) new_name = models.CharField(blank=True, max_length=255)
rationale = models.CharField(blank=True, max_length=1024) rationale = models.CharField(blank=True, max_length=1024)
class PendingEmailChange(models.Model): class PendingEmailChange(models.Model):
user = models.OneToOneField(User, unique=True, db_index=True) user = models.OneToOneField(User, unique=True, db_index=True)
new_email = models.CharField(blank=True, max_length=255, db_index=True) new_email = models.CharField(blank=True, max_length=255, db_index=True)
activation_key = models.CharField(('activation key'), max_length=32, unique=True, db_index=True) activation_key = models.CharField(('activation key'), max_length=32, unique=True, db_index=True)
class CourseEnrollment(models.Model): class CourseEnrollment(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
course_id = models.CharField(max_length=255, db_index=True) course_id = models.CharField(max_length=255, db_index=True)
created = models.DateTimeField(auto_now_add=True, null=True, db_index=True) created = models.DateTimeField(auto_now_add=True, null=True, db_index=True)
class Meta: class Meta:
unique_together = (('user', 'course_id'), ) unique_together = (('user', 'course_id'), )
...@@ -124,38 +129,45 @@ class CourseEnrollment(models.Model): ...@@ -124,38 +129,45 @@ class CourseEnrollment(models.Model):
#### Helper methods for use from python manage.py shell. #### Helper methods for use from python manage.py shell.
def get_user(email): def get_user(email):
u = User.objects.get(email = email) u = User.objects.get(email=email)
up = UserProfile.objects.get(user = u) up = UserProfile.objects.get(user=u)
return u,up return u, up
def user_info(email): def user_info(email):
u,up = get_user(email) u, up = get_user(email)
print "User id", u.id print "User id", u.id
print "Username", u.username print "Username", u.username
print "E-mail", u.email print "E-mail", u.email
print "Name", up.name print "Name", up.name
print "Location", up.location print "Location", up.location
print "Language", up.language print "Language", up.language
return u,up return u, up
def change_email(old_email, new_email): def change_email(old_email, new_email):
u = User.objects.get(email = old_email) u = User.objects.get(email=old_email)
u.email = new_email u.email = new_email
u.save() u.save()
def change_name(email, new_name): def change_name(email, new_name):
u,up = get_user(email) u, up = get_user(email)
up.name = new_name up.name = new_name
up.save() up.save()
def user_count(): def user_count():
print "All users", User.objects.all().count() print "All users", User.objects.all().count()
print "Active users", User.objects.filter(is_active = True).count() print "Active users", User.objects.filter(is_active=True).count()
return User.objects.all().count() return User.objects.all().count()
def active_user_count(): def active_user_count():
return User.objects.filter(is_active = True).count() return User.objects.filter(is_active=True).count()
def create_group(name, description): def create_group(name, description):
utg = UserTestGroup() utg = UserTestGroup()
...@@ -163,29 +175,31 @@ def create_group(name, description): ...@@ -163,29 +175,31 @@ def create_group(name, description):
utg.description = description utg.description = description
utg.save() utg.save()
def add_user_to_group(user, group): def add_user_to_group(user, group):
utg = UserTestGroup.objects.get(name = group) utg = UserTestGroup.objects.get(name=group)
utg.users.add(User.objects.get(username = user)) utg.users.add(User.objects.get(username=user))
utg.save() utg.save()
def remove_user_from_group(user, group): def remove_user_from_group(user, group):
utg = UserTestGroup.objects.get(name = group) utg = UserTestGroup.objects.get(name=group)
utg.users.remove(User.objects.get(username = user)) utg.users.remove(User.objects.get(username=user))
utg.save() utg.save()
default_groups = {'email_future_courses' : 'Receive e-mails about future MITx courses', default_groups = {'email_future_courses': 'Receive e-mails about future MITx courses',
'email_helpers' : 'Receive e-mails about how to help with MITx', 'email_helpers': 'Receive e-mails about how to help with MITx',
'mitx_unenroll' : 'Fully unenrolled -- no further communications', 'mitx_unenroll': 'Fully unenrolled -- no further communications',
'6002x_unenroll' : 'Took and dropped 6002x'} '6002x_unenroll': 'Took and dropped 6002x'}
def add_user_to_default_group(user, group): def add_user_to_default_group(user, group):
try: try:
utg = UserTestGroup.objects.get(name = group) utg = UserTestGroup.objects.get(name=group)
except UserTestGroup.DoesNotExist: except UserTestGroup.DoesNotExist:
utg = UserTestGroup() utg = UserTestGroup()
utg.name = group utg.name = group
utg.description = default_groups[group] utg.description = default_groups[group]
utg.save() utg.save()
utg.users.add(User.objects.get(username = user)) utg.users.add(User.objects.get(username=user))
utg.save() utg.save()
...@@ -4,6 +4,7 @@ from django.conf import settings ...@@ -4,6 +4,7 @@ from django.conf import settings
import views import views
class TrackMiddleware: class TrackMiddleware:
def process_request(self, request): def process_request(self, request):
try: try:
...@@ -11,34 +12,34 @@ class TrackMiddleware: ...@@ -11,34 +12,34 @@ class TrackMiddleware:
# names/passwords. # names/passwords.
if request.META['PATH_INFO'] in ['/event', '/login']: if request.META['PATH_INFO'] in ['/event', '/login']:
return return
# Removes passwords from the tracking logs # Removes passwords from the tracking logs
# WARNING: This list needs to be changed whenever we change # WARNING: This list needs to be changed whenever we change
# password handling functionality. # password handling functionality.
# #
# As of the time of this comment, only 'password' is used # As of the time of this comment, only 'password' is used
# The rest are there for future extension. # The rest are there for future extension.
# #
# Passwords should never be sent as GET requests, but # Passwords should never be sent as GET requests, but
# this can happen due to older browser bugs. We censor # this can happen due to older browser bugs. We censor
# this too. # this too.
# #
# We should manually confirm no passwords make it into log # We should manually confirm no passwords make it into log
# files when we change this. # files when we change this.
censored_strings = ['password', 'newpassword', 'new_password', censored_strings = ['password', 'newpassword', 'new_password',
'oldpassword', 'old_password'] 'oldpassword', 'old_password']
post_dict = dict(request.POST) post_dict = dict(request.POST)
get_dict = dict(request.GET) get_dict = dict(request.GET)
for string in censored_strings: for string in censored_strings:
if string in post_dict: if string in post_dict:
post_dict[string] = '*'*8 post_dict[string] = '*' * 8
if string in get_dict: if string in get_dict:
get_dict[string] = '*'*8 get_dict[string] = '*' * 8
event = { 'GET' : dict(get_dict), event = {'GET': dict(get_dict),
'POST' : dict(post_dict)} 'POST': dict(post_dict)}
# TODO: Confirm no large file uploads # TODO: Confirm no large file uploads
event = json.dumps(event) event = json.dumps(event)
event = event[:512] event = event[:512]
......
...@@ -10,61 +10,64 @@ from django.conf import settings ...@@ -10,61 +10,64 @@ from django.conf import settings
log = logging.getLogger("tracking") log = logging.getLogger("tracking")
def log_event(event): def log_event(event):
event_str = json.dumps(event) event_str = json.dumps(event)
log.info(event_str[:settings.TRACK_MAX_EVENT]) log.info(event_str[:settings.TRACK_MAX_EVENT])
def user_track(request): def user_track(request):
try: # TODO: Do the same for many of the optional META parameters try: # TODO: Do the same for many of the optional META parameters
username = request.user.username username = request.user.username
except: except:
username = "anonymous" username = "anonymous"
try: try:
scookie = request.META['HTTP_COOKIE'] # Get cookies scookie = request.META['HTTP_COOKIE'] # Get cookies
scookie = ";".join([c.split('=')[1] for c in scookie.split(";") if "sessionid" in c]).strip() # Extract session ID scookie = ";".join([c.split('=')[1] for c in scookie.split(";") if "sessionid" in c]).strip() # Extract session ID
except: except:
scookie = "" scookie = ""
try: try:
agent = request.META['HTTP_USER_AGENT'] agent = request.META['HTTP_USER_AGENT']
except: except:
agent = '' agent = ''
# TODO: Move a bunch of this into log_event # TODO: Move a bunch of this into log_event
event = { event = {
"username" : username, "username": username,
"session" : scookie, "session": scookie,
"ip" : request.META['REMOTE_ADDR'], "ip": request.META['REMOTE_ADDR'],
"event_source" : "browser", "event_source": "browser",
"event_type" : request.GET['event_type'], "event_type": request.GET['event_type'],
"event" : request.GET['event'], "event": request.GET['event'],
"agent" : agent, "agent": agent,
"page" : request.GET['page'], "page": request.GET['page'],
"time": datetime.datetime.utcnow().isoformat(), "time": datetime.datetime.utcnow().isoformat(),
} }
log_event(event) log_event(event)
return HttpResponse('success') return HttpResponse('success')
def server_track(request, event_type, event, page=None): def server_track(request, event_type, event, page=None):
try: try:
username = request.user.username username = request.user.username
except: except:
username = "anonymous" username = "anonymous"
try: try:
agent = request.META['HTTP_USER_AGENT'] agent = request.META['HTTP_USER_AGENT']
except: except:
agent = '' agent = ''
event = { event = {
"username" : username, "username": username,
"ip" : request.META['REMOTE_ADDR'], "ip": request.META['REMOTE_ADDR'],
"event_source" : "server", "event_source": "server",
"event_type" : event_type, "event_type": event_type,
"event" : event, "event": event,
"agent" : agent, "agent": agent,
"page" : page, "page": page,
"time": datetime.datetime.utcnow().isoformat(), "time": datetime.datetime.utcnow().isoformat(),
} }
log_event(event) log_event(event)
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
This module aims to give a little more fine-tuned control of caching and cache This module aims to give a little more fine-tuned control of caching and cache
invalidation. Import these instead of django.core.cache. invalidation. Import these instead of django.core.cache.
Note that 'default' is being preserved for user session caching, which we're Note that 'default' is being preserved for user session caching, which we're
not migrating so as not to inconvenience users by logging them all out. not migrating so as not to inconvenience users by logging them all out.
""" """
from functools import wraps from functools import wraps
...@@ -16,26 +16,27 @@ try: ...@@ -16,26 +16,27 @@ try:
except Exception: except Exception:
cache = cache.cache cache = cache.cache
def cache_if_anonymous(view_func): def cache_if_anonymous(view_func):
""" """
Many of the pages in edX are identical when the user is not logged Many of the pages in edX are identical when the user is not logged
in, but should not be cached when the user is logged in (because in, but should not be cached when the user is logged in (because
of the navigation bar at the top with the username). of the navigation bar at the top with the username).
The django middleware cache does not handle this correctly, because The django middleware cache does not handle this correctly, because
we access the session to put the csrf token in the header. This adds we access the session to put the csrf token in the header. This adds
the cookie to the vary header, and so every page is cached seperately the cookie to the vary header, and so every page is cached seperately
for each user (because each user has a different csrf token). for each user (because each user has a different csrf token).
Note that this decorator should only be used on views that do not Note that this decorator should only be used on views that do not
contain the csrftoken within the html. The csrf token can be included contain the csrftoken within the html. The csrf token can be included
in the header by ordering the decorators as such: in the header by ordering the decorators as such:
@ensure_csrftoken @ensure_csrftoken
@cache_if_anonymous @cache_if_anonymous
def myView(request): def myView(request):
""" """
@wraps(view_func) @wraps(view_func)
def _decorated(request, *args, **kwargs): def _decorated(request, *args, **kwargs):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
...@@ -45,12 +46,12 @@ def cache_if_anonymous(view_func): ...@@ -45,12 +46,12 @@ def cache_if_anonymous(view_func):
if not response: if not response:
response = view_func(request, *args, **kwargs) response = view_func(request, *args, **kwargs)
cache.set(cache_key, response, 60 * 3) cache.set(cache_key, response, 60 * 3)
return response return response
else: else:
#Don't use the cache #Don't use the cache
return view_func(request, *args, **kwargs) return view_func(request, *args, **kwargs)
return _decorated return _decorated
\ No newline at end of file
...@@ -2,6 +2,7 @@ from functools import wraps ...@@ -2,6 +2,7 @@ from functools import wraps
import copy import copy
import json import json
def expect_json(view_function): def expect_json(view_function):
@wraps(view_function) @wraps(view_function)
def expect_json_with_cloned_request(request, *args, **kwargs): def expect_json_with_cloned_request(request, *args, **kwargs):
......
...@@ -6,11 +6,13 @@ from django.utils.encoding import smart_str ...@@ -6,11 +6,13 @@ from django.utils.encoding import smart_str
import hashlib import hashlib
import urllib import urllib
def fasthash(string): def fasthash(string):
m = hashlib.new("md4") m = hashlib.new("md4")
m.update(string) m.update(string)
return m.hexdigest() return m.hexdigest()
def safe_key(key, key_prefix, version): def safe_key(key, key_prefix, version):
safe_key = urllib.quote_plus(smart_str(key)) safe_key = urllib.quote_plus(smart_str(key))
......
...@@ -5,12 +5,12 @@ from django.http import HttpResponseServerError ...@@ -5,12 +5,12 @@ from django.http import HttpResponseServerError
log = logging.getLogger("mitx") log = logging.getLogger("mitx")
class ExceptionLoggingMiddleware(object): class ExceptionLoggingMiddleware(object):
"""Just here to log unchecked exceptions that go all the way up the Django """Just here to log unchecked exceptions that go all the way up the Django
stack""" stack"""
if not settings.TEMPLATE_DEBUG: if not settings.TEMPLATE_DEBUG:
def process_exception(self, request, exception): def process_exception(self, request, exception):
log.exception(exception) log.exception(exception)
return HttpResponseServerError("Server Error - Please try again later.") return HttpResponseServerError("Server Error - Please try again later.")
...@@ -14,53 +14,57 @@ from mitxmako.shortcuts import render_to_response, render_to_string ...@@ -14,53 +14,57 @@ from mitxmako.shortcuts import render_to_response, render_to_string
import capa.calc import capa.calc
import track.views import track.views
def calculate(request): def calculate(request):
''' Calculator in footer of every page. ''' ''' Calculator in footer of every page. '''
equation = request.GET['equation'] equation = request.GET['equation']
try: try:
result = capa.calc.evaluator({}, {}, equation) result = capa.calc.evaluator({}, {}, equation)
except: except:
event = {'error':map(str,sys.exc_info()), event = {'error': map(str, sys.exc_info()),
'equation':equation} 'equation': equation}
track.views.server_track(request, 'error:calc', event, page='calc') track.views.server_track(request, 'error:calc', event, page='calc')
return HttpResponse(json.dumps({'result':'Invalid syntax'})) return HttpResponse(json.dumps({'result': 'Invalid syntax'}))
return HttpResponse(json.dumps({'result':str(result)})) return HttpResponse(json.dumps({'result': str(result)}))
def send_feedback(request): def send_feedback(request):
''' Feeback mechanism in footer of every page. ''' ''' Feeback mechanism in footer of every page. '''
try: try:
username = request.user.username username = request.user.username
email = request.user.email email = request.user.email
except: except:
username = "anonymous" username = "anonymous"
email = "anonymous" email = "anonymous"
try: try:
browser = request.META['HTTP_USER_AGENT'] browser = request.META['HTTP_USER_AGENT']
except: except:
browser = "Unknown" browser = "Unknown"
feedback = render_to_string("feedback_email.txt", feedback = render_to_string("feedback_email.txt",
{"subject":request.POST['subject'], {"subject": request.POST['subject'],
"url": request.POST['url'], "url": request.POST['url'],
"time": datetime.datetime.now().isoformat(), "time": datetime.datetime.now().isoformat(),
"feedback": request.POST['message'], "feedback": request.POST['message'],
"email":email, "email": email,
"browser":browser, "browser": browser,
"user":username}) "user": username})
send_mail("MITx Feedback / " +request.POST['subject'], send_mail("MITx Feedback / " + request.POST['subject'],
feedback, feedback,
settings.DEFAULT_FROM_EMAIL, settings.DEFAULT_FROM_EMAIL,
[ settings.DEFAULT_FEEDBACK_EMAIL ], [settings.DEFAULT_FEEDBACK_EMAIL],
fail_silently = False fail_silently=False
) )
return HttpResponse(json.dumps({'success':True})) return HttpResponse(json.dumps({'success': True}))
def info(request): def info(request):
''' Info page (link from main header) ''' ''' Info page (link from main header) '''
return render_to_response("info.html", {}) return render_to_response("info.html", {})
# From http://djangosnippets.org/snippets/1042/ # From http://djangosnippets.org/snippets/1042/
def parse_accept_header(accept): def parse_accept_header(accept):
"""Parse the Accept header *accept*, returning a list with pairs of """Parse the Accept header *accept*, returning a list with pairs of
...@@ -82,6 +86,7 @@ def parse_accept_header(accept): ...@@ -82,6 +86,7 @@ def parse_accept_header(accept):
result.sort(lambda x, y: -cmp(x[2], y[2])) result.sort(lambda x, y: -cmp(x[2], y[2]))
return result return result
def accepts(request, media_type): def accepts(request, media_type):
"""Return whether this request has an Accept header that matches type""" """Return whether this request has an Accept header that matches type"""
accept = parse_accept_header(request.META.get("HTTP_ACCEPT", "")) accept = parse_accept_header(request.META.get("HTTP_ACCEPT", ""))
......
...@@ -34,9 +34,10 @@ class DemoSystem(object): ...@@ -34,9 +34,10 @@ class DemoSystem(object):
context_dict.update(context) context_dict.update(context)
return self.lookup.get_template(template_filename).render(**context_dict) return self.lookup.get_template(template_filename).render(**context_dict)
def main(): def main():
parser = argparse.ArgumentParser(description='Check Problem Files') parser = argparse.ArgumentParser(description='Check Problem Files')
parser.add_argument("command", choices=['test', 'show']) # Watch? Render? Open? parser.add_argument("command", choices=['test', 'show']) # Watch? Render? Open?
parser.add_argument("files", nargs="+", type=argparse.FileType('r')) parser.add_argument("files", nargs="+", type=argparse.FileType('r'))
parser.add_argument("--seed", required=False, type=int) parser.add_argument("--seed", required=False, type=int)
parser.add_argument("--log-level", required=False, default="INFO", parser.add_argument("--log-level", required=False, default="INFO",
...@@ -67,13 +68,14 @@ def main(): ...@@ -67,13 +68,14 @@ def main():
# In case we want to do anything else here. # In case we want to do anything else here.
def command_show(problem): def command_show(problem):
"""Display the text for this problem""" """Display the text for this problem"""
print problem.get_html() print problem.get_html()
def command_test(problem): def command_test(problem):
# We're going to trap stdout/stderr from the problems (yes, some print) # We're going to trap stdout/stderr from the problems (yes, some print)
old_stdout, old_stderr = sys.stdout, sys.stderr old_stdout, old_stderr = sys.stdout, sys.stderr
try: try:
sys.stdout = StringIO() sys.stdout = StringIO()
...@@ -82,7 +84,7 @@ def command_test(problem): ...@@ -82,7 +84,7 @@ def command_test(problem):
check_that_suggested_answers_work(problem) check_that_suggested_answers_work(problem)
check_that_blanks_fail(problem) check_that_blanks_fail(problem)
log_captured_output(sys.stdout, log_captured_output(sys.stdout,
"captured stdout from {0}".format(problem)) "captured stdout from {0}".format(problem))
log_captured_output(sys.stderr, log_captured_output(sys.stderr,
"captured stderr from {0}".format(problem)) "captured stderr from {0}".format(problem))
...@@ -91,9 +93,10 @@ def command_test(problem): ...@@ -91,9 +93,10 @@ def command_test(problem):
finally: finally:
sys.stdout, sys.stderr = old_stdout, old_stderr sys.stdout, sys.stderr = old_stdout, old_stderr
def check_that_blanks_fail(problem): def check_that_blanks_fail(problem):
"""Leaving it blank should never work. Neither should a space.""" """Leaving it blank should never work. Neither should a space."""
blank_answers = dict((answer_id, u"") blank_answers = dict((answer_id, u"")
for answer_id in problem.get_question_answers()) for answer_id in problem.get_question_answers())
grading_results = problem.grade_answers(blank_answers) grading_results = problem.grade_answers(blank_answers)
try: try:
...@@ -113,7 +116,7 @@ def check_that_suggested_answers_work(problem): ...@@ -113,7 +116,7 @@ def check_that_suggested_answers_work(problem):
* Displayed answers use units but acceptable ones do not. * Displayed answers use units but acceptable ones do not.
- L1e0.xml - L1e0.xml
- Presents itself as UndefinedVariable (when it tries to pass to calc) - Presents itself as UndefinedVariable (when it tries to pass to calc)
* "a or d" is what's displayed, but only "a" or "d" is accepted, not the * "a or d" is what's displayed, but only "a" or "d" is accepted, not the
string "a or d". string "a or d".
- L1-e00.xml - L1-e00.xml
""" """
...@@ -129,14 +132,14 @@ def check_that_suggested_answers_work(problem): ...@@ -129,14 +132,14 @@ def check_that_suggested_answers_work(problem):
log.debug("Real answers: {0}".format(real_answers)) log.debug("Real answers: {0}".format(real_answers))
if real_answers: if real_answers:
try: try:
real_results = dict((answer_id, result) for answer_id, result real_results = dict((answer_id, result) for answer_id, result
in problem.grade_answers(all_answers).items() in problem.grade_answers(all_answers).items()
if answer_id in real_answers) if answer_id in real_answers)
log.debug(real_results) log.debug(real_results)
assert(all(result == 'correct' assert(all(result == 'correct'
for answer_id, result in real_results.items())) for answer_id, result in real_results.items()))
except UndefinedVariable as uv_exc: except UndefinedVariable as uv_exc:
log.error("The variable \"{0}\" specified in the ".format(uv_exc) + log.error("The variable \"{0}\" specified in the ".format(uv_exc) +
"solution isn't recognized (is it a units measure?).") "solution isn't recognized (is it a units measure?).")
except AssertionError: except AssertionError:
log.error("The following generated answers were not accepted for {0}:" log.error("The following generated answers were not accepted for {0}:"
...@@ -148,6 +151,7 @@ def check_that_suggested_answers_work(problem): ...@@ -148,6 +151,7 @@ def check_that_suggested_answers_work(problem):
log.error("Uncaught error in {0}".format(problem)) log.error("Uncaught error in {0}".format(problem))
log.exception(ex) log.exception(ex)
def log_captured_output(output_stream, stream_name): def log_captured_output(output_stream, stream_name):
output_stream.seek(0) output_stream.seek(0)
output_text = output_stream.read() output_text = output_stream.read()
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# #
# Used by responsetypes and capa_problem # Used by responsetypes and capa_problem
class CorrectMap(object): class CorrectMap(object):
''' '''
Stores map between answer_id and response evaluation result for each question Stores map between answer_id and response evaluation result for each question
...@@ -18,11 +19,11 @@ class CorrectMap(object): ...@@ -18,11 +19,11 @@ class CorrectMap(object):
Behaves as a dict. Behaves as a dict.
''' '''
def __init__(self,*args,**kwargs): def __init__(self, *args, **kwargs):
self.cmap = dict() # start with empty dict self.cmap = dict() # start with empty dict
self.items = self.cmap.items self.items = self.cmap.items
self.keys = self.cmap.keys self.keys = self.cmap.keys
self.set(*args,**kwargs) self.set(*args, **kwargs)
def __getitem__(self, *args, **kwargs): def __getitem__(self, *args, **kwargs):
return self.cmap.__getitem__(*args, **kwargs) return self.cmap.__getitem__(*args, **kwargs)
...@@ -35,9 +36,9 @@ class CorrectMap(object): ...@@ -35,9 +36,9 @@ class CorrectMap(object):
self.cmap[answer_id] = {'correctness': correctness, self.cmap[answer_id] = {'correctness': correctness,
'npoints': npoints, 'npoints': npoints,
'msg': msg, 'msg': msg,
'hint' : hint, 'hint': hint,
'hintmode' : hintmode, 'hintmode': hintmode,
'queuekey' : queuekey, 'queuekey': queuekey,
} }
def __repr__(self): def __repr__(self):
...@@ -49,69 +50,69 @@ class CorrectMap(object): ...@@ -49,69 +50,69 @@ class CorrectMap(object):
''' '''
return self.cmap return self.cmap
def set_dict(self,correct_map): def set_dict(self, correct_map):
''' '''
set internal dict to provided correct_map dict set internal dict to provided correct_map dict
for graceful migration, if correct_map is a one-level dict, then convert it to the new for graceful migration, if correct_map is a one-level dict, then convert it to the new
dict of dicts format. dict of dicts format.
''' '''
if correct_map and not (type(correct_map[correct_map.keys()[0]])==dict): if correct_map and not (type(correct_map[correct_map.keys()[0]]) == dict):
self.__init__() # empty current dict self.__init__() # empty current dict
for k in correct_map: self.set(k,correct_map[k]) # create new dict entries for k in correct_map: self.set(k, correct_map[k]) # create new dict entries
else: else:
self.cmap = correct_map self.cmap = correct_map
def is_correct(self,answer_id): def is_correct(self, answer_id):
if answer_id in self.cmap: return self.cmap[answer_id]['correctness'] == 'correct' if answer_id in self.cmap: return self.cmap[answer_id]['correctness'] == 'correct'
return None return None
def is_queued(self,answer_id): def is_queued(self, answer_id):
return answer_id in self.cmap and self.cmap[answer_id]['queuekey'] is not None return answer_id in self.cmap and self.cmap[answer_id]['queuekey'] is not None
def is_right_queuekey(self, answer_id, test_key): def is_right_queuekey(self, answer_id, test_key):
return answer_id in self.cmap and self.cmap[answer_id]['queuekey'] == test_key return answer_id in self.cmap and self.cmap[answer_id]['queuekey'] == test_key
def get_npoints(self,answer_id): def get_npoints(self, answer_id):
if self.is_correct(answer_id): if self.is_correct(answer_id):
npoints = self.cmap[answer_id].get('npoints',1) # default to 1 point if correct npoints = self.cmap[answer_id].get('npoints', 1) # default to 1 point if correct
return npoints or 1 return npoints or 1
return 0 # if not correct, return 0 return 0 # if not correct, return 0
def set_property(self,answer_id,property,value): def set_property(self, answer_id, property, value):
if answer_id in self.cmap: self.cmap[answer_id][property] = value if answer_id in self.cmap: self.cmap[answer_id][property] = value
else: self.cmap[answer_id] = {property:value} else: self.cmap[answer_id] = {property: value}
def get_property(self,answer_id,property,default=None): def get_property(self, answer_id, property, default=None):
if answer_id in self.cmap: return self.cmap[answer_id].get(property,default) if answer_id in self.cmap: return self.cmap[answer_id].get(property, default)
return default return default
def get_correctness(self,answer_id): def get_correctness(self, answer_id):
return self.get_property(answer_id,'correctness') return self.get_property(answer_id, 'correctness')
def get_msg(self,answer_id): def get_msg(self, answer_id):
return self.get_property(answer_id,'msg','') return self.get_property(answer_id, 'msg', '')
def get_hint(self,answer_id): def get_hint(self, answer_id):
return self.get_property(answer_id,'hint','') return self.get_property(answer_id, 'hint', '')
def get_hintmode(self,answer_id): def get_hintmode(self, answer_id):
return self.get_property(answer_id,'hintmode',None) return self.get_property(answer_id, 'hintmode', None)
def set_hint_and_mode(self,answer_id,hint,hintmode): def set_hint_and_mode(self, answer_id, hint, hintmode):
''' '''
- hint : (string) HTML text for hint - hint : (string) HTML text for hint
- hintmode : (string) mode for hint display ('always' or 'on_request') - hintmode : (string) mode for hint display ('always' or 'on_request')
''' '''
self.set_property(answer_id,'hint',hint) self.set_property(answer_id, 'hint', hint)
self.set_property(answer_id,'hintmode',hintmode) self.set_property(answer_id, 'hintmode', hintmode)
def update(self,other_cmap): def update(self, other_cmap):
''' '''
Update this CorrectMap with the contents of another CorrectMap Update this CorrectMap with the contents of another CorrectMap
''' '''
if not isinstance(other_cmap,CorrectMap): if not isinstance(other_cmap, CorrectMap):
raise Exception('CorrectMap.update called with invalid argument %s' % other_cmap) raise Exception('CorrectMap.update called with invalid argument %s' % other_cmap)
self.cmap.update(other_cmap.get_dict()) self.cmap.update(other_cmap.get_dict())
""" Standard resistor codes. """ Standard resistor codes.
http://en.wikipedia.org/wiki/Electronic_color_code http://en.wikipedia.org/wiki/Electronic_color_code
""" """
E6=[10,15,22,33,47,68] E6 = [10, 15, 22, 33, 47, 68]
E12=[10,12,15,18,22,27,33,39,47,56,68,82] E12 = [10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82]
E24=[10,12,15,18,22,27,33,39,47,56,68,82,11,13,16,20,24,30,36,43,51,62,75,91] E24 = [10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82, 11, 13, 16, 20, 24, 30, 36, 43, 51, 62, 75, 91]
E48=[100,121,147,178,215,261,316,383,464,562,681,825,105,127,154,187,226,274,332,402,487,590,715,866,110,133,162,196,237,287,348,422,511,619,750,909,115,140,169,205,249,301,365,442,536,649,787,953] E48 = [100, 121, 147, 178, 215, 261, 316, 383, 464, 562, 681, 825, 105, 127, 154, 187, 226, 274, 332, 402, 487, 590, 715, 866, 110, 133, 162, 196, 237, 287, 348, 422, 511, 619, 750, 909, 115, 140, 169, 205, 249, 301, 365, 442, 536, 649, 787, 953]
E96=[100,121,147,178,215,261,316,383,464,562,681,825,102,124,150,182,221,267,324,392,475,576,698,845,105,127,154,187,226,274,332,402,487,590,715,866,107,130,158,191,232,280,340,412,499,604,732,887,110,133,162,196,237,287,348,422,511,619,750,909,113,137,165,200,243,294,357,432,523,634,768,931,115,140,169,205,249,301,365,442,536,649,787,953,118,143,174,210,255,309,374,453,549,665,806,976] E96 = [100, 121, 147, 178, 215, 261, 316, 383, 464, 562, 681, 825, 102, 124, 150, 182, 221, 267, 324, 392, 475, 576, 698, 845, 105, 127, 154, 187, 226, 274, 332, 402, 487, 590, 715, 866, 107, 130, 158, 191, 232, 280, 340, 412, 499, 604, 732, 887, 110, 133, 162, 196, 237, 287, 348, 422, 511, 619, 750, 909, 113, 137, 165, 200, 243, 294, 357, 432, 523, 634, 768, 931, 115, 140, 169, 205, 249, 301, 365, 442, 536, 649, 787, 953, 118, 143, 174, 210, 255, 309, 374, 453, 549, 665, 806, 976]
E192=[100,121,147,178,215,261,316,383,464,562,681,825,101,123,149,180,218,264,320,388,470,569,690,835,102,124,150,182,221,267,324,392,475,576,698,845,104,126,152,184,223,271,328,397,481,583,706,856,105,127,154,187,226,274,332,402,487,590,715,866,106,129,156,189,229,277,336,407,493,597,723,876,107,130,158,191,232,280,340,412,499,604,732,887,109,132,160,193,234,284,344,417,505,612,741,898,110,133,162,196,237,287,348,422,511,619,750,909,111,135,164,198,240,291,352,427,517,626,759,920,113,137,165,200,243,294,357,432,523,634,768,931,114,138,167,203,246,298,361,437,530,642,777,942,115,140,169,205,249,301,365,442,536,649,787,953,117,142,172,208,252,305,370,448,542,657,796,965,118,143,174,210,255,309,374,453,549,665,806,976,120,145,176,213,258,312,379,459,556,673,816,988] E192 = [100, 121, 147, 178, 215, 261, 316, 383, 464, 562, 681, 825, 101, 123, 149, 180, 218, 264, 320, 388, 470, 569, 690, 835, 102, 124, 150, 182, 221, 267, 324, 392, 475, 576, 698, 845, 104, 126, 152, 184, 223, 271, 328, 397, 481, 583, 706, 856, 105, 127, 154, 187, 226, 274, 332, 402, 487, 590, 715, 866, 106, 129, 156, 189, 229, 277, 336, 407, 493, 597, 723, 876, 107, 130, 158, 191, 232, 280, 340, 412, 499, 604, 732, 887, 109, 132, 160, 193, 234, 284, 344, 417, 505, 612, 741, 898, 110, 133, 162, 196, 237, 287, 348, 422, 511, 619, 750, 909, 111, 135, 164, 198, 240, 291, 352, 427, 517, 626, 759, 920, 113, 137, 165, 200, 243, 294, 357, 432, 523, 634, 768, 931, 114, 138, 167, 203, 246, 298, 361, 437, 530, 642, 777, 942, 115, 140, 169, 205, 249, 301, 365, 442, 536, 649, 787, 953, 117, 142, 172, 208, 252, 305, 370, 448, 542, 657, 796, 965, 118, 143, 174, 210, 255, 309, 374, 453, 549, 665, 806, 976, 120, 145, 176, 213, 258, 312, 379, 459, 556, 673, 816, 988]
...@@ -4,6 +4,7 @@ from calc import evaluator, UndefinedVariable ...@@ -4,6 +4,7 @@ from calc import evaluator, UndefinedVariable
# #
# Utility functions used in CAPA responsetypes # Utility functions used in CAPA responsetypes
def compare_with_tolerance(v1, v2, tol): def compare_with_tolerance(v1, v2, tol):
''' Compare v1 to v2 with maximum tolerance tol ''' Compare v1 to v2 with maximum tolerance tol
tol is relative if it ends in %; otherwise, it is absolute tol is relative if it ends in %; otherwise, it is absolute
...@@ -14,17 +15,18 @@ def compare_with_tolerance(v1, v2, tol): ...@@ -14,17 +15,18 @@ def compare_with_tolerance(v1, v2, tol):
''' '''
relative = tol.endswith('%') relative = tol.endswith('%')
if relative: if relative:
tolerance_rel = evaluator(dict(),dict(),tol[:-1]) * 0.01 tolerance_rel = evaluator(dict(), dict(), tol[:-1]) * 0.01
tolerance = tolerance_rel * max(abs(v1), abs(v2)) tolerance = tolerance_rel * max(abs(v1), abs(v2))
else: else:
tolerance = evaluator(dict(),dict(),tol) tolerance = evaluator(dict(), dict(), tol)
return abs(v1-v2) <= tolerance return abs(v1 - v2) <= tolerance
def contextualize_text(text, context): # private def contextualize_text(text, context): # private
''' Takes a string with variables. E.g. $a+$b. ''' Takes a string with variables. E.g. $a+$b.
Does a substitution of those variables from the context ''' Does a substitution of those variables from the context '''
if not text: return text if not text: return text
for key in sorted(context, lambda x,y:cmp(len(y),len(x))): for key in sorted(context, lambda x, y: cmp(len(y), len(x))):
text=text.replace('$'+key, str(context[key])) text = text.replace('$' + key, str(context[key]))
return text return text
...@@ -13,4 +13,3 @@ ...@@ -13,4 +13,3 @@
# limitations under the License. # limitations under the License.
lookup = None lookup = None
...@@ -22,6 +22,7 @@ from django.http import HttpResponse ...@@ -22,6 +22,7 @@ from django.http import HttpResponse
from . import middleware from . import middleware
from django.conf import settings from django.conf import settings
def render_to_string(template_name, dictionary, context=None, namespace='main'): def render_to_string(template_name, dictionary, context=None, namespace='main'):
context_instance = Context(dictionary) context_instance = Context(dictionary)
# add dictionary to context_instance # add dictionary to context_instance
...@@ -43,6 +44,7 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'): ...@@ -43,6 +44,7 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'):
template = middleware.lookup[namespace].get_template(template_name) template = middleware.lookup[namespace].get_template(template_name)
return template.render(**context_dictionary) return template.render(**context_dictionary)
def render_to_response(template_name, dictionary, context_instance=None, namespace='main', **kwargs): def render_to_response(template_name, dictionary, context_instance=None, namespace='main', **kwargs):
""" """
Returns a HttpResponse whose content is filled with the result of calling Returns a HttpResponse whose content is filled with the result of calling
......
...@@ -3,7 +3,7 @@ Progress class for modules. Represents where a student is in a module. ...@@ -3,7 +3,7 @@ Progress class for modules. Represents where a student is in a module.
Useful things to know: Useful things to know:
- Use Progress.to_js_status_str() to convert a progress into a simple - Use Progress.to_js_status_str() to convert a progress into a simple
status string to pass to js. status string to pass to js.
- Use Progress.to_js_detail_str() to convert a progress into a more detailed - Use Progress.to_js_detail_str() to convert a progress into a more detailed
string to pass to js. string to pass to js.
...@@ -11,11 +11,12 @@ In particular, these functions have a canonical handing of None. ...@@ -11,11 +11,12 @@ In particular, these functions have a canonical handing of None.
For most subclassing needs, you should only need to reimplement For most subclassing needs, you should only need to reimplement
frac() and __str__(). frac() and __str__().
''' '''
from collections import namedtuple from collections import namedtuple
import numbers import numbers
class Progress(object): class Progress(object):
'''Represents a progress of a/b (a out of b done) '''Represents a progress of a/b (a out of b done)
...@@ -37,7 +38,7 @@ class Progress(object): ...@@ -37,7 +38,7 @@ class Progress(object):
if not (isinstance(a, numbers.Number) and if not (isinstance(a, numbers.Number) and
isinstance(b, numbers.Number)): isinstance(b, numbers.Number)):
raise TypeError('a and b must be numbers. Passed {0}/{1}'.format(a, b)) raise TypeError('a and b must be numbers. Passed {0}/{1}'.format(a, b))
if not (0 <= a <= b and b > 0): if not (0 <= a <= b and b > 0):
raise ValueError( raise ValueError(
'fraction a/b = {0}/{1} must have 0 <= a <= b and b > 0'.format(a, b)) 'fraction a/b = {0}/{1} must have 0 <= a <= b and b > 0'.format(a, b))
...@@ -66,13 +67,12 @@ class Progress(object): ...@@ -66,13 +67,12 @@ class Progress(object):
''' '''
return self.frac()[0] > 0 return self.frac()[0] > 0
def inprogress(self): def inprogress(self):
''' Returns True if fractional progress is strictly between 0 and 1. ''' Returns True if fractional progress is strictly between 0 and 1.
subclassing note: implemented in terms of frac(), assumes sanity subclassing note: implemented in terms of frac(), assumes sanity
checking is done at construction time. checking is done at construction time.
''' '''
(a, b) = self.frac() (a, b) = self.frac()
return a > 0 and a < b return a > 0 and a < b
...@@ -83,15 +83,14 @@ class Progress(object): ...@@ -83,15 +83,14 @@ class Progress(object):
checking is done at construction time. checking is done at construction time.
''' '''
(a, b) = self.frac() (a, b) = self.frac()
return a==b return a == b
def ternary_str(self): def ternary_str(self):
''' Return a string version of this progress: either ''' Return a string version of this progress: either
"none", "in_progress", or "done". "none", "in_progress", or "done".
subclassing note: implemented in terms of frac() subclassing note: implemented in terms of frac()
''' '''
(a, b) = self.frac() (a, b) = self.frac()
if a == 0: if a == 0:
return "none" return "none"
...@@ -111,8 +110,7 @@ class Progress(object): ...@@ -111,8 +110,7 @@ class Progress(object):
def __ne__(self, other): def __ne__(self, other):
''' The opposite of equal''' ''' The opposite of equal'''
return not self.__eq__(other) return not self.__eq__(other)
def __str__(self): def __str__(self):
''' Return a string representation of this string. ''' Return a string representation of this string.
...@@ -147,7 +145,6 @@ class Progress(object): ...@@ -147,7 +145,6 @@ class Progress(object):
return "NA" return "NA"
return progress.ternary_str() return progress.ternary_str()
@staticmethod @staticmethod
def to_js_detail_str(progress): def to_js_detail_str(progress):
''' '''
......
...@@ -198,12 +198,12 @@ class CapaModule(XModule): ...@@ -198,12 +198,12 @@ class CapaModule(XModule):
if self.system.DEBUG: if self.system.DEBUG:
log.exception(err) log.exception(err)
msg = '[courseware.capa.capa_module] <font size="+1" color="red">Failed to generate HTML for problem %s</font>' % (self.location.url()) msg = '[courseware.capa.capa_module] <font size="+1" color="red">Failed to generate HTML for problem %s</font>' % (self.location.url())
msg += '<p>Error:</p><p><pre>%s</pre></p>' % str(err).replace('<','&lt;') msg += '<p>Error:</p><p><pre>%s</pre></p>' % str(err).replace('<', '&lt;')
msg += '<p><pre>%s</pre></p>' % traceback.format_exc().replace('<','&lt;') msg += '<p><pre>%s</pre></p>' % traceback.format_exc().replace('<', '&lt;')
html = msg html = msg
else: else:
raise raise
content = {'name': self.metadata['display_name'], content = {'name': self.metadata['display_name'],
'html': html, 'html': html,
'weight': self.weight, 'weight': self.weight,
...@@ -336,7 +336,7 @@ class CapaModule(XModule): ...@@ -336,7 +336,7 @@ class CapaModule(XModule):
score_msg = get['response'] score_msg = get['response']
self.lcp.update_score(score_msg, queuekey) self.lcp.update_score(score_msg, queuekey)
return dict() # No AJAX return is needed return dict() # No AJAX return is needed
def get_answer(self, get): def get_answer(self, get):
''' '''
...@@ -379,7 +379,7 @@ class CapaModule(XModule): ...@@ -379,7 +379,7 @@ class CapaModule(XModule):
if not name.endswith('[]'): if not name.endswith('[]'):
answers[name] = get[key] answers[name] = get[key]
else: else:
name = name[:-2] name = name[:-2]
answers[name] = get.getlist(key) answers[name] = get.getlist(key)
return answers return answers
...@@ -430,7 +430,7 @@ class CapaModule(XModule): ...@@ -430,7 +430,7 @@ class CapaModule(XModule):
if self.system.DEBUG: if self.system.DEBUG:
msg = "Error checking problem: " + str(err) msg = "Error checking problem: " + str(err)
msg += '\nTraceback:\n' + traceback.format_exc() msg += '\nTraceback:\n' + traceback.format_exc()
return {'success':msg} return {'success': msg}
traceback.print_exc() traceback.print_exc()
raise Exception("error in capa_module") raise Exception("error in capa_module")
......
...@@ -9,22 +9,21 @@ from fs.errors import ResourceNotFoundError ...@@ -9,22 +9,21 @@ from fs.errors import ResourceNotFoundError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class CourseDescriptor(SequenceDescriptor): class CourseDescriptor(SequenceDescriptor):
module_class = SequenceModule module_class = SequenceModule
def __init__(self, system, definition=None, **kwargs): def __init__(self, system, definition=None, **kwargs):
super(CourseDescriptor, self).__init__(system, definition, **kwargs) super(CourseDescriptor, self).__init__(system, definition, **kwargs)
try: try:
self.start = time.strptime(self.metadata["start"], "%Y-%m-%dT%H:%M") self.start = time.strptime(self.metadata["start"], "%Y-%m-%dT%H:%M")
except KeyError: except KeyError:
self.start = time.gmtime(0) #The epoch self.start = time.gmtime(0) # The epoch
log.critical("Course loaded without a start date. " + str(self.id)) log.critical("Course loaded without a start date. " + str(self.id))
except ValueError, e: except ValueError, e:
self.start = time.gmtime(0) #The epoch self.start = time.gmtime(0) # The epoch
log.critical("Course loaded with a bad start date. " + str(self.id) + " '" + str(e) + "'") log.critical("Course loaded with a bad start date. " + str(self.id) + " '" + str(e) + "'")
def has_started(self): def has_started(self):
return time.gmtime() > self.start return time.gmtime() > self.start
...@@ -44,15 +43,15 @@ class CourseDescriptor(SequenceDescriptor): ...@@ -44,15 +43,15 @@ class CourseDescriptor(SequenceDescriptor):
@property @property
def title(self): def title(self):
return self.metadata['display_name'] return self.metadata['display_name']
@property @property
def number(self): def number(self):
return self.location.course return self.location.course
@property @property
def wiki_namespace(self): def wiki_namespace(self):
return self.location.course return self.location.course
@property @property
def org(self): def org(self):
return self.location.org return self.location.org
\ No newline at end of file
...@@ -81,7 +81,7 @@ class Location(_LocationBase): ...@@ -81,7 +81,7 @@ class Location(_LocationBase):
def check_list(list_): def check_list(list_):
for val in list_: for val in list_:
if val is not None and INVALID_CHARS.search(val) is not None: if val is not None and INVALID_CHARS.search(val) is not None:
log.debug('invalid characters val="%s", list_="%s"' % (val,list_)) log.debug('invalid characters val="%s", list_="%s"' % (val, list_))
raise InvalidLocationError(location) raise InvalidLocationError(location)
if isinstance(location, basestring): if isinstance(location, basestring):
...@@ -169,7 +169,7 @@ class ModuleStore(object): ...@@ -169,7 +169,7 @@ class ModuleStore(object):
calls to get_children() to cache. None indicates to cache all descendents calls to get_children() to cache. None indicates to cache all descendents
""" """
raise NotImplementedError raise NotImplementedError
def get_items(self, location, depth=0): def get_items(self, location, depth=0):
""" """
Returns a list of XModuleDescriptor instances for the items Returns a list of XModuleDescriptor instances for the items
......
...@@ -10,5 +10,6 @@ class ItemNotFoundError(Exception): ...@@ -10,5 +10,6 @@ class ItemNotFoundError(Exception):
class InsufficientSpecificationError(Exception): class InsufficientSpecificationError(Exception):
pass pass
class InvalidLocationError(Exception): class InvalidLocationError(Exception):
pass pass
...@@ -56,6 +56,7 @@ def location_to_query(location): ...@@ -56,6 +56,7 @@ def location_to_query(location):
return query return query
class MongoModuleStore(ModuleStore): class MongoModuleStore(ModuleStore):
""" """
A Mongodb backed ModuleStore A Mongodb backed ModuleStore
......
...@@ -51,6 +51,7 @@ def test_invalid_locations(): ...@@ -51,6 +51,7 @@ def test_invalid_locations():
assert_raises(InvalidLocationError, Location, None) assert_raises(InvalidLocationError, Location, None)
assert_raises(InvalidLocationError, Location, "tag://org/course/category/name with spaces/revision") assert_raises(InvalidLocationError, Location, "tag://org/course/category/name with spaces/revision")
def test_equality(): def test_equality():
assert_equals( assert_equals(
Location('tag', 'org', 'course', 'category', 'name'), Location('tag', 'org', 'course', 'category', 'name'),
......
...@@ -53,7 +53,7 @@ class XMLModuleStore(ModuleStore): ...@@ -53,7 +53,7 @@ class XMLModuleStore(ModuleStore):
class_ = getattr(import_module(module_path), class_name) class_ = getattr(import_module(module_path), class_name)
self.default_class = class_ self.default_class = class_
log.debug('XMLModuleStore: eager=%s, data_dir = %s' % (eager,self.data_dir)) log.debug('XMLModuleStore: eager=%s, data_dir = %s' % (eager, self.data_dir))
log.debug('default_class = %s' % self.default_class) log.debug('default_class = %s' % self.default_class)
for course_dir in os.listdir(self.data_dir): for course_dir in os.listdir(self.data_dir):
......
...@@ -3,7 +3,7 @@ Progress class for modules. Represents where a student is in a module. ...@@ -3,7 +3,7 @@ Progress class for modules. Represents where a student is in a module.
Useful things to know: Useful things to know:
- Use Progress.to_js_status_str() to convert a progress into a simple - Use Progress.to_js_status_str() to convert a progress into a simple
status string to pass to js. status string to pass to js.
- Use Progress.to_js_detail_str() to convert a progress into a more detailed - Use Progress.to_js_detail_str() to convert a progress into a more detailed
string to pass to js. string to pass to js.
...@@ -11,11 +11,12 @@ In particular, these functions have a canonical handing of None. ...@@ -11,11 +11,12 @@ In particular, these functions have a canonical handing of None.
For most subclassing needs, you should only need to reimplement For most subclassing needs, you should only need to reimplement
frac() and __str__(). frac() and __str__().
''' '''
from collections import namedtuple from collections import namedtuple
import numbers import numbers
class Progress(object): class Progress(object):
'''Represents a progress of a/b (a out of b done) '''Represents a progress of a/b (a out of b done)
...@@ -37,7 +38,7 @@ class Progress(object): ...@@ -37,7 +38,7 @@ class Progress(object):
if not (isinstance(a, numbers.Number) and if not (isinstance(a, numbers.Number) and
isinstance(b, numbers.Number)): isinstance(b, numbers.Number)):
raise TypeError('a and b must be numbers. Passed {0}/{1}'.format(a, b)) raise TypeError('a and b must be numbers. Passed {0}/{1}'.format(a, b))
if not (0 <= a <= b and b > 0): if not (0 <= a <= b and b > 0):
raise ValueError( raise ValueError(
'fraction a/b = {0}/{1} must have 0 <= a <= b and b > 0'.format(a, b)) 'fraction a/b = {0}/{1} must have 0 <= a <= b and b > 0'.format(a, b))
...@@ -66,13 +67,12 @@ class Progress(object): ...@@ -66,13 +67,12 @@ class Progress(object):
''' '''
return self.frac()[0] > 0 return self.frac()[0] > 0
def inprogress(self): def inprogress(self):
''' Returns True if fractional progress is strictly between 0 and 1. ''' Returns True if fractional progress is strictly between 0 and 1.
subclassing note: implemented in terms of frac(), assumes sanity subclassing note: implemented in terms of frac(), assumes sanity
checking is done at construction time. checking is done at construction time.
''' '''
(a, b) = self.frac() (a, b) = self.frac()
return a > 0 and a < b return a > 0 and a < b
...@@ -83,15 +83,14 @@ class Progress(object): ...@@ -83,15 +83,14 @@ class Progress(object):
checking is done at construction time. checking is done at construction time.
''' '''
(a, b) = self.frac() (a, b) = self.frac()
return a==b return a == b
def ternary_str(self): def ternary_str(self):
''' Return a string version of this progress: either ''' Return a string version of this progress: either
"none", "in_progress", or "done". "none", "in_progress", or "done".
subclassing note: implemented in terms of frac() subclassing note: implemented in terms of frac()
''' '''
(a, b) = self.frac() (a, b) = self.frac()
if a == 0: if a == 0:
return "none" return "none"
...@@ -111,8 +110,7 @@ class Progress(object): ...@@ -111,8 +110,7 @@ class Progress(object):
def __ne__(self, other): def __ne__(self, other):
''' The opposite of equal''' ''' The opposite of equal'''
return not self.__eq__(other) return not self.__eq__(other)
def __str__(self): def __str__(self):
''' Return a string representation of this string. ''' Return a string representation of this string.
...@@ -147,7 +145,6 @@ class Progress(object): ...@@ -147,7 +145,6 @@ class Progress(object):
return "NA" return "NA"
return progress.ternary_str() return progress.ternary_str()
@staticmethod @staticmethod
def to_js_detail_str(progress): def to_js_detail_str(progress):
''' '''
......
...@@ -2,9 +2,11 @@ import json ...@@ -2,9 +2,11 @@ import json
from x_module import XModule, XModuleDescriptor from x_module import XModule, XModuleDescriptor
class ModuleDescriptor(XModuleDescriptor): class ModuleDescriptor(XModuleDescriptor):
pass pass
class Module(XModule): class Module(XModule):
def get_html(self): def get_html(self):
return '<input type="hidden" class="schematic" name="{item_id}" height="480" width="640">'.format(item_id=self.item_id) return '<input type="hidden" class="schematic" name="{item_id}" height="480" width="640">'.format(item_id=self.item_id)
...@@ -53,9 +53,9 @@ class SequenceModule(XModule): ...@@ -53,9 +53,9 @@ class SequenceModule(XModule):
def handle_ajax(self, dispatch, get): # TODO: bounds checking def handle_ajax(self, dispatch, get): # TODO: bounds checking
''' get = request.POST instance ''' ''' get = request.POST instance '''
if dispatch=='goto_position': if dispatch == 'goto_position':
self.position = int(get['position']) self.position = int(get['position'])
return json.dumps({'success':True}) return json.dumps({'success': True})
raise self.system.exception404 raise self.system.exception404
def render(self): def render(self):
...@@ -81,7 +81,7 @@ class SequenceModule(XModule): ...@@ -81,7 +81,7 @@ class SequenceModule(XModule):
# of script, even if it occurs mid-string. Do this after json.dumps()ing # of script, even if it occurs mid-string. Do this after json.dumps()ing
# so that we can be sure of the quotations being used # so that we can be sure of the quotations being used
import re import re
params = {'items': re.sub(r'(?i)</(script)', r'\u003c/\1', json.dumps(contents)), # ?i = re.IGNORECASE for py2.6 compatability params = {'items': re.sub(r'(?i)</(script)', r'\u003c/\1', json.dumps(contents)), # ?i = re.IGNORECASE for py2.6 compatability
'element_id': self.location.html_id(), 'element_id': self.location.html_id(),
'item_id': self.id, 'item_id': self.id,
'position': self.position, 'position': self.position,
......
...@@ -36,7 +36,7 @@ class VideoModule(XModule): ...@@ -36,7 +36,7 @@ class VideoModule(XModule):
if dispatch == 'goto_position': if dispatch == 'goto_position':
self.position = int(float(get['position'])) self.position = int(float(get['position']))
log.info(u"NEW POSITION {0}".format(self.position)) log.info(u"NEW POSITION {0}".format(self.position))
return json.dumps({'success':True}) return json.dumps({'success': True})
raise Http404() raise Http404()
def get_progress(self): def get_progress(self):
......
...@@ -7,6 +7,7 @@ from functools import partial ...@@ -7,6 +7,7 @@ from functools import partial
log = logging.getLogger('mitx.' + __name__) log = logging.getLogger('mitx.' + __name__)
def dummy_track(event_type, event): def dummy_track(event_type, event):
pass pass
...@@ -171,11 +172,11 @@ class XModule(object): ...@@ -171,11 +172,11 @@ class XModule(object):
return None return None
def max_score(self): def max_score(self):
''' Maximum score. Two notes: ''' Maximum score. Two notes:
* This is generic; in abstract, a problem could be 3/5 points on one randomization, and 5/7 on another * This is generic; in abstract, a problem could be 3/5 points on one randomization, and 5/7 on another
* In practice, this is a Very Bad Idea, and (a) will break some code in place (although that code * In practice, this is a Very Bad Idea, and (a) will break some code in place (although that code
should get fixed), and (b) break some analytics we plan to put in place. should get fixed), and (b) break some analytics we plan to put in place.
''' '''
return None return None
def get_html(self): def get_html(self):
...@@ -193,8 +194,8 @@ class XModule(object): ...@@ -193,8 +194,8 @@ class XModule(object):
return None return None
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
''' dispatch is last part of the URL. ''' dispatch is last part of the URL.
get is a dictionary-like object ''' get is a dictionary-like object '''
return "" return ""
......
from django.utils.simplejson import dumps from django.utils.simplejson import dumps
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from certificates.models import GeneratedCertificate from certificates.models import GeneratedCertificate
class Command(BaseCommand): class Command(BaseCommand):
help = """ help = """
This command finds all GeneratedCertificate objects that do not have a This command finds all GeneratedCertificate objects that do not have a
certificate generated. These come into being when a user requests a certificate generated. These come into being when a user requests a
certificate, or when grade_all_students is called (for pre-generating certificate, or when grade_all_students is called (for pre-generating
certificates). certificates).
It returns a json formatted list of users and their user ids It returns a json formatted list of users and their user ids
""" """
def handle(self, *args, **options): def handle(self, *args, **options):
users = GeneratedCertificate.objects.filter( users = GeneratedCertificate.objects.filter(
download_url = None ) download_url=None)
user_output = [{'user_id':user.user_id, 'name':user.name} user_output = [{'user_id':user.user_id, 'name':user.name}
for user in users] for user in users]
self.stdout.write(dumps(user_output) + "\n") self.stdout.write(dumps(user_output) + "\n")
...@@ -90,4 +90,4 @@ class Migration(SchemaMigration): ...@@ -90,4 +90,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['certificates'] complete_apps = ['certificates']
\ No newline at end of file
...@@ -88,4 +88,4 @@ class Migration(SchemaMigration): ...@@ -88,4 +88,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['certificates'] complete_apps = ['certificates']
\ No newline at end of file
...@@ -89,4 +89,4 @@ class Migration(SchemaMigration): ...@@ -89,4 +89,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['certificates'] complete_apps = ['certificates']
\ No newline at end of file
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding field 'GeneratedCertificate.graded_certificate_id' # Adding field 'GeneratedCertificate.graded_certificate_id'
db.add_column('certificates_generatedcertificate', 'graded_certificate_id', self.gf('django.db.models.fields.CharField')(max_length=32, null=True), keep_default=False) db.add_column('certificates_generatedcertificate', 'graded_certificate_id', self.gf('django.db.models.fields.CharField')(max_length=32, null=True), keep_default=False)
...@@ -17,9 +18,8 @@ class Migration(SchemaMigration): ...@@ -17,9 +18,8 @@ class Migration(SchemaMigration):
# Adding field 'GeneratedCertificate.grade' # Adding field 'GeneratedCertificate.grade'
db.add_column('certificates_generatedcertificate', 'grade', self.gf('django.db.models.fields.CharField')(max_length=5, null=True), keep_default=False) db.add_column('certificates_generatedcertificate', 'grade', self.gf('django.db.models.fields.CharField')(max_length=5, null=True), keep_default=False)
def backwards(self, orm): def backwards(self, orm):
# Deleting field 'GeneratedCertificate.graded_certificate_id' # Deleting field 'GeneratedCertificate.graded_certificate_id'
db.delete_column('certificates_generatedcertificate', 'graded_certificate_id') db.delete_column('certificates_generatedcertificate', 'graded_certificate_id')
...@@ -29,7 +29,6 @@ class Migration(SchemaMigration): ...@@ -29,7 +29,6 @@ class Migration(SchemaMigration):
# Deleting field 'GeneratedCertificate.grade' # Deleting field 'GeneratedCertificate.grade'
db.delete_column('certificates_generatedcertificate', 'grade') db.delete_column('certificates_generatedcertificate', 'grade')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,20 +4,19 @@ from south.db import db ...@@ -4,20 +4,19 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding field 'GeneratedCertificate.name' # Adding field 'GeneratedCertificate.name'
db.add_column('certificates_generatedcertificate', 'name', self.gf('django.db.models.fields.CharField')(default='', max_length=255, blank=True), keep_default=False) db.add_column('certificates_generatedcertificate', 'name', self.gf('django.db.models.fields.CharField')(default='', max_length=255, blank=True), keep_default=False)
def backwards(self, orm): def backwards(self, orm):
# Deleting field 'GeneratedCertificate.name' # Deleting field 'GeneratedCertificate.name'
db.delete_column('certificates_generatedcertificate', 'name') db.delete_column('certificates_generatedcertificate', 'name')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,20 +4,19 @@ from south.db import db ...@@ -4,20 +4,19 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Changing field 'GeneratedCertificate.certificate_id' # Changing field 'GeneratedCertificate.certificate_id'
db.alter_column('certificates_generatedcertificate', 'certificate_id', self.gf('django.db.models.fields.CharField')(max_length=32, null=True)) db.alter_column('certificates_generatedcertificate', 'certificate_id', self.gf('django.db.models.fields.CharField')(max_length=32, null=True))
def backwards(self, orm): def backwards(self, orm):
# Changing field 'GeneratedCertificate.certificate_id' # Changing field 'GeneratedCertificate.certificate_id'
db.alter_column('certificates_generatedcertificate', 'certificate_id', self.gf('django.db.models.fields.CharField')(default=None, max_length=32)) db.alter_column('certificates_generatedcertificate', 'certificate_id', self.gf('django.db.models.fields.CharField')(default=None, max_length=32))
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding model 'RevokedCertificate' # Adding model 'RevokedCertificate'
db.create_table('certificates_revokedcertificate', ( db.create_table('certificates_revokedcertificate', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
...@@ -23,13 +24,11 @@ class Migration(SchemaMigration): ...@@ -23,13 +24,11 @@ class Migration(SchemaMigration):
)) ))
db.send_create_signal('certificates', ['RevokedCertificate']) db.send_create_signal('certificates', ['RevokedCertificate'])
def backwards(self, orm): def backwards(self, orm):
# Deleting model 'RevokedCertificate' # Deleting model 'RevokedCertificate'
db.delete_table('certificates_revokedcertificate') db.delete_table('certificates_revokedcertificate')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -7,7 +7,7 @@ from django.db import models ...@@ -7,7 +7,7 @@ from django.db import models
''' '''
Certificates are created for a student and an offering of a course. Certificates are created for a student and an offering of a course.
When a certificate is generated, a unique ID is generated so that When a certificate is generated, a unique ID is generated so that
the certificate can be verified later. The ID is a UUID4, so that the certificate can be verified later. The ID is a UUID4, so that
it can't be easily guessed and so that it is unique. Even though it can't be easily guessed and so that it is unique. Even though
we save these generated certificates (for later verification), we we save these generated certificates (for later verification), we
...@@ -15,7 +15,7 @@ also record the UUID so that if we regenerate the certificate it ...@@ -15,7 +15,7 @@ also record the UUID so that if we regenerate the certificate it
will have the same UUID. will have the same UUID.
If certificates are being generated on the fly, a GeneratedCertificate If certificates are being generated on the fly, a GeneratedCertificate
should be created with the user, certificate_id, and enabled set should be created with the user, certificate_id, and enabled set
when a student requests a certificate. When the certificate has been when a student requests a certificate. When the certificate has been
generated, the download_url should be set. generated, the download_url should be set.
...@@ -26,119 +26,119 @@ needs to be set to true. ...@@ -26,119 +26,119 @@ needs to be set to true.
''' '''
class GeneratedCertificate(models.Model): class GeneratedCertificate(models.Model):
user = models.ForeignKey(User, db_index=True) user = models.ForeignKey(User, db_index=True)
# This is the name at the time of request # This is the name at the time of request
name = models.CharField(blank=True, max_length=255) name = models.CharField(blank=True, max_length=255)
certificate_id = models.CharField(max_length=32, null=True, default=None) certificate_id = models.CharField(max_length=32, null=True, default=None)
graded_certificate_id = models.CharField(max_length=32, null=True, default=None) graded_certificate_id = models.CharField(max_length=32, null=True, default=None)
download_url = models.CharField(max_length=128, null=True) download_url = models.CharField(max_length=128, null=True)
graded_download_url = models.CharField(max_length=128, null=True) graded_download_url = models.CharField(max_length=128, null=True)
grade = models.CharField(max_length=5, null=True) grade = models.CharField(max_length=5, null=True)
# enabled should only be true if the student has earned a grade in the course # enabled should only be true if the student has earned a grade in the course
# The student must have a grade and request a certificate for enabled to be True # The student must have a grade and request a certificate for enabled to be True
enabled = models.BooleanField(default=False) enabled = models.BooleanField(default=False)
class RevokedCertificate(models.Model): class RevokedCertificate(models.Model):
""" """
This model is for when a GeneratedCertificate must be regenerated. This model This model is for when a GeneratedCertificate must be regenerated. This model
contains all the same fields, to store a record of what the GeneratedCertificate contains all the same fields, to store a record of what the GeneratedCertificate
was before it was revoked (at which time all of it's information can change when was before it was revoked (at which time all of it's information can change when
it is regenerated). it is regenerated).
GeneratedCertificate may be deleted once they are revoked, and then created again. GeneratedCertificate may be deleted once they are revoked, and then created again.
For this reason, the only link between a GeneratedCertificate and RevokedCertificate For this reason, the only link between a GeneratedCertificate and RevokedCertificate
is that they share the same user. is that they share the same user.
""" """
####-------------------New Fields--------------------#### ####-------------------New Fields--------------------####
explanation = models.TextField(blank=True) explanation = models.TextField(blank=True)
####---------Fields from GeneratedCertificate---------#### ####---------Fields from GeneratedCertificate---------####
user = models.ForeignKey(User, db_index=True) user = models.ForeignKey(User, db_index=True)
# This is the name at the time of request # This is the name at the time of request
name = models.CharField(blank=True, max_length=255) name = models.CharField(blank=True, max_length=255)
certificate_id = models.CharField(max_length=32, null=True, default=None) certificate_id = models.CharField(max_length=32, null=True, default=None)
graded_certificate_id = models.CharField(max_length=32, null=True, default=None) graded_certificate_id = models.CharField(max_length=32, null=True, default=None)
download_url = models.CharField(max_length=128, null=True) download_url = models.CharField(max_length=128, null=True)
graded_download_url = models.CharField(max_length=128, null=True) graded_download_url = models.CharField(max_length=128, null=True)
grade = models.CharField(max_length=5, null=True) grade = models.CharField(max_length=5, null=True)
enabled = models.BooleanField(default=False) enabled = models.BooleanField(default=False)
def revoke_certificate(certificate, explanation): def revoke_certificate(certificate, explanation):
""" """
This method takes a GeneratedCertificate. It records its information from the certificate This method takes a GeneratedCertificate. It records its information from the certificate
into a RevokedCertificate, and then marks the certificate as needing regenerating. into a RevokedCertificate, and then marks the certificate as needing regenerating.
When the new certificiate is regenerated it will have new IDs and download URLS. When the new certificiate is regenerated it will have new IDs and download URLS.
Once this method has been called, it is safe to delete the certificate, or modify the Once this method has been called, it is safe to delete the certificate, or modify the
certificate's name or grade until it has been generated again. certificate's name or grade until it has been generated again.
""" """
revoked = RevokedCertificate( user = certificate.user, revoked = RevokedCertificate(user=certificate.user,
name = certificate.name, name=certificate.name,
certificate_id = certificate.certificate_id, certificate_id=certificate.certificate_id,
graded_certificate_id = certificate.graded_certificate_id, graded_certificate_id=certificate.graded_certificate_id,
download_url = certificate.download_url, download_url=certificate.download_url,
graded_download_url = certificate.graded_download_url, graded_download_url=certificate.graded_download_url,
grade = certificate.grade, grade=certificate.grade,
enabled = certificate.enabled) enabled=certificate.enabled)
revoked.explanation = explanation revoked.explanation = explanation
certificate.certificate_id = None certificate.certificate_id = None
certificate.graded_certificate_id = None certificate.graded_certificate_id = None
certificate.download_url = None certificate.download_url = None
certificate.graded_download_url = None certificate.graded_download_url = None
certificate.save() certificate.save()
revoked.save() revoked.save()
def certificate_state_for_student(student, grade): def certificate_state_for_student(student, grade):
''' '''
This returns a dictionary with a key for state, and other information. The state is one of the This returns a dictionary with a key for state, and other information. The state is one of the
following: following:
unavailable - A student is not eligible for a certificate. unavailable - A student is not eligible for a certificate.
requestable - A student is eligible to request a certificate requestable - A student is eligible to request a certificate
generating - A student has requested a certificate, but it is not generated yet. generating - A student has requested a certificate, but it is not generated yet.
downloadable - The certificate has been requested and is available for download. downloadable - The certificate has been requested and is available for download.
If the state is "downloadable", the dictionary also contains "download_url" and "graded_download_url". If the state is "downloadable", the dictionary also contains "download_url" and "graded_download_url".
''' '''
if grade: if grade:
#TODO: Remove the following after debugging #TODO: Remove the following after debugging
if settings.DEBUG_SURVEY: if settings.DEBUG_SURVEY:
return {'state' : 'requestable' } return {'state': 'requestable'}
try: try:
generated_certificate = GeneratedCertificate.objects.get(user = student) generated_certificate = GeneratedCertificate.objects.get(user=student)
if generated_certificate.enabled: if generated_certificate.enabled:
if generated_certificate.download_url: if generated_certificate.download_url:
return {'state' : 'downloadable', return {'state': 'downloadable',
'download_url' : generated_certificate.download_url, 'download_url': generated_certificate.download_url,
'graded_download_url' : generated_certificate.graded_download_url} 'graded_download_url': generated_certificate.graded_download_url}
else: else:
return {'state' : 'generating'} return {'state': 'generating'}
else: else:
# If enabled=False, it may have been pre-generated but not yet requested # If enabled=False, it may have been pre-generated but not yet requested
# Our output will be the same as if the GeneratedCertificate did not exist # Our output will be the same as if the GeneratedCertificate did not exist
pass pass
except GeneratedCertificate.DoesNotExist: except GeneratedCertificate.DoesNotExist:
pass pass
return {'state' : 'requestable'} return {'state': 'requestable'}
else: else:
# No grade, no certificate. No exceptions # No grade, no certificate. No exceptions
return {'state' : 'unavailable'} return {'state': 'unavailable'}
...@@ -18,76 +18,74 @@ from student.models import UserProfile ...@@ -18,76 +18,74 @@ from student.models import UserProfile
log = logging.getLogger("mitx.certificates") log = logging.getLogger("mitx.certificates")
@login_required @login_required
def certificate_request(request): def certificate_request(request):
''' Attempt to send a certificate. ''' ''' Attempt to send a certificate. '''
if not settings.END_COURSE_ENABLED: if not settings.END_COURSE_ENABLED:
raise Http404 raise Http404
if request.method == "POST": if request.method == "POST":
honor_code_verify = request.POST.get('cert_request_honor_code_verify', 'false') honor_code_verify = request.POST.get('cert_request_honor_code_verify', 'false')
name_verify = request.POST.get('cert_request_name_verify', 'false') name_verify = request.POST.get('cert_request_name_verify', 'false')
id_verify = request.POST.get('cert_request_id_verify', 'false') id_verify = request.POST.get('cert_request_id_verify', 'false')
error = '' error = ''
def return_error(error): def return_error(error):
return HttpResponse(json.dumps({'success':False, return HttpResponse(json.dumps({'success': False,
'error': error })) 'error': error}))
if honor_code_verify != 'true': if honor_code_verify != 'true':
error += 'Please verify that you have followed the honor code to receive a certificate. ' error += 'Please verify that you have followed the honor code to receive a certificate. '
if name_verify != 'true': if name_verify != 'true':
error += 'Please verify that your name is correct to receive a certificate. ' error += 'Please verify that your name is correct to receive a certificate. '
if id_verify != 'true': if id_verify != 'true':
error += 'Please certify that you understand the unique ID on the certificate. ' error += 'Please certify that you understand the unique ID on the certificate. '
if len(error) > 0: if len(error) > 0:
return return_error(error) return return_error(error)
survey_response = record_exit_survey(request, internal_request=True) survey_response = record_exit_survey(request, internal_request=True)
if not survey_response['success']: if not survey_response['success']:
return return_error( survey_response['error'] ) return return_error(survey_response['error'])
grade = None grade = None
student_gradesheet = grades.grade_sheet(request.user) student_gradesheet = grades.grade_sheet(request.user)
grade = student_gradesheet['grade'] grade = student_gradesheet['grade']
if not grade: if not grade:
return return_error('You have not earned a grade in this course. ') return return_error('You have not earned a grade in this course. ')
generate_certificate(request.user, grade) generate_certificate(request.user, grade)
return HttpResponse(json.dumps({'success':True})) return HttpResponse(json.dumps({'success': True}))
else: else:
#This is not a POST, we should render the page with the form #This is not a POST, we should render the page with the form
grade_sheet = grades.grade_sheet(request.user) grade_sheet = grades.grade_sheet(request.user)
certificate_state = certificate_state_for_student(request.user, grade_sheet['grade']) certificate_state = certificate_state_for_student(request.user, grade_sheet['grade'])
if certificate_state['state'] != "requestable": if certificate_state['state'] != "requestable":
return redirect("/profile") return redirect("/profile")
user_info = UserProfile.objects.get(user=request.user) user_info = UserProfile.objects.get(user=request.user)
took_survey = student_took_survey(user_info) took_survey = student_took_survey(user_info)
if settings.DEBUG_SURVEY: if settings.DEBUG_SURVEY:
took_survey = False took_survey = False
survey_list = [] survey_list = []
if not took_survey: if not took_survey:
survey_list = exit_survey_list_for_student(request.user) survey_list = exit_survey_list_for_student(request.user)
context = {'certificate_state' : certificate_state,
'took_survey' : took_survey,
'survey_list' : survey_list,
'name' : user_info.name }
return render_to_response('cert_request.html', context)
context = {'certificate_state': certificate_state,
'took_survey': took_survey,
'survey_list': survey_list,
'name': user_info.name}
return render_to_response('cert_request.html', context)
# This method should only be called if the user has a grade and has requested a certificate # This method should only be called if the user has a grade and has requested a certificate
...@@ -96,11 +94,11 @@ def generate_certificate(user, grade): ...@@ -96,11 +94,11 @@ def generate_certificate(user, grade):
# states for a GeneratedCertificate object # states for a GeneratedCertificate object
if grade and user.is_active: if grade and user.is_active:
generated_certificate = None generated_certificate = None
try: try:
generated_certificate = GeneratedCertificate.objects.get(user = user) generated_certificate = GeneratedCertificate.objects.get(user=user)
except GeneratedCertificate.DoesNotExist: except GeneratedCertificate.DoesNotExist:
generated_certificate = GeneratedCertificate(user = user) generated_certificate = GeneratedCertificate(user=user)
generated_certificate.enabled = True generated_certificate.enabled = True
if generated_certificate.graded_download_url and (generated_certificate.grade != grade): if generated_certificate.graded_download_url and (generated_certificate.grade != grade):
...@@ -114,8 +112,8 @@ def generate_certificate(user, grade): ...@@ -114,8 +112,8 @@ def generate_certificate(user, grade):
ungraded_dl_url=generated_certificate.download_url, ungraded_dl_url=generated_certificate.download_url,
userid=user.id)) userid=user.id))
revoke_certificate(generated_certificate, "The grade on this certificate may be inaccurate.") revoke_certificate(generated_certificate, "The grade on this certificate may be inaccurate.")
user_name = UserProfile.objects.get(user = user).name user_name = UserProfile.objects.get(user=user).name
if generated_certificate.download_url and (generated_certificate.name != user_name): if generated_certificate.download_url and (generated_certificate.name != user_name):
log.critical(u"A Certificate has been pre-generated with the name of " log.critical(u"A Certificate has been pre-generated with the name of "
"{gen_name} but current name is {user_name} (user id is " "{gen_name} but current name is {user_name} (user id is "
...@@ -128,22 +126,21 @@ def generate_certificate(user, grade): ...@@ -128,22 +126,21 @@ def generate_certificate(user, grade):
userid=user.id)) userid=user.id))
revoke_certificate(generated_certificate, "The name on this certificate may be inaccurate.") revoke_certificate(generated_certificate, "The name on this certificate may be inaccurate.")
generated_certificate.grade = grade generated_certificate.grade = grade
generated_certificate.name = user_name generated_certificate.name = user_name
generated_certificate.save() generated_certificate.save()
certificate_id = generated_certificate.certificate_id certificate_id = generated_certificate.certificate_id
log.debug("Generating certificate for " + str(user.username) + " with ID: " + str(certificate_id)) log.debug("Generating certificate for " + str(user.username) + " with ID: " + str(certificate_id))
# TODO: If the certificate was pre-generated, send the email that it is ready to download # TODO: If the certificate was pre-generated, send the email that it is ready to download
if certificate_state_for_student(user, grade)['state'] == "downloadable": if certificate_state_for_student(user, grade)['state'] == "downloadable":
subject = render_to_string('emails/certificate_ready_subject.txt',{}) subject = render_to_string('emails/certificate_ready_subject.txt', {})
subject = ''.join(subject.splitlines()) subject = ''.join(subject.splitlines())
message = render_to_string('emails/certificate_ready.txt',{}) message = render_to_string('emails/certificate_ready.txt', {})
res=send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email,]) res = send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email, ])
else: else:
log.warning("Asked to generate a certificate for student " + str(user.username) + " but with a grade of " + str(grade) + " and active status " + str(user.is_active)) log.warning("Asked to generate a certificate for student " + str(user.username) + " but with a grade of " + str(grade) + " and active status " + str(user.is_active))
...@@ -3,10 +3,11 @@ import uuid ...@@ -3,10 +3,11 @@ import uuid
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
class ServerCircuit(models.Model): class ServerCircuit(models.Model):
# Later, add owner, who can edit, part of what app, etc. # Later, add owner, who can edit, part of what app, etc.
name = models.CharField(max_length=32, unique=True, db_index=True) name = models.CharField(max_length=32, unique=True, db_index=True)
schematic = models.TextField(blank=True) schematic = models.TextField(blank=True)
def __unicode__(self): def __unicode__(self):
return self.name+":"+self.schematic[:8] return self.name + ":" + self.schematic[:8]
...@@ -11,8 +11,9 @@ from mitxmako.shortcuts import render_to_response, render_to_string ...@@ -11,8 +11,9 @@ from mitxmako.shortcuts import render_to_response, render_to_string
from models import ServerCircuit from models import ServerCircuit
def circuit_line(circuit): def circuit_line(circuit):
''' Returns string for an appropriate input element for a circuit. ''' Returns string for an appropriate input element for a circuit.
TODO: Rename. ''' TODO: Rename. '''
if not circuit.isalnum(): if not circuit.isalnum():
raise Http404() raise Http404()
...@@ -28,10 +29,11 @@ def circuit_line(circuit): ...@@ -28,10 +29,11 @@ def circuit_line(circuit):
circuit_line.set('width', '640') circuit_line.set('width', '640')
circuit_line.set('height', '480') circuit_line.set('height', '480')
circuit_line.set('name', 'schematic') circuit_line.set('name', 'schematic')
circuit_line.set('id', 'schematic_'+circuit) circuit_line.set('id', 'schematic_' + circuit)
circuit_line.set('value', schematic) # We do it this way for security -- guarantees users cannot put funny stuff in schematic. circuit_line.set('value', schematic) # We do it this way for security -- guarantees users cannot put funny stuff in schematic.
return xml.etree.ElementTree.tostring(circuit_line) return xml.etree.ElementTree.tostring(circuit_line)
def edit_circuit(request, circuit): def edit_circuit(request, circuit):
try: try:
sc = ServerCircuit.objects.get(name=circuit) sc = ServerCircuit.objects.get(name=circuit)
...@@ -40,11 +42,12 @@ def edit_circuit(request, circuit): ...@@ -40,11 +42,12 @@ def edit_circuit(request, circuit):
if not circuit.isalnum(): if not circuit.isalnum():
raise Http404() raise Http404()
response = render_to_response('edit_circuit.html', {'name':circuit, response = render_to_response('edit_circuit.html', {'name': circuit,
'circuit_line':circuit_line(circuit)}) 'circuit_line': circuit_line(circuit)})
response['Cache-Control'] = 'no-cache' response['Cache-Control'] = 'no-cache'
return response return response
def save_circuit(request, circuit): def save_circuit(request, circuit):
if not circuit.isalnum(): if not circuit.isalnum():
raise Http404() raise Http404()
...@@ -63,4 +66,3 @@ def save_circuit(request, circuit): ...@@ -63,4 +66,3 @@ def save_circuit(request, circuit):
response = HttpResponse(json_str, mimetype='application/json') response = HttpResponse(json_str, mimetype='application/json')
response['Cache-Control'] = 'no-cache' response['Cache-Control'] = 'no-cache'
return response return response
""" """
Course settings module. All settings in the global_settings are Course settings module. All settings in the global_settings are
first applied, and then any settings in the settings.DATA_DIR/course_settings.json first applied, and then any settings in the settings.DATA_DIR/course_settings.json
are applied. A setting must be in ALL_CAPS. are applied. A setting must be in ALL_CAPS.
Settings are used by calling Settings are used by calling
from courseware.course_settings import course_settings from courseware.course_settings import course_settings
Note that courseware.course_settings.course_settings is not a module -- it's an object. So Note that courseware.course_settings.course_settings is not a module -- it's an object. So
importing individual settings is not possible: importing individual settings is not possible:
from courseware.course_settings.course_settings import GRADER # This won't work. from courseware.course_settings.course_settings import GRADER # This won't work.
...@@ -24,69 +24,67 @@ log = logging.getLogger("mitx.courseware") ...@@ -24,69 +24,67 @@ log = logging.getLogger("mitx.courseware")
global_settings_json = """ global_settings_json = """
{ {
"GRADER" : [ "GRADER" : [
{ {
"type" : "Homework", "type" : "Homework",
"min_count" : 12, "min_count" : 12,
"drop_count" : 2, "drop_count" : 2,
"short_label" : "HW", "short_label" : "HW",
"weight" : 0.15 "weight" : 0.15
}, },
{ {
"type" : "Lab", "type" : "Lab",
"min_count" : 12, "min_count" : 12,
"drop_count" : 2, "drop_count" : 2,
"category" : "Labs", "category" : "Labs",
"weight" : 0.15 "weight" : 0.15
}, },
{ {
"type" : "Midterm", "type" : "Midterm",
"name" : "Midterm Exam", "name" : "Midterm Exam",
"short_label" : "Midterm", "short_label" : "Midterm",
"weight" : 0.3 "weight" : 0.3
}, },
{ {
"type" : "Final", "type" : "Final",
"name" : "Final Exam", "name" : "Final Exam",
"short_label" : "Final", "short_label" : "Final",
"weight" : 0.4 "weight" : 0.4
} }
], ],
"GRADE_CUTOFFS" : { "GRADE_CUTOFFS" : {
"A" : 0.87, "A" : 0.87,
"B" : 0.7, "B" : 0.7,
"C" : 0.6 "C" : 0.6
} }
} }
""" """
class Settings(object): class Settings(object):
def __init__(self): def __init__(self):
# Load the global settings as a dictionary # Load the global settings as a dictionary
global_settings = json.loads(global_settings_json) global_settings = json.loads(global_settings_json)
# Load the course settings as a dictionary # Load the course settings as a dictionary
course_settings = {} course_settings = {}
try: try:
# TODO: this doesn't work with multicourse # TODO: this doesn't work with multicourse
with open( settings.DATA_DIR + "/course_settings.json") as course_settings_file: with open(settings.DATA_DIR + "/course_settings.json") as course_settings_file:
course_settings_string = course_settings_file.read() course_settings_string = course_settings_file.read()
course_settings = json.loads(course_settings_string) course_settings = json.loads(course_settings_string)
except IOError: except IOError:
log.warning("Unable to load course settings file from " + str(settings.DATA_DIR) + "/course_settings.json") log.warning("Unable to load course settings file from " + str(settings.DATA_DIR) + "/course_settings.json")
# Override any global settings with the course settings # Override any global settings with the course settings
global_settings.update(course_settings) global_settings.update(course_settings)
# Now, set the properties from the course settings on ourselves # Now, set the properties from the course settings on ourselves
for setting in global_settings: for setting in global_settings:
setting_value = global_settings[setting] setting_value = global_settings[setting]
setattr(self, setting, setting_value) setattr(self, setting, setting_value)
# Here is where we should parse any configurations, so that we can fail early # Here is where we should parse any configurations, so that we can fail early
self.GRADER = graders.grader_from_conf(self.GRADER) self.GRADER = graders.grader_from_conf(self.GRADER)
......
...@@ -12,15 +12,16 @@ from xmodule.modulestore.exceptions import ItemNotFoundError ...@@ -12,15 +12,16 @@ from xmodule.modulestore.exceptions import ItemNotFoundError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def check_course(course_id, course_must_be_open=True, course_required=True): def check_course(course_id, course_must_be_open=True, course_required=True):
""" """
Given a course_id, this returns the course object. By default, Given a course_id, this returns the course object. By default,
if the course is not found or the course is not open yet, this if the course is not found or the course is not open yet, this
method will raise a 404. method will raise a 404.
If course_must_be_open is False, the course will be returned If course_must_be_open is False, the course will be returned
without a 404 even if it is not open. without a 404 even if it is not open.
If course_required is False, a course_id of None is acceptable. The If course_required is False, a course_id of None is acceptable. The
course returned will be None. Even if the course is not required, course returned will be None. Even if the course is not required,
if a course_id is given that does not exist a 404 will be raised. if a course_id is given that does not exist a 404 will be raised.
...@@ -32,10 +33,10 @@ def check_course(course_id, course_must_be_open=True, course_required=True): ...@@ -32,10 +33,10 @@ def check_course(course_id, course_must_be_open=True, course_required=True):
course = modulestore().get_item(course_loc) course = modulestore().get_item(course_loc)
except (KeyError, ItemNotFoundError): except (KeyError, ItemNotFoundError):
raise Http404("Course not found.") raise Http404("Course not found.")
if course_must_be_open and not course.has_started(): if course_must_be_open and not course.has_started():
raise Http404("This course has not yet started.") raise Http404("This course has not yet started.")
return course return course
...@@ -44,10 +45,12 @@ def check_course(course_id, course_must_be_open=True, course_required=True): ...@@ -44,10 +45,12 @@ def check_course(course_id, course_must_be_open=True, course_required=True):
def course_static_url(course): def course_static_url(course):
return settings.STATIC_URL + "/" + course.metadata['data_dir'] + "/" return settings.STATIC_URL + "/" + course.metadata['data_dir'] + "/"
def course_image_url(course): def course_image_url(course):
return course_static_url(course) + "images/course_image.jpg" return course_static_url(course) + "images/course_image.jpg"
def get_course_about_section(course, section_key): def get_course_about_section(course, section_key):
""" """
This returns the snippet of html to be rendered on the course about page, given the key for the section. This returns the snippet of html to be rendered on the course about page, given the key for the section.
...@@ -78,7 +81,7 @@ def get_course_about_section(course, section_key): ...@@ -78,7 +81,7 @@ def get_course_about_section(course, section_key):
'effort', 'end_date', 'prerequisites']: 'effort', 'end_date', 'prerequisites']:
try: try:
with course.system.resources_fs.open(path("about") / section_key + ".html") as htmlFile: with course.system.resources_fs.open(path("about") / section_key + ".html") as htmlFile:
return htmlFile.read().decode('utf-8').format(COURSE_STATIC_URL = course_static_url(course) ) return htmlFile.read().decode('utf-8').format(COURSE_STATIC_URL=course_static_url(course))
except ResourceNotFoundError: except ResourceNotFoundError:
log.warning("Missing about section {key} in course {url}".format(key=section_key, url=course.location.url())) log.warning("Missing about section {key} in course {url}".format(key=section_key, url=course.location.url()))
return None return None
...@@ -91,6 +94,7 @@ def get_course_about_section(course, section_key): ...@@ -91,6 +94,7 @@ def get_course_about_section(course, section_key):
raise KeyError("Invalid about key " + str(section_key)) raise KeyError("Invalid about key " + str(section_key))
def get_course_info_section(course, section_key): def get_course_info_section(course, section_key):
""" """
This returns the snippet of html to be rendered on the course info page, given the key for the section. This returns the snippet of html to be rendered on the course info page, given the key for the section.
...@@ -111,7 +115,7 @@ def get_course_info_section(course, section_key): ...@@ -111,7 +115,7 @@ def get_course_info_section(course, section_key):
except ResourceNotFoundError: except ResourceNotFoundError:
log.exception("Missing info section {key} in course {url}".format(key=section_key, url=course.location.url())) log.exception("Missing info section {key} in course {url}".format(key=section_key, url=course.location.url()))
return "! Info section missing !" return "! Info section missing !"
raise KeyError("Invalid about key " + str(section_key)) raise KeyError("Invalid about key " + str(section_key))
\ No newline at end of file
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding model 'StudentModule' # Adding model 'StudentModule'
db.create_table('courseware_studentmodule', ( db.create_table('courseware_studentmodule', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
...@@ -24,16 +25,14 @@ class Migration(SchemaMigration): ...@@ -24,16 +25,14 @@ class Migration(SchemaMigration):
# Adding unique constraint on 'StudentModule', fields ['student', 'module_id', 'module_type'] # Adding unique constraint on 'StudentModule', fields ['student', 'module_id', 'module_type']
db.create_unique('courseware_studentmodule', ['student_id', 'module_id', 'module_type']) db.create_unique('courseware_studentmodule', ['student_id', 'module_id', 'module_type'])
def backwards(self, orm): def backwards(self, orm):
# Removing unique constraint on 'StudentModule', fields ['student', 'module_id', 'module_type'] # Removing unique constraint on 'StudentModule', fields ['student', 'module_id', 'module_type']
db.delete_unique('courseware_studentmodule', ['student_id', 'module_id', 'module_type']) db.delete_unique('courseware_studentmodule', ['student_id', 'module_id', 'module_type'])
# Deleting model 'StudentModule' # Deleting model 'StudentModule'
db.delete_table('courseware_studentmodule') db.delete_table('courseware_studentmodule')
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Adding index on 'StudentModule', fields ['created'] # Adding index on 'StudentModule', fields ['created']
db.create_index('courseware_studentmodule', ['created']) db.create_index('courseware_studentmodule', ['created'])
...@@ -23,9 +24,8 @@ class Migration(SchemaMigration): ...@@ -23,9 +24,8 @@ class Migration(SchemaMigration):
# Adding index on 'StudentModule', fields ['module_id'] # Adding index on 'StudentModule', fields ['module_id']
db.create_index('courseware_studentmodule', ['module_id']) db.create_index('courseware_studentmodule', ['module_id'])
def backwards(self, orm): def backwards(self, orm):
# Removing index on 'StudentModule', fields ['module_id'] # Removing index on 'StudentModule', fields ['module_id']
db.delete_index('courseware_studentmodule', ['module_id']) db.delete_index('courseware_studentmodule', ['module_id'])
...@@ -41,7 +41,6 @@ class Migration(SchemaMigration): ...@@ -41,7 +41,6 @@ class Migration(SchemaMigration):
# Removing index on 'StudentModule', fields ['created'] # Removing index on 'StudentModule', fields ['created']
db.delete_index('courseware_studentmodule', ['created']) db.delete_index('courseware_studentmodule', ['created'])
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -4,10 +4,11 @@ from south.db import db ...@@ -4,10 +4,11 @@ from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import models
class Migration(SchemaMigration): class Migration(SchemaMigration):
def forwards(self, orm): def forwards(self, orm):
# Removing unique constraint on 'StudentModule', fields ['module_id', 'module_type', 'student'] # Removing unique constraint on 'StudentModule', fields ['module_id', 'module_type', 'student']
db.delete_unique('courseware_studentmodule', ['module_id', 'module_type', 'student_id']) db.delete_unique('courseware_studentmodule', ['module_id', 'module_type', 'student_id'])
...@@ -20,9 +21,8 @@ class Migration(SchemaMigration): ...@@ -20,9 +21,8 @@ class Migration(SchemaMigration):
# Adding unique constraint on 'StudentModule', fields ['module_id', 'student'] # Adding unique constraint on 'StudentModule', fields ['module_id', 'student']
db.create_unique('courseware_studentmodule', ['module_id', 'student_id']) db.create_unique('courseware_studentmodule', ['module_id', 'student_id'])
def backwards(self, orm): def backwards(self, orm):
# Removing unique constraint on 'StudentModule', fields ['module_id', 'student'] # Removing unique constraint on 'StudentModule', fields ['module_id', 'student']
db.delete_unique('courseware_studentmodule', ['module_id', 'student_id']) db.delete_unique('courseware_studentmodule', ['module_id', 'student_id'])
...@@ -35,7 +35,6 @@ class Migration(SchemaMigration): ...@@ -35,7 +35,6 @@ class Migration(SchemaMigration):
# Adding unique constraint on 'StudentModule', fields ['module_id', 'module_type', 'student'] # Adding unique constraint on 'StudentModule', fields ['module_id', 'module_type', 'student']
db.create_unique('courseware_studentmodule', ['module_id', 'module_type', 'student_id']) db.create_unique('courseware_studentmodule', ['module_id', 'module_type', 'student_id'])
models = { models = {
'auth.group': { 'auth.group': {
'Meta': {'object_name': 'Group'}, 'Meta': {'object_name': 'Group'},
......
...@@ -63,7 +63,6 @@ class StudentModule(models.Model): ...@@ -63,7 +63,6 @@ class StudentModule(models.Model):
# TODO (cpennington): Remove these once the LMS switches to using XModuleDescriptors # TODO (cpennington): Remove these once the LMS switches to using XModuleDescriptors
class StudentModuleCache(object): class StudentModuleCache(object):
""" """
A cache of StudentModules for a specific student A cache of StudentModules for a specific student
...@@ -84,7 +83,7 @@ class StudentModuleCache(object): ...@@ -84,7 +83,7 @@ class StudentModuleCache(object):
# that can be put into a single query # that can be put into a single query
self.cache = [] self.cache = []
chunk_size = 500 chunk_size = 500
for id_chunk in [module_ids[i:i+chunk_size] for i in xrange(0, len(module_ids), chunk_size)]: for id_chunk in [module_ids[i:i + chunk_size] for i in xrange(0, len(module_ids), chunk_size)]:
self.cache.extend(StudentModule.objects.filter( self.cache.extend(StudentModule.objects.filter(
student=user, student=user,
module_state_key__in=id_chunk) module_state_key__in=id_chunk)
......
...@@ -36,7 +36,7 @@ class I4xSystem(object): ...@@ -36,7 +36,7 @@ class I4xSystem(object):
ajax_url - the url where ajax calls to the encapsulating module go. ajax_url - the url where ajax calls to the encapsulating module go.
xqueue_callback_url - the url where external queueing system (e.g. for grading) xqueue_callback_url - the url where external queueing system (e.g. for grading)
returns its response returns its response
track_function - function of (event_type, event), intended for logging track_function - function of (event_type, event), intended for logging
or otherwise tracking the event. or otherwise tracking the event.
TODO: Not used, and has inconsistent args in different TODO: Not used, and has inconsistent args in different
...@@ -278,7 +278,7 @@ def replace_static_urls(module, prefix): ...@@ -278,7 +278,7 @@ def replace_static_urls(module, prefix):
with urls that are /static/<prefix>/... with urls that are /static/<prefix>/...
""" """
original_get_html = module.get_html original_get_html = module.get_html
@wraps(original_get_html) @wraps(original_get_html)
def get_html(): def get_html():
return replace_urls(original_get_html(), staticfiles_prefix=prefix) return replace_urls(original_get_html(), staticfiles_prefix=prefix)
...@@ -308,9 +308,9 @@ def add_histogram(module): ...@@ -308,9 +308,9 @@ def add_histogram(module):
coursename = multicourse_settings.get_coursename_from_request(request) coursename = multicourse_settings.get_coursename_from_request(request)
github_url = multicourse_settings.get_course_github_url(coursename) github_url = multicourse_settings.get_course_github_url(coursename)
fn = module_xml.get('filename') fn = module_xml.get('filename')
if module_xml.tag=='problem': fn = 'problems/' + fn # grrr if module_xml.tag == 'problem': fn = 'problems/' + fn # grrr
edit_link = (github_url + '/tree/master/' + fn) if github_url is not None else None edit_link = (github_url + '/tree/master/' + fn) if github_url is not None else None
if module_xml.tag=='problem': edit_link += '.xml' # grrr if module_xml.tag == 'problem': edit_link += '.xml' # grrr
else: else:
edit_link = False edit_link = False
...@@ -328,13 +328,14 @@ def add_histogram(module): ...@@ -328,13 +328,14 @@ def add_histogram(module):
module.get_html = get_html module.get_html = get_html
return module return module
# TODO: TEMPORARY BYPASS OF AUTH! # TODO: TEMPORARY BYPASS OF AUTH!
@csrf_exempt @csrf_exempt
def xqueue_callback(request, userid, id, dispatch): def xqueue_callback(request, userid, id, dispatch):
# Parse xqueue response # Parse xqueue response
get = request.POST.copy() get = request.POST.copy()
try: try:
header = json.loads(get.pop('xqueue_header')[0]) # 'dict' header = json.loads(get.pop('xqueue_header')[0]) # 'dict'
except Exception as err: except Exception as err:
msg = "Error in xqueue_callback %s: Invalid return format" % err msg = "Error in xqueue_callback %s: Invalid return format" % err
raise Exception(msg) raise Exception(msg)
...@@ -344,12 +345,12 @@ def xqueue_callback(request, userid, id, dispatch): ...@@ -344,12 +345,12 @@ def xqueue_callback(request, userid, id, dispatch):
student_module_cache = StudentModuleCache(user, modulestore().get_item(id)) student_module_cache = StudentModuleCache(user, modulestore().get_item(id))
instance, instance_module, shared_module, module_type = get_module(request.user, request, id, student_module_cache) instance, instance_module, shared_module, module_type = get_module(request.user, request, id, student_module_cache)
if instance_module is None: if instance_module is None:
log.debug("Couldn't find module '%s' for user '%s'", log.debug("Couldn't find module '%s' for user '%s'",
id, request.user) id, request.user)
raise Http404 raise Http404
oldgrade = instance_module.grade oldgrade = instance_module.grade
old_instance_state = instance_module.state old_instance_state = instance_module.state
...@@ -360,7 +361,7 @@ def xqueue_callback(request, userid, id, dispatch): ...@@ -360,7 +361,7 @@ def xqueue_callback(request, userid, id, dispatch):
# We go through the "AJAX" path # We go through the "AJAX" path
# So far, the only dispatch from xqueue will be 'score_update' # So far, the only dispatch from xqueue will be 'score_update'
try: try:
ajax_return = instance.handle_ajax(dispatch, get) # Can ignore the "ajax" return in 'xqueue_callback' ajax_return = instance.handle_ajax(dispatch, get) # Can ignore the "ajax" return in 'xqueue_callback'
except: except:
log.exception("error processing ajax call") log.exception("error processing ajax call")
raise raise
...@@ -374,6 +375,7 @@ def xqueue_callback(request, userid, id, dispatch): ...@@ -374,6 +375,7 @@ def xqueue_callback(request, userid, id, dispatch):
return HttpResponse("") return HttpResponse("")
def modx_dispatch(request, dispatch=None, id=None): def modx_dispatch(request, dispatch=None, id=None):
''' Generic view for extensions. This is where AJAX calls go. ''' Generic view for extensions. This is where AJAX calls go.
...@@ -392,7 +394,7 @@ def modx_dispatch(request, dispatch=None, id=None): ...@@ -392,7 +394,7 @@ def modx_dispatch(request, dispatch=None, id=None):
student_module_cache = StudentModuleCache(request.user, modulestore().get_item(id)) student_module_cache = StudentModuleCache(request.user, modulestore().get_item(id))
instance, instance_module, shared_module, module_type = get_module(request.user, request, id, student_module_cache) instance, instance_module, shared_module, module_type = get_module(request.user, request, id, student_module_cache)
if instance_module is None: if instance_module is None:
log.debug("Couldn't find module '%s' for user '%s'", log.debug("Couldn't find module '%s' for user '%s'",
id, request.user) id, request.user)
......
class completion(object): class completion(object):
def __init__(self, **d): def __init__(self, **d):
self.dict = dict({'duration_total':0, self.dict = dict({'duration_total': 0,
'duration_watched':0, 'duration_watched': 0,
'done':True, 'done': True,
'questions_correct':0, 'questions_correct': 0,
'questions_incorrect':0, 'questions_incorrect': 0,
'questions_total':0}) 'questions_total': 0})
if d: if d:
self.dict.update(d) self.dict.update(d)
def __getitem__(self, key): def __getitem__(self, key):
...@@ -23,7 +23,7 @@ class completion(object): ...@@ -23,7 +23,7 @@ class completion(object):
'questions_correct', 'questions_correct',
'questions_incorrect', 'questions_incorrect',
'questions_total']: 'questions_total']:
result[item] = result[item]+other.dict[item] result[item] = result[item] + other.dict[item]
return completion(**result) return completion(**result)
def __contains__(self, key): def __contains__(self, key):
...@@ -33,6 +33,6 @@ class completion(object): ...@@ -33,6 +33,6 @@ class completion(object):
return repr(self.dict) return repr(self.dict)
if __name__ == '__main__': if __name__ == '__main__':
dict1=completion(duration_total=5) dict1 = completion(duration_total=5)
dict2=completion(duration_total=7) dict2 = completion(duration_total=7)
print dict1+dict2 print dict1 + dict2
...@@ -31,6 +31,7 @@ log = logging.getLogger("mitx.courseware") ...@@ -31,6 +31,7 @@ log = logging.getLogger("mitx.courseware")
template_imports = {'urllib': urllib} template_imports = {'urllib': urllib}
def user_groups(user): def user_groups(user):
if not user.is_authenticated(): if not user.is_authenticated():
return [] return []
...@@ -62,15 +63,15 @@ def courses(request): ...@@ -62,15 +63,15 @@ def courses(request):
for course in courses: for course in courses:
universities[course.org].append(course) universities[course.org].append(course)
return render_to_response("courses.html", { 'universities': universities }) return render_to_response("courses.html", {'universities': universities})
@cache_control(no_cache=True, no_store=True, must_revalidate=True) @cache_control(no_cache=True, no_store=True, must_revalidate=True)
def gradebook(request, course_id): def gradebook(request, course_id):
if 'course_admin' not in user_groups(request.user): if 'course_admin' not in user_groups(request.user):
raise Http404 raise Http404
course = check_course(course_id) course = check_course(course_id)
student_objects = User.objects.all()[:100] student_objects = User.objects.all()[:100]
student_info = [] student_info = []
...@@ -168,7 +169,7 @@ def index(request, course_id, chapter=None, section=None, ...@@ -168,7 +169,7 @@ def index(request, course_id, chapter=None, section=None,
- HTTPresponse - HTTPresponse
''' '''
course = check_course(course_id) course = check_course(course_id)
def clean(s): def clean(s):
''' Fixes URLs -- we convert spaces to _ in URLs to prevent ''' Fixes URLs -- we convert spaces to _ in URLs to prevent
funny encoding characters and keep the URLs readable. This undoes funny encoding characters and keep the URLs readable. This undoes
...@@ -258,18 +259,18 @@ def course_info(request, course_id): ...@@ -258,18 +259,18 @@ def course_info(request, course_id):
return render_to_response('info.html', {'course': course}) return render_to_response('info.html', {'course': course})
@ensure_csrf_cookie @ensure_csrf_cookie
@cache_if_anonymous @cache_if_anonymous
def course_about(request, course_id): def course_about(request, course_id):
def registered_for_course(course, user): def registered_for_course(course, user):
if user.is_authenticated(): if user.is_authenticated():
return CourseEnrollment.objects.filter(user = user, course_id=course.id).exists() return CourseEnrollment.objects.filter(user=user, course_id=course.id).exists()
else: else:
return False return False
course = check_course(course_id, course_must_be_open=False) course = check_course(course_id, course_must_be_open=False)
registered = registered_for_course(course, request.user) registered = registered_for_course(course, request.user)
return render_to_response('portal/course_about.html', {'course': course, 'registered': registered}) return render_to_response('portal/course_about.html', {'course': course, 'registered': registered})
@ensure_csrf_cookie @ensure_csrf_cookie
...@@ -281,7 +282,7 @@ def university_profile(request, org_id): ...@@ -281,7 +282,7 @@ def university_profile(request, org_id):
raise Http404("University Profile not found for {0}".format(org_id)) raise Http404("University Profile not found for {0}".format(org_id))
# Only grab courses for this org... # Only grab courses for this org...
courses=[c for c in all_courses if c.org == org_id] courses = [c for c in all_courses if c.org == org_id]
context = dict(courses=courses, org_id=org_id) context = dict(courses=courses, org_id=org_id)
template_file = "university_profile/{0}.html".format(org_id).lower() template_file = "university_profile/{0}.html".format(org_id).lower()
......
...@@ -2,6 +2,7 @@ import json ...@@ -2,6 +2,7 @@ import json
from datetime import datetime from datetime import datetime
from django.http import HttpResponse from django.http import HttpResponse
def heartbeat(request): def heartbeat(request):
""" """
Simple view that a loadbalancer can check to verify that the app is up Simple view that a loadbalancer can check to verify that the app is up
......
...@@ -25,18 +25,18 @@ from django.conf import settings ...@@ -25,18 +25,18 @@ from django.conf import settings
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# load course settings # load course settings
if hasattr(settings,'COURSE_SETTINGS'): # in the future, this could be replaced by reading an XML file if hasattr(settings, 'COURSE_SETTINGS'): # in the future, this could be replaced by reading an XML file
COURSE_SETTINGS = settings.COURSE_SETTINGS COURSE_SETTINGS = settings.COURSE_SETTINGS
elif hasattr(settings,'COURSE_NAME'): # backward compatibility elif hasattr(settings, 'COURSE_NAME'): # backward compatibility
COURSE_SETTINGS = {settings.COURSE_NAME: {'number': settings.COURSE_NUMBER, COURSE_SETTINGS = {settings.COURSE_NAME: {'number': settings.COURSE_NUMBER,
'title': settings.COURSE_TITLE, 'title': settings.COURSE_TITLE,
'location': settings.COURSE_LOCATION, 'location': settings.COURSE_LOCATION,
}, },
} }
else: # default to 6.002_Spring_2012 else: # default to 6.002_Spring_2012
COURSE_SETTINGS = {'6.002_Spring_2012': {'number': '6.002x', COURSE_SETTINGS = {'6.002_Spring_2012': {'number': '6.002x',
'title': 'Circuits and Electronics', 'title': 'Circuits and Electronics',
'location': 'i4x://edx/6002xs12/course/6.002 Spring 2012', 'location': 'i4x://edx/6002xs12/course/6.002 Spring 2012',
}, },
} }
...@@ -44,6 +44,7 @@ else: # default to 6.002_Spring_2012 ...@@ -44,6 +44,7 @@ else: # default to 6.002_Spring_2012
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# wrapper functions around course settings # wrapper functions around course settings
def get_coursename_from_request(request): def get_coursename_from_request(request):
if 'coursename' in request.session: if 'coursename' in request.session:
coursename = request.session['coursename'] coursename = request.session['coursename']
...@@ -51,6 +52,7 @@ def get_coursename_from_request(request): ...@@ -51,6 +52,7 @@ def get_coursename_from_request(request):
else: coursename = None else: coursename = None
return coursename return coursename
def get_course_settings(coursename): def get_course_settings(coursename):
if not coursename: if not coursename:
if hasattr(settings, 'COURSE_DEFAULT'): if hasattr(settings, 'COURSE_DEFAULT'):
...@@ -94,14 +96,18 @@ def get_course_title(coursename): ...@@ -94,14 +96,18 @@ def get_course_title(coursename):
def get_course_number(coursename): def get_course_number(coursename):
return get_course_property(coursename, 'number') return get_course_property(coursename, 'number')
def get_course_github_url(coursename): def get_course_github_url(coursename):
return get_course_property(coursename,'github_url') return get_course_property(coursename, 'github_url')
def get_course_default_chapter(coursename): def get_course_default_chapter(coursename):
return get_course_property(coursename,'default_chapter') return get_course_property(coursename, 'default_chapter')
def get_course_default_section(coursename): def get_course_default_section(coursename):
return get_course_property(coursename,'default_section') return get_course_property(coursename, 'default_section')
def get_course_location(coursename): def get_course_location(coursename):
return get_course_property(coursename, 'location') return get_course_property(coursename, 'location')
...@@ -3,12 +3,12 @@ from mitxmako.shortcuts import render_to_response ...@@ -3,12 +3,12 @@ from mitxmako.shortcuts import render_to_response
from multicourse import multicourse_settings from multicourse import multicourse_settings
def mitxhome(request): def mitxhome(request):
''' Home page (link from main header). List of courses. ''' ''' Home page (link from main header). List of courses. '''
if settings.DEBUG: if settings.DEBUG:
print "[djangoapps.multicourse.mitxhome] MITX_ROOT_URL = " + settings.MITX_ROOT_URL print "[djangoapps.multicourse.mitxhome] MITX_ROOT_URL = " + settings.MITX_ROOT_URL
if settings.ENABLE_MULTICOURSE: if settings.ENABLE_MULTICOURSE:
context = {'courseinfo' : multicourse_settings.COURSE_SETTINGS} context = {'courseinfo': multicourse_settings.COURSE_SETTINGS}
return render_to_response("mitxhome.html", context) return render_to_response("mitxhome.html", context)
return info(request) return info(request)
# Source: django-simplewiki. GPL license. # Source: django-simplewiki. GPL license.
import os import os
import sys import sys
......
# Source: django-simplewiki. GPL license. # Source: django-simplewiki. GPL license.
from django import forms from django import forms
from django.contrib import admin from django.contrib import admin
...@@ -6,17 +6,21 @@ from django.utils.translation import ugettext as _ ...@@ -6,17 +6,21 @@ from django.utils.translation import ugettext as _
from models import Article, Revision, Permission, ArticleAttachment from models import Article, Revision, Permission, ArticleAttachment
class RevisionInline(admin.TabularInline): class RevisionInline(admin.TabularInline):
model = Revision model = Revision
extra = 1 extra = 1
class RevisionAdmin(admin.ModelAdmin): class RevisionAdmin(admin.ModelAdmin):
list_display = ('article', '__unicode__', 'revision_date', 'revision_user', 'revision_text') list_display = ('article', '__unicode__', 'revision_date', 'revision_user', 'revision_text')
search_fields = ('article', 'counter') search_fields = ('article', 'counter')
class AttachmentAdmin(admin.ModelAdmin): class AttachmentAdmin(admin.ModelAdmin):
list_display = ('article', '__unicode__', 'uploaded_on', 'uploaded_by') list_display = ('article', '__unicode__', 'uploaded_on', 'uploaded_by')
class ArticleAdminForm(forms.ModelForm): class ArticleAdminForm(forms.ModelForm):
def clean(self): def clean(self):
cleaned_data = self.cleaned_data cleaned_data = self.cleaned_data
...@@ -30,16 +34,19 @@ class ArticleAdminForm(forms.ModelForm): ...@@ -30,16 +34,19 @@ class ArticleAdminForm(forms.ModelForm):
raise forms.ValidationError(_('Article slug and parent must be ' raise forms.ValidationError(_('Article slug and parent must be '
'unique together.')) 'unique together.'))
return cleaned_data return cleaned_data
class Meta: class Meta:
model = Article model = Article
class ArticleAdmin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin):
list_display = ('created_by', 'slug', 'modified_on', 'namespace') list_display = ('created_by', 'slug', 'modified_on', 'namespace')
search_fields = ('slug',) search_fields = ('slug',)
prepopulated_fields = {'slug': ('title',) } prepopulated_fields = {'slug': ('title',)}
inlines = [RevisionInline] inlines = [RevisionInline]
form = ArticleAdminForm form = ArticleAdminForm
save_on_top = True save_on_top = True
def formfield_for_foreignkey(self, db_field, request, **kwargs): def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'current_revision': if db_field.name == 'current_revision':
# Try to determine the id of the article being edited # Try to determine the id of the article being edited
...@@ -53,6 +60,7 @@ class ArticleAdmin(admin.ModelAdmin): ...@@ -53,6 +60,7 @@ class ArticleAdmin(admin.ModelAdmin):
return db_field.formfield(**kwargs) return db_field.formfield(**kwargs)
return super(ArticleAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) return super(ArticleAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
class PermissionAdmin(admin.ModelAdmin): class PermissionAdmin(admin.ModelAdmin):
search_fields = ('article', 'counter') search_fields = ('article', 'counter')
......
...@@ -26,19 +26,19 @@ try: ...@@ -26,19 +26,19 @@ try:
except: except:
from markdown import etree from markdown import etree
class CircuitExtension(markdown.Extension): class CircuitExtension(markdown.Extension):
def __init__(self, configs): def __init__(self, configs):
for key, value in configs : for key, value in configs:
self.setConfig(key, value) self.setConfig(key, value)
def extendMarkdown(self, md, md_globals): def extendMarkdown(self, md, md_globals):
## Because Markdown treats contigous lines as one block of text, it is hard to match ## Because Markdown treats contigous lines as one block of text, it is hard to match
## a regex that must occupy the whole line (like the circuit regex). This is why we have ## a regex that must occupy the whole line (like the circuit regex). This is why we have
## a preprocessor that inspects the lines and replaces the matched lines with text that is ## a preprocessor that inspects the lines and replaces the matched lines with text that is
## easier to match ## easier to match
md.preprocessors.add('circuit', CircuitPreprocessor(md), "_begin") md.preprocessors.add('circuit', CircuitPreprocessor(md), "_begin")
pattern = CircuitLink(r'processed-schematic:(?P<data>.*?)processed-schematic-end') pattern = CircuitLink(r'processed-schematic:(?P<data>.*?)processed-schematic-end')
pattern.md = md pattern.md = md
pattern.ext = self pattern.ext = self
...@@ -47,16 +47,16 @@ class CircuitExtension(markdown.Extension): ...@@ -47,16 +47,16 @@ class CircuitExtension(markdown.Extension):
class CircuitPreprocessor(markdown.preprocessors.Preprocessor): class CircuitPreprocessor(markdown.preprocessors.Preprocessor):
preRegex = re.compile(r'^circuit-schematic:(?P<data>.*)$') preRegex = re.compile(r'^circuit-schematic:(?P<data>.*)$')
def run(self, lines): def run(self, lines):
def convertLine(line): def convertLine(line):
m = self.preRegex.match(line) m = self.preRegex.match(line)
if m: if m:
return 'processed-schematic:{0}processed-schematic-end'.format( m.group('data') ) return 'processed-schematic:{0}processed-schematic-end'.format(m.group('data'))
else: else:
return line return line
return [ convertLine(line) for line in lines ] return [convertLine(line) for line in lines]
class CircuitLink(markdown.inlinepatterns.Pattern): class CircuitLink(markdown.inlinepatterns.Pattern):
...@@ -64,9 +64,9 @@ class CircuitLink(markdown.inlinepatterns.Pattern): ...@@ -64,9 +64,9 @@ class CircuitLink(markdown.inlinepatterns.Pattern):
data = m.group('data') data = m.group('data')
data = escape(data) data = escape(data)
return etree.fromstring("<div align='center'><input type='hidden' parts='' value='" + data + "' analyses='' class='schematic ctrls' width='640' height='480'/></div>") return etree.fromstring("<div align='center'><input type='hidden' parts='' value='" + data + "' analyses='' class='schematic ctrls' width='640' height='480'/></div>")
def makeExtension(configs=None): def makeExtension(configs=None):
to_return = CircuitExtension(configs=configs) to_return = CircuitExtension(configs=configs)
print "circuit returning " , to_return print "circuit returning ", to_return
return to_return return to_return
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment