class Schema(object):
     key = fields.String(description='The unique name of the resource.',
                         enum=CONFIGURATION_KEYS)
     value = fields.Custom(
         fields.Any(),
         attribute='key',
         description='The current value of the configuration setting. ',
         io='r',
         formatter=get_configuration,
     )
     description = fields.Custom(
         fields.String(),
         attribute='key',
         description=
         'An explanation of what the configuration setting does.',
         io='r',
         formatter=lambda key: _DEFAULT_CONFIGURATION_STORE[key][
             'description'],
     )
     defaultValue = fields.Custom(
         fields.Any(),
         attribute='key',
         description='The default value of the setting.',
         io='r',
         formatter=lambda key: _DEFAULT_CONFIGURATION_STORE[key]['value'],
     )
Example #2
0
        class Box(ModelResource):
            class Schema:
                description = fields.String()
                is_open = fields.Boolean(default=False)

            class Meta:
                model = 'box'
                manager = MemoryManager
                include_id = True
                include_type = True

            @ItemRoute.GET()
            def verbose_description(self, box):
                return box["description"] + " box"

            verbose_description.response_schema = fields.String()

            @ItemRoute.POST()
            def open(self, box):
                return self.manager.update(box, {"is_open": True})

            @Route.GET(schema=FieldSet({'greeting': fields.String()}))
            def greet(self, greeting):
                return greeting + " box!"

            open.response_schema = fields.Inline('self', attribute='Test')
Example #3
0
    def test_string_enum(self):
        foo = fields.String(enum=['foo', 'bar'])
        foo_nullable = fields.String(enum=['foo', 'bar'], nullable=True)

        with self.assertRaises(ValidationError):
            foo.convert("fork")

        self.assertEqual("foo", foo.convert("foo"))
        self.assertEqual("foo", foo_nullable.convert("foo"))
        self.assertEqual(None, foo_nullable.convert(None))
Example #4
0
    def agroup_summary(self, id: fields.String(),
                       year: fields.Integer()) -> fields.String():
        agroup_id = id
        report_year = year
        # current_year = datetime.datetime.now().year
        gs = [g.id for g in models.Group.query.filter(
            models.Group._deleted_at == None,
            models.Group.associate_group_id == agroup_id,
            models.Group.created_at <= report_year).all()]
        response = {
            # 'total_of_gr': len(gs),
            'total_of_gr': len(gs),
            'total_of_farmer': 0,
            'total_of_male': 0,
            'total_of_female': 0,
            'total_of_cert': 0,
            'total_of_area': 0,
            'total_of_approved_area': 0
        }
        start_time = datetime.datetime(report_year, 1, 1) \
            .strftime('%Y-%m-%d')
        end_time = datetime.datetime(report_year, 12, 30) \
            .strftime('%Y-%m-%d')
        for g in gs:
            cs = models.Certificate.query.filter(
                    models.Certificate.owner_group_id == g,
                    models.Certificate.certificate_expiry_date >= start_time,
                    models.Certificate.certificate_expiry_date <= end_time)\
                    .all()
            for cert in cs:
                if cert.re_verify_status == \
                        c.CertificateReVerifyStatusType.adding or \
                        cert.re_verify_status == \
                        c.CertificateReVerifyStatusType.converting:
                    response['total_of_area'] += cert.group_area
                if cert.re_verify_status == \
                        c.CertificateReVerifyStatusType.adding and \
                        cert.status == c.CertificateStatusType.approved:
                    response['total_of_approved_area'] += cert.group_area
                    response['total_of_cert'] += 1

            fs = models.Farmer.query.filter(
                models.Farmer.group_id == g,
                models.Farmer._deleted_at == None,
                models.Farmer.created_at <= report_year).all()
            males = models.Farmer.query.filter(
                models.Farmer.group_id == g,
                models.Farmer._deleted_at == None,
                models.Farmer.gender == 1,
                models.Farmer.created_at <= report_year).all()
            response['total_of_farmer'] += len(fs)
            response['total_of_male'] += len(males)
        response['total_of_female'] = \
            response['total_of_farmer'] - response['total_of_male']
        return json.dumps(response)
Example #5
0
 class Schema:
     province = fields.Inline('province')
     ward = fields.Inline('ward')
     district = fields.Inline('district')
     associate_group = fields.Inline('associate_group')
     province_id = fields.String()
     associate_group_id = fields.String()
     district_id = fields.String()
     ward_id = fields.String()
     _deleted_at = fields.DateString()
     _deleted_at._schema = c.DATETIME_SCHEMA
Example #6
0
 class Schema:
     owner_group = fields.Inline('group')
     owner_farmer = fields.Inline('farmer')
     certificate_start_date = fields.DateString()
     certificate_expiry_date = fields.DateString()
     certificate_expiry_date._schema = c.DATETIME_SCHEMA
     certificate_start_date._schema = c.DATETIME_SCHEMA
     owner_group_id = fields.String()
     owner_farmer_id = fields.String()
     _deleted_at = fields.DateString()
     _deleted_at._schema = c.DATETIME_SCHEMA
Example #7
0
 def group_summary(self, id: fields.String()) -> fields.String():
     total_farmer = models.Farmer.query\
         .filter_by(group_id=id, _deleted_at=None).all()
     total_male = models.Farmer.query\
         .filter_by(group_id=id, _deleted_at=None, gender=1).all()
     response = {
         'total_of_farmer': len(total_farmer),
         'total_of_male': len(total_male),
         'total_of_female': len(total_farmer) - len(total_male)
     }
     return json.dumps(response)
Example #8
0
    def test_fieldset_schema_io(self):
        fs = FieldSet({
            "id": fields.Number(io='r'),
            "name": fields.String(),
            "secret": fields.String(io='c'),
            "updateOnly": fields.String(io='u'),
        })

        self.assertEqual(
            {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "number",
                        "readOnly": True
                    },
                    "name": {
                        "type": "string"
                    },
                },
            },
            fs.response,
        )

        self.assertEqual(
            {
                "type": "object",
                "additionalProperties": False,
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "updateOnly": {
                        "type": "string"
                    },
                },
            },
            fs.update,
        )

        self.assertEqual(
            {
                "name": {
                    "type": "string"
                },
                "secret": {
                    "type": "string"
                }
            },
            fs.create['properties'],
        )

        self.assertEqual({"name", "secret"}, set(fs.create['required']))
Example #9
0
 def test_fieldset_format(self):
     self.assertEqual({
         "number": 42,
         "constant": "constant"
     },
                      FieldSet({
                          "number": fields.Number(),
                          "constant": fields.String(io='r'),
                          "secret": fields.String(io='w'),
                      }).format({
                          "number": 42,
                          "constant": "constant",
                          "secret": "secret"
                      }))
Example #10
0
        class Recipe(ModelResource):
            class Schema:
                name = fields.String()

            class Meta:
                model = 'recipe'
                manager = MemoryManager
                id_field_class = fields.Integer
                include_type = True

            ingredients = ItemAttributeRoute(
                fields.Array(
                    fields.Object({
                        "ingredient": fields.String(),
                        "amount": fields.String()
                    })))
Example #11
0
    class Schema(object):
        name = fields.String(
            description=
            'A unique human-readable name to denote the query policy.')

        description = fields.String(
            description=
            'A description of what data the query policy is governing the access to.'
        )

        policyFilters = POLICY_FILTERS_SCHEMA

        resource = fields.ItemUri(
            'web.server.api.permission_api_models.BackendResource',
            attribute='resource_id',
        )
Example #12
0
 class Schema(object):
     name = fields.Custom(
         fields.String(enum=RESOURCE_TYPES),
         formatter=lambda rsc_type: rsc_type.name,
         title='Name',
         description='The string representation of the resource type.',
     )
Example #13
0
 def test_string_schema(self):
     self.assertEqual({
                          "type": "string",
                          "minLength": 2,
                          "maxLength": 3,
                          "pattern": "[A-Z][0-9]{1,2}",
                      }, fields.String(min_length=2, max_length=3, pattern="[A-Z][0-9]{1,2}").response)
Example #14
0
    class Schema(object):
        textId = fields.String(description='The string id of an indicator',
                               attribute='text_id')
        text = fields.String(description='Indicator description text',
                             attribute='text')

        groupId = fields.Integer(description='Indicator group ID',
                                 attribute='group_id',
                                 io='w')

        indicatorGroup = fields.ToOne('indicator_groups',
                                      attribute='indicator_group',
                                      io='r')

        constituents = fields.ToMany(
            'web.server.api.indicator_api_models.IndicatorResource')
Example #15
0
 def area(self) -> fields.String():
     approved = request.args.get('approved')
     if approved == 'True' or approved == 'true':
         approved = True
     else:
         approved = False
     associate_group_id = current_user.associate_group_id
     if associate_group_id and is_region_role():
         gs = [g.id for g in models.Group.query.filter_by(
             associate_group_id=associate_group_id, _deleted_at=None).all()]
     else:
         gs = [g.id for g in models.Group.query.filter_by(
             _deleted_at=None).all()]
     sum = 0
     today = datetime.date.today()
     for g in gs:
         cs = models.Certificate.query.filter_by(
             owner_group_id=g, _deleted_at=None).all()
         for cert in cs:
             if cert.certificate_expiry_date and \
                             cert.certificate_expiry_date >= today:
                 if approved and cert.re_verify_status == \
                         c.CertificateReVerifyStatusType.adding and \
                         cert.status == \
                         c.CertificateStatusType.approved:
                     sum += cert.group_area
                 if not approved and \
                         (cert.re_verify_status ==
                             c.CertificateReVerifyStatusType.adding or
                         cert.re_verify_status ==
                             c.CertificateReVerifyStatusType.converting):
                     sum += cert.group_area
     return sum
Example #16
0
    class Meta:
        model = UserQuerySession
        filters = {'queryUuid': True}

        id_attribute = 'query_uuid'
        id_field_class = fields.String()
        permissions = {'read': 'view_resource'}
Example #17
0
        class FooMixin(object):
            class Schema:
                field_b = fields.Integer(io='r')
                field_c = fields.Integer(io='r')

            @Route.GET(response_schema=fields.String())
            def success(self):
                return 'success'
Example #18
0
    def test_fieldset_rebind(self):
        class FooResource(Resource):
            pass

        class BarResource(Resource):
            pass

        FieldSet({"foo": fields.String()}).bind(FooResource).bind(BarResource)
Example #19
0
 def session_log(self, session_uuid: fields.UUID()) -> fields.String():
     session = Session.query.filter(Session.uuid == session_uuid).first()
     session_log_path = path.session_logs + session.start.date().isoformat().replace('-', '') + '/' + \
         session.user.username + '_' + session.host.hostname + '_' + \
         session.start.time().isoformat().split('.')[0].replace(':', '') + '_' + \
         session_uuid + '.log'
     with open(session_log_path, 'rb') as session_log:
         return base64.b64encode(session_log.read()).decode(
             'utf-8', 'strict')
Example #20
0
 def test_inherited_filters(self):
     self.assertEqual(
         {
             'string': {
                 'eq': filters.EqualFilter
             },
             'email': {
                 None: filters.EqualFilter,
                 'eq': filters.EqualFilter,
                 'ne': filters.NotEqualFilter,
                 'in': filters.InFilter,
                 'contains': filters.StringContainsFilter,
                 'icontains': filters.StringIContainsFilter,
                 'endswith': filters.EndsWithFilter,
                 'iendswith': filters.IEndsWithFilter,
                 'startswith': filters.StartsWithFilter,
                 'istartswith': filters.IStartsWithFilter,
             },
             'uri': {
                 'eq': filters.EqualFilter,
                 'ne': filters.NotEqualFilter,
                 'endswith': filters.EndsWithFilter,
             },
             'date_string': {
                 None: filters.EqualFilter,
                 'eq': filters.EqualFilter,
                 'ne': filters.NotEqualFilter,
                 'in': filters.InFilter,
                 'lt': filters.LessThanFilter,
                 'lte': filters.LessThanEqualFilter,
                 'gt': filters.GreaterThanFilter,
                 'gte': filters.GreaterThanEqualFilter,
                 'between': filters.DateBetweenFilter,
             },
         },
         filters_for_fields(
             {
                 'string': fields.String(),
                 # inherit from String
                 'email': fields.Email(),
                 'uri': fields.Uri(),
                 # no filter defined for Raw
                 'raw': fields.Raw({}),
                 # inherits from String, but there is a filter for DateString
                 'date_string': fields.DateString(),
             },
             filter_names=FILTER_NAMES,
             filters_by_type=FILTERS_BY_TYPE,
             filters_expression={
                 'string': ['eq'],
                 'email': True,
                 'uri': ['eq', 'ne', 'endswith'],
                 '*': True,
             },
         ),
     )
Example #21
0
    def resolve_permissions(
        self, user, project_id: fields.Integer(minimum=1)
    ) -> fields.List(fields.String()):
        permissions = (UserPermissionLinker.query.filter_by(
            project_id=project_id, user_id=user.id).join(
                UserPermissionLinker.permission).with_entities(
                    Permission.slug).all())

        permissions = list(map(lambda x: x.slug, permissions))

        return permissions
Example #22
0
 class Schema(object):
     name = fields.String(description='The unique name of the resource.')
     resourceType = fields.Custom(
         fields.String(),
         attribute='resource_type',
         converter=None,
         formatter=lambda rsc_type: rsc_type.name.name,
         title='resourceType',
         description='The string representation of the resource type.',
         io='r',
     )
     users = fields.List(
         CONCISE_USER_SCHEMA,
         description=
         'The user(s) that hold one or more roles for this resource.',
         io='r',
     )
     groups = fields.List(
         CONCISE_GROUP_SCHEMA,
         description=
         'The group(s) that hold one or more roles for this resource.',
         io='r',
     )
Example #23
0
 class Schema(object):
     username = USERNAME_SCHEMA
     roles = fields.Custom(
         ROLE_MAP_SCHEMA,
         description='The role(s) that the user currently possesses.',
         attribute='roles',
         formatter=role_list_as_map,
         default=[],
     )
     firstName = fields.String(
         description='The first name of the user.',
         attribute='first_name',
         pattern=INTERNATIONALIZED_ALPHANUMERIC_AND_DELIMITER,
     )
     lastName = fields.String(
         description='The last name of the user.',
         attribute='last_name',
         pattern=INTERNATIONALIZED_ALPHANUMERIC_AND_DELIMITER_OR_EMPTY,
     )
     phoneNumber = fields.String(
         description='The phone number of the user.',
         attribute='phone_number')
     # Disabling this warning because Hyper-Schema is enforcing
     # that the value of this field MUST match one of the values
     # of the Enum.
     # pylint:disable=E1136
     # pylint: disable=E1101
     status = fields.Custom(
         fields.String(enum=USER_STATUSES),
         attribute='status_id',
         formatter=lambda status_id: UserStatusEnum(status_id).name.lower(),
         converter=lambda status_value: UserStatusEnum[status_value.upper()]
         .value,
         default=UserStatusEnum.ACTIVE.value,
         nullable=False,
     )
Example #24
0
 class Schema(object):
     permissions = fields.List(
         PERMISSION_SCHEMA,
         title='Role Permissions',
         description='The permissions the role has.',
     )
     resourceType = fields.Custom(
         fields.String(),
         attribute='resource_type',
         converter=None,
         formatter=lambda rsc_type: rsc_type.name.name,
         title='resourceType',
         description='The resource type this role is associated with.',
         io='r',
     )
Example #25
0
 def gender(self) -> fields.String():
     gender = int(request.args.get('type'))
     associate_group_id = current_user.associate_group_id
     if associate_group_id and is_region_role():
         gs = [g.id for g in models.Group.query.filter_by(
             associate_group_id=associate_group_id, _deleted_at=None).all()]
     else:
         gs = [g.id for g in models.Group.query.filter_by(
             _deleted_at=None).all()]
     sum = 0
     for g in gs:
         count = models.Farmer.query.filter_by(
             group_id=g, _deleted_at=None, gender=gender).all()
         sum += len(count)
     return sum
Example #26
0
        class FooResource(Resource):
            @Route.GET
            def foo(self):
                return 'foo'

            @Route.GET
            def baz(self):
                return 'baz'

            @baz.POST
            def baz(self, value):
                return 'baz: {}'.format(value)

            baz.request_schema = fields.String()

            class Meta:
                name = 'foo'
                exclude_routes = ('readBaz', )
Example #27
0
    def test_string_convert(self):
        with self.assertRaises(ValidationError):
            fields.String(min_length=8).convert("123456")

        with self.assertRaises(ValidationError):
            fields.String(max_length=10).convert("abcdefghijklmnopqrstuvwxyz")

        with self.assertRaises(ValidationError):
            fields.String(pattern="^[fF]oo$").convert("Boo")

        self.assertEqual("foo", fields.String(pattern="^[fF]oo$").convert("foo"))
        self.assertEqual("123456", fields.String().convert("123456"))
        self.assertEqual(None, fields.String(nullable=True).convert(None))
Example #28
0
    def test_fieldset_parse_request(self):
        app = Flask(__name__)
        env = {}
        with app.test_request_context():
            env = request.environ

        # Ensure allow empty POST
        fs = FieldSet(None)
        env['REQUEST_METHOD'] = 'POST'
        fs.parse_request(Request(env))

        # Ensure failure when there are fields
        fs = FieldSet({'field': fields.String()})
        with self.assertRaises(RequestMustBeJSON):
            fs.parse_request(Request(env))

        # Successful POST with data
        env['wsgi.input'] = StringIO('{"field": "data"}')
        env['CONTENT_TYPE'] = 'application/json'
        env['CONTENT_LENGTH'] = '17'
        fs.parse_request(Request(env))
Example #29
0
 def session_timed_log(
     self, session_uuid: fields.UUID()) -> fields.List(fields.String()):
     session = Session.query.filter(Session.uuid == session_uuid).first()
     session_base_path = path.session_logs + session.start.date().isoformat().replace('-', '') + '/' + \
         session.user.username + '_' + session.host.hostname + '_' + \
         session.start.time().isoformat().split('.')[0].replace(':', '') + '_' + \
         session_uuid
     session_log_path = session_base_path + '.log'
     session_timer_path = session_base_path + '.timer'
     results = []
     session_time = 0.
     session_seek = 0
     with open(session_log_path, 'rb') as session_log:
         with open(session_timer_path, 'r') as session_timer:
             session_data = session_log.read()
             # todo improve this (use csv reader?)
             lines = session_timer.readlines()
             for l in lines:
                 rel_time, rel_seek = l.split(' ')
                 rel_time = float(rel_time)
                 rel_seek = int(rel_seek)
                 session_time += rel_time
                 current_seek = session_seek
                 session_seek += rel_seek
                 results.append({
                     'session_time':
                     session_time,
                     'session_seek':
                     session_seek,
                     'session_size':
                     len(session_data),
                     'data_size':
                     rel_seek,
                     'data':
                     base64.b64encode(
                         session_data[current_seek:session_seek]).decode(
                             'utf-8', 'strict')
                 })
     return results
Example #30
0
class UserQuerySessionResource(PrincipalResource):
    '''Potion class for interacting with saved queries.
    '''
    class Meta:
        model = UserQuerySession
        filters = {'queryUuid': True}

        id_attribute = 'query_uuid'
        id_field_class = fields.String()
        permissions = {'read': 'view_resource'}

    class Schema:
        queryUuid = fields.String(attribute='query_uuid', nullable=True)
        userId = fields.Integer(attribute='user_id')
        queryBlob = fields.Any(attribute='query_blob')

    # pylint: disable=E1101
    @ItemRoute.GET('/by_query_uuid')
    # pylint: disable=R0201
    def by_query_uuid(self, user_query_session):
        return {
            'queryBlob': user_query_session.query_blob,
            'queryUuid': user_query_session.query_uuid,
            'username': user_query_session.user.username,
        }

    # pylint: disable=E1101
    @Route.POST(
        '/generate_link',
        rel='generateLink',
        schema=fields.Inline('self'),
        response_schema=fields.String(),
    )
    # pylint: disable=R0201
    def generate_link(self, query_session):
        return store_query_and_generate_link(query_session)