示例#1
0
    def put(self, resource_id, *args, **kwargs):
        """edit contact student"""
        from voluptuous import Schema
        from sosbeacon.student import Student

        student_key = ndb.Key(urlsafe = resource_id)
        if student_key.get().is_direct:
            super(StudentHandler, self).put(self, *args, **kwargs)
            return

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

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

        student = student_key.get().to_dict()
        if obj['contacts'][1]['first_name'] == '':
            obj['contacts'][1]['first_name'] = student['contacts'][1]['first_name']

        if obj['contacts'][1]['methods'][0]['value'] == obj['contacts'][1]['methods'][1]['value'] \
            == obj['contacts'][1]['methods'][2]['value'] == '':
                obj['contacts'][1]['methods'][0]['value'] = student['contacts'][1]['methods'][0]['value']
                obj['contacts'][1]['methods'][1]['value'] = student['contacts'][1]['methods'][1]['value']
                obj['contacts'][1]['methods'][2]['value'] = student['contacts'][1]['methods'][2]['value']

        student = Student.from_dict(obj)
        to_put = [student]
        ndb.put_multi(to_put)

        self.write_json_response(student.to_dict())
示例#2
0
    def test_from_dict(self, memcache_delete_mock):
        """Ensure merging two non-acked doesn't ack."""
        from sosbeacon.student import Student

        student_dict = {
            'name': 'ly hoang long',
            'groups': [],
            'is_direct': False,
        }

        student = Student.from_dict(student_dict)

        self.assertEqual(student_dict['name'], student.name)
        self.assertEqual(student_dict['groups'], student.groups)
        self.assertEqual(student_dict['is_direct'], student.is_direct)

        self.assertFalse(memcache_delete_mock.call_count)
示例#3
0
    def test_to_from_composition(self):
        """Ensure to_dict(from_dict(x)) returns a correctly setup object."""
        from datetime import datetime
        from sosbeacon.student import Student

        student_dict = {
            'name': 'ly hoang long',
            'added': datetime(2012, 8, 30, 7, 37),
            'groups': [],
            'is_direct': False,
        }

        student = Student.from_dict(student_dict)
        student.put()

        new_student = student.to_dict()

        self.assertEqual(student_dict, new_student)
示例#4
0
    def put(self, resource_id, *args, **kwargs):
        """edit contact student"""
        from voluptuous import Schema
        from sosbeacon.student import Student

        student_key = ndb.Key(urlsafe=resource_id)
        if student_key.get().is_direct:
            super(StudentHandler, self).put(self, *args, **kwargs)
            return

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

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

        student = student_key.get().to_dict()
        if obj['contacts'][1]['first_name'] == '':
            obj['contacts'][1]['first_name'] = student['contacts'][1][
                'first_name']

        if obj['contacts'][1]['methods'][0]['value'] == obj['contacts'][1]['methods'][1]['value'] \
            == obj['contacts'][1]['methods'][2]['value'] == '':
            obj['contacts'][1]['methods'][0]['value'] = student['contacts'][1][
                'methods'][0]['value']
            obj['contacts'][1]['methods'][1]['value'] = student['contacts'][1][
                'methods'][1]['value']
            obj['contacts'][1]['methods'][2]['value'] = student['contacts'][1][
                'methods'][2]['value']

        student = Student.from_dict(obj)
        to_put = [student]
        ndb.put_multi(to_put)

        self.write_json_response(student.to_dict())