class AppHandler(object):
    '''
  Handler for retrieving information about a specific app. Also
  allows app information to be updated, or for an app to be deleted. Only
  the creator can access and modify the app information.
  '''
    @public_api_auth
    def GET(self, app_id, auth_user=None, auth_app_id=None):
        '''
    Return a JSON object containing information about the specific app.
    '''
        try:
            app_id = ObjectId(app_id)
        except Exception, e:
            logging.error(e)
            return error_response(400, 'Not a valid app id')

        try:
            app = App.collection.find_one({'_id': app_id, 'deleted': False})
            if not app:
                message = 'App does not exist'
                logging.warn(message)
                return error_response(404, message)
            if not app.user_can_update(auth_user):
                message = 'App cannot be accessed by the user'
                logging.warn(message)
                return error_response(403, message)
            formatted_dict = app.formatted_dict()
            return encode_json(formatted_dict)
        except Exception, e:
            logging.error(e)
            return error_response(500, 'Server Error')
Example #2
0
 def GET(self, auth_user=None, auth_app_id=None):
     params = web.input(num=DEFAULT_NUM,
                        page=DEFAULT_PAGE,
                        sort=DEFAULT_SORT,
                        sort_dir=DEFAULT_SORT_DIR,
                        mastery=DEFAULT_FILTER)
     meta_data, stats = get_standards_stats(auth_user, params)
     return encode_json(stats)
Example #3
0
 def GET(self, auth_user=None, auth_app_id=None):
   params = web.input(
     num=DEFAULT_NUM,
     page=DEFAULT_PAGE,
     sort=DEFAULT_SORT,
     sort_dir=DEFAULT_SORT_DIR,
     mastery=DEFAULT_FILTER)
   meta_data, stats = get_standards_stats(auth_user, params)
   return encode_json(stats)
 def GET(self, auth_user=None, auth_app_id=None):
     '''
 Return JSON array of basic app info. Only returns apps
 created by the authenticated user.
 '''
     try:
         spec = {'deleted': False, 'creator': auth_user._id}
         apps = App.collection.find(spec)
         formatted_apps = [app.formatted_dict() for app in apps]
         return encode_json(formatted_apps)
     except Exception, e:
         logging.error(e)
         return error_response(500)
Example #5
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)
Example #6
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)
Example #7
0
  def _api_response(self, method, path, body={}, params={}):
    '''
    Signs the request using the oauth2 library.
    '''
    str_body = encode_json(body)
    req = self._get_request(method, path, str_body, extra_params=params)
    data = req.to_postdata()

    connection = httplib.HTTPConnection(self.host)
    if self.host.startswith('localhost') and method == 'POST':
      # Workaround for sending api POST requests to local server.
      connection.request(method, path, data)
    else:
      connection.request(method, path + '?' + data, str_body)
    return connection.getresponse()
    def _api_response(self, method, path, body={}, params={}):
        '''
    Signs the request using the oauth2 library.
    '''
        str_body = encode_json(body)
        req = self._get_request(method, path, str_body, extra_params=params)
        data = req.to_postdata()

        connection = httplib.HTTPConnection(self.host)
        if self.host.startswith('localhost') and method == 'POST':
            # Workaround for sending api POST requests to local server.
            connection.request(method, path, data)
        else:
            connection.request(method, path + '?' + data, str_body)
        return connection.getresponse()
Example #9
0
 def GET(self, auth_user=None, auth_app_id=None):
   '''
   Return JSON array of basic app info. Only returns apps
   created by the authenticated user.
   '''
   try:
     spec = {
       'deleted': False,
       'creator': auth_user._id
     }
     apps = App.collection.find(spec)
     formatted_apps = [app.formatted_dict() for app in apps]
     return encode_json(formatted_apps)
   except Exception, e:
     logging.error(e)
     return error_response(500)
Example #10
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)
Example #11
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')
Example #12
0
 def _api_response(self, method, path, body=None):
     '''
 Includes the access token as a header in the request.
 '''
     connection = httplib.HTTPConnection(self.host)
     if body:
         data = encode_json(body)
     else:
         data = None
     headers = {'X-OpenMinds-Access-Token': self.access_token}
     if method == 'GET':
         if data:
             path += '?' + data
         connection.request(method, path, None, headers)
     else:
         connection.request(method, path, data, headers)
     return connection.getresponse()
 def _api_response(self, method, path, body=None):
     """
 Includes the access token as a header in the request.
 """
     connection = httplib.HTTPConnection(self.host)
     if body:
         data = encode_json(body)
     else:
         data = None
     headers = {"X-OpenMinds-Access-Token": self.access_token}
     if method == "GET":
         if data:
             path += "?" + data
         connection.request(method, path, None, headers)
     else:
         connection.request(method, path, data, headers)
     return connection.getresponse()
Example #14
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)
Example #15
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)
Example #16
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)
Example #17
0
            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')

        params = web.input(num=DEFAULT_NUM,
                           page=DEFAULT_PAGE,
                           sort=DEFAULT_SORT,
                           sort_dir=DEFAULT_SORT_DIR,
                           mastery=DEFAULT_FILTER)
        meta_data, stats = get_items_stats(auth_user, item_ids, params)
        return encode_json(stats)


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:
Example #18
0
 def GET(self, auth_user=None, auth_app_id=None):
     response = {'data': 'classes'}
     return encode_json(response)
Example #19
0
 def GET(self, class_id, student_id, auth_user=None, auth_app_id=None):
   response = {'data': 'student'}
   return encode_json(response)
Example #20
0
 def GET(self, auth_user=None, auth_app_id=None):
   response = {'data': 'classes'}
   return encode_json(response)
Example #21
0
 def GET(self, class_id, student_id, auth_user=None, auth_app_id=None):
     response = {'data': 'student'}
     return encode_json(response)
Example #22
0
        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')

    params = web.input(
      num=DEFAULT_NUM,
      page=DEFAULT_PAGE,
      sort=DEFAULT_SORT,
      sort_dir=DEFAULT_SORT_DIR,
      mastery=DEFAULT_FILTER)
    meta_data, stats = get_items_stats(auth_user, item_ids, params)
    return encode_json(stats)


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:
        try:
            if params.data:
                data = decode_json(params.data)
            else:
                data = decode_json(web.ctx.data)
            App.validate(data)
        except Exception, e:
            logging.error(e)
            return error_response(400, 'Data did not pass validation')

        try:
            app = App(data)
            app.set_creator(auth_user)
            app.save()
            formatted_dict = app.formatted_dict()
            return encode_json(formatted_dict)
        except Exception, e:
            logging.error(e)
            return error_response(500)


class AppHandler(object):
    '''
  Handler for retrieving information about a specific app. Also
  allows app information to be updated, or for an app to be deleted. Only
  the creator can access and modify the app information.
  '''
    @public_api_auth
    def GET(self, app_id, auth_user=None, auth_app_id=None):
        '''
    Return a JSON object containing information about the specific app.
Example #24
0
      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')


class ClassHandler(OpenMindsAPIHandler):
  '''
  Handler for retrieving extended information about a specific class,
  or updating/deleting a class' information.
  '''

  @add_cors_headers
  @public_api_auth
  def GET(self, class_id=None, auth_user=None, auth_app_id=None):
    '''
    Return a JSON object containing extended information about the
Example #25
0
    try:
      if params.data:
        data = decode_json(params.data)
      else:
        data = decode_json(web.ctx.data)
      App.validate(data)
    except Exception, e:
      logging.error(e)
      return error_response(400, 'Data did not pass validation')

    try:
      app = App(data)
      app.set_creator(auth_user)
      app.save()
      formatted_dict = app.formatted_dict()
      return encode_json(formatted_dict)
    except Exception, e:
      logging.error(e)
      return error_response(500)


class AppHandler(object):
  '''
  Handler for retrieving information about a specific app. Also
  allows app information to be updated, or for an app to be deleted. Only
  the creator can access and modify the app information.
  '''

  @public_api_auth
  def GET(self, app_id, auth_user=None, auth_app_id=None):
    '''