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
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
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 )