# Path: mentoring/light_children.py
# class LightChild(Plugin, LightChildrenMixin):
#     """
#     Base class for the light children
#     """
#     entry_point = 'xblock.light_children'
# 
#     def __init__(self, parent):
#         self.parent = parent
#         self.xblock_container = parent.xblock_container
# 
#     @property
#     def runtime(self):
#         return self.parent.runtime
# 
#     @property
#     def xmodule_runtime(self):
#         try:
#             xmodule_runtime = self.parent.xmodule_runtime
#         except AttributeError:
#             # TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
#             class xmodule_runtime(object):
#                 course_id = 'sample-course'
#                 anonymous_student_id = 'student1'
#             xmodule_runtime = xmodule_runtime()
#         return xmodule_runtime
# 
#     def save(self):
#         pass
# 
# class Boolean(LightChildField):
#     pass
# 
# class Scope(object):
#     content = None
#     user_state = None
# 
# class String(LightChildField):
#     def __init__(self, *args, **kwargs):
#         self.value = kwargs.get('default', '') or ''
# 
#     def __str__(self):
#         return self.value
# 
#     def split(self, *args, **kwargs):
#         return self.value.split(*args, **kwargs)
# 
# Path: mentoring/models.py
# class Answer(models.Model):
#     """
#     Django model used to store AnswerBlock data that need to be shared
#     and queried accross XBlock instances (workaround).
#     """
# 
#     class Meta:
#         app_label = 'mentoring'
#         unique_together = (('student_id', 'course_id', 'name'),)
# 
#     name = models.CharField(max_length=50, db_index=True)
#     student_id = models.CharField(max_length=32, db_index=True)
#     course_id = models.CharField(max_length=50, db_index=True)
#     student_input = models.TextField(blank=True, default='')
#     created_on = models.DateTimeField('created on', auto_now_add=True)
#     modified_on = models.DateTimeField('modified on', auto_now=True)
# 
#     def save(self, *args, **kwargs):
#         # Force validation of max_length
#         self.full_clean()
#         super(Answer, self).save(*args, **kwargs)
# 
# Path: mentoring/utils.py
# def render_template(template_path, context={}):
#     """
#     Evaluate a template by resource path, applying the provided context
#     """
#     template_str = load_resource(template_path)
#     template = Template(template_str)
#     return template.render(Context(context))


import logging
from lazy import lazy
from xblock.fragment import Fragment
from .light_children import LightChild, Boolean, Scope, String
from .models import Answer
from .utils import render_template
        return bool(self.read_only or self.student_input)

    def save(self):
        """
        Replicate data changes on the related Django model used for sharing of data accross XBlocks
        """
        super(AnswerBlock, self).save()

        # Only attempt to locate a model object for this block when the answer has a name
        if self.name:
            answer_data = self.get_model_object()
            if answer_data.student_input != self.student_input and not self.read_only:
                answer_data.student_input = self.student_input
                answer_data.save()

    def get_model_object(self, name=None):
        """
        Fetches the Answer model object for the answer named `name`
        """
        # By default, get the model object for the current answer's name
        if not name:
            name = self.name
        # Consistency check - we should have a name by now
        if not name:
            raise ValueError, 'AnswerBlock.name field need to be set to a non-null/empty value'

        # TODO: Why do we need to use `xmodule_runtime` and not `runtime`?
        student_id = self.xmodule_runtime.anonymous_student_id
        course_id = self.xmodule_runtime.course_id


# Path: mentoring/light_children.py
# class LightChild(Plugin, LightChildrenMixin):
#     """
#     Base class for the light children
#     """
#     entry_point = 'xblock.light_children'
# 
#     def __init__(self, parent):
#         self.parent = parent
#         self.xblock_container = parent.xblock_container
# 
#     @property
#     def runtime(self):
#         return self.parent.runtime
# 
#     @property
#     def xmodule_runtime(self):
#         try:
#             xmodule_runtime = self.parent.xmodule_runtime
#         except AttributeError:
#             # TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
#             class xmodule_runtime(object):
#                 course_id = 'sample-course'
#                 anonymous_student_id = 'student1'
#             xmodule_runtime = xmodule_runtime()
#         return xmodule_runtime
# 
#     def save(self):
#         pass
# 
# class Boolean(LightChildField):
#     pass
# 
# class Scope(object):
#     content = None
#     user_state = None
# 
# class String(LightChildField):
#     def __init__(self, *args, **kwargs):
#         self.value = kwargs.get('default', '') or ''
# 
#     def __str__(self):
#         return self.value
# 
#     def split(self, *args, **kwargs):
#         return self.value.split(*args, **kwargs)
# 
# Path: mentoring/models.py
# class Answer(models.Model):
#     """
#     Django model used to store AnswerBlock data that need to be shared
#     and queried accross XBlock instances (workaround).
#     """
# 
#     class Meta:
#         app_label = 'mentoring'
#         unique_together = (('student_id', 'course_id', 'name'),)
# 
#     name = models.CharField(max_length=50, db_index=True)
#     student_id = models.CharField(max_length=32, db_index=True)
#     course_id = models.CharField(max_length=50, db_index=True)
#     student_input = models.TextField(blank=True, default='')
#     created_on = models.DateTimeField('created on', auto_now_add=True)
#     modified_on = models.DateTimeField('modified on', auto_now=True)
# 
#     def save(self, *args, **kwargs):
#         # Force validation of max_length
#         self.full_clean()
#         super(Answer, self).save(*args, **kwargs)
# 
# Path: mentoring/utils.py
# def render_template(template_path, context={}):
#     """
#     Evaluate a template by resource path, applying the provided context
#     """
#     template_str = load_resource(template_path)
#     template = Template(template_str)
#     return template.render(Context(context))


# Path: mentoring/answer.py
import logging
from lazy import lazy
from xblock.fragment import Fragment
from .light_children import LightChild, Boolean, Scope, String
from .models import Answer
from .utils import render_template

        return bool(self.read_only or self.student_input)

    def save(self):
        """
        Replicate data changes on the related Django model used for sharing of data accross XBlocks
        """
        super(AnswerBlock, self).save()

        # Only attempt to locate a model object for this block when the answer has a name
        if self.name:
            answer_data = self.get_model_object()
            if answer_data.student_input != self.student_input and not self.read_only:
                answer_data.student_input = self.student_input
                answer_data.save()

    def get_model_object(self, name=None):
        """
        Fetches the Answer model object for the answer named `name`
        """
        # By default, get the model object for the current answer's name
        if not name:
            name = self.name
        # Consistency check - we should have a name by now
        if not name:
            raise ValueError, 'AnswerBlock.name field need to be set to a non-null/empty value'

        # TODO: Why do we need to use `xmodule_runtime` and not `runtime`?
        student_id = self.xmodule_runtime.anonymous_student_id
        course_id = self.xmodule_runtime.course_id


        answer_data, created = Answer.objects.get_or_create(