class APIKey(db.Model): __tablename__ = 'api_keys' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) key = db.Column(db.String(120)) active = db.Column(db.Boolean()) created = db.Column(db.DateTime(timezone=True), default=db.func.now()) feedgroups = db.relationship("FeedGroup", backref="key") feeds = db.relationship("Feed", backref="key") articles = db.relationship("Article", backref="key") events = db.relationship("Event", backref="key") def generate_key_str(self): return str(uuid4()) def __repr__(self): if not self.name: return "<APIKey>" return '<APIKey "%s">' % self.name def jsonify(self, feedgroups=False, with_key_str=False): response = {} response['name'] = self.name if with_key_str: response['apikey'] = self.key if feedgroups: response['feedgroups'] = [ group.jsonify() for group in self.feedgroups ] response['active'] = self.active return response
class FeedGroup(db.Model): __tablename__ = "feed_groups" id = db.Column(db.Integer(), primary_key=True) key_id = db.Column(db.Integer(), db.ForeignKey("api_keys.id")) uid = db.Column(db.String(), default=uid()) name = db.Column(db.String(80)) feeds = db.relationship('Feed', backref="group") created = db.Column(db.DateTime(timezone=True), default=db.func.now()) active = db.Column(db.Boolean(), default=True) def __repr__(self): if self.name: return '<FeedGroup "%s" with %i feeds>' % (self.name, len(self.feeds)) return "<FeedGroup>" def jsonify(self): response = {} if self.created: response['name'] = self.name response['uid'] = self.uid response['created'] = time.mktime(self.created.timetuple()) response['active'] = self.active response['feeds'] = [feed.jsonify() for feed in self.feeds] return response
class APIKey(db.Model): """ An Emissary API Key. Reader keys cannot PUT, POST or DELETE. """ __tablename__ = 'api_keys' id = db.Column(db.Integer, primary_key=True) parent_id = db.Column(db.Integer(), db.ForeignKey("api_keys.id")) name = db.Column(db.String(80)) key = db.Column(db.String(120)) active = db.Column(db.Boolean()) reader = db.Column(db.Boolean(), default=False) created = db.Column(db.DateTime(timezone=True), default=db.func.now()) parent = db.relationship("APIKey", backref="readers", remote_side=[id]) feedgroups = db.relationship("FeedGroup", backref="key") feeds = db.relationship("Feed", backref="key") articles = db.relationship("Article", backref="key") events = db.relationship("Event", backref="key") def generate_key_str(self): """ Returns a SHA256 of the time as an API Key. """ return sha256(time.asctime() + str(time.time())).hexdigest() def __repr__(self): if not self.name: return "<APIKey>" return '<APIKey "%s">' % self.name def jsonify(self, feedgroups=False, with_key_str=False): response = {} response['name'] = self.name if with_key_str: response['apikey'] = self.key if feedgroups: response['feedgroups'] = [ group.jsonify() for group in self.feedgroups ] response['active'] = self.active response['reader'] = self.reader if self.reader: response['parent'] = self.parent.name return response
class Feed(db.Model): __tablename__ = "feeds" id = db.Column(db.Integer(), primary_key=True) key_id = db.Column(db.Integer(), db.ForeignKey("api_keys.id")) group_id = db.Column(db.Integer(), db.ForeignKey("feed_groups.id")) uid = db.Column(db.String(), default=uid()) name = db.Column(db.String(80)) url = db.Column(db.String(80)) schedule = db.Column(db.String(80)) active = db.Column(db.Boolean(), default=True) created = db.Column(db.DateTime(timezone=True), default=db.func.now()) articles = db.relationship('Article', backref="feed") def __repr__(self): if self.name: return '<Feed "%s" with %i articles>' % (self.name, len(self.articles)) return "<Feed>" def is_running(self): """ Ask the feedmanager what's happening. """ if not app.inbox: return None response_queue = app.queues[-1] qid = hex(id(response_queue)) app.inbox.put([qid, "check", self]) # Wait two seconds max for a response then = time.time() while response_queue.empty(): now = time.time() if (now - then) >= 0.5: return None return response_queue.get() def jsonify(self, articles=False): response = {} if self.created: response['name'] = self.name response['uid'] = self.uid response['url'] = self.url response['created'] = time.mktime(self.created.timetuple()) response['schedule'] = self.schedule response['active'] = self.active response['article_count'] = len(self.articles) response['running'] = self.is_running() if self.group: response['group'] = self.group.name else: response['group'] = None return response