class TwittersSchema(BaseSchema):
    tweetdate = fields.DateTime()
    nick = fields.String(
        required=True,
        validate=[validate.Length(min=3, max=15)],
    )
    tweet = fields.String(
        required=True,
        validate=[validate.Length(min=0, max=50)],
    )

    def create(self, validated_data):
        return Twitters.objects.create(**validated_data)
class AverageTempRequestSchema(Schema):
    """Schema for requesting average temp.

    ---
    parameters:
      latitude: float
      longitude: float
      services: ["ACCUWEATHER, WEATHER_DOT_COM, NOAA]
    """

    lat = fields.Float(
        load_only=True,
        validate=Range(min=-180, max=180),
        data_key="latitude",
        required=True,
    )
    lon = fields.Float(
        load_only=True,
        validate=Range(min=-180, max=180),
        data_key="longitude",
        required=True,
    )
    services = fields.List(
        fields.String(
            validate=[OneOf(AverageWeatherService.valid_services)],
            load_only=True,
        ),
        validate=Length(min=1),
        required=True,
    )
class ExampleSerializer(Schema):
    number = fields.Integer()
    text = fields.String()

    def create(self, validated_data):
        return Object(**validated_data)

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            setattr(instance, key, value)
        return instance
class AverageTempFormRequestSchema(Schema):
    """Schema for requesting average temp.

    ---
    parameters:
      latitude: float
      longitude: float
      services: ["ACCUWEATHER, WEATHER_DOT_COM, NOAA]
    """
    class Meta:
        """Schema metaclass."""

        unknown = EXCLUDE

    lat = fields.Float(
        load_only=True,
        validate=Range(min=-180, max=180),
        data_key="latitude",
        required=True,
    )
    lon = fields.Float(
        load_only=True,
        validate=Range(min=-180, max=180),
        data_key="longitude",
        required=True,
    )
    services = fields.List(
        fields.String(
            validate=[OneOf(AverageWeatherService.valid_services)],
            load_only=True,
        ),
        validate=Length(min=1),
        required=True,
    )

    @pre_load
    def cast_latitude_longitude(self, data, **kwargs):
        """Query dict has a particular type of list to extract.

        Parameters
        ----------
        data: QueryDict (mutable)
            request.POST mutable version taken from form.

        Returns
        -------
        QueryDict (mutable)
            Query dict with a standard type of list or None
            value if no list was given.
        """
        data["services"] = data.getlist("services", None)
        return data
Beispiel #5
0
class TickerSchema(Schema):
    name = fields.String(max=50, dump_only=True)
    symbol = fields.String(max=10, dump_only=True)