def get(self, parent_id): """Retrieve all questions linked to the given quest, optionally filtering them by question_group. """ parser = resource.RequestParser() parser.add_argument('question_group', type=parse_question_groups) question_groups = parser.parse_args()['question_group'] if question_groups is None: return super(QuestionList, self).get(parent_id) else: parent_count = self.parent_resource_type.query.filter_by( id=parent_id).count() if not parent_count: return flask.Response('', 404) else: child_query = self.resource_type.query.filter_by( quest_id=parent_id) if len(question_groups) == 1: child_query = child_query.filter_by( question_group=question_groups[0]) else: child_query = child_query.filter( self.resource_type.question_group.in_(question_groups)) children = child_query.all() return { self.child_link_name: [self.as_dict(child) for child in children] }
class QuestBase(object): """Provide a common as_dict method and a parser.""" parser = resource.RequestParser() parser.add_argument('name', type=str) parser.add_argument('summary', type=str) parser.add_argument('inquiry_questions', type=lambda x: map(str, list(x))) parser.add_argument('pbl_description', type=str) parser.add_argument('mentor_guide', type=str) parser.add_argument('min_grade_level', type=int) parser.add_argument('max_grade_level', type=int) parser.add_argument('hours_required', type=int) parser.add_argument('minutes_required', type=int) parser.add_argument('video_links', type=lambda x: map(str, list(x))) parser.add_argument('icon_url', type=str) view_fields = ('id', 'url', 'name', 'summary', 'icon_url', 'inquiry_questions', 'pbl_description', 'mentor_guide', 'min_grade_level', 'max_grade_level', 'hours_required', 'minutes_required', 'video_links', 'creator_id', 'creator_url') tag_fields = ('id', 'url', 'name') def as_dict(self, quest): """Return a serializable dictionary representing the given quest.""" resp = {field: getattr(quest, field) for field in self.view_fields} resp['tags'] = [{ field: getattr(tag, field) for field in self.tag_fields } for tag in quest.tags] return resp
class TagBase(object): """Provide a common as_dict method and a parser.""" parser = resource.RequestParser() parser.add_argument('name', type=str, required=True) view_fields = ('id', 'url', 'name', 'creator_id', 'creator_url') def as_dict(self, tag): """Return a serializable dictionary representing the given quest.""" return {field: getattr(tag, field) for field in self.view_fields}
class MultipleChoiceBase(object): """Provide an as_dict method and a parser.""" parser = resource.RequestParser() parser.add_argument('answer', type=str, required=True) parser.add_argument('is_correct', type=bool, required=True) parser.add_argument('order', type=int, required=True) view_fields = ('id', 'url', 'answer', 'is_correct', 'order', 'question_id', 'question_url', 'creator_id', 'creator_url') def as_dict(self, answer): """Return a serializable dictionary representing the given quest.""" return {field: getattr(answer, field) for field in self.view_fields}
class AnswerBase(object): """Provide an as_dict method and a parser.""" parser = resource.RequestParser() parser.add_argument('answer_text', type=str) parser.add_argument('answer_upload_url', type=str) parser.add_argument('answer_multiple_choice', type=int) view_fields = ('id', 'url', 'question_type', 'answer_text', 'answer_upload_url', 'answer_multiple_choice', 'question_id', 'question_url', 'creator_id', 'creator_url') def as_dict(self, answer): """Return a serializable dictionary representing the given quest.""" return {field: getattr(answer, field) for field in self.view_fields}
def test_add_argument(self): """Test our over-ride of the add_argument method.""" parser = resource.RequestParser() parser.add_argument('snakes') parser.add_argument('ladders', required=True, type=int) parser.add_argument('cats', required=False, type=int) # no type is given, default behaviour self.assertEqual(parser.args[0].type('abc'), 'abc') # required, raise errors for None self.assertRaises(ValueError, parser.args[1].type, None) self.assertEqual(parser.args[1].type('1'), 1) # not required, return None without failing to call int() on it self.assertEqual(parser.args[2].type(None), None) self.assertEqual(parser.args[2].type('1'), 1)
def make_parser(with_question_type=False): """Return a parser for the Question resource. Allows question_type to be an argument depending on the value of with_question_type. """ parser = resource.RequestParser() parser.add_argument('description', type=str, required=True) parser.add_argument('question_group', type=str, required=True, choices=question_models.QUESTION_GROUPS) if with_question_type: parser.add_argument('question_type', type=str, required=True, choices=question_models.QUESTION_TYPES) return parser
class UserBase(object): """Provide a shared as_dict method and a parser.""" parser = resource.RequestParser() parser.add_argument('name', type=str) parser.add_argument('email', type=str) parser.add_argument('description', type=str) parser.add_argument('avatar_url', type=str) view_fields = ('id', 'name', 'avatar_url', 'url') organization_fields = ('id', 'url', 'name', 'icon_url') def as_dict(self, user): """Return a serializable dictionary representing the given user.""" resp = {field: getattr(user, field) for field in self.view_fields} resp['organizations'] = [{ field: getattr(organization, field) for field in self.organization_fields } for organization in user.organizations] return resp
class MissionBase(object): """Provide a common as_dict method and a parser.""" parser = resource.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('description', type=str, required=True) parser.add_argument('points', type=int, required=True) view_fields = ('id', 'url', 'name', 'description', 'points', 'creator_id', 'creator_url') quest_fields = ('id', 'url', 'name', 'summary', 'icon_url', 'creator_id', 'creator_url') def as_dict(self, mission): """Return a serializable dictionary representing the given mission.""" resp = {field: getattr(mission, field) for field in self.view_fields} resp['quests'] = [{ field: getattr(quest, field) for field in self.quest_fields } for quest in mission.quests] return resp
class OrganizationBase(object): """Provide a common as_dict method and a parser.""" parser = resource.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('description', type=str, required=True) parser.add_argument('icon_url', type=str) view_fields = ('id', 'url', 'name', 'description', 'icon_url', 'creator_id', 'creator_url') user_fields = ('id', 'url', 'name', 'avatar_url') def as_dict(self, organization): """Return a serializable dictionary representing the given org.""" resp = { field: getattr(organization, field) for field in self.view_fields } resp['members'] = [{ field: getattr(user, field) for field in self.user_fields } for user in organization.members] return resp