diff --git a/courseware/content_parser.py b/courseware/content_parser.py index 0954f60489..190a63fba7 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -146,11 +146,11 @@ def propogate_downward_tag(element, attribute_name, parent_attribute = None): def user_groups(user): # TODO: Rewrite in Django key = 'user_group_names_{user.id}'.format(user=user) - cache_expiration = 60 * 60 * 4 # four hours - group_names = cache.get(key) + cache_expiration = 60 * 60 # one hour + group_names = cache.get(fasthash(key)) if group_names is None: group_names = [u.name for u in UserTestGroup.objects.filter(users=user)] - cache.set(key, group_names, cache_expiration) + cache.set(fasthash(key), group_names, cache_expiration) return group_names @@ -176,12 +176,12 @@ def course_file(user): cache_key = filename + "_processed?dev_content:" + str(options['dev_content']) + "&groups:" + str(sorted(groups)) - tree_string = cache.get(cache_key) + tree_string = cache.get(fasthash(cache_key)) if not tree_string: tree = course_xml_process(etree.XML(render_to_string(filename, options, namespace = 'course'))) tree_string = etree.tostring(tree) - cache.set(cache_key, tree_string, 60) + cache.set(fasthash(cache_key), tree_string, 60) else: tree = etree.XML(tree_string) diff --git a/student/management/__init__.py b/student/management/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/student/management/commands/assigngroups.py b/student/management/commands/assigngroups.py new file mode 100644 index 0000000000..150905ab7e --- /dev/null +++ b/student/management/commands/assigngroups.py @@ -0,0 +1,86 @@ +import os.path + +from lxml import etree + +from django.core.management.base import BaseCommand +from django.conf import settings +from django.contrib.auth.models import User + +import mitxmako.middleware as middleware +from student.models import UserTestGroup + +import random +import sys +import datetime + +import json + +middleware.MakoMiddleware() + +def group_from_value(groups, v): + ''' 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 + 'a' if v<0.3, 'b' if 0.3<=v<0.7, and 'c' if v>0.7 +''' + sum = 0 + for (g,p) in groups: + sum = sum + p + if sum > v: + return g + return g # For round-off errors + +class Command(BaseCommand): + help = \ +''' Assign users to test groups. Takes a list +of groups: +a:0.3,b:0.4,c:0.3 file.txt "Testing something" +Will assign each user to group a, b, or c with +probability 0.3, 0.4, 0.3. Probabilities must +add up to 1. + +Will log what happened to file.txt. +''' + def handle(self, *args, **options): + if len(args) != 3: + print "Invalid number of options" + sys.exit(-1) + + # Extract groups from string + group_strs = [x.split(':') for x in args[0].split(',')] + groups = [(group,float(value)) for group,value in group_strs] + print "Groups", groups + + ## Confirm group probabilities add up to 1 + total = sum(zip(*groups)[1]) + print "Total:", total + if abs(total-1)>0.01: + print "Total not 1" + sys.exit(-1) + + ## Confirm groups don't already exist + for group in dict(groups): + if UserTestGroup.objects.filter(name=group).count() != 0: + print group, "already exists!" + sys.exit(-1) + + group_objects = {} + + ## Create groups + for group in dict(groups): + utg = UserTestGroup() + utg.name=group + utg.description = json.dumps({"description":args[2]}, + {"time":datetime.datetime.utcnow().isoformat()}) + group_objects[group]=utg + group_objects[group].save() + + ## Assign groups + users = list(User.objects.all()) + for user in users: + v = random.uniform(0,1) + group = group_from_value(groups,v) + group_objects[group].users.add(user) + + ## Save groups + for group in group_objects: + group_objects[group].save() diff --git a/student/management/commands/massemail.py b/student/management/commands/massemail.py new file mode 100644 index 0000000000..306ae51c0e --- /dev/null +++ b/student/management/commands/massemail.py @@ -0,0 +1,27 @@ +import os.path + +from lxml import etree + +from django.core.management.base import BaseCommand +from django.conf import settings +from django.contrib.auth.models import User + +import mitxmako.middleware as middleware + +middleware.MakoMiddleware() + +class Command(BaseCommand): + help = \ +'''Sends an e-mail to all users. Takes a single +parameter -- name of e-mail template -- located +in templates/email. Adds a .txt for the message +body, and an _subject.txt for the subject. ''' + def handle(self, *args, **options): + #text = open(args[0]).read() + #subject = open(args[1]).read() + users = User.objects.all() + text = middleware.lookup['main'].get_template('email/'+args[0]+".txt").render() + subject = middleware.lookup['main'].get_template('email/'+args[0]+"_subject.txt").render().strip() + for user in users: + if user.is_active: + user.email_user(subject, text)