def profile(request, user_slug): if user_slug == "me": return redirect("profile", request.me.slug, permanent=False) user = get_object_or_404(User, slug=user_slug) if not request.me.is_moderator: # hide unverified and deleted users if user.moderation_status != User.MODERATION_STATUS_APPROVED or user.deleted_at: raise Http404() # handle auth redirect if user.id == request.me.id: goto = request.GET.get("goto") if goto and goto.startswith(settings.APP_HOST): return redirect(goto) tags = Tag.objects.filter(is_visible=True).all() intro = Post.get_user_intro(user) projects = Post.objects.filter(author=user, type=Post.TYPE_PROJECT, is_visible=True).all() # select user tags and calculate similarity with me active_tags = {t.tag_id for t in UserTag.objects.filter(user=user).all()} similarity = {} if user.id != request.me.id: my_tags = {t.tag_id for t in UserTag.objects.filter(user=request.me).all()} similarity = calculate_similarity(my_tags, active_tags, tags) achievements = UserAchievement.objects.filter(user=user).select_related("achievement") expertises = UserExpertise.objects.filter(user=user).all() comments = Comment.visible_objects()\ .filter(author=user, post__is_visible=True)\ .order_by("-created_at")\ .select_related("post") posts = Post.objects_for_user(request.me)\ .filter(author=user, is_visible=True)\ .exclude(type__in=[Post.TYPE_INTRO, Post.TYPE_PROJECT, Post.TYPE_WEEKLY_DIGEST])\ .order_by("-published_at") return render(request, "users/profile.html", { "user": user, "intro": intro, "projects": projects, "tags": tags, "active_tags": active_tags, "achievements": [ua.achievement for ua in achievements], "expertises": expertises, "comments": comments[:3], "comments_total": comments.count(), "posts": posts[:15], "posts_total": posts.count(), "similarity": similarity, })
def bookmarks(request): user = request.me posts = Post.objects_for_user(user)\ .filter(bookmarks__user=user, deleted_at__isnull=True)\ .order_by('-bookmarks__created_at')\ .all() return render(request, "bookmarks.html", { "posts": paginate(request, posts), })
def profile_posts(request, user_slug): if user_slug == "me": return redirect("profile_posts", request.me.slug, permanent=False) user = get_object_or_404(User, slug=user_slug) posts = Post.objects_for_user(request.me) \ .filter(author=user, is_visible=True) \ .exclude(type__in=[Post.TYPE_INTRO, Post.TYPE_PROJECT, Post.TYPE_WEEKLY_DIGEST]) \ .order_by("-published_at") return render(request, "users/profile/posts.html", { "user": user, "posts": paginate(request, posts, settings.PROFILE_POSTS_PAGE_SIZE), })
def profile(request, user_slug): if user_slug == "me": return redirect("profile", request.me.slug, permanent=False) user = get_object_or_404(User, slug=user_slug) if not request.me.is_moderator: # hide unverified users if user.moderation_status != User.MODERATION_STATUS_APPROVED: raise Http404() if user.id == request.me.id: goto = request.GET.get("goto") if goto and goto.startswith(settings.APP_HOST): return redirect(goto) tags = Tag.objects.filter(is_visible=True).all() intro = Post.get_user_intro(user) projects = Post.objects.filter(author=user, type=Post.TYPE_PROJECT, is_visible=True).all() active_tags = {t.tag_id for t in UserTag.objects.filter(user=user).all()} achievements = UserAchievement.objects.filter( user=user).select_related("achievement") expertises = UserExpertise.objects.filter(user=user).all() comments = Comment.visible_objects().filter( author=user, post__is_visible=True).order_by("-created_at")[:3] posts = Post.objects_for_user(request.me)\ .filter(author=user, is_visible=True)\ .exclude(type__in=[Post.TYPE_INTRO, Post.TYPE_PROJECT]) return render( request, "users/profile.html", { "user": user, "intro": intro, "projects": projects, "tags": tags, "active_tags": active_tags, "achievements": [ua.achievement for ua in achievements], "expertises": expertises, "comments": comments, "posts": paginate(request, posts), })
def feed(request, post_type=POST_TYPE_ALL, topic_slug=None, label_code=None, ordering=ORDERING_ACTIVITY): post_type = post_type or Post if request.me: request.me.update_last_activity() posts = Post.objects_for_user(request.me) else: posts = Post.visible_objects() # filter posts by type if post_type != POST_TYPE_ALL: posts = posts.filter(type=post_type) # filter by topic topic = None if topic_slug: topic = get_object_or_404(Topic, slug=topic_slug) posts = posts.filter(topic=topic) # filter by label if label_code: posts = posts.filter(label_code=label_code) # hide muted users if request.me: muted = Muted.objects.filter(user_from=request.me).values_list("user_to_id").all() if muted: posts = posts.exclude(author_id__in=muted) # hide non-public posts and intros from unauthorized users if not request.me: posts = posts.exclude(is_public=False).exclude(type=Post.TYPE_INTRO) # exclude shadow banned posts, but show them in "new" tab if ordering != ORDERING_NEW: if request.me: posts = posts.exclude(Q(is_shadow_banned=True) & ~Q(author_id=request.me.id)) else: posts = posts.exclude(is_shadow_banned=True) # hide no-feed posts (show only inside rooms and topics) if not topic and not label_code: posts = posts.filter(is_visible_in_feeds=True) # order posts by some metric if ordering: if ordering == ORDERING_ACTIVITY: posts = posts.order_by("-last_activity_at") elif ordering == ORDERING_NEW: posts = posts.order_by("-published_at", "-created_at") elif ordering == ORDERING_TOP: posts = posts.order_by("-upvotes") elif ordering == ORDERING_HOT: posts = posts.order_by("-hotness") elif ordering == ORDERING_TOP_WEEK: posts = posts.filter( published_at__gte=datetime.utcnow() - timedelta(days=7) ).order_by("-upvotes") elif ordering == ORDERING_TOP_MONTH: posts = posts.filter( published_at__gte=datetime.utcnow() - timedelta(days=31) ).order_by("-upvotes") elif ordering == ORDERING_TOP_YEAR: posts = posts.filter( published_at__gte=datetime.utcnow() - timedelta(days=365) ).order_by("-upvotes") else: raise Http404() # split results into pinned and unpinned posts on main page pinned_posts = [] if ordering == ORDERING_ACTIVITY: pinned_posts = posts.filter(is_pinned_until__gte=datetime.utcnow()) posts = posts.exclude(id__in=[p.id for p in pinned_posts]) return render(request, "feed.html", { "post_type": post_type or POST_TYPE_ALL, "ordering": ordering, "topic": topic, "label_code": label_code, "posts": paginate(request, posts), "pinned_posts": pinned_posts, "date_month_ago": datetime.utcnow() - timedelta(days=30), })
def feed(request, post_type=POST_TYPE_ALL, topic_slug=None, ordering=ORDERING_ACTIVITY): post_type = post_type or Post if request.me: request.me.update_last_activity() posts = Post.objects_for_user(request.me) else: posts = Post.visible_objects() # filter posts by type if post_type != POST_TYPE_ALL: posts = posts.filter(type=post_type) # filter by topic topic = None if topic_slug: topic = get_object_or_404(Topic, slug=topic_slug) posts = posts.filter(topic=topic) # hide non-public posts and intros from unauthorized users if not request.me: posts = posts.exclude(is_public=False).exclude(type=Post.TYPE_INTRO) # exclude shadow banned posts, but show them in "new" tab if ordering != ORDERING_NEW: if request.me: posts = posts.exclude( Q(is_shadow_banned=True) & ~Q(author_id=request.me.id)) else: posts = posts.exclude(is_shadow_banned=True) # no type and topic? probably it's the main page, let's apply some more filters if not topic and post_type == POST_TYPE_ALL: posts = posts.filter(is_visible_on_main_page=True) # order posts by some metric if ordering: if ordering == ORDERING_ACTIVITY: posts = posts.order_by("-last_activity_at") elif ordering == ORDERING_NEW: posts = posts.order_by("-published_at", "-created_at") elif ordering == ORDERING_TOP: posts = posts.order_by("-upvotes") elif ordering == ORDERING_TOP_WEEK: posts = posts.filter(published_at__gte=datetime.utcnow() - timedelta(days=7)).order_by("-upvotes") elif ordering == ORDERING_TOP_MONTH: posts = posts.filter(published_at__gte=datetime.utcnow() - timedelta(days=31)).order_by("-upvotes") else: raise Http404() # split results into pinned and unpinned posts on main page pinned_posts = [] if ordering == ORDERING_ACTIVITY: pinned_posts = posts.filter(is_pinned_until__gte=datetime.utcnow()) posts = posts.exclude(id__in=[p.id for p in pinned_posts]) return render( request, "feed.html", { "post_type": post_type or POST_TYPE_ALL, "ordering": ordering, "topic": topic, "posts": paginate(request, posts), "pinned_posts": pinned_posts, })