コード例 #1
0
class RequestSchema(Schema):
    class Meta():
        dateformat = '%Y-%m-%dT%H:%M:%S.%fZ'
        strict = True

    starttime = fields.DateTime(
        required=False,
        metadata={"label": "Minimum time"},
        description="Start data selection from this UTC datetime")

    endtime = fields.DateTime(
        required=False,
        metadata={"label": "Maximum time"},
        description="End data selection at this UTC datetime")

    maxlatitude = fields.Float(validate=validate.Range(max=90, min=-90),
                               required=False,
                               metadata={"label": "Maximum latitude"},
                               description="Maximum latitude")

    minlatitude = fields.Float(validate=validate.Range(max=90, min=-90),
                               required=False,
                               metadata={"label": "Minimum latitude"},
                               description="Minimum latitude")

    maxlongitude = fields.Float(validate=validate.Range(max=180, min=-180),
                                required=False,
                                metadata={"label": "Maximum longitude"},
                                description="Maximum longitude")

    minlongitude = fields.Float(validate=validate.Range(max=180, min=-180),
                                required=False,
                                metadata={"label": "Minimum longitude"},
                                description="Minimum longitude")

    mindepth = fields.Float(required=False,
                            metadata={"label": "Minimum depth"},
                            description="Minimum depth")

    maxdepth = fields.Float(required=False,
                            metadata={"label": "Maximum depth"},
                            description="Maximum depth")

    minmagnitude = fields.Float(required=False,
                                metadata={"label": "Minimum magnitude"},
                                description="Minimum magnitude")

    maxmagnitude = fields.Float(required=False,
                                metadata={"label": "Maximum magnitude"},
                                description="Maximum magnitude")

    includeallorigins = fields.String(
        validate=validate.OneOf(['true', 'false']),
        default='false',
        missing='false',
        metadata={"label": "Should all origins be included?"})

    includeallmagnitudes = fields.String(
        validate=validate.OneOf(['true', 'false']),
        default='false',
        missing='false',
        metadata={"label": "Should all magnitudes be included?"})

    includearrivals = fields.String(
        validate=validate.OneOf(['true', 'false']),
        default='false',
        missing='false',
        metadata={"label": "Should arrivals be included?"})

    eventid = fields.String(metadata={"label": "Event ID"},
                            validate=validate.Length(min=1, max=300),
                            description="Event ID")

    limit = fields.Integer(metadata={"label": "How many results to return"},
                           description="How many results to return")

    offset = fields.Integer(
        metadata={"label": "Offset from the begining of the result set"},
        description="Offset from the begining of the result set")

    contributor = fields.String(metadata={"label": "Contributor"},
                                validate=validate.Length(min=1, max=300),
                                description="Contributor")

    catalog = fields.String(metadata={"label": "Catalog"},
                            validate=validate.Length(min=1, max=300),
                            description="Catalog")

    updatedafter = fields.DateTime(required=False,
                                   metadata={"label": "Updated after"},
                                   description="Updated after")

    format = fields.String(validate=validate.OneOf(["xml", "text"]),
                           default="xml",
                           missing="xml",
                           metadata={"label": "Format of returned data"})

    nodate = fields.String(metadata={"label": "No data"},
                           validate=validate.Length(min=1, max=300),
                           description="No data")

    latitude = fields.Float(validate=validate.Range(max=90, min=-90),
                            required=False,
                            metadata={"label": "Latitude"},
                            description="Latitude")

    longitude = fields.Float(validate=validate.Range(max=180, min=-180),
                             required=False,
                             metadata={"label": "Longitude"},
                             description="Longitude")

    minradius = fields.Float(required=False,
                             metadata={"label": "Minimum radius"},
                             description="Minimum radius")

    maxradius = fields.Float(required=False,
                             metadata={"label": "Maximum radius"},
                             description="Maximum radius")
コード例 #2
0
ファイル: users.py プロジェクト: 99Kies/CTFd_python3
class UserSchema(ma.ModelSchema):
    class Meta:
        model = Users
        include_fk = True
        dump_only = ("id", "oauth_id", "created")
        load_only = ("password",)

    name = field_for(
        Users,
        "name",
        required=True,
        allow_none=False,
        validate=[
            validate.Length(min=1, max=128, error="User names must not be empty")
        ],
    )
    email = field_for(
        Users,
        "email",
        allow_none=False,
        validate=[
            validate.Email("Emails must be a properly formatted email address"),
            validate.Length(min=1, max=128, error="Emails must not be empty"),
        ],
    )
    website = field_for(
        Users,
        "website",
        validate=[
            # This is a dirty hack to let website accept empty strings so you can remove your website
            lambda website: validate.URL(
                error="Websites must be a proper URL starting with http or https",
                schemes={"http", "https"},
            )(website)
            if website
            else True
        ],
    )
    country = field_for(Users, "country", validate=[validate_country_code])
    password = field_for(Users, "password")

    @pre_load
    def validate_name(self, data):
        name = data.get("name")
        if name is None:
            return

        existing_user = Users.query.filter_by(name=name).first()
        current_user = get_current_user()
        if is_admin():
            user_id = data.get("id")
            if user_id:
                if existing_user and existing_user.id != user_id:
                    raise ValidationError(
                        "User name has already been taken", field_names=["name"]
                    )
            else:
                if existing_user:
                    if current_user:
                        if current_user.id != existing_user.id:
                            raise ValidationError(
                                "User name has already been taken", field_names=["name"]
                            )
                    else:
                        raise ValidationError(
                            "User name has already been taken", field_names=["name"]
                        )
        else:
            if name == current_user.name:
                return data
            else:
                name_changes = get_config("name_changes", default=True)
                if bool(name_changes) is False:
                    raise ValidationError(
                        "Name changes are disabled", field_names=["name"]
                    )
                if existing_user:
                    raise ValidationError(
                        "User name has already been taken", field_names=["name"]
                    )

    @pre_load
    def validate_email(self, data):
        email = data.get("email")
        if email is None:
            return

        existing_user = Users.query.filter_by(email=email).first()
        current_user = get_current_user()
        if is_admin():
            user_id = data.get("id")
            if user_id:
                if existing_user and existing_user.id != user_id:
                    raise ValidationError(
                        "Email address has already been used", field_names=["email"]
                    )
            else:
                if existing_user:
                    if current_user:
                        if current_user.id != existing_user.id:
                            raise ValidationError(
                                "Email address has already been used",
                                field_names=["email"],
                            )
                    else:
                        raise ValidationError(
                            "Email address has already been used", field_names=["email"]
                        )
        else:
            if email == current_user.email:
                return data
            else:
                confirm = data.get("confirm")

                if bool(confirm) is False:
                    raise ValidationError(
                        "Please confirm your current password", field_names=["confirm"]
                    )

                test = verify_password(
                    plaintext=confirm, ciphertext=current_user.password
                )
                if test is False:
                    raise ValidationError(
                        "Your previous password is incorrect", field_names=["confirm"]
                    )

                if existing_user:
                    raise ValidationError(
                        "Email address has already been used", field_names=["email"]
                    )
                if check_email_is_whitelisted(email) is False:
                    raise ValidationError(
                        "Only email addresses under {domains} may register".format(
                            domains=get_config("domain_whitelist")
                        ),
                        field_names=["email"],
                    )
                if get_config("verify_emails"):
                    current_user.verified = False

    @pre_load
    def validate_password_confirmation(self, data):
        password = data.get("password")
        confirm = data.get("confirm")
        target_user = get_current_user()

        if is_admin():
            pass
        else:
            if password and (bool(confirm) is False):
                raise ValidationError(
                    "Please confirm your current password", field_names=["confirm"]
                )

            if password and confirm:
                test = verify_password(
                    plaintext=confirm, ciphertext=target_user.password
                )
                if test is True:
                    return data
                else:
                    raise ValidationError(
                        "Your previous password is incorrect", field_names=["confirm"]
                    )
            else:
                data.pop("password", None)
                data.pop("confirm", None)

    views = {
        "user": [
            "website",
            "name",
            "country",
            "affiliation",
            "bracket",
            "id",
            "oauth_id",
        ],
        "self": [
            "website",
            "name",
            "email",
            "country",
            "affiliation",
            "bracket",
            "id",
            "oauth_id",
            "password",
        ],
        "admin": [
            "website",
            "name",
            "created",
            "country",
            "banned",
            "email",
            "affiliation",
            "secret",
            "bracket",
            "hidden",
            "id",
            "oauth_id",
            "password",
            "type",
            "verified",
        ],
    }

    def __init__(self, view=None, *args, **kwargs):
        if view:
            if isinstance(view, string_types):
                kwargs["only"] = self.views[view]
            elif isinstance(view, list):
                kwargs["only"] = view

        super(UserSchema, self).__init__(*args, **kwargs)
        for school in result['schools']:
            assert isinstance(school, models.School)
            assert isinstance(school.id, int)
        dump_result = unpack(schema.dump(result))
        assert dump_result == data


def _repr_validator_list(validators):
    return sorted([repr(validator) for validator in validators])


@pytest.mark.parametrize(
    'defaults,new,expected',
    [
        (
            [validate.Length()],
            [],
            [validate.Length()],
        ),
        (
            [validate.Range(max=100),
             validate.Length(min=3)],
            [validate.Range(max=1000)],
            [validate.Range(max=1000),
             validate.Length(min=3)],
        ),
        (
            [validate.Range(max=1000)],
            [validate.Length(min=3)],
            [validate.Range(max=1000),
             validate.Length(min=3)],
コード例 #4
0
class RoomTypeSchema(ma.Schema):
    id = fields.Integer(dump_only=False)
    url = ma.URLFor('api.roomtyperesource', id='<id>', _external=True)
    name = fields.String(required=True, validate=validate.Length(3))
    sleeps = fields.Integer(allow_none=True)
コード例 #5
0
class DictionaryDeleteSchema(Schema):
    key = fields.Str(required=True, validate=validate.Length(max=20))
コード例 #6
0
from marshmallow import fields, validate


login_schema = {
    'email': fields.Str(validate=validate.Length(min=1), max=320, required=True),
    'password': fields.Str(validate=validate.Length(min=6, max=100), required=True)
}
コード例 #7
0
class OwnerSchema(ma.Schema):
    id = fields.Integer(dump_only=True)
    url = ma.URLFor('api.ownerresource', id='<id>', _external=True)
    name = fields.String(required=True, validate=validate.Length(3))
    email = fields.Nested('OwnerEmailSchema', many=True)
コード例 #8
0
 class UserLoginSchema(Schema):
     login = fields.String(required=True, validate=validate.Length(min=3, max=100))
     password = fields.String(required=True, validate=validate.Length(min=3, max=100))
コード例 #9
0
ファイル: commands.py プロジェクト: claudinoac/booksales-api
class CreateCustomerCommand:
    first_name: str = field(metadata=dict(validate=validate.Length(min=3, max=250)))
    last_name: str = field(metadata=dict(validate=validate.Length(min=3, max=250)))
    email: str = field(metadata=dict(validate=validate.Email()))
    phone: int = field(metadata=dict(validate=validate.Range(min=9999999)))
コード例 #10
0
class ResetPasswordSchema(marshmallow.Schema):
    email = fields.Email(required=True, validate=ensure_unique_user)
    password = fields.Str(required=True,
                          validate=[validate.Length(min=8, max=200)])
コード例 #11
0
class ChangePasswordSchema(marshmallow.Schema):
    old_password = fields.Str(required=True,
                              validate=[validate.Length(min=8, max=200)])
    new_password = fields.Str(required=True,
                              validate=[validate.Length(min=8, max=200)])
コード例 #12
0
class AuthSchema(marshmallow.Schema):
    email = fields.Email(required=True)
    password = fields.Str(required=True,
                          validate=[validate.Length(min=8, max=200)])
コード例 #13
0
def test_length_max():
    assert validate.Length(1, 3)("foo") == "foo"
    assert validate.Length(1, 3)([1, 2, 3]) == [1, 2, 3]
    assert validate.Length(None, 1)("a") == "a"
    assert validate.Length(None, 1)([1]) == [1]
    assert validate.Length()("") == ""
    assert validate.Length()([]) == []
    assert validate.Length(2, 2)("ab") == "ab"
    assert validate.Length(2, 2)([1, 2]) == [1, 2]

    with pytest.raises(ValidationError):
        validate.Length(1, 2)("foo")
    with pytest.raises(ValidationError):
        validate.Length(1, 2)([1, 2, 3])
    with pytest.raises(ValidationError):
        validate.Length(None, 2)("foo")
    with pytest.raises(ValidationError):
        validate.Length(None, 2)([1, 2, 3])
コード例 #14
0
def test_length_min():
    assert validate.Length(3, 5)("foo") == "foo"
    assert validate.Length(3, 5)([1, 2, 3]) == [1, 2, 3]
    assert validate.Length(0)("a") == "a"
    assert validate.Length(0)([1]) == [1]
    assert validate.Length()("") == ""
    assert validate.Length()([]) == []
    assert validate.Length(1, 1)("a") == "a"
    assert validate.Length(1, 1)([1]) == [1]

    with pytest.raises(ValidationError):
        validate.Length(4, 5)("foo")
    with pytest.raises(ValidationError):
        validate.Length(4, 5)([1, 2, 3])
    with pytest.raises(ValidationError):
        validate.Length(5)("foo")
    with pytest.raises(ValidationError):
        validate.Length(5)([1, 2, 3])
コード例 #15
0
ファイル: common.py プロジェクト: adarshsrivastava11/zenodo
class CommonMetadataSchemaV1(Schema, StrictKeysMixin, RefResolverMixin):
    """Common metadata schema."""

    doi = DOIField()
    publication_date = DateString(required=True)
    title = SanitizedUnicode(required=True, validate=validate.Length(min=3))
    creators = fields.Nested(
        PersonSchemaV1, many=True, validate=validate.Length(min=1))
    description = SanitizedHTML(
        required=True, validate=validate.Length(min=3))
    keywords = fields.List(SanitizedUnicode())
    notes = SanitizedUnicode()
    version = SanitizedUnicode()
    language = SanitizedUnicode()
    access_right = fields.Str(validate=validate.OneOf(
        choices=[
            AccessRight.OPEN,
            AccessRight.EMBARGOED,
            AccessRight.RESTRICTED,
            AccessRight.CLOSED,
        ],
    ))
    embargo_date = DateString()
    access_conditions = SanitizedHTML()
    subjects = fields.Nested(SubjectSchemaV1, many=True)
    contributors = fields.List(fields.Nested(ContributorSchemaV1))
    references = fields.List(SanitizedUnicode(attribute='raw_reference'))
    related_identifiers = fields.Nested(
        RelatedIdentifierSchemaV1, many=True)
    alternate_identifiers = fields.Nested(
        AlternateIdentifierSchemaV1, many=True)

    @validates('language')
    def validate_language(self, value):
        """Validate that language is ISO 639-3 value."""
        try:
            pycountry.languages.get(alpha_3=value)
        except KeyError:
            raise ValidationError(_('Language must be a lower-cased 3-letter ISO 639-3 string.'),
                field_name=['language']
            )

    @validates('embargo_date')
    def validate_embargo_date(self, value):
        """Validate that embargo date is in the future."""
        if arrow.get(value).date() <= arrow.utcnow().date():
            raise ValidationError(
                _('Embargo date must be in the future.'),
                field_names=['embargo_date']
            )

    @validates('license')
    def validate_license_ref(self, value):
        """Validate if license resolves."""
        if not self.validate_jsonref(value):
            raise ValidationError(
                _('Invalid choice.'),
                field_names=['license'],
            )

    @validates('grants')
    def validate_grants_ref(self, values):
        """Validate if license resolves."""
        for v in values:
            if not self.validate_jsonref(v):
                raise ValidationError(
                    _('Invalid grant.'),
                    field_names=['grants'],
                )

    @validates('doi')
    def validate_doi(self, value):
        """Validate if doi exists."""
        if value and has_request_context():
            required_doi = self.context.get('required_doi')
            if value == required_doi:
                return

            err = ValidationError(_('DOI already exists in Zenodo.'),
                                  field_names=['doi'])

            try:
                doi_pid = PersistentIdentifier.get('doi', value)
            except PIDDoesNotExistError:
                return

            # If the DOI exists, check if it's been assigned to this record
            # by fetching the recid and comparing both PIDs record UUID
            try:
                recid_pid = PersistentIdentifier.get(
                    'recid', self.context['recid'])
            except PIDDoesNotExistError:
                # There's no way to verify if this DOI belongs to this record
                raise err

            doi_uuid = doi_pid.get_assigned_object()
            recid_uuid = recid_pid.get_assigned_object()

            if doi_uuid and doi_uuid == recid_uuid:
                return
            else:  # DOI exists and belongs to a different record
                raise err

    @validates_schema()
    def validate_license(self, data):
        """Validate license."""
        acc = data.get('access_right')
        if acc in [AccessRight.OPEN, AccessRight.EMBARGOED] and \
                'license' not in data:
            raise ValidationError(
                _('Required when access right is open or embargoed.'),
                field_names=['license']
            )
        if acc == AccessRight.EMBARGOED and 'embargo_date' not in data:
            raise ValidationError(
                _('Required when access right is embargoed.'),
                field_names=['embargo_date']
            )
        if acc == AccessRight.RESTRICTED and 'access_conditions' not in data:
            raise ValidationError(
                _('Required when access right is restricted.'),
                field_names=['access_conditions']
            )

    @pre_load()
    def preload_accessrights(self, data):
        """Remove invalid access rights combinations."""
        # Default value
        if 'access_right' not in data:
            data['access_right'] = AccessRight.OPEN

        # Pop values which should not be set for a given access right.
        if data.get('access_right') not in [
                AccessRight.OPEN, AccessRight.EMBARGOED]:
            data.pop('license', None)
        if data.get('access_right') != AccessRight.RESTRICTED:
            data.pop('access_conditions', None)
        if data.get('access_right') != AccessRight.EMBARGOED:
            data.pop('embargo_date', None)

    @pre_load()
    def preload_publicationdate(self, data):
        """Default publication date."""
        if 'publication_date' not in data:
            data['publication_date'] = arrow.utcnow().date().isoformat()

    @post_load()
    def postload_keywords_filter(self, data):
        """Filter empty keywords."""
        if 'keywords' in data:
            data['keywords'] = [
                kw for kw in data['keywords'] if kw.strip()
            ]

    @post_load()
    def postload_references(self, data):
        """Filter empty references and wrap them."""
        if 'references' in data:
            data['references'] = [
                {'raw_reference': ref}
                for ref in data['references'] if ref.strip()
            ]
コード例 #16
0
class DictItemSchema(ma.SQLAlchemySchema):
    id = fields.Int(
        required=False,
        allow_none=False,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'id主键不能为空', 'invalid': '主键不合法'},
        data_key='id')

    dict_code = fields.String(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'dictCode字典编码不能为空', 'invalid': '字典编码不合法'},
        data_key='dictCode')

    item_code = fields.String(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'itemCode项编码不能为空', 'invalid': '项编码不合法'},
        data_key='itemCode')

    item_value = fields.String(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'itemValue项值不能为空', 'invalid': '项值不合法'},
        data_key='itemValue')

    sort_no = fields.Int(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'sortNo排序不能为空', 'invalid': '排序不合法'},
        data_key='sortNo')

    item_desc = fields.String(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'itemDesc描述不能为空', 'invalid': '描述不合法'},
        data_key='itemDesc')

    is_valid = fields.String(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'isValid是否有效 Y启用 N不启用不能为空', 'invalid': '是否有效 Y启用 N不启用不合法'},
        data_key='isValid')

    org_code = fields.String(
        required=False,
        allow_none=False,
        validate=[validate.Length(min=1)],

        error_messages={'required': 'orgCode机构代码不能为空', 'invalid': '机构代码不合法'},
        data_key='orgCode')

    create_by = fields.String(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'createBy创建人不能为空', 'invalid': '创建人不合法'},
        data_key='createBy')

    create_time = fields.DateTime(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],
        format='%Y-%m-%d %H:%M:%S',
        error_messages={'required': 'createTime创建时间不能为空', 'invalid': '创建时间不合法'},
        data_key='createTime')

    update_by = fields.String(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'updateBy更新人不能为空', 'invalid': '更新人不合法'},
        data_key='updateBy')

    update_time = fields.DateTime(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],
        format='%Y-%m-%d %H:%M:%S',
        error_messages={'required': 'updateTime更新时间不能为空', 'invalid': '更新时间不合法'},
        data_key='updateTime')

    revision = fields.Int(
        required=False,
        allow_none=True,
        # validate=[validate.Length(min=1)],

        error_messages={'required': 'revision版本号不能为空', 'invalid': '版本号不合法'},
        data_key='revision')

    class Meta:
        model = DictItem
        load_instance = True
        sqla_session = db.session
コード例 #17
0
class RankingItemSchema(RankingSchema):
    name = fields.Str(required=True, validate=[validate.Length(min=1, max=20)])
    mode = fields.Str(required=True)
    score = fields.Integer(required=True)
コード例 #18
0
ファイル: teams.py プロジェクト: mrigank-9594/srhctf_
class TeamSchema(ma.ModelSchema):
    class Meta:
        model = Teams
        include_fk = True
        dump_only = ('id', 'oauth_id', 'created', 'members')
        load_only = ('password', )

    name = field_for(Teams,
                     'name',
                     required=True,
                     validate=[
                         validate.Length(min=1,
                                         max=128,
                                         error='Team names must not be empty')
                     ])
    email = field_for(Teams,
                      'email',
                      validate=validate.Email(
                          'Emails must be a properly formatted email address'))
    website = field_for(
        Teams,
        'website',
        validate=[
            # This is a dirty hack to let website accept empty strings so you can remove your website
            lambda website: validate.
            URL(error=
                'Websites must be a proper URL starting with http or https',
                schemes={'http', 'https'})(website) if website else True
        ])
    country = field_for(Teams, 'country', validate=[validate_country_code])

    @pre_load
    def validate_name(self, data):
        name = data.get('name')
        if name is None:
            return

        existing_team = Teams.query.filter_by(name=name).first()
        current_team = get_current_team()
        # Admins should be able to patch anyone but they cannot cause a collision.
        if is_admin():
            team_id = int(data.get('id', 0))
            if team_id:
                if existing_team and existing_team.id != team_id:
                    raise ValidationError('Team name has already been taken',
                                          field_names=['name'])
            else:
                # If there's no Team ID it means that the admin is creating a team with no ID.
                if existing_team:
                    if current_team:
                        if current_team.id != existing_team.id:
                            raise ValidationError(
                                'Team name has already been taken',
                                field_names=['name'])
                    else:
                        raise ValidationError(
                            'Team name has already been taken',
                            field_names=['name'])
        else:
            # We need to allow teams to edit themselves and allow the "conflict"
            if data['name'] == current_team.name:
                return data
            else:
                name_changes = get_config('name_changes', default=True)
                if bool(name_changes) is False:
                    raise ValidationError('Name changes are disabled',
                                          field_names=['name'])

                if existing_team:
                    raise ValidationError('Team name has already been taken',
                                          field_names=['name'])

    @pre_load
    def validate_email(self, data):
        email = data.get('email')
        if email is None:
            return

        existing_team = Teams.query.filter_by(email=email).first()
        if is_admin():
            team_id = data.get('id')
            if team_id:
                if existing_team and existing_team.id != team_id:
                    raise ValidationError(
                        'Email address has already been used',
                        field_names=['email'])
            else:
                if existing_team:
                    raise ValidationError(
                        'Email address has already been used',
                        field_names=['email'])
        else:
            current_team = get_current_team()
            if email == current_team.email:
                return data
            else:
                if existing_team:
                    raise ValidationError(
                        'Email address has already been used',
                        field_names=['email'])

    @pre_load
    def validate_password_confirmation(self, data):
        password = data.get('password')
        confirm = data.get('confirm')

        if is_admin():
            pass
        else:
            current_team = get_current_team()
            current_user = get_current_user()

            if current_team.captain_id != current_user.id:
                raise ValidationError(
                    'Only the captain can change the team password',
                    field_names=['captain_id'])

            if password and (bool(confirm) is False):
                raise ValidationError('Please confirm your current password',
                                      field_names=['confirm'])

            if password and confirm:
                test = verify_password(plaintext=confirm,
                                       ciphertext=current_team.password)
                if test is True:
                    return data
                else:
                    raise ValidationError(
                        'Your previous password is incorrect',
                        field_names=['confirm'])
            else:
                data.pop('password', None)
                data.pop('confirm', None)

    @pre_load
    def validate_captain_id(self, data):
        captain_id = data.get('captain_id')
        if captain_id is None:
            return

        if is_admin():
            team_id = data.get('id')
            if team_id:
                target_team = Teams.query.filter_by(id=team_id).first()
            else:
                target_team = get_current_team()
            captain = Users.query.filter_by(id=captain_id).first()
            if captain in target_team.members:
                return
            else:
                raise ValidationError('Invalid Captain ID',
                                      field_names=['captain_id'])
        else:
            current_team = get_current_team()
            current_user = get_current_user()
            if current_team.captain_id == current_user.id:
                return
            else:
                raise ValidationError(
                    'Only the captain can change team captain',
                    field_names=['captain_id'])

    views = {
        'user': [
            'website',
            'name',
            'country',
            'affiliation',
            'bracket',
            'members',
            'id',
            'oauth_id',
            'captain_id',
        ],
        'self': [
            'website',
            'name',
            'email',
            'country',
            'affiliation',
            'bracket',
            'members',
            'id',
            'oauth_id',
            'password',
            'captain_id',
        ],
        'admin': [
            'website',
            'name',
            'created',
            'country',
            'banned',
            'email',
            'affiliation',
            'secret',
            'bracket',
            'members',
            'hidden',
            'id',
            'oauth_id',
            'password',
            'captain_id',
        ]
    }

    def __init__(self, view=None, *args, **kwargs):
        if view:
            if isinstance(view, string_types):
                kwargs['only'] = self.views[view]
            elif isinstance(view, list):
                kwargs['only'] = view

        super(TeamSchema, self).__init__(*args, **kwargs)
コード例 #19
0
# Class to add, update and delete data via SQLALchemy sessions
class CRUD():
    def add(self, resource):
        db.session.add(resource)
        return db.session.commit()

    def update(self):
        return db.session.commit()

    def delete(self, resource):
        db.session.delete(resource)
        return db.session.commit()


# Create the validation for what we see as "not blank"
NOT_BLANK = validate.Length(min=1, error='Field cannot be blank')
PASSWORD_LENGTH = validate.Length(min=10, error='Password too short')

#############################################
########## Models
#############################################

# https://pythonhosted.org/Flask-Security/quickstart.html
# Association Table for Roles and Users
roles_users = db.Table(
    'roles_users', db.Column('user_id', db.Integer(),
                             db.ForeignKey('user.id')),
    db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))


class Role(db.Model, RoleMixin):
コード例 #20
0
ファイル: Model.py プロジェクト: deepaksabat/Flask-Rest-API
class CommentSchema(ma.Schema):
    id = fields.Integer(dump_only=True)
    category_id = fields.Integer(required=True)
    comment = fields.String(required=True, validate=validate.Length(1))
    creation_date = fields.DateTime()
コード例 #21
0
class ResortSchema(ma.Schema):
    id = fields.Integer(dump_only=False)
    url = ma.URLFor('api.resortresource', id='<id>', _external=True)
    name = fields.String(required=True, validate=validate.Length(3))
コード例 #22
0
ファイル: Model.py プロジェクト: deepaksabat/Flask-Rest-API
class PostSchema(ma.Schema):
    id = fields.Integer(dump_only=True)
    category_id = fields.Integer(required=True)
    title = fields.String(required=True, validate=validate.Length(1))
    description = fields.String(required=True, validate=validate.Length(1))
    creation_date = fields.DateTime()
コード例 #23
0
class EventLogSchema(ma.Schema):
    id = fields.Integer(dump_only=True)
    url = ma.URLFor('api.eventlogresource', id='<id>', _external=True)
    timestamp = fields.DateTime(dump_only=True)
    google_id = fields.String(required=True)
    description = fields.String(required=True, validate=validate.Length(min=3))
コード例 #24
0
class TitleSchema(ma.Schema):
    title_id = fields.Integer(dump_only=True)
    title = fields.String(required=False, validate=validate.Length(1))
    #creation_date = fields.DateTime()
    description = fields.String(required=False, validate=validate.Length(1))
コード例 #25
0
class DictionaryPutSchema(Schema):
    value = fields.Str(required=True, validate=validate.Length(max=100))
コード例 #26
0
ファイル: users.py プロジェクト: csnp/njit-ctf
class UserSchema(ma.ModelSchema):
    class Meta:
        model = Users
        include_fk = True
        dump_only = ("id", "oauth_id", "created")
        load_only = ("password", )

    name = field_for(
        Users,
        "name",
        required=True,
        allow_none=False,
        validate=[
            validate.Length(min=1,
                            max=128,
                            error="User names must not be empty")
        ],
    )
    email = field_for(
        Users,
        "email",
        allow_none=False,
        validate=[
            validate.Email(
                "Emails must be a properly formatted email address"),
            validate.Length(min=1, max=128, error="Emails must not be empty"),
        ],
    )
    website = field_for(
        Users,
        "website",
        validate=[
            # This is a dirty hack to let website accept empty strings so you can remove your website
            lambda website: validate.URL(
                error=
                "Websites must be a proper URL starting with http or https",
                schemes={"http", "https"},
            )(website) if website else True
        ],
    )
    country = field_for(Users, "country", validate=[validate_country_code])
    password = field_for(Users, "password", required=True, allow_none=False)
    fields = Nested(UserFieldEntriesSchema,
                    partial=True,
                    many=True,
                    attribute="field_entries")

    @pre_load
    def validate_name(self, data):
        name = data.get("name")
        if name is None:
            return
        name = name.strip()

        existing_user = Users.query.filter_by(name=name).first()
        current_user = get_current_user()
        if is_admin():
            user_id = data.get("id")
            if user_id:
                if existing_user and existing_user.id != user_id:
                    raise ValidationError("User name has already been taken",
                                          field_names=["name"])
            else:
                if existing_user:
                    if current_user:
                        if current_user.id != existing_user.id:
                            raise ValidationError(
                                "User name has already been taken",
                                field_names=["name"])
                    else:
                        raise ValidationError(
                            "User name has already been taken",
                            field_names=["name"])
        else:
            if name == current_user.name:
                return data
            else:
                name_changes = get_config("name_changes", default=True)
                if bool(name_changes) is False:
                    raise ValidationError("Name changes are disabled",
                                          field_names=["name"])
                if existing_user:
                    raise ValidationError("User name has already been taken",
                                          field_names=["name"])

    @pre_load
    def validate_email(self, data):
        email = data.get("email")
        if email is None:
            return
        email = email.strip()

        existing_user = Users.query.filter_by(email=email).first()
        current_user = get_current_user()
        if is_admin():
            user_id = data.get("id")
            if user_id:
                if existing_user and existing_user.id != user_id:
                    raise ValidationError(
                        "Email address has already been used",
                        field_names=["email"])
            else:
                if existing_user:
                    if current_user:
                        if current_user.id != existing_user.id:
                            raise ValidationError(
                                "Email address has already been used",
                                field_names=["email"],
                            )
                    else:
                        raise ValidationError(
                            "Email address has already been used",
                            field_names=["email"])
        else:
            if email == current_user.email:
                return data
            else:
                confirm = data.get("confirm")

                if bool(confirm) is False:
                    raise ValidationError(
                        "Please confirm your current password",
                        field_names=["confirm"])

                test = verify_password(plaintext=confirm,
                                       ciphertext=current_user.password)
                if test is False:
                    raise ValidationError(
                        "Your previous password is incorrect",
                        field_names=["confirm"])

                if existing_user:
                    raise ValidationError(
                        "Email address has already been used",
                        field_names=["email"])
                if check_email_is_whitelisted(email) is False:
                    raise ValidationError(
                        "Only email addresses under {domains} may register".
                        format(domains=get_config("domain_whitelist")),
                        field_names=["email"],
                    )
                if get_config("verify_emails"):
                    current_user.verified = False

    @pre_load
    def validate_password_confirmation(self, data):
        password = data.get("password")
        confirm = data.get("confirm")
        target_user = get_current_user()

        if is_admin():
            pass
        else:
            if password and (bool(confirm) is False):
                raise ValidationError("Please confirm your current password",
                                      field_names=["confirm"])

            if password and confirm:
                test = verify_password(plaintext=confirm,
                                       ciphertext=target_user.password)
                if test is True:
                    return data
                else:
                    raise ValidationError(
                        "Your previous password is incorrect",
                        field_names=["confirm"])
            else:
                data.pop("password", None)
                data.pop("confirm", None)

    @pre_load
    def validate_fields(self, data):
        """
        This validator is used to only allow users to update the field entry for their user.
        It's not possible to exclude it because without the PK Marshmallow cannot load the right instance
        """
        fields = data.get("fields")
        if fields is None:
            return

        current_user = get_current_user()

        if is_admin():
            user_id = data.get("id")
            if user_id:
                target_user = Users.query.filter_by(id=data["id"]).first()
            else:
                target_user = current_user

            # We are editting an existing user
            if self.view == "admin" and self.instance:
                target_user = self.instance
                provided_ids = []
                for f in fields:
                    f.pop("id", None)
                    field_id = f.get("field_id")

                    # # Check that we have an existing field for this. May be unnecessary b/c the foriegn key should enforce
                    field = UserFields.query.filter_by(
                        id=field_id).first_or_404()

                    # Get the existing field entry if one exists
                    entry = UserFieldEntries.query.filter_by(
                        field_id=field.id, user_id=target_user.id).first()
                    if entry:
                        f["id"] = entry.id
                        provided_ids.append(entry.id)

                # Extremely dirty hack to prevent deleting previously provided data.
                # This needs a better soln.
                entries = (UserFieldEntries.query.options(
                    load_only("id")).filter_by(user_id=target_user.id).all())
                for entry in entries:
                    if entry.id not in provided_ids:
                        fields.append({"id": entry.id})
        else:
            provided_ids = []
            for f in fields:
                # Remove any existing set
                f.pop("id", None)
                field_id = f.get("field_id")
                value = f.get("value")

                # # Check that we have an existing field for this. May be unnecessary b/c the foriegn key should enforce
                field = UserFields.query.filter_by(id=field_id).first_or_404()

                if field.required is True and value.strip() == "":
                    raise ValidationError(f"Field '{field.name}' is required",
                                          field_names=["fields"])

                if field.editable is False:
                    raise ValidationError(
                        f"Field '{field.name}' cannot be editted",
                        field_names=["fields"],
                    )

                # Get the existing field entry if one exists
                entry = UserFieldEntries.query.filter_by(
                    field_id=field.id, user_id=current_user.id).first()

                if entry:
                    f["id"] = entry.id
                    provided_ids.append(entry.id)

            # Extremely dirty hack to prevent deleting previously provided data.
            # This needs a better soln.
            entries = (UserFieldEntries.query.options(
                load_only("id")).filter_by(user_id=current_user.id).all())
            for entry in entries:
                if entry.id not in provided_ids:
                    fields.append({"id": entry.id})

    @post_dump
    def process_fields(self, data):
        """
        Handle permissions levels for fields.
        This is post_dump to manipulate JSON instead of the raw db object

        Admins can see all fields.
        Users (self) can see their edittable and public fields
        Public (user) can only see public fields
        """
        # Gather all possible fields
        removed_field_ids = []
        fields = UserFields.query.all()

        # Select fields for removal based on current view and properties of the field
        for field in fields:
            if self.view == "user":
                if field.public is False:
                    removed_field_ids.append(field.id)
            elif self.view == "self":
                if field.editable is False and field.public is False:
                    removed_field_ids.append(field.id)

        # Rebuild fuilds
        fields = data.get("fields")
        if fields:
            data["fields"] = [
                field for field in fields
                if field["field_id"] not in removed_field_ids
            ]

    views = {
        "user": [
            "website",
            "name",
            "country",
            "affiliation",
            "bracket",
            "id",
            "oauth_id",
            "fields",
            "team_id",
        ],
        "self": [
            "website",
            "name",
            "email",
            "country",
            "affiliation",
            "bracket",
            "id",
            "oauth_id",
            "password",
            "fields",
            "team_id",
        ],
        "admin": [
            "website",
            "name",
            "created",
            "country",
            "banned",
            "email",
            "affiliation",
            "secret",
            "bracket",
            "hidden",
            "id",
            "oauth_id",
            "password",
            "type",
            "verified",
            "fields",
            "team_id",
        ],
    }

    def __init__(self, view=None, *args, **kwargs):
        if view:
            if isinstance(view, string_types):
                kwargs["only"] = self.views[view]
            elif isinstance(view, list):
                kwargs["only"] = view
        self.view = view

        super(UserSchema, self).__init__(*args, **kwargs)
コード例 #27
0
class UserSchema(Schema):
    user_id = fields.Int(dump_only=True)
    username = fields.String(validate=validate.Length(max=30), required=True)
    email = fields.Email(validate=validate.Length(max=50), required=True)
コード例 #28
0
class AccountSchema(Schema):
    """ Account data which is required for the operation of runner itself
    """
    username = fields.String(required=True, validate=validate.Length(min=1))
    password = fields.String(required=True, validate=validate.Length(min=1))
コード例 #29
0
ファイル: views.py プロジェクト: dragos-blaj/clean-up
class CitySchema(Schema):
    name = fields.Str(validate=validate.Length(min=3, max=100), required=True)
    zipcode = fields.Str(validate=validate.Length(equal=6), required=True)
コード例 #30
0
    self.email=email
    self.name=name

  def add(self,{resource}):
     db.session.add({resource})
     return session_commit ()

  def update(self):
      return session_commit()

  def delete(self,{resource}):
     db.session.delete({resource})
     return session_commit()

class {Resources}Schema(Schema):

    not_blank = validate.Length(min=1, error='Field cannot be blank')
    name = fields.String(validate=not_blank)
    email = fields.Email()
    
    class Meta:
       fields = ('id', 'email', 'name')
 
def  session_commit ():
      try:
        db.session.commit()
      except SQLAlchemyError as e:
         db.session.rollback()
         reason=str(e)
         return reason