def show_friends(login_name): try: user = sqla.session.query( sqlm.User).filter_by(my_url=login_name.strip().lower())[0] except IndexError: abort(404) my_friend_requests = sqlm.Friendship.query.filter_by(blocked=False, pending=True) \ .filter(sqlm.Friendship.user == user).all() incoming_friend_requests = sqlm.Friendship.query.filter_by(blocked=False, pending=True) \ .filter(sqlm.Friendship.friend == user).all() friend_status_updates = sqlm.StatusUpdate.query \ .filter(sqlm.StatusUpdate.author_id.in_([u.id for u in user.friends()])) \ .filter_by(hidden=False) \ .order_by(sqla.desc(sqlm.StatusUpdate.created))[0:5] if current_user.is_authenticated: friend_blog_entries = sqla.session.query(sqlm.Blog) \ .join(sqlm.Blog.recent_entry) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqlm.BlogEntry.author_id.in_([u.id for u in user.friends()])) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[0:5] else: friend_blog_entries = sqla.session.query(sqlm.Blog) \ .join(sqlm.Blog.recent_entry) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqlm.BlogEntry.author_id.in_([u.id for u in user.friends()])) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[0:5] return render_template( "profile/friends.jade", profile=user, my_friend_requests=my_friend_requests, incoming_friend_requests=incoming_friend_requests, friend_status_updates=friend_status_updates, friend_blog_entries=friend_blog_entries, page_title="%s's Friends - %s" % (str(user.display_name), app.get_site_config("core.site-name")), )
def view_dashboard(): if current_user.is_admin: _followed_topics = sqla.session.query(sqlm.Topic) \ .join(sqlm.topic_watchers_table) \ .join(sqlm.Topic.recent_post) \ .filter(sqlm.topic_watchers_table.c.user_id == current_user.id) \ .order_by(sqlm.Post.created.desc())[:5] else: _followed_topics = sqla.session.query(sqlm.Topic) \ .join(sqlm.topic_watchers_table) \ .join(sqlm.Topic.recent_post) \ .filter(sqlm.topic_watchers_table.c.user_id == current_user.id) \ .filter(sqlm.Topic.category.has(sqlm.Category.restricted==False)) \ .order_by(sqlm.Post.created.desc())[:5] _followed_blogs = sqla.session.query(sqlm.Blog) \ .join(sqlm.blog_subscriber_table) \ .join(sqlm.Blog.recent_entry) \ .filter(sqlm.blog_subscriber_table.c.user_id == current_user.id) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[:5] return render_template("dashboard.jade", followed_topics = _followed_topics, followed_blogs = _followed_blogs, page_title="Your Dashboard - %s" % (app.get_site_config("core.site-name"),) )
def show_signatures(login_name): try: user = sqla.session.query( sqlm.User).filter_by(my_url=login_name.strip().lower())[0] except IndexError: abort(404) signatures = sqla.session.query(sqlm.Signature).filter_by(owner=user) \ .order_by(sqla.desc(sqlm.Signature.created)).all() parser = ForumPostParser() for s in signatures: try: s.parsed = parser.parse(s.html, _object=s) except: s.parsed = "" return render_template( "profile/view_signatures.jade", signatures=signatures, profile=user, page_title="%s's Signatures - %s" % (str(user.display_name), app.get_site_config("core.site-name")))
def status_update_index(): count = session.get("count", 15) authors = session.get("authors", []) search = session.get("search", "") if request.method == 'POST': request_json = request.get_json(force=True) count = int(request_json.get("count", 15)) if count > 1000: count = 1000 session["count"] = count search = request_json.get("search", "")[0:100] session["search"] = search if request_json.get("authors"): author_objects = list( sqla.session.query(sqlm.User) \ .filter(sqlm.User.id.in_(request_json.get("authors"))) \ .all() ) session["authors"] = [{ "id": a.id, "text": a.display_name } for a in author_objects] authors = [{ "id": a.id, "text": a.display_name } for a in author_objects] query_ = sqla.session.query(sqlm.StatusUpdate).filter_by(hidden=False) if authors: query_ = query_.filter( sqlm.StatusUpdate.author_id.in_([a["id"] for a in authors])) status_updates = parse_search_string(search, sqlm.StatusUpdate, query_, [ "message", ]).order_by(sqla.desc(sqlm.StatusUpdate.created))[:count] cleaner = ForumHTMLCleaner() if request.method == 'POST': parsed_statuses = [] for status in status_updates: parsed_status = {} parsed_status["_id"] = status.id parsed_status["profile_address"] = url_for( 'view_profile', login_name=status.author.my_url) parsed_status["user_name"] = status.author.display_name parsed_status["message"] = cleaner.escape(status.message) parsed_status["user_avatar"] = status.author.get_avatar_url("60") if status.attached_to_user != None: parsed_status[ "attached_to_user"] = status.attached_to_user.display_name parsed_status["attached_to_user_url"] = url_for( 'view_profile', login_name=status.attached_to_user.my_url) else: parsed_status["attached_to_user"] = False parsed_status["user_avatar_x"] = status.author.avatar_60_x parsed_status["user_avatar_y"] = status.author.avatar_60_y parsed_status["created"] = humanize_time(status.created) parsed_status["comment_count"] = status.get_comment_count() parsed_statuses.append(parsed_status) return app.jsonify(status_updates=parsed_statuses) else: return render_template("core/status_index.jade", page_title="Status Updates - %s" % (app.get_site_config("core.site-name"), ), status_updates=status_updates, count=count, search=search, authors=json.dumps(authors))
def message_index(pk, page, post): try: topic = sqla.session.query(sqlm.PrivateMessage).filter_by(id=pk)[0] except IndexError: return abort(404) try: pm_user = sqla.session.query(sqlm.PrivateMessageUser).filter_by( pm=topic, author=current_user)[0] pm_user.last_viewed = arrow.utcnow().datetime.replace(tzinfo=None) sqla.session.add(pm_user) sqla.session.commit() if pm_user.exited or pm_user.blocked: return abort(404) except IndexError: sqla.session.rollback() if current_user.login_name in ["scarlet", "zoop"]: pass else: return abort(404) try: page = int(page) except: page = 1 if topic.last_seen_by is None: topic.last_seen_by = {} try: flag_modified(topic, "last_seen_by") except: pass if post == "latest_post": try: post = sqla.session.query(sqlm.PrivateMessageReply).filter_by(pm=topic) \ .order_by(sqla.desc(sqlm.PrivateMessageReply.created))[0] except: return abort(404) elif post == "last_seen": try: last_seen = arrow.get( topic.last_seen_by.get(str(current_user.id), arrow.utcnow().timestamp)).datetime except: last_seen = arrow.get(arrow.utcnow().timestamp).datetime try: post = sqla.session.query(sqlm.PrivateMessageReply).filter_by(pm=topic) \ .filter(sqlm.PrivateMessageReply.created < last_seen) \ .order_by(sqlm.PrivateMessageReply.created.desc())[0] except IndexError: try: post = int(post) post = sqla.session.query(sqlm.PrivateMessageReply).filter_by( pm=topic, id=post)[0] except: return abort(404) else: if post != "": try: post = int(post) post = sqla.session.query(sqlm.PrivateMessageReply).filter_by( pm=topic, id=post)[0] except: return abort(404) else: post = "" pagination = 20 if post != "": try: topic.last_seen_by[str(current_user.id)] = arrow.utcnow().timestamp sqla.session.add(topic) sqla.session.commit() except: pass target_date = post.created posts_before_target = sqla.session.query(sqlm.PrivateMessageReply).filter_by(pm=topic) \ .filter(sqlm.PrivateMessageReply.created < post.created) \ .count() page = int(math.floor( float(posts_before_target) / float(pagination))) + 1 return render_template( "core/messages_topic.jade", page_title="%s - %s" % (str(topic.title), app.get_site_config("core.site-name")), topic=topic, initial_page=page, initial_post=str(post.id)) try: topic.last_seen_by[str(current_user.id)] = arrow.utcnow().timestamp sqla.session.add(topic) sqla.session.commit() except: pass return render_template( "core/messages_topic.jade", page_title="%s - %s" % (str(topic.title), app.get_site_config("core.site-name")), topic=topic, initial_page=page, )
def character_list_api(): try: current = int(request.args.get("start")) except: current = 0 try: draw = int(request.args.get("draw")) except: draw = 0 try: length = int(request.args.get("length")) except: length = 10 try: order = int(request.args.get("order[0][column]")) except: order = 4 if order == 0: order = "name" elif order == 1: order = "age" elif order == 2: order = "species" elif order == 3: order = "created" else: order = "created" try: direction = request.args.get("order[0][dir]") except: direction = "desc" query = request.args.get("search[value]", "")[0:100] if current_user.is_admin: character_count = sqla.session.query(sqlm.Character).count() filtered_character_count = parse_search_string( query, sqlm.Character, sqla.session.query(sqlm.Character), ["name",] ).count() else: character_count = sqla.session.query(sqlm.Character).count() filtered_character_count = parse_search_string( query, sqlm.Character, sqla.session.query(sqlm.Character), ["name",] ).filter_by(hidden=False).count() if direction == "desc": if current_user.is_admin: characters = parse_search_string( query, sqlm.Character, sqla.session.query(sqlm.Character), ["name",] ).order_by(sqla.desc(getattr(sqlm.Character, order)))[current:current+length] else: characters = parse_search_string( query, sqlm.Character, sqla.session.query(sqlm.Character), ["name",] ).filter_by(hidden=False).order_by(sqla.desc(getattr(sqlm.Character, order)))[current:current+length] else: if current_user.is_admin: characters = parse_search_string( query, sqlm.Character, sqla.session.query(sqlm.Character), ["name",] ).order_by(getattr(sqlm.Character, order))[current:current+length] else: characters = parse_search_string( query, sqlm.Character, sqla.session.query(sqlm.Character), ["name",] ).filter_by(hidden=False).order_by(getattr(sqlm.Character, order))[current:current+length] table_data = [] for i, character in enumerate(characters): if character.hidden: table_data.append( [ """<a href="/characters/%s">%s</a> """ % ( character.slug, character.name + " (HIDDEN)" ), character.author.display_name, get_preview(character.other, 50), humanize_time(character.created), "", "" ] ) else: table_data.append( [ """<a href="/characters/%s">%s</a> """ % ( character.slug, character.name ), character.author.display_name, get_preview(character.other, 50), humanize_time(character.created), "", "" ] ) data = { "draw": draw, "recordsTotal": character_count, "recordsFiltered": filtered_character_count, "data": table_data } return app.jsonify(data)
def search_lookup(): request_json = request.get_json(force=True) content_type = request_json.get("content_type", "posts") session["content_type"] = content_type query = request_json.get("q", "")[0:300] if len(query) < 3: return app.jsonify( error="Please enter at least 3 characters to search.") difference = (arrow.utcnow().datetime - arrow.get(current_user.last_search_time).datetime).seconds if difference < 120 and not current_user.is_admin: return app.jsonify( error="Please wait %s seconds before searching again." % (120 - difference)) try: session["query"] = query except: pass try: timezone = pytz.timezone(current_user.time_zone) except: timezone = pytz.timezone("US/Pacific") try: start_date = arrow.get(request_json.get("start_date", ""), [ "M/D/YY", ]) offset = timezone.utcoffset(start_date.naive).total_seconds() start_date = start_date.replace(seconds=-offset).datetime.replace( tzinfo=None) session["start_date"] = request_json.get("start_date", "") except: start_date = False session["start_date"] = "" try: # created end_date = arrow.get(request_json.get("end_date", ""), [ "M/D/YY", ]) offset = timezone.utcoffset(end_date.naive).total_seconds() end_date = end_date.replace(seconds=-offset).replace( hours=24).datetime.replace(tzinfo=None) session["end_date"] = request_json.get("end_date", "") except: end_date = False session["end_date"] = "" try: # category categories = list( sqla.session.query(sqlm.Category) \ .filter(sqlm.Category.id.in_(request_json.get("categories"))) \ .all() ) session["categories"] = [{ "id": c.id, "name": c.name } for c in categories] except: categories = [] session["categories"] = [] try: # category blogs = list( sqla.session.query(sqlm.Blog) \ .filter(sqlm.Blog.id.in_(request_json.get("blogs"))) \ .all() ) # session["categories"] = [{"id": c.id, "name": c.name} for c in categories] except: blogs = [] # session["categories"] = [] try: if content_type == "posts": topics = list( sqla.session.query(sqlm.Topic) \ .filter(sqlm.Topic.id.in_(request_json.get("topics"))) \ .all() ) elif content_type == "messages": topics = list( sqla.session.query(sqlm.PrivateMessage) \ .filter(sqlm.PrivateMessage.id.in_(request_json.get("topics"))) \ .all() ) session["topics"] = [{"id": t.id, "title": t.title} for t in topics] except: topics = [] session["topics"] = [] try: authors = list( sqla.session.query(sqlm.User) \ .filter(sqlm.User.id.in_(request_json.get("authors"))) \ .all() ) session["search_authors"] = [{ "id": a.id, "display_name": a.display_name } for a in authors] except: authors = [] session["search_authors"] = [] pagination = 20 try: page = int(request_json.get("page", 1)) except: page = 1 if content_type == "posts": query_ = sqla.session.query(sqlm.Post) model_ = sqlm.Post elif content_type == "topics": query_ = sqla.session.query(sqlm.Topic) model_ = sqlm.Topic elif content_type == "status": query_ = sqla.session.query(sqlm.StatusUpdate) model_ = sqlm.StatusUpdate elif content_type == "messages": query_ = sqla.session.query(sqlm.PrivateMessageReply) model_ = sqlm.PrivateMessageReply elif content_type == "blogs": query_ = sqla.session.query(sqlm.BlogEntry) model_ = sqlm.BlogEntry if start_date: query_ = query_.filter(model_.created >= start_date) if end_date: query_ = query_.filter(model_.created <= end_date) if categories and content_type == "topics": query_ = query_.filter( model_.category_id.in_([c.id for c in categories])) if blogs and content_type == "blogs": query_ = query_.filter(model_.blog_id.in_([b.id for b in blogs])) if topics and content_type == "posts": query_ = query_.filter(model_.topic_id.in_([t.id for t in topics])) if topics and content_type == "messages": query_ = query_.filter(model_.pm_id.in_([t.id for t in topics])) if authors: query_ = query_.filter(model_.author_id.in_([a.id for a in authors])) parsed_results = [] if content_type == "posts": if current_user.is_admin: query_ = search(query_, query) \ .join(sqlm.Post.topic) \ .filter(model_.hidden==False) else: _cat_perms = current_user.get_category_permission_subquery() query_ = search(query_, query) \ .join(sqlm.Post.topic) \ .join(_cat_perms, _cat_perms.c.category_id == sqlm.Topic.category_id) \ .filter(_cat_perms.c.category_can_view_topics == True) \ .filter(model_.hidden==False) results = query_.order_by(sqla.desc(model_.created)).paginate( page, pagination, False) has_next = len( query_.order_by(sqla.desc(model_.created)).paginate( pagination + 1, 1, False).items) > 0 if has_next: count = 21 elif len(results.items) == 0: count = 0 else: count = 20 for result in results.items: parsed_result = {} parsed_result["time"] = humanize_time(result.created) parsed_result["title"] = result.topic.title parsed_result["url"] = "/t/" + str( result.topic.slug) + "/page/1/post/" + str(result.id) parsed_result["description"] = result.html parsed_result["author_profile_link"] = result.author.my_url parsed_result["author_name"] = result.author.display_name parsed_result["readmore"] = True parsed_results.append(parsed_result) elif content_type == "topics": query_ = parse_search_string(query, model_, query_, ["title",]) \ .filter(model_.hidden==False) if not current_user.is_admin: _cat_perms = current_user.get_category_permission_subquery() query_ = query_ \ .join(_cat_perms, _cat_perms.c.category_id == sqlm.Topic.category_id) \ .filter(_cat_perms.c.category_can_view_topics == True) count = query_.count() if current_user.is_admin: results = query_ \ .join(sqlm.Topic.recent_post) \ .order_by(sqla.desc(sqlm.Post.created)).paginate(page, pagination, False) else: results = query_ \ .join(sqlm.Topic.recent_post) \ .order_by(sqla.desc(sqlm.Post.created)).paginate(page, pagination, False) for result in results.items: parsed_result = {} parsed_result["time"] = humanize_time(result.created) parsed_result["title"] = result.title parsed_result["url"] = "/t/" + result.slug parsed_result["description"] = "" parsed_result["author_profile_link"] = result.author.my_url parsed_result["author_name"] = result.author.display_name parsed_result["readmore"] = False parsed_results.append(parsed_result) elif content_type == "blogs": query_ = parse_search_string(query, model_, query_, ["html", "title"]) count = query_.count() results = query_ \ .join(sqlm.BlogEntry.blog) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[(page-1)*pagination:pagination*page] for result in results: parsed_result = {} parsed_result["time"] = humanize_time(result.created) parsed_result["title"] = result.title parsed_result["url"] = "/blog/" + str( result.blog.slug) + "/e/" + str(result.slug) parsed_result["description"] = result.html parsed_result["author_profile_link"] = result.author.my_url parsed_result["author_name"] = result.author.display_name parsed_result["readmore"] = True parsed_results.append(parsed_result) elif content_type == "status": query_ = parse_search_string(query, model_, query_, [ "message", ]).filter(model_.hidden == False) count = query_.count() results = query_ \ .order_by(sqla.desc(sqlm.StatusUpdate.created))[(page-1)*pagination:pagination*page] for result in results: parsed_result = {} parsed_result["time"] = humanize_time(result.created) parsed_result["title"] = result.message parsed_result["description"] = "" parsed_result["url"] = "/status/" + str(result.id) parsed_result["author_profile_link"] = result.author.my_url parsed_result["author_name"] = result.author.display_name parsed_result["readmore"] = False parsed_results.append(parsed_result) elif content_type == "messages": query_ = query_.join(sqlm.PrivateMessageUser, sqlm.PrivateMessageUser.pm_id == sqlm.PrivateMessageReply.pm_id) \ .filter( sqlm.PrivateMessageUser.author == current_user, sqlm.PrivateMessageUser.blocked == False, sqlm.PrivateMessageUser.exited == False ).order_by(sqlm.PrivateMessageReply.created.desc()) query_ = parse_search_string(query, model_, query_, [sqlm.PrivateMessageReply.message]) count = query_.count() results = query_[(page - 1) * pagination:pagination * page] for result in results: parsed_result = {} parsed_result["time"] = humanize_time(result.created) parsed_result["title"] = result.pm.title parsed_result["description"] = result.message parsed_result["url"] = "/messages/" + str( result.pm.id) + "/page/1/post/" + str(result.id) parsed_result["author_profile_link"] = result.author.my_url parsed_result["author_name"] = result.author.display_name parsed_result["readmore"] = True parsed_results.append(parsed_result) current_user.last_search_time = arrow.utcnow().datetime.replace( tzinfo=None) return app.jsonify(results=parsed_results, count=count, pagination=pagination)
def blog_index(slug, page): try: blog = sqla.session.query(sqlm.Blog).filter_by(slug=slug)[0] except IndexError: sqla.session.rollback() return abort(404) try: page = int(page) except: page = 1 request.canonical = app.config['BASE'] + "/blog/%s/page/%s" % ( slug, page, ) if blog.privacy_setting == "you" and current_user != blog.author: return abort(404) elif blog.privacy_setting == "editors" and ( current_user != blog.author and current_user not in blog.editors): return abort(404) elif blog.privacy_setting == "members" and not current_user.is_authenticated: return abort(404) if current_user == blog.author or current_user.is_admin: entries = sqla.session.query(sqlm.BlogEntry) \ .filter_by(hidden=False, blog=blog) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[(page-1)*10:page*10] entry_count = sqla.session.query(sqlm.BlogEntry) \ .filter_by(hidden=False, blog=blog).count() drafts = sqla.session.query(sqlm.BlogEntry) \ .filter_by(hidden=False, blog=blog, draft=True) \ .order_by(sqla.desc(sqlm.BlogEntry.created)).all() else: entries = sqla.session.query(sqlm.BlogEntry) \ .filter_by(hidden=False, blog=blog) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[(page-1)*10:page*10] entry_count = sqla.session.query(sqlm.BlogEntry) \ .filter_by(hidden=False, blog=blog) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqlm.BlogEntry.draft.isnot(True)).count() drafts = [] clean_html_parser = ForumPostParser() for entry in entries: entry.parsed = clean_html_parser.parse(entry.html, _object=entry) entry.parsed_truncated = str(BeautifulSoup(entry.parsed[:1000], "lxml")) + "..." description = clean_html_parser.parse(blog.description, _object=blog) comments = sqla.session.query(sqlm.BlogComment) \ .filter_by(hidden=False, blog=blog) \ .order_by(sqla.desc(sqlm.BlogComment.created))[0:10] pages = int(math.ceil(float(entry_count) / 10.0)) if pages > 10: pages = 10 pages = [p + 1 for p in range(pages)] return render_template("blogs/blog_entry_listing.jade", blog=blog, drafts=drafts, entries=entries, description=description, comments=comments, page=page, pages=pages, entry_count=entry_count, page_title=blog.name + " - %s" % (app.get_site_config("core.site-name"), ))
def blogs_index(page): if current_user.is_authenticated: my_blogs = sqla.session.query(sqlm.Blog) \ .filter_by(author=current_user) \ .filter(sqlm.Blog.disabled.isnot(True)).all() else: my_blogs = [] page = int(page) minimum = (int(page) - 1) * int(10) maximum = int(page) * int(10) request.canonical = app.config['BASE'] + "/blogs/page/%s" % (page, ) if current_user.is_authenticated: comments = sqla.session.query(sqlm.BlogComment) \ .join(sqlm.BlogComment.blog) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.desc(sqlm.BlogComment.created))[0:10] entries = sqla.session.query(sqlm.BlogEntry) \ .join(sqlm.BlogEntry.blog) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[minimum:maximum] count = sqla.session.query(sqlm.Blog) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .filter(sqlm.Blog.disabled.isnot(True)).count() featured_blog_entries = sqla.session.query(sqlm.BlogEntry) \ .join(sqlm.BlogEntry.blog) \ .join(sqlm.BlogEntry.author) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.User.banned.isnot(True)) \ .filter(sqlm.BlogEntry.featured == True) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[0:5] random_blogs = sqla.session.query(sqlm.Blog) \ .join(sqlm.Blog.recent_entry) \ .join(sqlm.Blog.author) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.User.banned.isnot(True)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.func.random())[0:10] else: comments = sqla.session.query(sqlm.BlogComment) \ .join(sqlm.BlogComment.blog) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all" )) \ .order_by(sqla.desc(sqlm.BlogComment.created))[0:10] entries = sqla.session.query(sqlm.BlogEntry) \ .join(sqlm.BlogEntry.blog) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[minimum:maximum] count = sqla.session.query(sqlm.Blog) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all" )) \ .filter(sqlm.Blog.disabled.isnot(True)).count() featured_blog_entries = sqla.session.query(sqlm.BlogEntry) \ .join(sqlm.BlogEntry.blog) \ .join(sqlm.BlogEntry.author) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.User.banned.isnot(True)) \ .filter(sqlm.BlogEntry.featured == True) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[0:5] random_blogs = sqla.session.query(sqlm.Blog) \ .join(sqlm.Blog.recent_entry) \ .join(sqlm.Blog.author) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.User.banned.isnot(True)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all" )) \ .order_by(sqla.func.random())[0:10] pages = int(math.ceil(float(count) / 10.0)) if pages > 10: pages = 10 pages = [p + 1 for p in range(pages)] clean_html_parser = ForumPostParser() for entry in entries: entry.preview = str( BeautifulSoup( clean_html_parser.parse(entry.html, _object=entry)[:500], "lxml")) + "..." return render_template("blogs/list_of_blogs.jade", page_title="Blogs - %s" % (app.get_site_config("core.site-name"), ), my_blogs=my_blogs, random_blogs=random_blogs, featured_blog_entries=featured_blog_entries, entries=entries, pages=pages, comments=comments, page=page)
def view_profile(login_name): try: user = sqla.session.query( sqlm.User).filter_by(my_url=login_name.strip().lower())[0] except IndexError: abort(404) parser = ForumPostParser() try: user.parsed_about_me = parser.parse(user.about_me, _object=user) except: user.parsed_about_me = "" age = False if user.birthday: today = arrow.utcnow().datetime age = today.year - user.birthday.year - ( (today.month, today.day) < (user.birthday.month, user.birthday.day)) if user.data != None: favorite_phrase = user.data.get("favorite_phrase", []) favorite_emotes = [ """<img src="/static/emotes/%s" />""" % emote for emote in user.data.get("favorite_emotes", []) ] else: favorite_phrase = [] favorite_emotes = [] recent_visitor_logs = sqla.session.query(sqlm.SiteLog.user_id, sqla.func.max(sqlm.SiteLog.time).label("time")) \ .filter(sqlm.SiteLog.user_id.isnot(None)) \ .filter(sqlm.SiteLog.path.like("/member/"+str(user.my_url))) \ .group_by(sqlm.SiteLog.user_id).order_by(sqla.desc(sqla.func.max(sqlm.SiteLog.time))).limit(5).subquery() recent_visitors = sqla.session.query(sqlm.User, recent_visitor_logs.c.time) \ .join(recent_visitor_logs, sqla.and_( recent_visitor_logs.c.user_id == sqlm.User.id, recent_visitor_logs.c.user_id != user.id ))[:5] recent_posts = [ ] #sqla.session.query(sqlm.Post).filter_by(author=user, hidden=False) \ # .join(sqlm.Post.topic) \ # .filter(sqlm.Topic.category.has(sqlm.Category.restricted==False)) \ # .order_by(sqla.desc(sqlm.Post.created))[:5] recent_topics = [ ] #sqla.session.query(sqlm.Topic).filter_by(author=user, hidden=False) \ # .filter(sqlm.Topic.category.has(sqlm.Category.restricted==False)) \ # .order_by(sqla.desc(sqlm.Topic.created))[:5] # try: # label = sqla.session.query(sqlm.Label).filter_by(label="ask")[0] # ask_me = sqla.session.query(sqlm.Topic).filter_by(author=user, hidden=False, label=label) \ # .order_by(sqla.desc(sqlm.Topic.created))[:5] # except IndexError: ask_me = [] recent_status_updates = sqla.session.query(sqlm.StatusUpdate) \ .filter_by(author=user, hidden=False) \ .order_by(sqla.desc(sqlm.StatusUpdate.created))[:5] recent_status_updates_to_user = sqla.session.query(sqlm.StatusUpdate) \ .filter_by(attached_to_user=user, hidden=False) \ .order_by(sqla.desc(sqlm.StatusUpdate.created))[:5] if current_user in user.friends(): recent_blog_entries = sqla.session.query(sqlm.BlogEntry) \ .filter(sqlm.BlogEntry.author==user) \ .join(sqlm.Blog, sqlm.BlogEntry.blog_id == sqlm.Blog.id) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members", sqlm.Blog.privacy_setting == "friends" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[0:5] elif current_user.is_authenticated: recent_blog_entries = sqla.session.query(sqlm.BlogEntry) \ .filter(sqlm.BlogEntry.author==user) \ .join(sqlm.Blog, sqlm.BlogEntry.blog_id == sqlm.Blog.id) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", sqlm.Blog.privacy_setting == "members" )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[0:5] else: recent_blog_entries = sqla.session.query(sqlm.BlogEntry) \ .filter(sqlm.BlogEntry.author==user) \ .join(sqlm.Blog, sqlm.BlogEntry.blog_id == sqlm.Blog.id) \ .filter(sqlm.Blog.disabled.isnot(True)) \ .filter(sqlm.BlogEntry.draft.isnot(True)) \ .filter(sqlm.BlogEntry.published.isnot(None)) \ .filter(sqla.or_( sqlm.Blog.privacy_setting == "all", )) \ .order_by(sqla.desc(sqlm.BlogEntry.published))[0:5] if user.data != None: if "my_fields" in user.data: custom_fields = user.data["my_fields"] else: custom_fields = [] available_fields = sqlm.User.AVAILABLE_PROFILE_FIELDS else: custom_fields = [] available_fields = [] if current_user != user: if user.profile_visits is None: user.profile_visits = 0 user.profile_visits += 1 sqla.session.add(user) sqla.session.commit() return render_template( "profile.jade", profile=user, page_title="%s - %s" % (str(user.display_name), app.get_site_config("core.site-name")), post_count=user.post_count, custom_fields=custom_fields, available_fields=available_fields, topic_count=user.topic_count, status_update_count=user.status_update_created, favorite_phrase=favorite_phrase, common_emotes=favorite_emotes, boops_given=user.boops_given, boops_received=user.boops_received, recent_posts=recent_posts, recent_topics=recent_topics, recent_visitors=recent_visitors, profile_visits=user.profile_visits, recent_blog_entries=recent_blog_entries, recent_status_updates=recent_status_updates, recent_status_updates_to_user=recent_status_updates_to_user, status_update_comments_count=user.status_update_comments_created, ask_me=ask_me, age=age)
def send_notification_emails(sqla): __banned_users_to_check = sqla.session.query( sqlm.User).filter_by(banned=True).all() for _u in __banned_users_to_check: notifications = sqla.session.query(sqlm.Notification) \ .filter_by(seen=False, acknowledged=False, emailed=False) \ .filter_by(user=_u) \ .order_by(sqla.desc(sqlm.Notification.created)).all() for n in notifications: n.emailed = True sqla.session.add(n) sqla.session.commit() __muted_users_to_check = sqla.session.query( sqlm.User).filter_by(emails_muted=True).all() for _u in __muted_users_to_check: notifications = sqla.session.query(sqlm.Notification) \ .filter_by(seen=False, acknowledged=False, emailed=False) \ .filter_by(user=_u) \ .order_by(sqla.desc(sqlm.Notification.created)).all() for n in notifications: n.emailed = True sqla.session.add(n) sqla.session.commit() _users_to_check = sqla.session.query(sqlm.User).filter_by( banned=False, validated=True).all() notification_formats = {} notification_full_names = {} for t in sqlm.Notification.NOTIFICATION_CATEGORIES: notification_formats[t[0]] = t[3] notification_full_names[t[0]] = (t[4], t[5]) for u in _users_to_check: if u.banned: continue if u.minimum_time_between_emails == None: u.minimum_time_between_emails = 360 notifications = sqla.session.query(sqlm.Notification) \ .filter_by(seen=False, acknowledged=False, emailed=False) \ .filter_by(user=u) \ .order_by(sqla.desc(sqlm.Notification.created)).all() notifications_count = len(notifications) try: if u.last_sent_notification_email > arrow.utcnow().replace( minutes=-u.minimum_time_between_emails).datetime.replace( tzinfo=None): continue except: pass _list = [] _list_k = {} _list_url = {} _details = [] _details_k = {} _summaries = [] _summaries_k = {} _total = 0 for n in notifications: if u.notification_preferences is None: u.notification_preferences = {} flag_modified(u, "notification_preferences") sqla.session.add(u) sqla.session.commit() if not u.notification_preferences.get(n.category, { "email": True }).get("email"): n.emailed = True sqla.session.add(n) sqla.session.commit() continue else: _total += 1 if notification_formats[n.category] == "summarized": if n.category not in _summaries_k: _summaries_k[n.category] = 1 _summaries.append(n.category) else: _summaries_k[n.category] += 1 if notification_formats[n.category] == "listed": if n.category not in _summaries_k: _summaries_k[n.category] = 1 _summaries.append(n.category) else: _summaries_k[n.category] += 1 if _base_url + n.url in _list_url: _list_url[_base_url + n.url] += 1 continue else: _list_url[_base_url + n.url] = 1 if n.category not in _list_k: _list_k[n.category] = [{ "message": n.author.display_name + " " + n.message, "url": _base_url + n.url }] _list.append(n.category) else: _list_k[n.category].append({ "message": n.author.display_name + " " + n.message, "url": _base_url + n.url }) if notification_formats[n.category] == "detailed": if n.category not in _summaries_k: _summaries_k[n.category] = 1 _summaries.append(n.category) else: _summaries_k[n.category] += 1 if n.category not in _details_k: _details_k[n.category] = [{ "url": _base_url + n.url, "message": n.author.display_name + " " + n.message, "description": get_preview_for_email(n.snippet) }] _details.append(n.category) else: _details_k[n.category].append({ "url": _base_url + n.url, "message": n.author.display_name + " " + n.message, "description": get_preview_for_email(n.snippet) }) if not u.emails_muted: _to_email_address = False if _debug: if not u.is_admin and not u.is_allowed_during_construction: continue else: _to_email_address = u.email_address else: _to_email_address = u.email_address if len(_list) == 0 and len(_details) == 0 and len(_summaries) == 0: continue _template = get_template_lookup().get_template("notification") _rendered = _template.render( _user=u, _base=_base_url, _list=_list, _list_k=_list_k, _list_url=_list_url, _details=_details, _details_k=_details_k, _summaries=_summaries, _summaries_k=_summaries_k, _notification_names=notification_full_names) u.last_sent_notification_email = arrow.utcnow().datetime.replace( tzinfo=None) sqla.session.add(u) sqla.session.commit() notifications_update = sqla.session.query(sqlm.Notification) \ .filter_by(seen=False, acknowledged=False, emailed=False) \ .filter_by(user=u) \ .all() for n in notifications_update: n.emailed = True sqla.session.add(n) sqla.session.commit() if _total == 1: subject = "You have a notification at %s" % ( app.get_site_config("core.site-name"), ) else: subject = "You have %s notifications at %s" % ( _total, app.get_site_config("core.site-name")) if not app.settings_file.get("lockout_on", False): result = requests.post( _mgurl + "/messages", auth=("api", _api), data={ "from": "%s <%s>" % (app.get_site_config("core.site-email-name"), app.get_site_config("core.site-email")), "to": _to_email_address, "subject": subject, "text": _rendered }) else: result = "LOCKDOWN ON" new_email_log = sqlm.EmailLog() new_email_log.to = u new_email_log.sent = arrow.utcnow().datetime.replace(tzinfo=None) new_email_log.subject = "You have %s notifications at %s" % ( _total, app.get_site_config("core.site-name")) new_email_log.body = _rendered new_email_log.result = str(result) sqla.session.add(new_email_log) sqla.session.commit()