Exemple #1
0
class AssignmentHandler(OpenMindsAPIHandler):
    @add_cors_headers
    @public_api_auth
    def GET(self, assignment_id, auth_user=None, auth_app_id=None):
        try:
            assignment_id = ObjectId(assignment_id)
        except Exception, e:
            logging.warn(e)
            return error_response(400, 'Not a valid assignment id')

        try:
            assignment = Assignment.collection.find_one({
                '_id': assignment_id,
                'deleted': False
            })
            if not assignment:
                message = 'Assignment does not exist'
                logging.warn(message)
                return error_response(404, message)

            else:
                item_id_strs = assignment.get_item_ids()
            item_ids = [ObjectId(item_id_str) for item_id_str in item_id_strs]

        except Exception, e:
            logging.error(e)
            return error_response(500, 'Server Error')
Exemple #2
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)
Exemple #3
0
  def GET(self, auth_user=None, auth_app_id=None):
    client = littlelives_info.get_client();

    littlelives_id = auth_user.get('littlelives_id', None)
    if not littlelives_id:
      message = 'Could not authenticate'
      logging.warn(message)
      return error_response(400, message)

    user_info = client.get_user(littlelives_id)
    if not user_info:
      message = 'User not found'
      logging.warn(message)
      return error_response(404, message)
    else:
      return encode_json(user_info)
Exemple #4
0
  def GET(self):
    '''
    Authenticates the user based on a username and password. If the
    authentication is successful, returns a JSON object containing a user id
    and the auth token. The auth token should be written to a cookie that gets
    sent on subsequent requests.
    '''
    params = web.input(username=None, password=None)
    if params.username == None or params.password == None:
      logging.warn('Missing username or password.')
      return error_response(400)

    try:
      user = User.collection.find_one(
          {'username': params.username, 'deleted': False})
    except Exception, e:
      logging.error(e)
      return error_response(500, 'Server Error')
Exemple #5
0
 def PUT(self, user_id, auth_user=None, auth_app_id=None):
   '''
   Updates the given user, and returns a JSON object containing
   the new timestamp.
   '''
   try:
     id_type, user_id = self.parse_user_id(user_id, auth_user)
   except Exception, e:
     logging.warn(e)
     return error_response(400, 'Not a valid user id')
Exemple #6
0
 def DELETE(self, user_id, auth_user=None, auth_app_id=None):
   '''
   Delete the given user, and returns a JSON object containing
   a success notification.
   '''
   try:
     id_type, user_id = self.parse_user_id(user_id, auth_user)
   except Exception, e:
     logging.warn(e)
     return error_response(400, 'Not a valid user id')
Exemple #7
0
  def GET(self, user_id, auth_user=None, auth_app_id=None):
    '''
    Return a JSON object containing extended information about the
    specific user.
    '''

    try:
      id_type, user_id = self.parse_user_id(user_id, auth_user)
    except Exception, e:
      logging.warn(e)
      return error_response(400, 'Not a valid user id')
Exemple #8
0
 def POST(self, auth_user=None, auth_app_id=None):
   '''Record the given events.'''
   list_id = None
   params = web.input(data=None)
   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')
Exemple #9
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.')
Exemple #10
0
  def GET(self, auth_user=None, auth_app_id=None):
    '''
    Returns a JSON array of basic information about users.
    '''
    params = web.input(assignment_id=None, class_id=None)

    class_id = None
    if params.class_id is not None:
      try:
        class_id = ObjectId(params.class_id)
      except Exception, e:
        logging.warn(e)
        return error_response(400)
Exemple #11
0
  def PUT(self, list_id, auth_user=None, auth_app_id=None):
    '''
    Update the given list, and returns a JSON object containing the
    new timestamp.
    '''
    item_list = get_generic_document(list_id, List)

    try:
      data = decode_json(web.ctx.data)
      List.validate(data)
    except ValueError, e:
      logging.warn(e)
      return error_response(400, 'Could not parse JSON')
Exemple #12
0
class ListHandler(OpenMindsAPIHandler):
    @add_cors_headers
    @public_api_auth
    def GET(self, list_id, auth_user=None, auth_app_id=None):
        try:
            list_id = ObjectId(list_id)
        except Exception, e:
            logging.warn(e)
            return error_response(400, 'Not a valid list id')

        try:
            item_list = List.collection.find_one({
                '_id': list_id,
                'deleted': False
            })
            if not item_list:
                message = 'List does not exist'
                logging.warn(message)
                return error_response(404, message)
            item_ids = [ObjectId(i) for i in item_list.get('items', [])]
        except Exception, e:
            logging.error(e)
            return error_response(500, 'Server Error')
Exemple #13
0
  def GET(self, auth_user=None, auth_app_id=None):
    params = web.input(username=None, password=None)
    if params.username == None or params.password == None:
      logging.warn('Missing username or password.')
      return error_response(400, 'Could not authenticate')

    client = littlelives_info.get_client();
    auth_info = client.authenticate(params.username, params.password)
    if 'success' in auth_info or 'sucess' in auth_info:
      # temp workaround for typo in littlelives response
      success = auth_info.get('success', auth_info.get('sucess'))
      if success and 'id' in auth_info:
        # Associate the littlelives id with the user.
        littlelives_id = auth_info['id']
        auth_user.littlelives_id = littlelives_id

        status = {'success': True, 'id': littlelives_id}

        # Check if the user is a teacher on littlelives.
        user_info = client.get_user(littlelives_id)
        if user_info:
          role = user_info.get('role', None)
          if role == 'TCHR':
            auth_user.littlelives_teacher = True
            status['teacher'] = True

        try:
          auth_user.save()
          return encode_json(status)
        except Exception, e:
          logging.error(e)
          return error_response(500, 'Server Error')
      else:
        message = 'Could not authenticate'
        logging.warn(message)
        return error_response(400, message)
Exemple #14
0
  def GET(self, auth_user=None, auth_app_id=None):
    '''
    Returns a JSON array of basic information about lists created
    by the authenticated user.
    '''
    params = web.input(
        search=None, num=50, grade=None, standard=None, section=None)

    try:
      item_lists = get_lists(auth_user, params)
      formatted_lists = [item_list.formatted_dict() for item_list in item_lists]
      return encode_json(formatted_lists)
    except Exception, e:
      logging.error(e)
      return error_response(500)
Exemple #15
0
class SamplingsHandler(OpenMindsAPIHandler):
    @add_cors_headers
    @public_api_auth
    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')
Exemple #16
0
 def POST(self, auth_user=None, auth_app_id=None):
   '''
   Creates a new user and returns a JSON object containing the
   id and auth token for the new user.
   '''
   params = web.input(data=None)
   try:
     if params.data:
       data = decode_json(params.data)
     else:
       data = decode_json(web.ctx.data)
     User.validate(data)
   except ValueError, e:
     logging.warn(e)
     return error_response(400, 'Could not parse JSON')
Exemple #17
0
 def GET(self, auth_user=None, auth_app_id=None):
   '''
   Returns a JSON array of basic information about classes owned
   by the authenticated user.
   '''
   spec = {
     'creator': auth_user._id,
     'deleted': False,
   }
   try:
     classes = Class.collection.find(spec)
     formatted_classes = [c.formatted_dict() for c in classes]
     return encode_json(formatted_classes)
   except Exception, e:
     logging.error(e)
     return error_response(500, 'Server Error')
Exemple #18
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')
Exemple #19
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)
Exemple #20
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)
Exemple #21
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')
Exemple #22
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)
Exemple #23
0
      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
  def GET(self, assignment_id, auth_user=None, auth_app_id=None):
    try:
Exemple #24
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')
Exemple #25
0
 def GET(self, assignment_id, auth_user=None, auth_app_id=None):
     try:
         assignment_id = ObjectId(assignment_id)
     except Exception, e:
         logging.warn(e)
         return error_response(400, 'Not a valid assignment id')
Exemple #26
0
    Creates a new class and returns a JSON object containing the
    id for the new class.
    '''
    params = web.input(data=None)
    try:
      if params.data:
        data = decode_json(params.data)
      else:
        data = decode_json(web.ctx.data)
      Class.validate(data)
    except ValueError, e:
      logging.warn(e)
      return error_response(400, 'Could not parse JSON')
    except ValidationError, e:
      logging.warn(e)
      return error_response(400, e.error)

    try:
      school_class = Class()
      school_class.update_class(data)
      school_class.reset_code()
      school_class.set_creator(auth_user)
      school_class.save()
      response = {
        'id': str(school_class._id),
        'code': school_class.code,
      }
      return encode_json(response)
    except Exception, e:
      return error_response(500, 'Server Error')
Exemple #27
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')
Exemple #28
0
 def GET(self, assignment_id, auth_user=None, auth_app_id=None):
   try:
     assignment_id = ObjectId(assignment_id)
   except Exception, e:
     logging.warn(e)
     return error_response(400, 'Not a valid assignment id')
Exemple #29
0
            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
    def GET(self, assignment_id, auth_user=None, auth_app_id=None):