コード例 #1
0
ファイル: student.py プロジェクト: SOSbeacon/SchoolBeacon-GAE
def _get_group(group_name, group_lookup, school_urlsafe):
    """Return a group for the group name passed in. Checks the group cache
    first if not there then queries by the lower case name. If not there then
    creates a new group.
    """
    #check for existing group by name
    if group_name.lower() in group_lookup:
        logging.debug("group found in cache")
        error = "group found in cache"
        create_error_log(error, 'ERR')
        return group_lookup[group_name.lower()], None

    from .group import Group
    school_key = ndb.Key(urlsafe=school_urlsafe)
    group = Group.query(Group.name_ == group_name,
                        Group.school == school_key,
                        namespace='_x_').get()

    if group:
        logging.debug("group found in datastore")
        error = "group found in datastore"
        create_error_log(error, 'ERR')
        group_lookup[group_name.lower()] = group.key
        return group.key, None

    logging.debug("No group found for %s, creating a new one", group_name)
    group = Group(name=group_name)
    school_key = ndb.Key(urlsafe=school_urlsafe)
    group.school = school_key
    future = group.put_async()
    return group.key, future
コード例 #2
0
ファイル: student.py プロジェクト: SOSbeacon/SchoolBeacon-GAE
def _get_group(group_name, group_lookup, school_urlsafe):
    """Return a group for the group name passed in. Checks the group cache
    first if not there then queries by the lower case name. If not there then
    creates a new group.
    """
    #check for existing group by name
    if group_name.lower() in group_lookup:
        logging.debug("group found in cache")
        error = "group found in cache"
        create_error_log(error, 'ERR')
        return group_lookup[group_name.lower()], None

    from .group import Group
    school_key = ndb.Key(urlsafe = school_urlsafe)
    group = Group.query(Group.name_ == group_name, Group.school == school_key, namespace = '_x_').get()

    if group:
        logging.debug("group found in datastore")
        error = "group found in datastore"
        create_error_log(error, 'ERR')
        group_lookup[group_name.lower()] = group.key
        return group.key, None

    logging.debug("No group found for %s, creating a new one", group_name)
    group = Group(name=group_name)
    school_key = ndb.Key(urlsafe = school_urlsafe)
    group.school = school_key
    future = group.put_async()
    return group.key, future
コード例 #3
0
ファイル: message.py プロジェクト: SOSbeacon/SchoolBeacon-GAE
def broadcast_to_groups(group_keys, event_key, message_key, batch_id):
    """Scan over the given set of groups, sending the broadcast to everyone
    in those groups.
    """
    from sosbeacon.group import ADMIN_GROUPS_ID
    from sosbeacon.group import Group
    from sosbeacon.utils import insert_tasks

    # This is done to dedupe the group list, for better resilience.
    group_keys = list(set(group_keys))

    if len(group_keys) == 1 and group_keys[0].id() == ADMIN_GROUPS_ID:
        group_keys = Group.query().order(Group.key).iter(keys_only=True)

    tasks = []
    for group_key in group_keys:
        # TODO: Batch tasks or start
        tasks.append(get_group_broadcast_task(group_key, event_key, message_key, batch_id))

        if len(tasks) > 10:
            insert_tasks(tasks, GROUP_TX_QUEUE)
            tasks = []

    if tasks:
        insert_tasks(tasks, GROUP_TX_QUEUE)
コード例 #4
0
ファイル: message.py プロジェクト: SOSbeacon/SchoolBeacon-GAE
def broadcast_to_groups(group_keys, event_key, message_key, batch_id):
    """Scan over the given set of groups, sending the broadcast to everyone
    in those groups.
    """
    from sosbeacon.group import ADMIN_GROUPS_ID
    from sosbeacon.group import Group
    from sosbeacon.utils import insert_tasks

    # This is done to dedupe the group list, for better resilience.
    group_keys = list(set(group_keys))

    if len(group_keys) == 1 and group_keys[0].id() == ADMIN_GROUPS_ID:
        group_keys = Group.query().order(Group.key).iter(keys_only=True)

    tasks = []
    for group_key in group_keys:
        # TODO: Batch tasks or start
        tasks.append(
            get_group_broadcast_task(group_key, event_key, message_key,
                                     batch_id))

        if len(tasks) > 10:
            insert_tasks(tasks, GROUP_TX_QUEUE)
            tasks = []

    if tasks:
        insert_tasks(tasks, GROUP_TX_QUEUE)
コード例 #5
0
def process_school(request, schema, entity):
    from voluptuous import Schema

    obj = json.loads(request.body)
    schema = Schema(schema, extra=True)

    try:
        obj = schema(obj)
    except:
        logging.exception('validation failed')
        logging.info(obj)

    school = entity.from_dict(obj)
    to_put = [school]

    if not obj.get('key'):
        # this is a new school. add the all groups group
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        group_admin = Group(key=ndb.Key(
            Group, ADMIN_GROUPS_ID + "%s" %
            (school.key.id()), namespace="_x_"))
        group_admin.name = "Admin"
        group_admin.school = school.key
        group_admin.default = True

        group_staff = Group(key=ndb.Key(
            Group, STAFF_GROUPS_ID + "%s" %
            (school.key.id()), namespace="_x_"))
        group_staff.name = "Staff"
        group_staff.school = school.key
        group_staff.default = True

        to_put.append(group_admin)
        to_put.append(group_staff)

    ndb.put_multi(to_put)

    return school
コード例 #6
0
    def test_from_dict(self, memcache_delete_mock):
        """Ensure merging two non-acked doesn't ack."""
        from datetime import datetime
        from sosbeacon.group import Group

        group_dict = {
            'name': 'Group',
        }

        group = Group.from_dict(group_dict)

        self.assertEqual(group_dict['name'], group.name)

        self.assertFalse(memcache_delete_mock.call_count)
コード例 #7
0
    def test_to_from_composition(self):
        """Ensure to_dict(from_dict(x)) returns a correctly setup object."""
        from datetime import datetime
        from sosbeacon.group import Group

        group_dict = {
            'name': 'Group',
        }

        group = Group.from_dict(group_dict)
        group.put()

        new_school = group.to_dict()

        self.assertEqual(group_dict, new_school)
コード例 #8
0
def process_group(request, schema, entity):
    from voluptuous import Schema
    from sosbeacon.group import Group

    session_store = sessions.get_store()
    session = session_store.get_session()

    if not 'u' in session:
        return False

    if not 's' in session:
        return False

    obj = json.loads(request.body)
    schema = Schema(schema, extra=True)

    if obj['name'] == '':
        return False

    #check group duplicate
    group_name = obj['name'].lower()
    school_key = ndb.Key(urlsafe=session.get('s'))

    check_name = Group.query(Group.name_ == group_name,
                             Group.school == school_key,
                             namespace='_x_')

    if check_name.get():
        return False

    try:
        obj = schema(obj)
    except:
        logging.exception('validation failed')
        logging.info(obj)

    obj['school'] = school_key

    group = entity.from_dict(obj)
    to_put = [group]
    ndb.put_multi(to_put)

    return group
コード例 #9
0
def process_school(request, schema, entity):
    from voluptuous import Schema

    obj = json.loads(request.body)
    schema = Schema(schema, extra=True)

    try:
        obj = schema(obj)
    except:
        logging.exception('validation failed')
        logging.info(obj)

    school = entity.from_dict(obj)
    to_put = [school]

    if not obj.get('key'):
        # this is a new school. add the all groups group
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        group_admin = Group(key=ndb.Key(Group, ADMIN_GROUPS_ID + "%s" % (school.key.id()),
            namespace="_x_"))
        group_admin.name = "Admin"
        group_admin.school = school.key
        group_admin.default = True

        group_staff = Group(key=ndb.Key(Group, STAFF_GROUPS_ID + "%s" % (school.key.id()),
            namespace="_x_"))
        group_staff.name = "Staff"
        group_staff.school = school.key
        group_staff.default = True

        to_put.append(group_admin)
        to_put.append(group_staff)

    ndb.put_multi(to_put)

    return school
コード例 #10
0
def process_group(request, schema, entity):
    from voluptuous import Schema
    from sosbeacon.group import Group

    session_store = sessions.get_store()
    session = session_store.get_session()

    if not 'u' in session:
        return False

    if not 's' in session:
        return False

    obj = json.loads(request.body)
    schema = Schema(schema, extra=True)

    if obj['name'] == '':
        return False

    #check group duplicate
    group_name = obj['name'].lower()
    school_key = ndb.Key(urlsafe = session.get('s'))

    check_name = Group.query(Group.name_ == group_name, Group.school == school_key, namespace = '_x_')

    if check_name.get():
        return False

    try:
        obj = schema(obj)
    except:
        logging.exception('validation failed')
        logging.info(obj)

    obj['school'] = school_key

    group = entity.from_dict(obj)
    to_put = [group]
    ndb.put_multi(to_put)

    return group
コード例 #11
0
    def setUp(self):
        from sosbeacon.school import School
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.setup_env(app_id='testapp')
        self.testbed.init_datastore_v3_stub()

        url_map = [
            webapp2.Route(r'/service/group/<resource_id:.+>',
                          handler='sosbeacon.service.GroupHandler'),
            webapp2.Route(r'/service/group<:/?>',
                          handler='sosbeacon.service.GroupListHandler'),

            webapp2.Route(r'/service/admin/user<:/?>',
                          handler='sosbeacon.service.UserListHandler'),

            ('/authentication/login', LoginUserHandler)
        ]

        app = webapp2.WSGIApplication(
            url_map,
            config=webapp_config
        )
        self.testapp = webtest.TestApp(app)

        self.school1 = School(
            id='100',
            name='School_Test',
        )

        self.school2 = School(
            id='200',
            name='School_Test_2',
        )

        self.school1.put()

        params = {
            'name': 'longly',
            'email': '*****@*****.**',
            'phone': '84973796065',
            'password': '******',
            'schools': [self.school1.key.urlsafe()]
        }
        self.testapp.post_json('/service/admin/user/', params)

        email = '*****@*****.**'
        password = '******'

        params1 = {'email': email, 'password': password}
        self.testapp.post('/authentication/login', params1)

        self.group1 = Group(
            id='1',
            name='Group 1',
            school=self.school1.key
        )

        self.group2 = Group(
            id='2',
            name='Group 2',
            school=self.school1.key
        )

        self.group3 = Group(
            id='3',
            name='Group 3',
            school=self.school1.key
        )

        self.group4 = Group(
            id='4',
            name='Group 4',
            school=self.school2.key
        )

        self.group_admin = Group(
            id=ADMIN_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Admin',
            school=self.school1.key,
            default = True
        )

        self.group_staff = Group(
            id=STAFF_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Staff',
            school=self.school1.key,
            default = True
        )
コード例 #12
0
class TestGroupService(unittest.TestCase):
    """Test service of group"""
    def setUp(self):
        from sosbeacon.school import School
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.setup_env(app_id='testapp')
        self.testbed.init_datastore_v3_stub()

        url_map = [
            webapp2.Route(r'/service/group/<resource_id:.+>',
                          handler='sosbeacon.service.GroupHandler'),
            webapp2.Route(r'/service/group<:/?>',
                          handler='sosbeacon.service.GroupListHandler'),

            webapp2.Route(r'/service/admin/user<:/?>',
                          handler='sosbeacon.service.UserListHandler'),

            ('/authentication/login', LoginUserHandler)
        ]

        app = webapp2.WSGIApplication(
            url_map,
            config=webapp_config
        )
        self.testapp = webtest.TestApp(app)

        self.school1 = School(
            id='100',
            name='School_Test',
        )

        self.school2 = School(
            id='200',
            name='School_Test_2',
        )

        self.school1.put()

        params = {
            'name': 'longly',
            'email': '*****@*****.**',
            'phone': '84973796065',
            'password': '******',
            'schools': [self.school1.key.urlsafe()]
        }
        self.testapp.post_json('/service/admin/user/', params)

        email = '*****@*****.**'
        password = '******'

        params1 = {'email': email, 'password': password}
        self.testapp.post('/authentication/login', params1)

        self.group1 = Group(
            id='1',
            name='Group 1',
            school=self.school1.key
        )

        self.group2 = Group(
            id='2',
            name='Group 2',
            school=self.school1.key
        )

        self.group3 = Group(
            id='3',
            name='Group 3',
            school=self.school1.key
        )

        self.group4 = Group(
            id='4',
            name='Group 4',
            school=self.school2.key
        )

        self.group_admin = Group(
            id=ADMIN_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Admin',
            school=self.school1.key,
            default = True
        )

        self.group_staff = Group(
            id=STAFF_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Staff',
            school=self.school1.key,
            default = True
        )

    def test_service_url(self):
        """Ensure link to service group exists"""
        response = self.testapp.get('/service/group')
        self.assertEqual(response.status_int, 200)

    def test_service_create_group(self):
        """Ensure create success new event with json object send from client"""
        params = {
            'name': 'Group Test',
            }

        response = self.testapp.post_json('/service/group', params)
        obj = json.loads(response.normal_body)

        self.assertEqual(response.status_int, 200)
        self.assertEqual(obj['name'], params['name'])

    def test_service_create_exists_name_group(self):
        """Ensure can not create duplicate name group"""
        self.group1.put()
        params = {
            'name' : 'Group 1',
        }
        response = self.testapp.post_json('/service/group', params, status=400)

        self.assertEqual(response.status_int, 400)

    def test_service_get_filter_group(self):
        """Ensure server response group which same school"""
        to_put = [self.group1, self.group2, self.group3, self.group4, self.group_admin, self.group_staff]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/group')
        obj = json.loads(response.normal_body)

        self.assertEqual(len(obj), 5)

    def test_service_edit_group(self):
        """Ensure group will be update new data"""
        self.group1.put()
        params = {
            'name':'Update Group'
        }

        response = self.testapp.put_json('/service/group/%s' % self.group1.key.urlsafe(), params)
        obj = json.loads(response.normal_body)

        self.assertEqual(response.status_int, 200)
        self.assertEqual(obj['name'], params['name'])

#    def test_service_edit_duplicate_group(self):
#        """Ensure user can not edit duplication group name"""
#        self.group1.put()
#        params = {
#            'name':'Group 1'
#        }
#        response = self.testapp.put_json('/service/group/%s' % self.group1.key.urlsafe(), params)
#        logging.info(response)
#        self.assertEqual(response.status_int, 400)

    def test_service_edit_default_group(self):
        """Ensure can not update information of default group"""
        self.group_admin.put()
        params = {
            'name':'Update Group Admin'
        }
        response = self.testapp.put_json('/service/group/%s' % self.group_admin.key.urlsafe(), params, status=400)
        self.assertEqual(response.status_int, 400)

    def test_service_delete_group(self):
        """Ensure this group will be None object"""
        from google.appengine.ext import ndb
        self.group1.put()
        response = self.testapp.delete('/service/group/%s' %self.group1.key.urlsafe())
        query_group = ndb.Key(urlsafe=self.group1.key.urlsafe())
        self.assertIsNone(query_group.get())
        self.assertEqual(response.status_int, 200)

    def test_service_delete_default_group(self):
        """Ensure can not delete default group"""
        self.group_admin.put()
        response = self.testapp.delete('/service/group/%s' % self.group_admin.key.urlsafe(), status=400)
        self.assertEqual(response.status_int, 400)

    def test_number_student_of_group(self):
        """Ensure number student of group always > 1"""
        to_put = [self.group1, self.group2, self.group3, self.group4, self.group_admin, self.group_staff]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/group')
        obj = json.loads(response.normal_body)

        for i in obj:
            self.assertGreaterEqual(1, i['number_student'])

    def test_sort_admin_staff_group(self):
        """Ensure admin and staff always at position 1 and 2"""
        to_put = [self.group1, self.group2, self.group3, self.group4, self.group_admin, self.group_staff]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/group')
        obj = json.loads(response.normal_body)

        self.assertEqual(obj[0]['name'], self.group_admin.name)
        self.assertEqual(obj[1]['name'], self.group_staff.name)