Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
schema = getattr(endpoint.Meta, f.__name__, {}).get("returns", schema_class)
data = f(endpoint, *args, **kwargs)
try:
model = schema()
model._endpoint = endpoint
# if schema class is a ResultSet, tell it how to load more results
if issubclass(schema_class, ResultSet):
model._more = functools.partial(wrapper, endpoint)
# if results are of type Model, make sure to set the endpoint on each item
if data is not None and 'results' in data \
and hasattr(model._fields['results'], 'model_class') \
and issubclass(model._fields['results'].model_class, Model):
def initialize_result_type(item_data):
item = model._fields['results'].model_class(item_data, strict=False)
item._endpoint = endpoint
return item
# Use generator so results are not iterated over more than necessary
data['results'] = (initialize_result_type(item_data) for item_data in data['results'])
model.import_data(data, strict=False)
model.validate()
except SchematicsDataError as e:
raise ValidationError(e.messages)
return model
class DateDimension(Model):
type = StringType()
count = IntType()
min = DateTimeType()
max = DateTimeType()
class GeoDimension(Model):
type = StringType()
bbox = ListType(FloatType)
class CategoryDimensionComponent(Model):
name = StringType()
count = IntType()
class CategoryDimension(Model):
type = StringType()
options = ListType(ModelType(CategoryDimensionComponent))
class NumberDimension(Model):
type = StringType()
count = IntType()
min = IntType()
from predicthq.endpoints.schemas import Model, StringType, DateTimeType, ModelType
class Industry(Model):
id = StringType()
name = StringType()
class Account(Model):
id = StringType()
name = StringType()
description = StringType()
industry = ModelType(Industry)
created_at = DateTimeType()
updated_at = DateTimeType()
class Options:
serialize_when_none = False
q = StringType()
id = ListType(StringType)
location = StringListType(StringModelType(Location), separator="+")
country = ListType(StringType)
type = ListType(StringType(choices=('planet', 'continent', 'country', 'region', 'county', 'local', 'major', 'metro', 'all')))
def validate(self, *args, **kwargs):
super(SearchParams, self).validate(*args, **kwargs)
if not any((self.q, self.id, self.location, self.country)):
raise SchematicsValidationError("Places search requires one of q, id, location or country")
class Place(Model):
id = StringType()
type = StringType()
name = StringType()
county = StringType()
region = StringType()
country = StringType()
country_alpha2 = StringType()
country_alpha3 = StringType()
location = GeoJSONPointType()
class PlaceResultSet(ResultSet):
results = ResultType(Place)
results = ResultType(SavedSignal)
class DataPoint(Model):
uid = StringType(required=True)
date = DateTimeType(required=True)
latitude = FloatType(min_value=-90, max_value=90, required=True)
longitude = FloatType(min_value=-180, max_value=180, required=True)
initiated = DateTimeType()
completed = DateTimeType()
# @todo: Support custom dimensions from signal
class SignalDataPoints(Model):
id = StringType(required=True)
data_points = ListType(ModelType(DataPoint), required=True)
chunk_size = IntType(default=1000, max_value=5000, required=True)
class DateDimension(Model):
type = StringType()
count = IntType()
min = DateTimeType()
max = DateTimeType()
class GeoDimension(Model):
date = ModelType(DateDimension)
initiated = ModelType(DateDimension)
completed = ModelType(DateDimension)
location = ModelType(GeoDimension)
# @todo: Support custom dimensions from signal
class MeanAnalysisComponent(Model):
mean = FloatType()
std_deviation = FloatType()
expected = FloatType()
excess = FloatType()
class CountAnalysisComponent(Model):
count = IntType()
expected = FloatType()
excess = FloatType()
class DailyAnalysis(Model):
date = DateType()
demand = ModelType(CountAnalysisComponent)
lead = ModelType(MeanAnalysisComponent)
span = ModelType(MeanAnalysisComponent)
class AnalysisResultSet(ResultSet):
from schematics.common import NONEMPTY
from predicthq.endpoints.schemas import (
PaginatedMixin, SortableMixin, Model, ResultSet, ListType, StringType, GeoJSONPointType,
StringListType, StringModelType, Area, ModelType, IntRange, IntType, DateTimeRange,
DateTimeType, FloatType, ResultType, DictType, DateType, Place, DateAround,
LocationAround, BooleanType, BrandUnsafe, Entity
)
class SearchParams(PaginatedMixin, SortableMixin, Model):
class Options:
serialize_when_none = False
id = ListType(StringType)
q = StringType()
label = ListType(StringType)
category = ListType(StringType)
start = ModelType(DateTimeRange)
start_around = ModelType(DateAround)
end = ModelType(DateTimeRange)
end_around = ModelType(DateAround)
active = ModelType(DateTimeRange)
updated = ModelType(DateTimeRange)
state = StringType(choices=('active', 'deleted'))
deleted_reason = StringType(choices=('cancelled', 'duplicate', 'invalid', 'postponed'))
class Dimension(Model):
class Options:
serialize_when_none = True
roles = {
"create": wholelist(),
"update": wholelist()
}
name = StringType(required=True)
type = StringType(choices=("category", "number", "date"), required=True)
class SignalID(Model):
id = StringType(required=True)
class Signal(Model):
class Options:
serialize_when_none = True
roles = {
"create": whitelist("name", "country", "dimensions"),
"update": blacklist("created_at", "updated_at")
}
id = StringType()
name = StringType(required=True)
country = StringType(max_length=2, min_length=2, required=True)
brand_unsafe = ModelType(BrandUnsafe)
entity = ModelType(Entity)
class Entities(Model):
class Options:
serialize_when_none = True
entity_id = StringType()
name = StringType()
type = StringType()
formatted_address = StringType()
class Event(Model):
class Options:
serialize_when_none = True
id = StringType()
title = StringType()
description = StringType()
start = DateTimeType()
end = DateTimeType()
timezone = StringType()
duration = IntType()
category = StringType()
labels = ListType(StringType())
country = StringType()
rank = IntType()
relevance = FloatType()
state = StringType()
first_seen = DateTimeType()
updated = DateTimeType()
deleted_reason = StringType()
duplicate_of_id = StringType()
class EventResultSet(ResultSet):
overflow = BooleanType()
results = ResultType(Event)
class CountResultSet(Model):
count = IntType()
top_rank = FloatType()
rank_levels = DictType(IntType)
categories = DictType(IntType)
labels = DictType(IntType)
class TopEventsSearchParams(SortableMixin, Model):
limit = IntType(min_value=0, max_value=10)
class CalendarParams(SearchParams):
top_events = ModelType(TopEventsSearchParams)