Exemple #1
0
class Contact(EndpointsModel):
    """A person or group that can be used as a creator or a contact."""
    class ContactType(messages.Enum):
        INDIVIDUAL = 1
        GROUP = 2

    _message_fields_schema = ("id", "acceptTypes", "displayName", "imageUrls",
                              "phoneNumber", "priority", "source", "type")

    user = EndpointsUserProperty(required=True, raise_unauthorized=True)

    acceptTypes = ndb.StringProperty(repeated=True)
    displayName = ndb.StringProperty(required=True)
    imageUrls = ndb.StringProperty(repeated=True)
    phoneNumber = ndb.StringProperty()
    priority = ndb.IntegerProperty()
    source = ndb.StringProperty()
    type = msgprop.EnumProperty(ContactType)

    def IdSet(self, value):
        if not isinstance(value, basestring):
            raise TypeError("ID must be a string.")

        self.UpdateFromKey(ndb.Key("User", self.user.email(), Contact, value))

    @EndpointsAliasProperty(setter=IdSet, required=True)
    def id(self):
        if self.key is not None:
            return self.key.pairs()[1][1]
Exemple #2
0
class Subscription(EndpointsModel):
    """Model for subscriptions"""

    _message_fields_schema = ("id", "collection", "userToken", "verifyToken", "operation", "callbackUrl")

    user = EndpointsUserProperty(required=True, raise_unauthorized=True)
    collection = ndb.StringProperty(required=True)
    userToken = ndb.StringProperty(required=True)
    verifyToken = ndb.StringProperty(required=True)
    operation = msgprop.EnumProperty(Operation, repeated=True)
    callbackUrl = ndb.StringProperty(required=True)
Exemple #3
0
class Patient(EndpointsModel):
    name = ndb.StringProperty()
    user = EndpointsUserProperty(required=True, raise_unauthorized=True)
    date_of_birth = EndpointsDateProperty()
    age = ndb.IntegerProperty()

    def calculate_age(self):
        today = date.today()
        birthday = self.date_of_birth
        self.age = today.year - birthday.year - ((today.month, today.day) < (birthday.month , birthday.day))

    def _pre_put_hook(self):
        if self.date_of_birth:
            self.calculate_age()
Exemple #4
0
class TimelineItem(EndpointsModel):
    """Model for timeline cards.

    Since the created property is auto_now_add=True, Cards will document when
    they were inserted immediately after being stored.
    """
    class TimelineContact(EndpointsModel):
        class ContactType(messages.Enum):
            INDIVIDUAL = 1
            GROUP = 2

        acceptTypes = ndb.StringProperty(repeated=True)
        displayName = ndb.StringProperty()
        id = ndb.StringProperty(required=True)
        imageUrls = ndb.StringProperty(repeated=True)
        phoneNumber = ndb.StringProperty()
        source = ndb.StringProperty()
        type = msgprop.EnumProperty(ContactType)

    _message_fields_schema = ("id", "attachments", "bundleId", "canonicalUrl",
                              "created", "displayTime", "html", "htmlPages",
                              "inReplyTo", "isBundleCover", "isDeleted",
                              "isPinned", "menuItems", "recipients",
                              "sourceItemId", "speakableText", "text", "title",
                              "updated")

    user = EndpointsUserProperty(required=True, raise_unauthorized=True)

    attachments = ndb.StructuredProperty(Attachment, repeated=True)
    bundleId = ndb.StringProperty()
    canonicalUrl = ndb.StringProperty()
    created = EndpointsDateTimeProperty(auto_now_add=True)
    displayTime = EndpointsDateTimeProperty()
    html = ndb.TextProperty()
    htmlPages = ndb.TextProperty(repeated=True)
    inReplyTo = ndb.IntegerProperty()
    isBundleCover = ndb.BooleanProperty(default=False)
    isDeleted = ndb.BooleanProperty(default=False)
    isPinned = ndb.BooleanProperty(default=False)
    menuItems = ndb.LocalStructuredProperty(MenuItem, repeated=True)
    recipients = ndb.LocalStructuredProperty(TimelineContact, repeated=True)
    sourceItemId = ndb.StringProperty()
    speakableText = ndb.TextProperty()
    text = ndb.StringProperty()
    title = ndb.StringProperty()
    updated = EndpointsDateTimeProperty(auto_now=True)
Exemple #5
0
class Location(EndpointsModel):
    """Model for location"""

    _latest = False

    _message_fields_schema = (
        "id",
        "timestamp",
        "latitude",
        "longitude",
        "accuracy",
        "displayName",
        "address"
    )

    user = EndpointsUserProperty(required=True, raise_unauthorized=True)
    timestamp = EndpointsDateTimeProperty(auto_now_add=True)
    latitude = ndb.FloatProperty()
    longitude = ndb.FloatProperty()
    accuracy = ndb.FloatProperty()
    displayName = ndb.StringProperty()
    address = ndb.StringProperty()

    def IdSet(self, value):
        if not isinstance(value, basestring):
            raise TypeError("ID must be a string.")

        if value == "latest":
            self._latest = True
            loc_query = Location.query().order(-Location.timestamp)
            loc_query = loc_query.filter(Location.user == self.user)
            loc = loc_query.get()
            if loc is not None:
                self.UpdateFromKey(loc.key)
            return

        if value.isdigit():
            self.UpdateFromKey(ndb.Key(Location, int(value)))

    @EndpointsAliasProperty(setter=IdSet, required=False)
    def id(self):
        if self._latest:
            return "latest"
        if self.key is not None:
            return str(self.key.integer_id())
Exemple #6
0
class Score(EndpointsModel):
    _message_fields_schema = ('id', 'outcome', 'played', 'player')

    outcome = ndb.StringProperty(required=True)
    played = ndb.DateTimeProperty(auto_now_add=True)
    player = EndpointsUserProperty(required=True, raise_unauthorized=True)

    def OrderSet(self, value):
        if not isinstance(value, Order):
            raise TypeError('Expected an enum, received: %s.' % (value, ))

        if value == Order.WHEN:
            super(Score, self).OrderSet('-played')
        elif value == Order.TEXT:
            super(Score, self).OrderSet('outcome')
        else:
            raise TypeError('Unexpected value of Order: %s.' % (value, ))

    @EndpointsAliasProperty(setter=OrderSet,
                            property_type=Order,
                            default=Order.WHEN)
    def order(self):
        return super(Score, self).order
Exemple #7
0
class TimelineItem(EndpointsModel):
    """Model for timeline cards.

    Since the created property is auto_now_add=True, Cards will document when
    they were inserted immediately after being stored.
    """
    class Attachment(EndpointsModel):
        """Attachment to a timeline card

        Due to limitations in Cloud Endpoints this works a bit differently than
        the attachments in the official API. Normally you would add attachments
        by uploading the media data (as image/audio/video). Attachments in this
        implementation can only by of type image and are represented either as
        URL or Data-URI and can be added/retrieved/updated directly by filling
        the attachments field in the timeline.insert method.
        """
        id = ndb.StringProperty()
        contentType = ndb.StringProperty()
        contentUrl = ndb.StringProperty()
        isProcessingContent = ndb.BooleanProperty(default=False)

    class TimelineContact(EndpointsModel):
        class ContactType(messages.Enum):
            INDIVIDUAL = 1
            GROUP = 2

        acceptTypes = ndb.StringProperty(repeated=True)
        displayName = ndb.StringProperty()
        id = ndb.StringProperty(required=True)
        imageUrls = ndb.StringProperty(repeated=True)
        phoneNumber = ndb.StringProperty()
        source = ndb.StringProperty()
        type = msgprop.EnumProperty(ContactType)

    class Notification(EndpointsModel):

        level = ndb.StringProperty(default="DEFAULT")
        deliveryTime = EndpointsDateTimeProperty()

    _message_fields_schema = ("id", "attachments", "bundleId", "canonicalUrl",
                              "created", "creator", "displayTime", "html",
                              "htmlPages", "inReplyTo", "isBundleCover",
                              "isDeleted", "isPinned", "location", "menuItems",
                              "notification", "pinScore", "recipients",
                              "sourceItemId", "speakableText", "text", "title",
                              "updated")

    user = EndpointsUserProperty(required=True, raise_unauthorized=True)

    attachments = ndb.LocalStructuredProperty(Attachment, repeated=True)
    bundleId = ndb.StringProperty()
    canonicalUrl = ndb.StringProperty()
    created = EndpointsDateTimeProperty(auto_now_add=True)
    creator = ndb.LocalStructuredProperty(TimelineContact)
    displayTime = EndpointsDateTimeProperty()
    html = ndb.TextProperty()
    htmlPages = ndb.TextProperty(repeated=True)
    inReplyTo = ndb.IntegerProperty()
    isBundleCover = ndb.BooleanProperty()
    isDeleted = ndb.BooleanProperty()
    isPinned = ndb.BooleanProperty()
    location = ndb.LocalStructuredProperty(Location)
    menuItems = ndb.LocalStructuredProperty(MenuItem, repeated=True)
    notification = ndb.LocalStructuredProperty(Notification)
    pinScore = ndb.IntegerProperty()
    recipients = ndb.LocalStructuredProperty(TimelineContact, repeated=True)
    sourceItemId = ndb.StringProperty()
    speakableText = ndb.TextProperty()
    text = ndb.StringProperty()
    title = ndb.StringProperty()
    updated = EndpointsDateTimeProperty(auto_now=True)

    def IncludeDeletedSet(self, value):
        """
        If value is true all timelineItems will be returned.
        Otherwise a filter for non-deleted items is necessary for the query.
        """
        if value is None or value is False:
            self._endpoints_query_info._AddFilter(
                TimelineItem.isDeleted == False)

    @EndpointsAliasProperty(setter=IncludeDeletedSet,
                            property_type=messages.BooleanField,
                            default=False)
    def includeDeleted(self):
        """
        includedDeleted is only used as parameter in query_methods
        so there should never be a reason to actually retrieve the value
        """
        return None

    def PinnedOnlySet(self, value):
        """
        If value is true only pinned timelineItems will be returned.
        Otherwise all timelineItems are returned.
        """
        if value is True:
            self._endpoints_query_info._AddFilter(
                TimelineItem.isPinned == True)

    @EndpointsAliasProperty(setter=PinnedOnlySet,
                            property_type=messages.BooleanField,
                            default=False)
    def pinnedOnly(self):
        """
        pinnedOnly is only used as parameter in query_methods
        so there should never be a reason to actually retrieve the value
        """
        return None

    def MaxResultsSet(self, value):
        """Setter to be used for default limit EndpointsAliasProperty.

        Simply sets the limit on the entity's query info object, and the query
        info object handles validation.

        Args:
          value: The limit value to be set.
        """
        self._endpoints_query_info.limit = value

    @EndpointsAliasProperty(setter=MaxResultsSet,
                            property_type=messages.IntegerField,
                            default=20)
    def maxResults(self):
        """Getter to be used for default limit EndpointsAliasProperty.

        Uses the ProtoRPC property_type IntegerField since a limit.

        Returns:
          The integer (or null) limit from the query info on the entity.
        """
        return self._endpoints_query_info.limit