Пример #1
0
def select_list(userid, rating, limit, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = ["SELECT jo.journalid, jo.title, jo.unixtime FROM journal jo WHERE"]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))
        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" jo.rating <= %i" % (rating,))

    if otherid:
        statement.append(
            " AND jo.userid = %i AND jo.settings !~ '[%sh]'" % (otherid, "" if frienduser.check(userid, otherid) else "f"))
    else:
        statement.append(" AND jo.settings !~ 'h'")

    statement.append("ORDER BY jo.journalid DESC LIMIT %i" % limit)

    query = [{
        "journalid": i[0],
        "title": i[1],
        "unixtime": i[2],
    } for i in d.execute("".join(statement))]

    return query[::-1] if backid else query
Пример #2
0
def select_query(userid, rating, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = [" FROM journal jo"
                 " JOIN profile pr ON jo.userid = pr.userid"
                 " WHERE jo.settings !~ 'h'"]

    if otherid:
        statement.append(" AND jo.userid = %i")

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" AND (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        statement.append(m.MACRO_FRIENDUSER_JOURNAL % (userid, userid, userid))

        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))

        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" AND jo.rating <= %i AND jo.settings !~ 'f'" % (rating,))

    if backid:
        statement.append(" AND jo.journalid > %i" % backid)
    elif nextid:
        statement.append(" AND jo.journalid < %i" % nextid)
    return statement
Пример #3
0
def select_latest(userid, rating, otherid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = ["SELECT jo.journalid, jo.title, jo.unixtime FROM journal jo WHERE"]

    if userid:
        if d.is_sfw_mode():
            statement.append(" (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))
        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" jo.rating <= %i" % (rating,))

    if otherid:
        statement.append(
            " AND jo.userid = %i AND jo.settings !~ '[%sh]'" % (otherid, "" if frienduser.check(userid, otherid) else "f"))

    statement.append("ORDER BY jo.journalid DESC LIMIT 1")
    query = d.execute("".join(statement), options="single")

    if query:
        return {
            "journalid": query[0],
            "title": query[1],
            "unixtime": query[2],
            "content": files.read("%s%s%i.txt" % (m.MACRO_SYS_JOURNAL_PATH, d.get_hash_path(query[0]), query[0])),
            "comments": d.execute("SELECT COUNT(*) FROM journalcomment WHERE targetid = %i AND settings !~ 'h'",
                                  [query[0]], ["element"]),
        }
Пример #4
0
def select_list(userid, rating, limit, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = ["SELECT jo.journalid, jo.title, jo.unixtime FROM journal jo WHERE"]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))
        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" jo.rating <= %i" % (rating,))

    if otherid:
        statement.append(
            " AND jo.userid = %i AND jo.settings !~ '[%sh]'" % (otherid, "" if frienduser.check(userid, otherid) else "f"))
    else:
        statement.append(" AND jo.settings !~ 'h'")

    statement.append("ORDER BY jo.journalid DESC LIMIT %i" % limit)

    query = [{
        "journalid": i[0],
        "title": i[1],
        "unixtime": i[2],
    } for i in d.execute("".join(statement))]

    return query[::-1] if backid else query
Пример #5
0
def select_query(userid, rating, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = [" FROM journal jo"
                 " JOIN profile pr ON jo.userid = pr.userid"
                 " WHERE jo.settings !~ 'h'"]

    if otherid:
        statement.append(" AND jo.userid = %i")

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" AND (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        statement.append(m.MACRO_FRIENDUSER_JOURNAL % (userid, userid, userid))

        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))

        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" AND jo.rating <= %i AND jo.settings !~ 'f'" % (rating,))

    if backid:
        statement.append(" AND jo.journalid > %i" % backid)
    elif nextid:
        statement.append(" AND jo.journalid < %i" % nextid)
    return statement
Пример #6
0
def select_list(userid, rating, limit, otherid=None, pending=False, backid=None, nextid=None, config=None, options=[]):
    if config is None:
        config = d.get_config(userid)

    statement = ["SELECT su.submitid, su.title, su.rating, co.unixtime, "
                 "su.userid, pr.username, su.settings, cpr.username, cpr.userid "]
    statement.extend(select_query(userid, rating, otherid, pending,
                                  backid, nextid, config))
    statement.append(" ORDER BY co.unixtime%s LIMIT %i" % ("" if backid else " DESC", limit))

    query = []
    for i in d.execute("".join(statement)):
        query.append({
            "contype": 10,
            "collection": True,
            "submitid": i[0],
            "title": i[1],
            "rating": i[2],
            "unixtime": i[3],
            "userid": i[4],
            "username": i[5],  # username of creator
            "collector": i[7],  # username of collector
            "collectorid": i[8],
            "sub_media": media.get_submission_media(i[0]),
        })

    return query[::-1] if backid else query
Пример #7
0
def select_latest(userid, rating, otherid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = ["SELECT jo.journalid, jo.title, jo.unixtime FROM journal jo WHERE"]

    if userid:
        if d.is_sfw_mode():
            statement.append(" (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))
        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" jo.rating <= %i" % (rating,))

    if otherid:
        statement.append(
            " AND jo.userid = %i AND jo.settings !~ '[%sh]'" % (otherid, "" if frienduser.check(userid, otherid) else "f"))

    statement.append("ORDER BY jo.journalid DESC LIMIT 1")
    query = d.execute("".join(statement), options="single")

    if query:
        return {
            "journalid": query[0],
            "title": query[1],
            "unixtime": query[2],
            "content": files.read("%s%s%i.txt" % (m.MACRO_SYS_JOURNAL_PATH, d.get_hash_path(query[0]), query[0])),
            "comments": d.execute("SELECT COUNT(*) FROM journalcomment WHERE targetid = %i AND settings !~ 'h'",
                                  [query[0]], ["element"]),
        }
Пример #8
0
def select_query(userid, rating, otherid=None, folderid=None,
                 backid=None, nextid=None, subcat=None, exclude=None,
                 options=[], config=None, profile_page_filter=False,
                 index_page_filter=False, featured_filter=False):
    if config is None:
        config = d.get_config(userid)
    statement = [
        "FROM submission su "
        "INNER JOIN profile pr ON su.userid = pr.userid "
        "LEFT JOIN folder f USING (folderid) "
        "WHERE su.settings !~ 'h'"]
    if profile_page_filter:
        statement.append(" AND COALESCE(f.settings !~ 'u', true)")
    if index_page_filter:
        statement.append(" AND COALESCE(f.settings !~ 'm', true)")
    if featured_filter:
        statement.append(" AND COALESCE(f.settings ~ 'f', false)")

    # Logged in users will see their own submissions regardless of rating
    # EXCEPT if they are in SFW mode
    if userid and not d.is_sfw_mode():
        statement.append(" AND (su.rating <= %i OR su.userid = %i)" % (rating, userid))
    else:
        statement.append(" AND su.rating <= %i" % (rating,))

    if otherid:
        statement.append(" AND su.userid = %i" % (otherid,))

    if folderid:
        statement.append(" AND su.folderid = %i" % (folderid,))

    if exclude:
        statement.append(" AND su.submitid != %i" % (exclude,))

    if subcat:
        statement.append(" AND su.subtype >= %i AND su.subtype < %i" % (subcat, subcat + 1000))

    if "critique" in options:
        statement.append(" AND su.settings ~ 'q' AND su.unixtime > %i" % (d.get_time() - 259200,))

    if backid:
        statement.append(" AND su.submitid > %i" % (backid,))
    elif nextid:
        statement.append(" AND su.submitid < %i" % (nextid,))
    elif "offset" in options:
        statement.append(" AND su.unixtime < %i" % (d.get_time() - 1800,))

    if userid:
        statement.append(m.MACRO_FRIENDUSER_SUBMIT % (userid, userid, userid))

        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "su"))

        statement.append(m.MACRO_BLOCKTAG_SUBMIT % (userid, userid))
    else:
        statement.append(" AND su.settings !~ 'f'")
    return statement
Пример #9
0
def edit_preferences(userid,
                     timezone=None,
                     preferences=None,
                     jsonb_settings=None):
    """
    Apply changes to stored preferences for a given user.
    :param userid: The userid to apply changes to
    :param timezone: (optional) new Timezone to set for user
    :param preferences: (optional) old-style char preferences, overwrites all previous settings
    :param jsonb_settings: (optional) JSON preferences, overwrites all previous settings
    :return: None
    """
    config = d.get_config(userid)

    tooyoung = False
    if preferences is not None:
        tooyoung |= get_user_age(userid) < preferences.rating.minimum_age
    if jsonb_settings is not None:
        sfwrating = jsonb_settings.max_sfw_rating
        sfwrating = ratings.CODE_MAP.get(sfwrating, ratings.GENERAL)
        tooyoung |= get_user_age(userid) < sfwrating.minimum_age

    if tooyoung:
        raise WeasylError("birthdayInsufficient")
    if timezone is not None and timezone not in pytz.all_timezones:
        raise WeasylError('invalidTimezone')

    db = d.connect()
    updates = {}
    if preferences is not None:
        # update legacy preferences
        # clear out the option codes that are being replaced.
        for i in Config.all_option_codes:
            config = config.replace(i, "")
        config_str = config + preferences.to_code()
        updates['config'] = config_str
        d._get_config.invalidate(userid)
    if jsonb_settings is not None:
        # update jsonb preferences
        updates['jsonb_settings'] = jsonb_settings.get_raw()
        d._get_profile_settings.invalidate(userid)

    d.engine.execute(
        tables.profile.update().where(tables.profile.c.userid == userid),
        updates)

    # update TZ
    if timezone is not None:
        tz = db.query(orm.UserTimezone).get(userid)
        if tz is None:
            tz = orm.UserTimezone(userid=userid)
            db.add(tz)
        tz.timezone = timezone
        db.flush()
        tz.cache()
    else:
        db.flush()
Пример #10
0
def edit_preferences(userid, timezone=None,
                     preferences=None, jsonb_settings=None):
    """
    Apply changes to stored preferences for a given user.
    :param userid: The userid to apply changes to
    :param timezone: (optional) new Timezone to set for user
    :param preferences: (optional) old-style char preferences, overwrites all previous settings
    :param jsonb_settings: (optional) JSON preferences, overwrites all previous settings
    :return: None
    """
    config = d.get_config(userid)

    tooyoung = False
    if preferences is not None:
        tooyoung |= get_user_age(userid) < preferences.rating.minimum_age
    if jsonb_settings is not None:
        sfwrating = jsonb_settings.max_sfw_rating
        sfwrating = ratings.CODE_MAP.get(sfwrating, ratings.GENERAL)
        tooyoung |= get_user_age(userid) < sfwrating.minimum_age

    if tooyoung:
        raise WeasylError("birthdayInsufficient")
    if timezone is not None and timezone not in pytz.all_timezones:
        raise WeasylError('invalidTimezone')

    db = d.connect()
    updates = {}
    if preferences is not None:
        # update legacy preferences
        # clear out the option codes that are being replaced.
        for i in Config.all_option_codes:
            config = config.replace(i, "")
        config_str = config + preferences.to_code()
        updates['config'] = config_str
        d._get_config.invalidate(userid)
    if jsonb_settings is not None:
        # update jsonb preferences
        updates['jsonb_settings'] = jsonb_settings.get_raw()
        d._get_profile_settings.invalidate(userid)

    d.engine.execute(
        tables.profile.update().where(tables.profile.c.userid == userid),
        updates
    )

    # update TZ
    if timezone is not None:
        tz = db.query(orm.UserTimezone).get(userid)
        if tz is None:
            tz = orm.UserTimezone(userid=userid)
            db.add(tz)
        tz.timezone = timezone
        db.flush()
        tz.cache()
    else:
        db.flush()
Пример #11
0
def select_near(userid,
                rating,
                limit,
                otherid,
                folderid,
                submitid,
                config=None):
    if config is None:
        config = d.get_config(userid)

    statement = [
        """
        SELECT su.submitid, su.title, su.rating, su.unixtime, su.userid,
               pr.username, su.settings, su.subtype
          FROM submission su
         INNER JOIN profile pr ON su.userid = pr.userid
         WHERE su.userid = %i
               AND su.settings !~ 'h'
    """ % (otherid, )
    ]

    if userid:
        # Users always see their own content.
        statement.append(" AND (su.rating <= %i OR su.userid = %i)" %
                         (rating, userid))
        statement.append(m.MACRO_IGNOREUSER % (userid, "su"))
        statement.append(m.MACRO_FRIENDUSER_SUBMIT % (userid, userid, userid))
        statement.append(m.MACRO_BLOCKTAG_SUBMIT % (userid, userid))
    else:
        statement.append(" AND su.rating <= %i AND su.settings !~ 'f'" %
                         (rating, ))

    if folderid:
        statement.append(" AND su.folderid = %i" % folderid)

    query = [{
        "contype": 10,
        "submitid": i[0],
        "title": i[1],
        "rating": i[2],
        "unixtime": i[3],
        "userid": i[4],
        "username": i[5],
        "subtype": i[7],
    } for i in d.execute("".join(statement))]

    query.sort(key=lambda i: i['submitid'])
    older = [i for i in query if i["submitid"] < submitid][-limit:]
    newer = [i for i in query if i["submitid"] > submitid][:limit]
    media.populate_with_submission_media(older + newer)

    return {
        "older": older,
        "newer": newer,
    }
Пример #12
0
def select_char(userid, rating, limit, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)
    query = []
    statement = ["""
        SELECT ch.charid, ch.char_name, ch.rating, fa.unixtime, ch.userid, pr.username, ch.settings
        FROM favorite fa
            INNER JOIN character ch ON fa.targetid = ch.charid
            INNER JOIN profile pr ON ch.userid = pr.userid
        WHERE fa.type = 'f'
            AND ch.settings !~ 'h'
    """]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (ch.rating <= %i)" % (rating,))
        else:
            statement.append(" AND (ch.userid = %i OR ch.rating <= %i)" % (userid, rating))
        statement.append(m.MACRO_FRIENDUSER_CHARACTER % (userid, userid, userid))
        statement.append(m.MACRO_IGNOREUSER % (userid, "ch"))
        statement.append(m.MACRO_BLOCKTAG_CHAR % (userid, userid))
    else:
        statement.append(" AND ch.rating <= %i AND ch.settings !~ 'f'" % (rating,))

    if otherid:
        statement.append(" AND fa.userid = %i" % (otherid,))

    if backid:
        statement.append(" AND fa.unixtime > "
                         "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'f'))"
                         % (otherid, backid))
    elif nextid:
        statement.append(" AND fa.unixtime < "
                         "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'f'))"
                         % (otherid, nextid))

    statement.append(" ORDER BY fa.unixtime%s LIMIT %i" % ("" if backid else " DESC", limit))

    from weasyl import character
    query = [{
        "contype": 20,
        "charid": i[0],
        "title": i[1],
        "rating": i[2],
        "unixtime": i[3],
        "userid": i[4],
        "username": i[5],
        "sub_media": character.fake_media_items(i[0], i[4], d.get_sysname(i[5]), i[6]),
    } for i in d.execute("".join(statement))]

    return query[::-1] if backid else query
Пример #13
0
def select_journal(userid, rating, limit, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)
    query = []
    statement = ["""
        SELECT jo.journalid, jo.title, jo.rating, fa.unixtime, jo.userid, pr.username, pr.config
        FROM favorite fa
            INNER JOIN journal jo ON fa.targetid = jo.journalid
            INNER JOIN profile pr ON jo.userid = pr.userid
        WHERE fa.type = 'j'
            AND jo.settings !~ 'h'
    """]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" AND (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        statement.append(m.MACRO_FRIENDUSER_JOURNAL % (userid, userid, userid))
        statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))
        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" AND jo.rating <= %i AND jo.settings !~ 'f'" % (rating,))

    if otherid:
        statement.append(" AND fa.userid = %i" % (otherid,))

    if backid:
        statement.append(" AND fa.unixtime > "
                         "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'j'))"
                         % (otherid, backid))
    elif nextid:
        statement.append(" AND fa.unixtime < "
                         "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'j'))"
                         % (otherid, nextid))

    statement.append(" ORDER BY fa.unixtime%s LIMIT %i" % ("" if backid else " DESC", limit))

    query = [{
        "contype": 30,
        "journalid": i[0],
        "title": i[1],
        "rating": i[2],
        "unixtime": i[3],
        "userid": i[4],
        "username": i[5],
    } for i in d.execute("".join(statement))]
    media.populate_with_user_media(query)

    return query[::-1] if backid else query
Пример #14
0
def insert(userid, otherid):
    if ignoreuser.check(otherid, userid):
        raise WeasylError("IgnoredYou")
    elif ignoreuser.check(userid, otherid):
        raise WeasylError("YouIgnored")

    try:
        d.execute("INSERT INTO watchuser VALUES (%i, %i, '%s')",
                  [userid, otherid, WatchSettings.from_code(d.get_config(userid)).to_code()])
    except IntegrityError:
        pass

    welcome.followuser_remove(userid, otherid)
    welcome.followuser_insert(userid, otherid)
Пример #15
0
def template_fields(userid):
    config = d.get_config(userid)
    rating = d.get_rating(userid)
    submissions = list(filter_submissions(userid, recent_submissions()))
    ret = partition_submissions(submissions)

    return ret + [
        # Recent site news update
        siteupdate.select(),
        # Recent critique submissions
        submission.select_list(userid, rating, 4, options=["critique"], config=config),
        # Currently streaming users
        profile.select_streaming(userid, rating, 4),
        # Recently popular submissions
        list(itertools.islice(filter_submissions(userid, submission.select_recently_popular(), incidence_limit=1), 11)),
    ]
Пример #16
0
def insert(userid, otherid):
    if ignoreuser.check(otherid, userid):
        raise WeasylError("IgnoredYou")
    elif ignoreuser.check(userid, otherid):
        raise WeasylError("YouIgnored")

    try:
        d.execute("INSERT INTO watchuser VALUES (%i, %i, '%s')", [
            userid, otherid,
            WatchSettings.from_code(d.get_config(userid)).to_code()
        ])
    except IntegrityError:
        pass

    welcome.followuser_remove(userid, otherid)
    welcome.followuser_insert(userid, otherid)
Пример #17
0
def select_user_list(userid, rating, limit, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = [
        "SELECT jo.journalid, jo.title, jo.userid, pr.username, pr.config, jo.rating, jo.unixtime"
        " FROM journal jo"
        " JOIN profile pr ON jo.userid = pr.userid"
        " WHERE jo.settings !~ 'h'"]

    if otherid:
        statement.append(" AND jo.userid = %i")

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (jo.rating <= %i)" % (rating,))
        else:
            statement.append(" AND (jo.userid = %i OR jo.rating <= %i)" % (userid, rating))
        statement.append(m.MACRO_FRIENDUSER_JOURNAL % (userid, userid, userid))

        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))

        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" AND jo.rating <= %i AND jo.settings !~ 'f'" % (rating,))

    if backid:
        statement.append(" AND jo.journalid > %i" % backid)
    elif nextid:
        statement.append(" AND jo.journalid < %i" % nextid)

    statement.append(" ORDER BY jo.journalid%s LIMIT %i" % ("" if backid else " DESC", limit))

    query = [{
        "contype": 30,
        "journalid": i[0],
        "title": i[1],
        "userid": i[2],
        "username": i[3],
        "rating": i[5],
        "unixtime": i[6],
    } for i in d.execute("".join(statement))]
    media.populate_with_user_media(query)

    return query[::-1] if backid else query
Пример #18
0
def select_submit_query(userid,
                        rating,
                        otherid=None,
                        backid=None,
                        nextid=None,
                        config=None):
    if config is None:
        config = d.get_config(userid)

    statement = [
        " FROM favorite fa INNER JOIN"
        " submission su ON fa.targetid = su.submitid"
        " INNER JOIN profile pr ON su.userid = pr.userid"
        " WHERE fa.type = 's' AND su.settings !~ 'h'"
    ]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (su.rating <= %i)" % (rating, ))
        else:
            statement.append(" AND (su.userid = %i OR su.rating <= %i)" %
                             (userid, rating))
        statement.append(m.MACRO_IGNOREUSER % (userid, "su"))
        statement.append(m.MACRO_BLOCKTAG_SUBMIT % (userid, userid))
        statement.append(m.MACRO_FRIENDUSER_SUBMIT % (userid, userid, userid))
    else:
        statement.append(" AND su.rating <= %i" % (rating, ))
        statement.append(" AND su.settings !~ 'f'")

    if otherid:
        statement.append(" AND fa.userid = %i" % otherid)

    if backid:
        statement.append(
            " AND fa.unixtime > "
            "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 's'))"
            % (otherid, backid))
    elif nextid:
        statement.append(
            " AND fa.unixtime < "
            "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 's'))"
            % (otherid, nextid))

    return statement
Пример #19
0
def select_near(userid, rating, limit, otherid, folderid, submitid, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = ["""
        SELECT su.submitid, su.title, su.rating, su.unixtime, su.userid,
               pr.username, su.settings, su.subtype
          FROM submission su
         INNER JOIN profile pr ON su.userid = pr.userid
         WHERE su.userid = %i
               AND su.settings !~ 'h'
    """ % (otherid,)]

    if userid:
        # Users always see their own content.
        statement.append(" AND (su.rating <= %i OR su.userid = %i)" % (rating, userid))
        statement.append(m.MACRO_IGNOREUSER % (userid, "su"))
        statement.append(m.MACRO_FRIENDUSER_SUBMIT % (userid, userid, userid))
        statement.append(m.MACRO_BLOCKTAG_SUBMIT % (userid, userid))
    else:
        statement.append(" AND su.rating <= %i AND su.settings !~ 'f'" % (rating,))

    if folderid:
        statement.append(" AND su.folderid = %i" % folderid)

    query = [{
        "contype": 10,
        "submitid": i[0],
        "title": i[1],
        "rating": i[2],
        "unixtime": i[3],
        "userid": i[4],
        "username": i[5],
        "subtype": i[7],
    } for i in d.execute("".join(statement))]

    query.sort(key=lambda i: i['submitid'])
    older = [i for i in query if i["submitid"] < submitid][-limit:]
    newer = [i for i in query if i["submitid"] > submitid][:limit]
    media.populate_with_submission_media(older + newer)

    return {
        "older": older,
        "newer": newer,
    }
Пример #20
0
def template_fields(userid):
    config = d.get_config(userid)
    rating = d.get_rating(userid)
    submissions = list(filter_submissions(userid, recent_submissions()))
    ret = partition_submissions(submissions)

    return ret + [
        # Recent site news update
        siteupdate.select(),
        # Recent critique submissions
        submission.select_list(
            userid, rating, 4, options=["critique"], config=config),
        # Currently streaming users
        profile.select_streaming(userid, rating, 4),
        # Recently popular submissions
        list(
            itertools.islice(
                filter_submissions(userid,
                                   submission.select_recently_popular(),
                                   incidence_limit=1), 11)),
    ]
Пример #21
0
def select_submit_query(userid, rating, otherid=None, backid=None, nextid=None, config=None):
    if config is None:
        config = d.get_config(userid)

    statement = [
        " FROM favorite fa INNER JOIN"
        " submission su ON fa.targetid = su.submitid"
        " INNER JOIN profile pr ON su.userid = pr.userid"
        " WHERE fa.type = 's' AND su.settings !~ 'h'"]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (su.rating <= %i)" % (rating,))
        else:
            statement.append(" AND (su.userid = %i OR su.rating <= %i)" % (userid, rating))
        statement.append(m.MACRO_IGNOREUSER % (userid, "su"))
        statement.append(m.MACRO_BLOCKTAG_SUBMIT % (userid, userid))
        statement.append(m.MACRO_FRIENDUSER_SUBMIT % (userid, userid, userid))
    else:
        statement.append(" AND su.rating <= %i" % (rating,))
        statement.append(" AND su.settings !~ 'f'")

    if otherid:
        statement.append(" AND fa.userid = %i" % otherid)

    if backid:
        statement.append(" AND fa.unixtime > "
                         "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 's'))"
                         % (otherid, backid))
    elif nextid:
        statement.append(" AND fa.unixtime < "
                         "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 's'))"
                         % (otherid, nextid))

    return statement
Пример #22
0
def select_char(userid,
                rating,
                limit,
                otherid=None,
                backid=None,
                nextid=None,
                config=None):
    if config is None:
        config = d.get_config(userid)
    query = []
    statement = [
        """
        SELECT ch.charid, ch.char_name, ch.rating, fa.unixtime, ch.userid, pr.username, ch.settings
        FROM favorite fa
            INNER JOIN character ch ON fa.targetid = ch.charid
            INNER JOIN profile pr ON ch.userid = pr.userid
        WHERE fa.type = 'f'
            AND ch.settings !~ 'h'
    """
    ]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (ch.rating <= %i)" % (rating, ))
        else:
            statement.append(" AND (ch.userid = %i OR ch.rating <= %i)" %
                             (userid, rating))
        statement.append(m.MACRO_FRIENDUSER_CHARACTER %
                         (userid, userid, userid))
        statement.append(m.MACRO_IGNOREUSER % (userid, "ch"))
        statement.append(m.MACRO_BLOCKTAG_CHAR % (userid, userid))
    else:
        statement.append(" AND ch.rating <= %i AND ch.settings !~ 'f'" %
                         (rating, ))

    if otherid:
        statement.append(" AND fa.userid = %i" % (otherid, ))

    if backid:
        statement.append(
            " AND fa.unixtime > "
            "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'f'))"
            % (otherid, backid))
    elif nextid:
        statement.append(
            " AND fa.unixtime < "
            "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'f'))"
            % (otherid, nextid))

    statement.append(" ORDER BY fa.unixtime%s LIMIT %i" %
                     ("" if backid else " DESC", limit))

    from weasyl import character
    query = [{
        "contype":
        20,
        "charid":
        i[0],
        "title":
        i[1],
        "rating":
        i[2],
        "unixtime":
        i[3],
        "userid":
        i[4],
        "username":
        i[5],
        "sub_media":
        character.fake_media_items(i[0], i[4], d.get_sysname(i[5]), i[6]),
    } for i in d.execute("".join(statement))]

    return query[::-1] if backid else query
Пример #23
0
def select_statistics(userid):
    if "i" in d.get_config(userid) and d.get_userid() not in staff.MODS:
        return
    return _select_statistics(userid)
Пример #24
0
def select_journal(userid,
                   rating,
                   limit,
                   otherid=None,
                   backid=None,
                   nextid=None,
                   config=None):
    if config is None:
        config = d.get_config(userid)
    query = []
    statement = [
        """
        SELECT jo.journalid, jo.title, jo.rating, fa.unixtime, jo.userid, pr.username, pr.config
        FROM favorite fa
            INNER JOIN journal jo ON fa.targetid = jo.journalid
            INNER JOIN profile pr ON jo.userid = pr.userid
        WHERE fa.type = 'j'
            AND jo.settings !~ 'h'
    """
    ]

    if userid:
        # filter own content in SFW mode
        if d.is_sfw_mode():
            statement.append(" AND (jo.rating <= %i)" % (rating, ))
        else:
            statement.append(" AND (jo.userid = %i OR jo.rating <= %i)" %
                             (userid, rating))
        statement.append(m.MACRO_FRIENDUSER_JOURNAL % (userid, userid, userid))
        statement.append(m.MACRO_IGNOREUSER % (userid, "jo"))
        statement.append(m.MACRO_BLOCKTAG_JOURNAL % (userid, userid))
    else:
        statement.append(" AND jo.rating <= %i AND jo.settings !~ 'f'" %
                         (rating, ))

    if otherid:
        statement.append(" AND fa.userid = %i" % (otherid, ))

    if backid:
        statement.append(
            " AND fa.unixtime > "
            "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'j'))"
            % (otherid, backid))
    elif nextid:
        statement.append(
            " AND fa.unixtime < "
            "(SELECT unixtime FROM favorite WHERE (userid, targetid, type) = (%i, %i, 'j'))"
            % (otherid, nextid))

    statement.append(" ORDER BY fa.unixtime%s LIMIT %i" %
                     ("" if backid else " DESC", limit))

    query = [{
        "contype": 30,
        "journalid": i[0],
        "title": i[1],
        "rating": i[2],
        "unixtime": i[3],
        "userid": i[4],
        "username": i[5],
    } for i in d.execute("".join(statement))]
    media.populate_with_user_media(query)

    return query[::-1] if backid else query
Пример #25
0
def select_statistics(userid):
    if "i" in d.get_config(userid) and d.get_userid() not in staff.MODS:
        return
    return _select_statistics(userid)
Пример #26
0
def select_query(userid,
                 rating,
                 otherid=None,
                 folderid=None,
                 backid=None,
                 nextid=None,
                 subcat=None,
                 exclude=None,
                 options=[],
                 config=None,
                 profile_page_filter=False,
                 index_page_filter=False,
                 featured_filter=False):
    if config is None:
        config = d.get_config(userid)
    statement = [
        "FROM submission su "
        "INNER JOIN profile pr ON su.userid = pr.userid "
        "LEFT JOIN folder f USING (folderid) "
        "WHERE su.settings !~ 'h'"
    ]
    if profile_page_filter:
        statement.append(" AND COALESCE(f.settings !~ 'u', true)")
    if index_page_filter:
        statement.append(" AND COALESCE(f.settings !~ 'm', true)")
    if featured_filter:
        statement.append(" AND COALESCE(f.settings ~ 'f', false)")

    # Logged in users will see their own submissions regardless of rating
    # EXCEPT if they are in SFW mode
    if userid and not d.is_sfw_mode():
        statement.append(" AND (su.rating <= %i OR su.userid = %i)" %
                         (rating, userid))
    else:
        statement.append(" AND su.rating <= %i" % (rating, ))

    if otherid:
        statement.append(" AND su.userid = %i" % (otherid, ))

    if folderid:
        statement.append(" AND su.folderid = %i" % (folderid, ))

    if exclude:
        statement.append(" AND su.submitid != %i" % (exclude, ))

    if subcat:
        statement.append(" AND su.subtype >= %i AND su.subtype < %i" %
                         (subcat, subcat + 1000))

    if "critique" in options:
        statement.append(" AND su.settings ~ 'q' AND su.unixtime > %i" %
                         (d.get_time() - 259200, ))

    if backid:
        statement.append(" AND su.submitid > %i" % (backid, ))
    elif nextid:
        statement.append(" AND su.submitid < %i" % (nextid, ))
    elif "offset" in options:
        statement.append(" AND su.unixtime < %i" % (d.get_time() - 1800, ))

    if userid:
        statement.append(m.MACRO_FRIENDUSER_SUBMIT % (userid, userid, userid))

        if not otherid:
            statement.append(m.MACRO_IGNOREUSER % (userid, "su"))

        statement.append(m.MACRO_BLOCKTAG_SUBMIT % (userid, userid))
    else:
        statement.append(" AND su.settings !~ 'f'")
    return statement