def __init__(self, post, id, to_comment_id=None, author=None, created=None, text=None, archive=False, files=None): if post and id: if isinstance(post, Post): self.post = post elif isinstance(post, (int, long)): self.post = Post.from_data(b26(post)) else: self.post = Post.from_data(post) self.id = int(id) res = db.fetchone( "SELECT c.author, u.login, i.name, i.avatar, " "c.to_comment_id, " "c.anon_login, " "c.created at time zone %s AS created, " "c.text, c.files, c.updated " "FROM posts.comments c " "JOIN users.logins u ON u.id=c.author " "JOIN users.info i ON u.id=i.id " "WHERE post_id=%s AND comment_id=%s;", [self.post.tz, unb26(self.post.id), self.id]) if not res: raise CommentNotFound(self.post.id, id) if author: self.author = author else: login = res['anon_login'] or res['login'] self.author = User.from_data(res['author'], login, info={ 'name': res['name'] or login, 'avatar': res['avatar'] }) self.to_comment_id = to_comment_id or res['to_comment_id'] self.created = created or res['created'] self.text = text or res['text'] self.recommendations = 0 self.recommended = False self.files = res['files'] self.updated = res['updated'] self.comments = [] self.archive = archive if isinstance(files, (list, tuple)): self.files = files
def index_posts(): res = db.fetchall("SELECT u.id user_id, u.login, p.id post_id, " "p.type post_type, " "p.title, p.tags, p.text, p.created, p.private " "FROM posts.posts p " "JOIN users.logins u ON p.author=u.id;") for r in res: post = dict(r) post['post_id'] = b26(post['post_id']) _id = post['post_id'] es.index(index='point-posts', id=_id, doc_type='post', body=post)
def index_comments(): res = db.fetchall("SELECT u.id user_id, u.login, c.post_id, " "p.type post_type, " "c.comment_id, c.text, c.created, p.private " "FROM posts.comments c " "JOIN users.logins u ON c.author=u.id " "JOIN posts.posts p ON p.id=c.post_id;") for r in res: c = dict(r) c['post_id'] = b26(c['post_id']) _id = '%s-%s' % (c['post_id'], c['comment_id']) es.index(index='point-comments', id=_id, doc_type='post', body=c)
def __init__(self, post, id, to_comment_id=None, author=None, created=None, text=None, archive=False, files=None): if post and id: if isinstance(post, Post): self.post = post elif isinstance(post, (int, long)): self.post = Post.from_data(b26(post)) else: self.post = Post.from_data(post) self.id = int(id) res = db.fetchone("SELECT c.author, u.login, i.name, i.avatar, " "c.to_comment_id, " "c.anon_login, " "c.created at time zone %s AS created, " "c.text, c.files, c.updated " "FROM posts.comments c " "JOIN users.logins u ON u.id=c.author " "JOIN users.info i ON u.id=i.id " "WHERE post_id=%s AND comment_id=%s;", [self.post.tz, unb26(self.post.id), self.id]) if not res: raise CommentNotFound(self.post.id, id) if author: self.author = author else: login = res['anon_login'] or res['login'] self.author = User.from_data(res['author'], login, info={'name': res['name'] or login, 'avatar': res['avatar']}) self.to_comment_id = to_comment_id or res['to_comment_id'] self.created = created or res['created'] self.text = text or res['text'] self.recommendations = 0 self.recommended = False self.files = res['files'] self.updated = res['updated'] self.comments = [] self.archive = archive if isinstance(files, (list, tuple)): self.files = files
def __init__(self, post_id, author=None, type=None, tags=None, private=None, created=None, title=None, link=None, text=None, edited=None, tz=settings.timezone, archive=None, files=None, pinned=None, tune=None): self._comments_count = None if post_id: if isinstance(post_id, (int, long)): if post_id == 0: raise PostNotFound(post_id) self.id = b26(post_id) else: self.id = post_id.lower() res = db.fetchone("SELECT p.author, u.login, p.type, p.private, " "p.created at time zone %s AS created, " "p.tags, p.title, p.link, p.text, p.files, " "p.edited, p.archive, p.pinned, p.tune " "FROM posts.posts p " "JOIN users.logins u ON p.author=u.id " "WHERE p.id=%s;", [tz, unb26(self.id)]) if not res: raise PostNotFound(post_id) self.author = User.from_data(res[0], res[1]) self.created = res['created'] if type is not None: self.type = type else: self.type = res['type'] if private is not None: self.private = private else: self.private = res['private'] if tags is not None: self.tags = [ t.decode('utf8').strip() if isinstance(t, str) else t for t in tags ] else: self.tags = res['tags'] if title is not None: if isinstance(title, str): self.title = title.decode('utf8').strip()[:128] elif isinstance(title, unicode): self.title = title.strip()[:128] else: self.title = res['title'] if link is not None: self.link = link else: self.link = res['link'] if text is not None: if isinstance(text, str): self.text = text.decode('utf8').strip()[:1048576] elif isinstance(text, unicode): self.text = text.strip()[:1048576] if len(self.text) < 3: raise PostTextError else: self.text = res['text'] if edited is not None: self.edited = edited else: self.edited = res['edited'] self.editable = not self.edited and \ self.created + \ timedelta(seconds=settings.edit_expire) >= \ datetime.now() # and \ #self.comments_count() == 0 if archive is not None: self.archive = archive else: self.archive = res['archive'] if pinned is not None: self.pinned = pinned else: self.pinned = res['pinned'] if tune is not None: self.tune = tune else: self.tune = res['tune'] if isinstance(files, (list, tuple)): self.files = files else: self.files = res['files'] #elif not author: # raise PostAuthorError #elif not text: # raise PostTextError else: self.id = None self.author = author self.type = type self.tags = tags self.private = private if isinstance(title, str): self.title = title.decode('utf-8').strip()[:128] elif isinstance(title, unicode): self.title = title.strip()[:128] else: self.title = title self.link = link if isinstance(text, str): try: self.text = text.decode('utf-8').strip()[:1048576] except UnicodeDecodeError: raise PostTextError elif isinstance(text, unicode): self.text = text.strip()[:1048576] else: self.text = text self.created = created self.edited = False self.editable = True self.archive = False if archive is None else archive self.pinned = False if pinned is None else pinned self.tune = tune self.tz = tz
def save(self): if self.tags: self.tags = [ t[:64] for t in filter(None, self.tags)[:10] ] try: if not isinstance(self.files, (list, tuple)): self.files = None except AttributeError: self.files = None if self.id: db.perform("DELETE FROM posts.tags WHERE post_id=%s;", [unb26(self.id)]) if self.tags: for t in self.tags: if isinstance(t, str): t = t.decode('utf-8') db.perform("INSERT INTO posts.tags " "(post_id, user_id, tag) VALUES (%s, %s, %s);", [unb26(self.id), self.author.id, t]) db.perform("UPDATE posts.posts SET tags=%s, private=%s," "text=%s, edited=%s, archive=%s, pinned=%s, files=%s " "WHERE id=%s;", [self.tags, bool(self.private), self.text, self.edited, self.archive, self.pinned, self.files, unb26(self.id)]) else: if not self.created: self.created = datetime.now() res = db.fetchone("INSERT INTO posts.posts " "(author, type, private, tags, title, link, text, " "created, edited, archive, pinned, tune, files) " "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) " "RETURNING id;", [self.author.id, self.type, bool(self.private), self.tags, self.title, self.link, self.text, self.created, self.edited, self.archive, self.pinned, Json(self.tune), self.files]) if not res: raise PostError self.id = b26(res[0]) if self.tags: for t in self.tags: try: db.perform("INSERT INTO posts.tags " "(post_id, user_id, tag) " "VALUES (%s, %s, %s);", [unb26(self.id), self.author.id, t]) except IntegrityError: pass #try: # es = elasticsearch.Elasticsearch(host=settings.elasticsearch_host, port=settings.elasticsearch_port) # es.index(index='point-posts', id=self.id, doc_type='post', body={ # 'post_id': self.id, # 'post_type': self.type, # 'created': self.created, # 'private': self.private, # 'user_id': self.author.id, # 'login': self.author.login, # 'title': self.title, # 'tags': self.tags, # 'text': self.text, # }) #except elasticsearch.ConnectionError, e: # log.error('Elasticsearch: %s' % e) return self.id
def save(self): if self.tags: self.tags = [t[:64] for t in filter(None, self.tags)[:10]] try: if not isinstance(self.files, (list, tuple)): self.files = None except AttributeError: self.files = None if self.id: db.perform("DELETE FROM posts.tags WHERE post_id=%s;", [unb26(self.id)]) if self.tags: for t in self.tags: if isinstance(t, str): t = t.decode("utf-8") db.perform( "INSERT INTO posts.tags " "(post_id, user_id, tag) VALUES (%s, %s, %s);", [unb26(self.id), self.author.id, t], ) db.perform( "UPDATE posts.posts SET tags=%s, private=%s," "text=%s, edited=%s, archive=%s, pinned=%s, files=%s " "WHERE id=%s;", [ self.tags, bool(self.private), self.text, self.edited, self.archive, self.pinned, self.files, unb26(self.id), ], ) else: if not self.created: self.created = datetime.now() res = db.fetchone( "INSERT INTO posts.posts " "(author, type, private, tags, title, link, text, " "created, edited, archive, pinned, files) " "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) " "RETURNING id;", [ self.author.id, self.type, bool(self.private), self.tags, self.title, self.link, self.text, self.created, self.edited, self.archive, self.pinned, self.files, ], ) if not res: raise PostError self.id = b26(res[0]) if self.tags: for t in self.tags: try: db.perform( "INSERT INTO posts.tags " "(post_id, user_id, tag) " "VALUES (%s, %s, %s);", [unb26(self.id), self.author.id, t], ) except IntegrityError: pass try: es = elasticsearch.Elasticsearch() es.index( index="point-posts", id=self.id, doc_type="post", body={ "post_id": self.id, "post_type": self.type, "created": self.created, "private": self.private, "user_id": self.author.id, "login": self.author.login, "title": self.title, "tags": self.tags, "text": self.text, }, ) except elasticsearch.ConnectionError, e: log.error("Elasticsearch: %s" % e)