class Follower(StringIdModel): """A follower of a Bridgy Fed user. Key name is 'USER_DOMAIN FOLLOWER_ID', e.g.: 'snarfed.org https://mastodon.social/@swentel'. """ STATUSES = ('active', 'inactive') # most recent AP Follow activity (JSON). must have a composite actor object # with an inbox, publicInbox, or sharedInbox! last_follow = ndb.TextProperty() status = ndb.StringProperty(choices=STATUSES, default='active') created = ndb.DateTimeProperty(auto_now_add=True) updated = ndb.DateTimeProperty(auto_now=True) @classmethod def _id(cls, user_domain, follower_id): assert user_domain assert follower_id return '%s %s' % (user_domain, follower_id) @classmethod def get_or_create(cls, user_domain, follower_id, **kwargs): logging.info('new Follower for %s %s', user_domain, follower_id) return cls.get_or_insert(cls._id(user_domain, follower_id), **kwargs)
class Team(ndb.Model): """ Teams represent FIRST Robotics Competition teams. key_name is like 'frc177' """ team_number = ndb.IntegerProperty(required=True) name = ndb.TextProperty(indexed=False) nickname = ndb.TextProperty(indexed=False) school_name = ndb.TextProperty(indexed=False) home_cmp = ndb.StringProperty() # city, state_prov, country, and postalcode are from FIRST city = ndb.StringProperty() # Equivalent to locality. From FRCAPI state_prov = ndb.StringProperty() # Equivalent to region. From FRCAPI country = ndb.StringProperty() # From FRCAPI postalcode = ( ndb.StringProperty() ) # From ElasticSearch only. String because it can be like "95126-1215" website = ndb.TextProperty(indexed=False) first_tpid = (ndb.IntegerProperty() ) # from USFIRST. FIRST team ID number. -greg 5/20/2010 first_tpid_year = ( ndb.IntegerProperty() ) # from USFIRST. Year tpid is applicable for. -greg 9 Jan 2011 rookie_year = ndb.IntegerProperty() motto = ndb.TextProperty(indexed=False) created = ndb.DateTimeProperty(auto_now_add=True, indexed=False) updated = ndb.DateTimeProperty(auto_now=True, indexed=False)
class SportCenter(ndb.Model): title = ndb.StringProperty() email_address = ndb.StringProperty() street = ndb.TextProperty() city = ndb.TextProperty() zip_number = ndb.StringProperty() country = ndb.TextProperty() created = ndb.DateTimeProperty(auto_now_add=True) updated = ndb.DateTimeProperty(auto_now=True) deleted = ndb.BooleanProperty(default=False) @classmethod def create(cls, text): with client.context( ): # with client.context() is obligatory to use in the new ndb library message = cls(text=text) message.put() return message @classmethod def fetch_all(cls): with client.context(): messages = cls.query().fetch() return messages
class Heartbeat(Model): """Bot health metadata.""" # Name of the bot. bot_name = ndb.StringProperty() # Time of the last heartbeat. last_beat_time = ndb.DateTimeProperty() # Task payload containing information on current task execution. task_payload = ndb.StringProperty() # Expected end time for task. task_end_time = ndb.DateTimeProperty() # Source version (for accountability). source_version = ndb.StringProperty() # Platform id (esp important for Android platform for OS version). platform_id = ndb.StringProperty() # Keywords is used for searching. keywords = ndb.StringProperty(repeated=True) def populate_indices(self): """Populate keywords for fast job searching.""" self.keywords = list( search_tokenizer.tokenize(self.bot_name) | search_tokenizer.tokenize(self.task_payload)) def _pre_put_hook(self): """Pre-put hook.""" self.populate_indices()
class Publish(ndb.Model): """A comment, like, repost, or RSVP published into a silo. Child of a :class:`PublishedPage` entity. """ STATUSES = ('new', 'complete', 'failed', 'deleted') # Turn off instance and memcache caching. See Source for details. _use_cache = False _use_memcache = False type = ndb.StringProperty(choices=PUBLISH_TYPES) status = ndb.StringProperty(choices=STATUSES, default='new') source = ndb.KeyProperty() html = ndb.TextProperty() # raw HTML fetched from source published = ndb.JsonProperty(compressed=True) created = ndb.DateTimeProperty(auto_now_add=True) updated = ndb.DateTimeProperty(auto_now=True) def type_label(self): """Returns silo-specific string type, e.g. 'favorite' instead of 'like'.""" for cls in sources.values(): # global if cls.__name__ == self.source.kind(): return cls.TYPE_LABELS.get(self.type, self.type) return self.type
class SessionModel(ndb.Model): created_on = ndb.DateTimeProperty(auto_now_add=True, indexed=False) updated_on = ndb.DateTimeProperty(auto_now=True, indexed=False) expires_on = ndb.DateTimeProperty(indexed=True) data = ndb.TextProperty(indexed=False) def delete(self): self.key.delete() @classmethod def delete_by_id(cls, sid): ndb.Key(cls, sid).delete() def has_expired(self): return self.expires_on and self.expires_on <= datetime.utcnow() def should_slide_expiry(self): if not self.expires_on or not self.updated_on: return False # Use a precision of 5 minutes return (datetime.utcnow() - self.updated_on).total_seconds() > 300 def get_data(self): return json.loads(want_bytes(self.data)) def set_data(self, data): self.data = json.dumps(dict(data))
class Extra(ndb.Model): user = ndb.StringProperty() related = ndb.StringProperty() tag = ndb.StringProperty() comment = ndb.StringProperty() # Reference to Post channelurl = ndb.KeyProperty(kind=ChannelUrl) # System stuff idate = ndb.DateTimeProperty(auto_now_add=True) udate = ndb.DateTimeProperty(auto_now=True) def __unicode__(self): retval = '' if self.comment: retval += ' ' + self.comment if self.tag: retval += ' <a href="/url/tag/' + self.tag + '/"><span class="label ' if self.tag == 'WTF': retval += 'label-danger' elif self.tag == 'NSFW': retval += 'label-warning' else: retval += 'label-primary' retval += '">' + self.tag + '</span></a>' if self.related: retval += ' <a href="/url/view/' + self.related + '/"><span class="label label-info">' + self.related + '</span></a>' return retval.strip() def __str__(self): return unicode(self).encode('utf-8')
class LastUpdatedPicture(StringIdModel): """Stores the last user in a given silo that we updated profile picture for. Key id is the silo's SHORT_NAME. """ last = ndb.KeyProperty() created = ndb.DateTimeProperty(auto_now_add=True, required=True, tzinfo=timezone.utc) updated = ndb.DateTimeProperty(auto_now=True, tzinfo=timezone.utc)
class Activity(StringIdModel): """An activity with responses to be propagated. The key name is the activity id as a tag URI. Currently only used for posts sent to us by the browser extension. """ source = ndb.KeyProperty() created = ndb.DateTimeProperty(auto_now_add=True) updated = ndb.DateTimeProperty(auto_now=True) activity_json = ndb.TextProperty()
class ChannelUrl(ndb.Model): # Reference to Channel & Url channel = ndb.KeyProperty(kind=Channel) url = ndb.KeyProperty(kind=Url) # System stuff idate = ndb.DateTimeProperty(auto_now_add=True) udate = ndb.DateTimeProperty(auto_now=True) def rating(self): value = 0 rates = Rate.query(Rate.channelurl == self.key) for rate in rates: value = value + rate.value return value def extras(self, plain=False): xtra = '' extras = Extra.query(Extra.channelurl == self.key) for extra in extras: if extra.comment: xtra = xtra + ' ' + extra.comment if extra.tag: if plain: xtra = xtra + extra.tag elif extra.tag == 'WTF': xtra = xtra + ' <span class="label label-danger">' + extra.tag + '</span>' elif extra.tag == 'NSFW': xtra = xtra + ' <span class="label label-warning">' + extra.tag + '</span>' else: xtra = xtra + ' <span class="label label-primary">' + extra.tag + '</span>' if extra.related: if plain: xtra = xtra + extra.related else: xtra = xtra + ' <span class="label label-info">' + extra.related + '</span>' return xtra.strip() def posts(self): chl = self.channel.get() msg = '' msgs = [] posts = Post.query(Post.channelurl == self.key) for post in posts: msgs.append('%s/%s @ %s' % (post.user, chl.name, post.date)) return ', '.join(msgs) def __unicode__(self): return unicode(self.key.id()) def __str__(self): return unicode(self).encode('utf-8')
class Domain(StringIdModel): """A domain owned by a user. Ownership is proven via IndieAuth. Supports secret tokens associated with each domain. Clients can include a token with requests that operate on a given domain, eg sending posts and responses from the browser extension. Key id is the string domain, eg 'example.com'. """ tokens = ndb.StringProperty(repeated=True) auth = ndb.KeyProperty(IndieAuth) created = ndb.DateTimeProperty(auto_now_add=True, tzinfo=timezone.utc) updated = ndb.DateTimeProperty(auto_now=True, tzinfo=timezone.utc)
class Commodities_buy_sell(ndb.Model): user_id = ndb.StringProperty() commodity_name = ndb.StringProperty() buy_or_sell = ndb.StringProperty() price = ndb.IntegerProperty() buy_sell_date = ndb.DateTimeProperty() created_by = ndb.StringProperty() created_on = ndb.DateTimeProperty() created_reference_id = ndb.StringProperty() deleted_by = ndb.StringProperty() deleted_on = ndb.DateTimeProperty() deleted_reference_id = ndb.StringProperty() record_status = ndb.StringProperty()
class Channel(ndb.Model): name = ndb.StringProperty() private = ndb.BooleanProperty(default=False) # System stuff idate = ndb.DateTimeProperty(auto_now_add=True) udate = ndb.DateTimeProperty(auto_now=True) def __unicode__(self): return self.name def __str__(self): return unicode(self).encode('utf-8')
class Feed(models.StringIdModel): """An HTML feed uploaded by the browser extension, converted to AS1. Key is token generated by browser extension. """ html = ndb.TextProperty(required=True) actor_json = ndb.StringProperty() as1_json = ndb.TextProperty(required=True) created = ndb.DateTimeProperty(auto_now_add=True, required=True, tzinfo=timezone.utc) updated = ndb.DateTimeProperty(auto_now=True, required=True, tzinfo=timezone.utc)
class Url(ndb.Model): url = ndb.StringProperty() title = ndb.StringProperty() # Valid url: >=1 valid = ndb.IntegerProperty(default=1) status = ndb.StringProperty() last_check = ndb.DateTimeProperty(auto_now_add=True) # FullTextSearch related stuff document_date = ndb.DateTimeProperty() # System stuff idate = ndb.DateTimeProperty(auto_now_add=True) udate = ndb.DateTimeProperty(auto_now=True) def parse(self): return urlparse(self.url) def loc_title(self): loc = urlparse(self.url).scheme + '//' + urlparse(self.url).netloc if self.title: return loc + ' - ' + self.title else: new_title = ''.join(self.url.split('/')[-1:]) if len(new_title) > 40: return loc + ' - ' + new_title[:20] + '...' + new_title[ len(new_title) - 10:] else: return loc + ' - ' + new_title def short(self): if self.title: return self.title else: new_title = ''.join(self.url.split('/')[-1:]) if len(new_title) > 40: return new_title[:20] + '...' + new_title[len(new_title) - 10:] else: return new_title def __unicode__(self): if self.title: return self.title else: return ''.join(self.url.split('/')[-1:]) def __str__(self): return unicode(self).encode('utf-8')
class ArticleEntity(ndb.Model): @classmethod def _get_kind(cls): return 'BlogPost' slug = ndb.StringProperty() title = ndb.StringProperty() content = ndb.StringProperty() summary = ndb.StringProperty(default="") is_published = ndb.BooleanProperty(default=False) created_date = ndb.DateTimeProperty(auto_now_add=True) modified_date = ndb.DateTimeProperty(auto_now=True) published_date = ndb.DateTimeProperty(indexed=True) categories = ndb.KeyProperty(repeated=True) primary_media_image = ndb.KeyProperty(kind=MediaEntity)
class User(ndb.Model): username = ndb.StringProperty() password = ndb.StringProperty() user_type = ndb.StringProperty() name = ndb.StringProperty() data_set = ndb.StringProperty() created = ndb.DateTimeProperty(auto_now_add=True)
class House(ndb.Model): address = ndb.StringProperty() json_property = ndb.JsonProperty() indexed_int = ndb.IntegerProperty() unindexed_int = ndb.IntegerProperty(indexed=False) text_property = ndb.TextProperty() datetime_property = ndb.DateTimeProperty()
class Rate(ndb.Model): user = ndb.StringProperty() value = ndb.IntegerProperty() # Reference to ChannelUrl channelurl = ndb.KeyProperty(kind=ChannelUrl) # System stuff idate = ndb.DateTimeProperty(auto_now_add=True) udate = ndb.DateTimeProperty(auto_now=True) def __unicode__(self): return '%s %+d' % (self.user, self.value) def __str__(self): return unicode(self).encode('utf-8')
class Post(ndb.Model): user = ndb.StringProperty() date = ndb.DateTimeProperty() # Reference to ChannelUrl channelurl = ndb.KeyProperty(kind=ChannelUrl) # System stuff idate = ndb.DateTimeProperty(auto_now_add=True) udate = ndb.DateTimeProperty(auto_now=True) def __unicode__(self): return unicode(self.key.id()) def __str__(self): return unicode(self).encode('utf-8')
class Heartbeat(Model): """Bot health metadata.""" # Name of the bot. bot_name = StringProperty() # Time of the last heartbeat. last_beat_time = ndb.DateTimeProperty() # Task payload containing information on current task execution. task_payload = StringProperty() # Expected end time for task. task_end_time = ndb.DateTimeProperty() # Source version (for accountability). source_version = StringProperty()
class DataBundle(Model): """Represents a data bundle associated with a fuzzer.""" VALID_NAME_REGEX = NAME_CHECK_REGEX # The data bundle's name (important for identifying shared bundles). name = StringProperty() # Name of cloud storage bucket on GCS. bucket_name = StringProperty() # Data bundle's source (for accountability). # TODO(ochang): Remove. source = StringProperty() # If data bundle can be unpacked locally or needs nfs. is_local = ndb.BooleanProperty(default=True) # Creation timestamp. timestamp = ndb.DateTimeProperty() # Whether or not bundle should be synced to worker instead. # Fuzzer scripts are usually run on trusted hosts, so data bundles are synced # there. In libFuzzer's case, we want the bundle to be on the same machine as # where the libFuzzer binary will run (untrusted). sync_to_worker = ndb.BooleanProperty(default=False)
class FiledBug(Model): """Metadata information for issues that were filed automatically.""" # Timestamp when the issue was filed. timestamp = ndb.DateTimeProperty() # ID of the test case that is associated with the filed issue. testcase_id = ndb.IntegerProperty() # Tracking issue tracker bug for this testcase. bug_information = ndb.IntegerProperty(default=0) # Group ID associated with this issue. group_id = ndb.IntegerProperty() # Crash type for easy reference. crash_type = StringProperty() # Crash state for easy reference. crash_state = StringProperty() # Is it a security bug? security_flag = ndb.BooleanProperty() # Platform id. platform_id = StringProperty()
class Lock(Model): """Lock entity.""" # Expiration time for the lock. expiration_time = ndb.DateTimeProperty() # The bot name denoting the holder of the lock. holder = StringProperty()
class News(ndb.Model): # Note: # A StringProperty must be indexed. Previously setting ``indexed=False`` was allowed, # but this usage is no longer supported. content = ndb.StringProperty(indexed=True) link = ndb.StringProperty(indexed=True) link_text = ndb.StringProperty(indexed=True) date = ndb.DateTimeProperty(auto_now_add=True) def __unicode__(self): if self.link: if self.link_text: return '%s: <a target="_blank" href="%s">%s</a>' % (self.content, self.link, self.link_text) else: text = '' comments = [] url = Url.query(Url.url == self.link).get(keys_only=True) channel_url = ChannelUrl.query(ChannelUrl.url == url).get(keys_only=True) extra = Extra.query(Extra.channelurl == channel_url).get(keys_only=False) if extra and extra.comment: text = extra.comment if not text: text = ''.join(self.link.split('/')[-1:]) return '%s: <a target="_blank" href="%s">%s</a>' % (self.content, self.link, text) return self.content def __str__(self): return unicode(self).encode('utf-8')
class Session(ndb.Model): name_session = ndb.StringProperty() image_session = ndb.StringProperty() pdf_session = ndb.StringProperty() id_rythme = ndb.IntegerProperty() id_session = ndb.IntegerProperty() lastChanged = ndb.DateTimeProperty(auto_now=True)
class Championship(ndb.Model): national_championship = ndb.BooleanProperty() region = ndb.KeyProperty(kind=Region) state = ndb.KeyProperty(kind=State) competition = ndb.KeyProperty(kind=Competition) year = ndb.ComputedProperty(lambda self: self.competition.get().year) residency_deadline = ndb.DateTimeProperty() residency_timezone = ndb.StringProperty() @staticmethod def NationalsId(year): return str(year) @staticmethod def RegionalsId(year, region): return '%s_%d' % (region.key.id(), year) @staticmethod def StateChampionshipId(year, state): return '%s_%d' % (state.key.id(), year) def GetEligibleStateKeys(self): if self.state: return [self.state] if self.region: return State.query(State.region == self.region).fetch( keys_only=True) # National championships are not based on residence, they're based on # citizenship. return None
class Site(ndb.Model): """Defines a site.""" name = ndb.StringProperty() url = ndb.StringProperty() created_by = ndb.KeyProperty(kind=User) last_edited_by = ndb.KeyProperty(kind=User) created_on = ndb.DateTimeProperty(auto_now_add=True) last_modified_on = ndb.DateTimeProperty(auto_now=True) @classmethod def to_compact(cls, site): return { 'name': site.name, 'url': site.url, 'id': site.key.urlsafe().decode('utf-8'), }
class Album(ndb.Model): data_set = ndb.StringProperty() genre = ndb.KeyProperty(kind=Genre) artist = ndb.KeyProperty(kind=Artist) period = ndb.KeyProperty(kind=Period) album_name = ndb.StringProperty() created = ndb.DateTimeProperty(auto_now_add=True)
class Subscription(ndb.Model): """Models a webhook subscription.""" request_method = ndb.StringProperty(required=True, default='post') request_format = ndb.StringProperty(required=True, default=CONTENT_TYPE_JSON) request_url = ndb.StringProperty(required=True) active = ndb.BooleanProperty(required=True, default=False) event = ndb.StringProperty(required=True) secret = ndb.StringProperty(required=True) timestamp = ndb.DateTimeProperty(auto_now_add=True) owner_key = ndb.KeyProperty(kind=Employee) @classmethod def create_from_dict(cls, d, persist=True): new_subscription = cls() new_subscription.owner_key = Employee.get_current_employee().key new_subscription.request_url = d['request_url'] new_subscription.active = d['active'] new_subscription.event = d['event'] new_subscription.secret = d['secret'] if persist is True: new_subscription.put() return new_subscription @classmethod def all_active_for_event(cls, event): return cls.query( cls.active == True, # noqa cls.event == event, )