def from_email(cls, email): query = """ SELECT issuers.* FROM issuers INNER JOIN issuer_emails ON issuers.id = issuer_emails.issuer_id WHERE issuer_emails.email = %s """ data = db_query(query, (email, ))[0] return cls(data)
def save(): query = """ INSERT INTO issuer_email (email, issuer_id) VALUES (%s, %s) ON CONFLICT (id) DO UPDATE SET """ result = db_query(query, ( self.data['email'], self.data['issuer_id'], ))
def latest(): query = "SELECT * FROM docs ORDER BY issued_at DESC LIMIT 20" result = db_query(query) response = [{ "title": doc['title'], "slug": doc['slug'], "short_text": doc['short_text'], "content": doc['body_plain'], "date": doc['issued_at'].__str__(), "media": doc['media'], "issuer_id": doc.data['issuer_id'], } for doc in result] return response
def release(slug): query = "SELECT * FROM docs WHERE slug = %s" try: doc = db_query(query, (slug, ))[0] except IndexError: raise NotFoundError("Release not found.") response = { "title": doc['title'], "slug": doc['slug'], "short_text": doc['short_text'], "content": doc['body_plain'], "date": doc['issued_at'].__str__(), "media": doc['media'], } return response
def save(self): query = """ INSERT INTO issuers (id, slug, name, created_at) VALUES (%s, %s, %s, %s) ON CONFLICT (id) DO UPDATE SET slug=EXCLUDED.slug, name=EXCLUDED.name, created_at=EXCLUDED.created_at """ result = db_query(query, ( self.data['id'], self.data['slug'], self.data['name'], self.data['created_at'].strftime('%Y-%m-%d %H:%M:%S'), ))
def save(self): query = """ INSERT INTO docs (id, issuer_id, issuer_name, from_email, slug, title, short_text, body_html, body_plain, media, meta, issued_at) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT (id) DO UPDATE SET issuer_id=EXCLUDED.issuer_id, issuer_name=EXCLUDED.issuer_name, from_email=EXCLUDED.from_email, slug=EXCLUDED.slug, title=EXCLUDED.title, short_text=EXCLUDED.short_text, body_html=EXCLUDED.body_html, body_plain=EXCLUDED.body_plain, media=EXCLUDED.media, meta=EXCLUDED.meta, issued_at=EXCLUDED.issued_at """ result = db_query(query, ( self.data['id'], self.data['issuer_id'], self.data['issuer_name'], self.data['from_email'], slugify(self.data['title']), self.data['title'], self.data['body_plain'][:255], self.data['body_html'], self.data['body_plain'], json.dumps([{k: v for k, v in m.items() if k in ['type', 'url', 'filename']} for m in self.data['media']]), json.dumps({}), self.data['date'].strftime('%Y-%m-%d %H:%M:%S'), ))
def from_email(cls, email): data = db_query("SELECT * FROM issuer_emails WHERE email = %s", (email, )) return cls(data)
def one_by_slug(cls, slug): query = "SELECT id, slug, name FROM issuers WHERE slug = %s LIMIT 1" issuer = db_query(query, (slug, ))[0] return cls(issuer)
def all(cls): query = "SELECT id, slug, name FROM issuers ORDER BY name desc LIMIT 50" issuers = db_query(query) return [cls(issuer) for issuer in issuers]
def get_by_issuer(cls, issuer): query = 'SELECT * FROM docs WHERE issuer_id = %s ORDER BY issued_at' issuer_id = issuer.get_id() results = db_query(query, (issuer_id, )) return [cls(row) for row in results]