Commit aed01a66 by Ibrahim Awwal

Define APIModel.to_json() for classes to override to post custom JSON that isn't…

Define APIModel.to_json() for classes to override to post custom JSON that isn't just {attribute:value}
parent d43d35ff
...@@ -46,10 +46,13 @@ class APIModel(object): ...@@ -46,10 +46,13 @@ class APIModel(object):
if attribute in kwargs: if attribute in kwargs:
setattr(self, attribute, kwargs[attribute]) setattr(self, attribute, kwargs[attribute])
def to_json(self):
attributes = dict([(key, getattr(self,key, None)) for key in self.__attributes__ if hasattr(self, key)])
return json.dumps({self.json_root:attributes})
def save(self): def save(self):
# TODO: Think of a better way to handle nested resources, currently you have to manually set __base_url__ # TODO: Think of a better way to handle nested resources, currently you have to manually set __base_url__
attributes = dict([(key, getattr(self,key, None)) for key in self.__attributes__ if hasattr(self, key)]) params = self.to_json()
params = json.dumps({self.json_root:attributes})
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
post_url = slumber.url_join(HOST, self.url()) post_url = slumber.url_join(HOST, self.url())
if self.id: # This object was retrieved from the service or otherwise persisted if self.id: # This object was retrieved from the service or otherwise persisted
...@@ -146,8 +149,13 @@ class Submission(APIModel): ...@@ -146,8 +149,13 @@ class Submission(APIModel):
return slumber.url_join('questions', self.question_id, 'submissions') return slumber.url_join('questions', self.question_id, 'submissions')
@staticmethod @staticmethod
def get_by_question_id_and_id(question_id, id): def get_by_question_id_and_id(question_id, _id):
return Submission(**API.questions(question_id).submissions(id).get()) return Submission(**API.questions(question_id).submissions(_id).get())
@staticmethod
def get_by_id(_id):
return Submission(**API.submissions(_id).get())
class Rubric(APIModel): class Rubric(APIModel):
__attributes__ = ['rubric_type', 'title', 'total_points', 'published'] __attributes__ = ['rubric_type', 'title', 'total_points', 'published']
__slots__ = ['rubric_type', 'title', 'total_points', 'published', 'entries'] __slots__ = ['rubric_type', 'title', 'total_points', 'published', 'entries']
...@@ -177,7 +185,7 @@ class Rubric(APIModel): ...@@ -177,7 +185,7 @@ class Rubric(APIModel):
self.entries.append(entry) self.entries.append(entry)
return entry return entry
def create_evaluation(self, user_id, submission_id, entry_values): def create_evaluation(self, user_id, question_id, submission_id, entry_values):
# TODO: When async API is implemented, entries should be created in a callback # TODO: When async API is implemented, entries should be created in a callback
evaluation = Evaluation(rubric_id=self.id, user_id=user_id, submission_id=submission_id) evaluation = Evaluation(rubric_id=self.id, user_id=user_id, submission_id=submission_id)
evaluation.save() evaluation.save()
...@@ -270,11 +278,27 @@ class Example(APIModel): ...@@ -270,11 +278,27 @@ class Example(APIModel):
__base_url__ = 'examples' __base_url__ = 'examples'
class Evaluation(APIModel): class Evaluation(APIModel):
__attributes__ = ['rubric_id', 'user_id', 'submission_id', 'comments', 'offset'] __attributes__ = ['rubric_id', 'user_id', 'submission_id', 'comments', 'offset', 'question_id']
__slots__ = __attributes__ __slots__ = __attributes__ + ['entries'] # this is an ugly hack
# TODO: Build some sort of association with entry values? __base_url = 'evaluations'
def url(self):
if self.id:
return slumber.url_join('questions', self.question_id, 'submissions', self.submission_id, 'evaluations')
else:
return slumber.url_join('questions', self.question_id, 'submissions', self.submission_id, 'evaluations', self.id)
def add_entry(self):
if self.entries is None:
self.entries = {}
def to_json(self):
attributes = dict([(key, getattr(self,key, None)) for key in self.__attributes__ if hasattr(self, key)])
attributes.pop('question_id', None) # Remove question_id from params
return json.dumps({self.json_root:attributes})
class RubricEntryValue(APIModel): class RubricEntryValue(APIModel):
"""
This is weird, because you have to set the entries as a child of the evaluation
but there's no way to access them individually. Maybe this shouldn't be a class.
"""
__attributes__ = ['rubric_entry_id', 'evaluation_id', 'present'] __attributes__ = ['rubric_entry_id', 'evaluation_id', 'present']
__slots__ = __attributes__ __slots__ = __attributes__
@property @property
......
...@@ -69,7 +69,7 @@ instructor_evals = [] ...@@ -69,7 +69,7 @@ instructor_evals = []
for question in questions['local']: for question in questions['local']:
for submission in local_submissions[question][:5]: for submission in local_submissions[question][:5]:
entries_dict = { entry.id:value for entry, value in zip(rubric.entries, local_true_scores[submission.id]) } entries_dict = { entry.id:value for entry, value in zip(rubric.entries, local_true_scores[submission.id]) }
evaluation = rubric.create_evaluation(user_id=inst1.id, submission_id=submission.id, entry_values=entries_dict) evaluation = rubric.create_evaluation(user_id=inst1.id, question_id=question.id, submission_id=submission.id, entry_values=entries_dict)
#evaluation.save() #evaluation.save()
instructor_evals.append(evaluation) instructor_evals.append(evaluation)
......
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