This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmodels.py
165 lines (121 loc) · 4.91 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import unicode_literals
from django.db import models
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
import policies
from django.http import Http404
class Environment(models.Model):
name = models.CharField(max_length=200,default='')
def __unicode__(self):
return "{} {}: {}".format(self.__class__.__name__, self.pk, self.name)
class Mooclet(models.Model):
name = models.CharField(max_length=100,default='')
policy = models.ForeignKey('Policy',blank=True,null=True)
environment = models.ForeignKey(Environment)
mooclet_id = models.PositiveIntegerField(blank=True)
class Meta:
unique_together = ('environment','mooclet_id')
def __unicode__(self):
return "{}: {}".format(self.__class__.__name__, self.name)
def run(self, policy=None, context={}):
context['mooclet'] = self
if not self.version_set.exists():
raise Http404('mooclet has no versions')
if not policy:
if self.policy:
policy = self.policy
else:
# print 'no policy found'
raise Http404('no policy found')
version = policy.run_policy(context)
return version
class Version(models.Model):
'''
Mooclet version
'''
name = models.CharField(max_length=200,default='')
mooclet = models.ForeignKey(Mooclet)
text = models.TextField(blank=True,default='')
version_id = models.PositiveIntegerField(blank=True)
# mooclet_version_id = models.PositiveIntegerField(blank=True)
# @property
# def environment(self):
# return self.mooclet.environment.pk
# class Meta:
# unique_together = ('environment','version_id')
def __unicode__(self):
return "{} {}: {}".format(self.__class__.__name__, self.pk, self.name)
# class MoocletPolicyState(models.Model):
# pass
class Learner(models.Model):
name = models.CharField(max_length=100)
environment = models.ForeignKey(Environment)
learner_id = models.PositiveIntegerField(blank=True)
class Meta:
unique_together = ('environment','learner_id')
class Variable(models.Model):
name = models.CharField(max_length=100)
environment = models.ForeignKey(Environment)
variable_id = models.PositiveIntegerField(blank=True)
def __unicode__(self):
return "{} {}: {}".format(self.__class__.__name__, self.pk, self.name)
def get_data(self,context=None):
'''
return relevant value objects for the variable type
'''
return self.value_set.all()
def get_data_dicts(self,context=None):
'''
return relevant values for the variable type, as a list of dicts
'''
return self.get_data(context).values()
class Value(models.Model):
'''
user variable observation, can be associated with either course, mooclet, or mooclet version
examples of user variables:
course-level: general student characteristics
quiz-level: number of attempts
mooclet: ?
version: student rating of an explanation, instructors prior judgement
'''
variable = models.ForeignKey(Variable)
learner = models.ForeignKey(Learner,null=True,blank=True)
mooclet = models.ForeignKey(Mooclet,null=True,blank=True)
version = models.ForeignKey(Version,null=True,blank=True)
policy = models.ForeignKey('Policy',null=True,blank=True)
value = models.FloatField(blank=True,null=True)
text = models.TextField(blank=True,default='')
timestamp = models.DateTimeField(null=True,auto_now=True)
# value_id = models.PositiveIntegerField(blank=True)
# class Meta:
# unique_together = ('environment','value_id')
# @property
# def environment(self):
# return self.variable.environment
class Policy(models.Model):
name = models.CharField(max_length=100)
environment = models.ForeignKey(Environment,null=True,blank=True,default=None)
policy_id = models.PositiveIntegerField(blank=True)
# variables = models.ManyToManyField('Variable') # might use this for persistent "state variables"?
class Meta:
verbose_name_plural = 'policies'
unique_together = ('environment','policy_id')
def __unicode__(self):
return self.name
def get_policy_function(self):
try:
return getattr(policies, self.name)
except:
print "policy function matching specified name not found"
# TODO look through custom user-provided functions
return None
def get_variables(self):
# TODO implement returning all, subsets, etc.
# return self.variables.all()
return Variable.objects.all()
def run_policy(self, context):
# insert all version ids here?
policy_function = self.get_policy_function()
variables = self.get_variables()
version_id = policy_function(variables,context)
return version_id