Example #1
0
def crabtags(crabtag):
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    elif session.get("current_user") is not None:
        page_n = request.args.get("p", 1, type=int)
        if request.args.get("ajax_json"):
            blocks = dict()
            for block in ("title", "heading", "body"):
                blocks[block] = render_template(
                    f"crabtag-ajax-{block}.html",
                    current_page="crabtag",
                    crabtag=crabtag,
                    page_n=page_n,
                )
            return jsonify(blocks)
        else:
            molts = models.Molt.query_fast_with_tag(crabtag)
            molts = utils.get_current_user().filter_molt_query(molts)
            molts = molts.paginate(page_n, config.MOLTS_PER_PAGE, False)
            return render_template(
                ("crabtag-content.html"
                 if request.args.get("ajax_content") else "crabtag.html"),
                current_page="crabtag",
                page_n=page_n,
                molts=molts,
                crabtag=crabtag,
            )
    else:
        return redirect("/login")
Example #2
0
def bookmarks():
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    elif session.get("current_user") is not None:
        current_user = utils.get_current_user()
        page_n = request.args.get("p", 1, type=int)
        bookmarks = current_user.query_bookmarks()
        bookmarks = utils.get_current_user().filter_molt_query(bookmarks)
        bookmarks = bookmarks.paginate(page_n, config.MOLTS_PER_PAGE, False)
        if request.args.get("ajax_json"):
            blocks = dict()
            for block in ("title", "heading", "body"):
                blocks[block] = render_template(
                    f"bookmarks-ajax-{block}.html",
                    current_page="bookmarks",
                    page_n=page_n,
                    bookmarks=bookmarks,
                )
            return jsonify(blocks)
        else:
            return render_template(
                "bookmarks-content.html"
                if request.args.get("ajax_content") else "bookmarks.html",
                current_page="bookmarks",
                page_n=page_n,
                bookmarks=bookmarks,
            )
    else:
        return redirect("/login")
Example #3
0
def notifications():
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    elif session.get("current_user") is not None:
        page_n = request.args.get("p", 1, type=int)
        notifications = utils.get_current_user().get_notifications(
            paginated=True, page=page_n)
        if request.args.get("ajax_json"):
            blocks = dict()
            for block in ("title", "heading", "body"):
                blocks[block] = render_template(
                    f"notifications-ajax-{block}.html",
                    current_page="notifications",
                    notifications=notifications,
                )
            return jsonify(blocks)
        else:
            return render_template(
                "notifications.html",
                current_page="notifications",
                notifications=notifications,
            )
    else:
        return redirect("/login")
Example #4
0
def user_following(username, tab):
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    elif session.get("current_user") is not None:
        this_user = models.Crab.get_by_username(username)
        if this_user is None:
            return render_template("not-found.html", noun="user")
        elif this_user.banned:
            return render_template(
                "not-found.html",
                message="This user has been banned.",
            )
        else:
            followx = None
            if tab == "ing":
                followx = this_user.following
            elif tab == "ers":
                followx = this_user.followers
            elif tab == "ers_you_know":
                followx = utils.get_current_user().get_mutuals_for(this_user)
            return render_template(
                "followx.html",
                current_page=("own-profile" if this_user
                              == utils.get_current_user() else ""),
                followx=followx,
                this_user=this_user,
                tab="follow" + tab,
            )
    else:
        return redirect("/login")
Example #5
0
def signupsuccess():
    if request.method == "POST":
        return utils.common_molt_actions()

    current_user = utils.get_current_user()
    recommended_users = models.Crab.query.filter(
        models.Crab.username.in_(config.RECOMMENDED_USERS)).all()
    if current_user.referrer and current_user.referrer not in recommended_users:
        recommended_users.append(current_user.referrer)
    recommended_users.append
    return render_template(
        "signup_success.html",
        recommended_users=recommended_users,
    )
Example #6
0
def index():
    current_user = utils.get_current_user()

    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    if current_user is not None:
        page_n = request.args.get("p", 1, type=int)

        if request.args.get("ajax_json"):
            blocks = {}
            for block in ("title", "heading", "body"):
                blocks[block] = render_template(
                    f"timeline-ajax-{block}.html",
                    current_page="home",
                    page_n=page_n,
                )
            return jsonify(blocks)
        else:
            if request.args.get("ajax_content"):
                molts = current_user.query_timeline().paginate(
                    page_n, config.MOLTS_PER_PAGE, False)

                return render_template(
                    "timeline-content.html",
                    current_page="home",
                    page_n=page_n,
                    molts=molts,
                )
            else:
                return render_template(
                    "timeline.html",
                    current_page="home",
                    page_n=page_n,
                )
    else:
        featured_molt = models.Molt.query.filter_by(
            id=config.FEATURED_MOLT_ID).first()
        featured_user = models.Crab.query.filter_by(
            username=config.FEATURED_CRAB_USERNAME).first()
        return render_template(
            "welcome.html",
            featured_molt=featured_molt,
            featured_user=featured_user,
            fullwidth=True,
            current_page="welcome",
            hide_sidebar=True,
        )
Example #7
0
def molt_quotes_page(username, molt_id):
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    else:
        primary_molt = models.Molt.get_by_ID(molt_id)
        quotes = primary_molt.query_quotes()
        return render_template(
            "quotes.html",
            current_page="molt-page",
            molt=primary_molt,
            quotes=quotes,
        )
Example #8
0
def search():
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    elif session.get("current_user") is not None:
        query = request.args.get("q")
        page_n = request.args.get("p", 1, type=int)
        ajax_content = request.args.get("ajax_content")

        if request.args.get("ajax_json"):
            blocks = dict()
            for block in ("title", "heading", "body"):
                blocks[block] = render_template(
                    f"search-ajax-{block}.html",
                    current_page="search",
                    query=query,
                    page_n=page_n,
                )
            return jsonify(blocks)
        else:
            if query:
                crab_results = models.Crab.search(query)
                crab_results = (utils.get_current_user().
                                filter_user_query_by_not_blocked(crab_results))
                molt_results = models.Molt.search(query)
                molt_results = utils.get_current_user().filter_molt_query(
                    molt_results)
                molt_results = molt_results.paginate(page_n,
                                                     config.MOLTS_PER_PAGE,
                                                     False)
            else:
                molt_results = tuple()
                crab_results = tuple()

            return render_template(
                "search-results.html" if ajax_content else "search.html",
                current_page="search",
                query=query,
                page_n=page_n,
                molt_results=molt_results,
                crab_results=crab_results,
            )
    else:
        return redirect("/login")
Example #9
0
def molt_page(username, molt_id):
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    else:
        primary_molt = models.Molt.get_by_ID(molt_id)
        ajax_content = request.args.get("ajax_content")
        current_user = utils.get_current_user()

        # Check if blocked (if logged in)
        is_blocked = False
        if current_user and primary_molt:
            is_blocked = primary_molt.author.is_blocking(
                current_user) or primary_molt.author.is_blocked_by(
                    current_user)

        if (primary_molt is None or is_blocked
                or primary_molt.author.username != username):
            social_title = "Unavailable Post"
            return render_template("not-found.html", noun="molt")
        elif primary_molt.author.banned:
            social_title = "Unavailable Post"
            return render_template(
                "not-found.html",
                message="The author of this Molt has been banned.",
            )
        else:
            social_title = f"{primary_molt.author.display_name}'s post on Crabber"
            replies = primary_molt.query_replies()
            if current_user:
                replies = current_user.filter_molt_query(replies)
            return render_template(
                "molt-page-replies.html" if ajax_content else "molt-page.html",
                current_page="molt-page",
                molt=primary_molt,
                replies=replies,
                social_title=social_title,
            )
Example #10
0
def wild_west():
    current_user = utils.get_current_user()

    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    if current_user is not None:
        page_n = request.args.get("p", 1, type=int)
        # Ajax page switching
        if request.args.get("ajax_json"):
            blocks = dict()
            for block in ("title", "heading", "body"):
                blocks[block] = render_template(
                    f"wild-west-ajax-{block}.html",
                    current_page="wild-west",
                    page_n=page_n,
                )
            return jsonify(blocks)
        else:
            # Ajax content loading
            if request.args.get("ajax_content"):
                molts = current_user.query_wild()
                molts = molts.paginate(page_n, config.MOLTS_PER_PAGE, False)
                return render_template(
                    "wild-west-content.html",
                    current_page="wild-west",
                    page_n=page_n,
                    molts=molts,
                )
            # Page skeleton
            else:
                return render_template(
                    "wild-west.html",
                    current_page="wild-west",
                    page_n=page_n,
                )
    else:
        return redirect("/login")
Example #11
0
def settings():
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        # Handle style preferences
        if request.form.get("user_action") == "style_settings":
            current_user = utils.get_current_user()

            spooky_mode = request.form.get("spooky_mode") == "on"
            light_mode = request.form.get("light_mode") == "on"
            dyslexic_mode = request.form.get("dyslexic_mode") == "on"
            comicsans_mode = request.form.get("comicsans_mode") == "on"

            current_user.set_preference("spooky_mode", spooky_mode)
            current_user.set_preference("light_mode", light_mode)
            current_user.set_preference("dyslexic_mode", dyslexic_mode)
            current_user.set_preference("comicsans_mode", comicsans_mode)
            return "Saved preferences.", 200
        # Everything else
        else:
            return utils.common_molt_actions()

    # Display page
    elif session.get("current_user") is not None:
        if request.args.get("ajax_json"):
            blocks = dict()
            for block in ("title", "heading", "body"):
                blocks[block] = render_template(
                    f"settings-ajax-{block}.html",
                    current_page="settings",
                )
            return jsonify(blocks)
        else:
            return render_template(
                "settings.html",
                current_page="settings",
            )
    else:
        return redirect("/login")
Example #12
0
def stats():
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    current_user = utils.get_current_user()

    # Query follow counts for users
    most_followed = models.Crab.query_most_popular()
    most_referrals = models.Crab.query_most_referrals()
    newest_user = models.Crab.query_all().order_by(
        models.Crab.register_time.desc())

    if current_user:
        most_followed = current_user.filter_user_query_by_not_blocked(
            most_followed)
        most_referrals = current_user.filter_user_query_by_not_blocked(
            most_referrals)
        newest_user = current_user.filter_user_query_by_not_blocked(
            newest_user)

    most_followed = most_followed.first()
    most_referrals = most_referrals.first()
    newest_user = newest_user.first()

    best_molt = models.Molt.query_most_liked()
    talked_molt = models.Molt.query_most_replied()

    if current_user:
        best_molt = current_user.filter_molt_query(best_molt)
        talked_molt = current_user.filter_molt_query(talked_molt)

    best_molt = best_molt.first()
    talked_molt = talked_molt.first()

    trendy_tag = (models.Crabtag.query_most_popular().first() or (None, ))[0]
    if trendy_tag:
        trendy_tag_molts = models.Molt.order_query_by_likes(
            models.Molt.query_with_tag(trendy_tag))
        if current_user:
            trendy_tag_molts = current_user.filter_molt_query(trendy_tag_molts)
        trendy_tag_molts = trendy_tag_molts.limit(3).all()
    else:
        trendy_tag_molts = list()

    stats_dict = dict(
        users=models.Crab.active_user_count(),
        mini_stats=[
            dict(number=models.Molt.total_count(), label="molts sent"),
            dict(
                number=models.Molt.deleted_count(),
                label="molts deleted",
                sublabel="what are they hiding?",
            ),
            dict(number=models.Like.total_count(), label="likes given"),
            dict(number=models.TrophyCase.total_count(),
                 label="trophies awarded"),
        ],
        crab_king=most_followed,
        party_starter=most_referrals,
        baby_crab=newest_user,
        best_molt=best_molt,
        talked_molt=talked_molt,
        trendy_tag=trendy_tag,
        trendy_tag_molts=trendy_tag_molts,
    )

    if request.args.get("ajax_json"):
        blocks = dict()
        for block in ("title", "heading", "body"):
            blocks[block] = render_template(
                f"stats-ajax-{block}.html",
                stats=stats_dict,
                current_page="stats",
            )
        return jsonify(blocks)
    else:
        return render_template(
            "stats.html",
            stats=stats_dict,
            current_page="stats",
        )
Example #13
0
def user(username):
    # Handle forms and redirect to clear post data on browser
    if request.method == "POST":
        return utils.common_molt_actions()

    # Display page
    else:
        current_user = utils.get_current_user()
        current_tab = request.args.get("tab", default="molts")
        this_user = models.Crab.get_by_username(username)

        # Check if blocked (if logged in)
        current_user_is_blocked = False
        if current_user and this_user:
            current_user_is_blocked = this_user.is_blocking(current_user)

        if this_user is None or current_user_is_blocked:
            return render_template("not-found.html", noun="user")
        else:
            social_title = f"{this_user.display_name} on Crabber"
            m_page_n = request.args.get("molts-p", 1, type=int)
            r_page_n = request.args.get("replies-p", 1, type=int)
            l_page_n = request.args.get("likes-p", 1, type=int)

            if request.args.get("ajax_json"):
                blocks = dict()
                for block in ("title", "heading", "body"):
                    blocks[block] = render_template(
                        f"profile-ajax-{block}.html",
                        current_page=("own-profile"
                                      if this_user == current_user else ""),
                        this_user=this_user,
                        current_tab=current_tab,
                    )
                return jsonify(blocks)
            elif request.args.get("ajax_section"):
                section = request.args.get("ajax_section")
                hex_ID = request.args.get("hex_ID")

                molts = replies = likes = None

                if section == "molts":
                    # TODO: Expand threads automatically
                    molts = this_user.query_profile_molts(current_user)
                    if current_user:
                        molts = current_user.filter_molt_query(molts)
                    molts = molts.paginate(m_page_n, config.MOLTS_PER_PAGE,
                                           False)
                elif section == "replies":
                    replies = this_user.query_profile_replies(current_user)
                    if current_user:
                        replies = current_user.filter_molt_query(replies)
                    replies = replies.paginate(r_page_n, config.MOLTS_PER_PAGE,
                                               False)
                elif section == "likes":
                    likes = this_user.query_likes()
                    if current_user:
                        likes = current_user.filter_molt_query(likes)
                    likes = likes.paginate(l_page_n, config.MOLTS_PER_PAGE)
                return render_template(
                    f"profile-ajax-tab-{section}.html",
                    current_page=("own-profile"
                                  if this_user == current_user else ""),
                    molts=molts,
                    this_user=this_user,
                    likes=likes,
                    current_tab=current_tab,
                    replies=replies,
                    hexID=hex_ID,
                )
            else:
                return render_template(
                    "profile.html",
                    current_page=("own-profile"
                                  if this_user == current_user else ""),
                    this_user=this_user,
                    current_tab=current_tab,
                    m_page_n=m_page_n,
                    r_page_n=r_page_n,
                    l_page_n=l_page_n,
                    social_title=social_title,
                )