示例#1
0
 def GET(self, auth_user=None, auth_app_id=None):
   '''
   Returns a JSON array of basic information about assignment.
   '''
   params = web.input(
       student_user_id=None, class_id=None, availability=None, num=50)
   depth = parse_int_param(util.get_header('X-OpenMinds-Depth'), 0)
   try:
     num = parse_int_param(params.num, 50)
   except Exception, e:
     logging.error(e)
     return error_response(400, 'Could not parse num parameter.')
示例#2
0
def get_lists_stats(auth_user, params):
  # Find lists that match the list filtering
  list_spec = {'deleted': False}
  grade = parse_int_param(params.grade, None)
  section = parse_int_param(params.section, None)
  if grade is not None:
    list_spec['grade'] = grade
  if params.standard is not None:
    list_spec['standard'] = params.standard
    # Only add the section to the spec if a standard is also defined.
    if params.section is not None:
      list_spec['section'] = section
  try:
    item_lists = List.collection.find(list_spec, {'_id': 1})
    list_ids = [l._id for l in item_lists]
  except Exception, e:
    logging.error(e)
    return error_response(500)
示例#3
0
def get_lists_stats(auth_user, params):
    # Find lists that match the list filtering
    list_spec = {'deleted': False}
    grade = parse_int_param(params.grade, None)
    section = parse_int_param(params.section, None)
    if grade is not None:
        list_spec['grade'] = grade
    if params.standard is not None:
        list_spec['standard'] = params.standard
        # Only add the section to the spec if a standard is also defined.
        if params.section is not None:
            list_spec['section'] = section
    try:
        item_lists = List.collection.find(list_spec, {'_id': 1})
        list_ids = [l._id for l in item_lists]
    except Exception, e:
        logging.error(e)
        return error_response(500)
示例#4
0
 def GET(self, auth_user=None, auth_app_id=None):
   '''
   Returns a JSON array of basic information about assignment templates.
   FIXME(dbanks)
   Right now you get all assignment templates.
   We may want to control that somehow so that some templates (created
   by us) are public-domain.  Others, auth_user has to be on acl for that
   template.
   '''
   params = web.input(num=50)
   depth = parse_int_param(util.get_header('X-OpenMinds-Depth'), 0)
   try:
     num = parse_int_param(params.num, 50)
     spec = {'deleted': False}
     templates = AssignmentTemplate.collection.find(spec).limit(num)
     formatted_templates = \
       [template.formatted_dict(depth=depth) for template in templates]
     return encode_json(formatted_templates)
   except Exception, e:
     logging.error(e)
     return error_response(500)
示例#5
0
def get_lists(auth_user, params):
  num = parse_int_param(params.num, 50)
  grade = parse_int_param(params.grade, None)
  section = parse_int_param(params.section, None)
  if params.search in ('all', 'created'):
    search = params.search
  else:
    search = 'all'

  spec = {'deleted': False}
  if search == 'created':
    spec['creator'] = auth_user._id
  if grade is not None:
    spec['grade'] = grade
  if params.standard is not None:
    spec['standard'] = params.standard
    # Only add the section to the spec if a standard is also defined.
    if params.section is not None:
      spec['section'] = section

  item_lists = List.collection.find(spec).limit(num)
  return item_lists
示例#6
0
  def GET(self, grade):
    grade = parse_int_param(grade, 5)
    common_core = CommonCore()
    data = common_core.get_grade(grade)
    if not data:
      logging.warn('Cannot find data for grade %d' % grade)
      return error_response(404)

    try:
      grade_json = encode_json(data)
      return grade_json
    except Exception, e:
      logging.error(e)
      return error_response(500)
示例#7
0
 def POST(self, auth_user=None, auth_app_id=None):
   '''
   Creates a new AssignmentTemplate and returns a JSON object containing the
   id for the new AssignmentTemplate.
   '''
   params = web.input(data=None)
   depth = parse_int_param(util.get_header('X-OpenMinds-Depth'), 0)
   try:
     if params.data:
       data = decode_json(params.data)
     else:
       data = decode_json(web.ctx.data)
     AssignmentTemplate.validate(data)
   except ValueError, e:
     logging.warn(e)
     return error_response(400, 'Could not parse JSON')
示例#8
0
  def POST(self, auth_user=None, auth_app_id=None):
    '''
    Creates a new Assignment and returns a JSON object containing the
    id for the new Assignment.

    You could pass up a fully specified assignment, but as a convenience we also
    allow you to pass up an assignment template id, which will fill in
    all the data for you.
    '''
    params = web.input(data=None)
    depth = parse_int_param(util.get_header('X-OpenMinds-Depth'), 0)

    try:
      if params.data:
        data = decode_json(params.data)
      else:
        data = decode_json(web.ctx.data)
    except ValueError, e:
      logging.warn(e)
      return error_response(400, 'Could not parse JSON')
示例#9
0
  def GET(self, auth_user=None, auth_app_id=None):
    params = web.input(num=20, class_only=0)
    num_entries = parse_int_param(params.num, 20)
    #class_only = parse_int_param_as_bool(params.class_only)
    class_only = auth_user.get('littlelives_teacher', False)

    spec = {
      'points': {'$exists': True},
      '$or': [{'flagged': {'$exists': False}},{'flagged': False}],
    }

    if class_only:
      key = 'acl.%s' % str(auth_user._id)
      spec[key] = {'$exists': True}

    users_data = User.collection.find(
        spec,
        {'username': 1, 'name': 1, 'points': 1},
        sort=[('points', pymongo.DESCENDING)],
        limit=num_entries)
    users = [User(data) for data in users_data]
    users = [user.formatted_dict() for user in users]
    return encode_json(users)
示例#10
0
def parse_page_params(params):
  num_items = parse_int_param(params.num, DEFAULT_NUM)
  page = max(1, parse_int_param(params.page, DEFAULT_PAGE))
  skip_num = (page-1) * num_items
  return num_items, page, skip_num
示例#11
0
  def GET(self, raw_list_id, raw_app_id, auth_user=None, auth_app_id=None):
    try:
      list_id = ObjectId(raw_list_id)
    except Exception, e:
      logging.warn(e)
      return error_response(400, 'Not a valid list id')

    try:
      app_id = ObjectId(raw_app_id)
    except Exception, e:
      logging.warn(e)
      return error_response(400, 'Not a valid app id')

    try:
      params = web.input(num_samples=1)
      num_samples = parse_int_param(params.num_samples, 1)
      if num_samples <= 0:
        return error_response(400, 'Invalid num_samples param.')

      # We want the n most recent 'samplings' of this user in this
      # app with this list.
      samplings = get_sampling_stats(auth_user, list_id, app_id, num_samples)
      return encode_json(samplings)
    except Exception, e:
      logging.error(e)
      return error_response(500, 'Server Error')


class AssignmentHandler(OpenMindsAPIHandler):
  @add_cors_headers
  @public_api_auth
示例#12
0
        assignment_spec['classId'] = {'$in': class_ids}
      elif params.class_id is not None:
        try:
          class_id = ObjectId(params.class_id)
        except Exception, e:
          logging.warn(e)
          return error_response(400, 'class_id is not a valid id')

        if not can_see_class_data(auth_user, class_id):
          return error_response(401)
        assignment_spec['classId'] = params.class_id
      else:
        assignment_spec['creator'] = auth_user._id

      if params.availability is not None:
        assignment_spec['availability'] = parse_int_param(
            params.availability, Assignment.AVAILABILITY_OPEN)

      assignments = Assignment.collection.find(assignment_spec).limit(num)
      formatted_assignments = [assignment.formatted_dict(depth=depth)
                             for assignment in assignments]
      return encode_json(formatted_assignments)
    except Exception, e:
      logging.error(e)
      return error_response(500)

  @add_cors_headers
  @public_api_auth
  def POST(self, auth_user=None, auth_app_id=None):
    '''
    Creates a new Assignment and returns a JSON object containing the
    id for the new Assignment.
示例#13
0
def parse_page_params(params):
    num_items = parse_int_param(params.num, DEFAULT_NUM)
    page = max(1, parse_int_param(params.page, DEFAULT_PAGE))
    skip_num = (page - 1) * num_items
    return num_items, page, skip_num
示例#14
0
    def GET(self, raw_list_id, raw_app_id, auth_user=None, auth_app_id=None):
        try:
            list_id = ObjectId(raw_list_id)
        except Exception, e:
            logging.warn(e)
            return error_response(400, 'Not a valid list id')

        try:
            app_id = ObjectId(raw_app_id)
        except Exception, e:
            logging.warn(e)
            return error_response(400, 'Not a valid app id')

        try:
            params = web.input(num_samples=1)
            num_samples = parse_int_param(params.num_samples, 1)
            if num_samples <= 0:
                return error_response(400, 'Invalid num_samples param.')

            # We want the n most recent 'samplings' of this user in this
            # app with this list.
            samplings = get_sampling_stats(auth_user, list_id, app_id,
                                           num_samples)
            return encode_json(samplings)
        except Exception, e:
            logging.error(e)
            return error_response(500, 'Server Error')


class AssignmentHandler(OpenMindsAPIHandler):
    @add_cors_headers