Here are the examples of the python api sqlalchemy.orm.lazyload taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
59 Examples
3
Source : test_options.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_unbound_cache_key_wildcard_one(self):
User, Address = self.classes("User", "Address")
query_path = self._make_path_registry([User, "addresses"])
opt = lazyload("*")
eq_(
opt._generate_cache_key(query_path),
(("relationship:_sa_default", ("lazy", "select")),),
)
def test_bound_cache_key_wildcard_two(self):
3
Source : test_query.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_m2o_joinedload_not_others(self):
self._eagerload_mappings(addresses_lazy="joined")
Address = self.classes.Address
sess = create_session()
q = (
sess.query(Address)
.options(lazyload("*"), joinedload("user"))
.yield_per(1)
.filter_by(id=1)
)
def go():
result = q.all()
assert result[0].user
self.assert_sql_count(testing.db, go, 1)
class HintsTest(QueryTest, AssertsCompiledSQL):
3
Source : clients.py
with Mozilla Public License 2.0
from kronusdev
with Mozilla Public License 2.0
from kronusdev
def idlist(self, page=1, **kwargs):
posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(app_id=self.id)
posts=posts.order_by(Submission.created_utc.desc())
posts=posts.offset(100*(page-1)).limit(101)
return [x[0] for x in posts.all()]
def comments_idlist(self, page=1, **kwargs):
3
Source : clients.py
with Mozilla Public License 2.0
from kronusdev
with Mozilla Public License 2.0
from kronusdev
def comments_idlist(self, page=1, **kwargs):
posts = g.db.query(Comment.id).options(lazyload('*')).filter_by(app_id=self.id)
posts=posts.order_by(Comment.created_utc.desc())
posts=posts.offset(100*(page-1)).limit(101)
return [x[0] for x in posts.all()]
class ClientAuth(Base, Stndrd):
3
Source : admin.py
with Mozilla Public License 2.0
from kronusdev
with Mozilla Public License 2.0
from kronusdev
def refund(v):
for u in g.db.query(User).all():
if u.id == 253: continue
posts=sum([x[0]+x[1]-1 for x in g.db.query(Submission.upvotes).options(lazyload('*')).filter_by(author_id = u.id, is_banned = False, deleted_utc = 0).all()])
comments=sum([x[0]+x[1]-1 for x in g.db.query(Comment.upvotes).options(lazyload('*')).filter_by(author_id = u.id, is_banned = False, deleted_utc = 0).all()])
u.coins = int(posts+comments)
g.db.add(u)
return "sex"
@app.get("/admin/dump_cache")
3
Source : chat.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def random_post(args, guild, v):
"""Fetches a random post from this channel's Guild."""
total=g.db.query(Submission).options(lazyload('*')).filter_by(board_id=guild.id, deleted_utc=0, is_banned=False).count()
offset=random.randint(0, total-1)
post=g.db.query(Submission).options(lazyload('*')).filter_by(board_id=guild.id, deleted_utc=0, is_banned=False).order_by(Submission.id.asc()).offset(offset).first()
speak(f"Random post requested by @{v.username}: < a href=\"https://{app.config['SERVER_NAME']}{post.permalink}\">{post.title} < /a>", v, guild, as_guild=True)
@command('shrug', syntax='[text]')
3
Source : clients.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def idlist(self, page=1, **kwargs):
posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(app_id=self.id)
posts=posts.order_by(Submission.created_utc.desc())
posts=posts.offset(100*(page-1)).limit(101)
return [x[0] for x in posts.all()]
def comments_idlist(self, page=1, **kwargs):
3
Source : clients.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def comments_idlist(self, page=1, **kwargs):
posts = g.db.query(Comment.id).options(lazyload('*')).filter_by(app_id=self.id)
posts=posts.order_by(Comment.created_utc.desc())
posts=posts.offset(100*(page-1)).limit(101)
return [x[0] for x in posts.all()]
class ClientAuth(Base, Stndrd):
3
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def system_notif_count(self):
return self.notifications.options(
lazyload('*')
).join(
Notification.comment
).filter(
Notification.read==False,
Comment.author_id==1
).count()
@property
0
Source : adjacency_list.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def find_document(path, compareto):
query = session.query(Document)
attribute = Document._root
for i, match in enumerate(
re.finditer(r"/([\w_]+)(?:\[@([\w_]+)(?:=(.*))?\])?", path)
):
(token, attrname, attrvalue) = match.group(1, 2, 3)
target_node = aliased(_Node)
query = query.join(attribute.of_type(target_node)).filter(
target_node.tag == token
)
attribute = target_node.children
if attrname:
attribute_entity = aliased(_Attribute)
if attrvalue:
query = query.join(
target_node.attributes.of_type(attribute_entity)
).filter(
and_(
attribute_entity.name == attrname,
attribute_entity.value == attrvalue,
)
)
else:
query = query.join(
target_node.attributes.of_type(attribute_entity)
).filter(attribute_entity.name == attrname)
return (
query.options(lazyload(Document._root))
.filter(target_node.text == compareto)
.all()
)
for path, compareto in (
0
Source : optimized_al.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def find_document(path, compareto):
query = session.query(Document)
for i, match in enumerate(
re.finditer(r"/([\w_]+)(?:\[@([\w_]+)(?:=(.*))?\])?", path)
):
(token, attrname, attrvalue) = match.group(1, 2, 3)
if not i:
parent = Document
target_node = aliased(_Node)
query = query.join(parent._nodes.of_type(target_node)).filter(
target_node.parent_id.is_(None)
)
else:
parent = target_node
target_node = aliased(_Node)
query = query.join(parent.children.of_type(target_node))
query = query.filter(target_node.tag == token)
if attrname:
attribute_entity = aliased(_Attribute)
query = query.join(
target_node.attributes.of_type(attribute_entity)
)
if attrvalue:
query = query.filter(
and_(
attribute_entity.name == attrname,
attribute_entity.value == attrvalue,
)
)
else:
query = query.filter(attribute_entity.name == attrname)
return (
query.options(lazyload(Document._nodes))
.filter(target_node.text == compareto)
.all()
)
for path, compareto in (
0
Source : test_baked.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def _test_baked_lazy_loading(self, set_option):
User, Address = self.classes.User, self.classes.Address
base_bq = self.bakery(lambda s: s.query(User))
if set_option:
base_bq += lambda q: q.options(lazyload(User.addresses))
base_bq += lambda q: q.order_by(User.id)
assert_result = self.static.user_address_result
for i in range(4):
for cond1, cond2 in itertools.product(
*[(False, True) for j in range(2)]
):
bq = base_bq._clone()
sess = Session()
if cond1:
bq += lambda q: q.filter(User.name == "jack")
else:
bq += lambda q: q.filter(User.name.like("%ed%"))
if cond2:
ct = func.count(Address.id).label("count")
subq = (
sess.query(ct, Address.user_id)
.group_by(Address.user_id)
.having(ct > 2)
.subquery()
)
bq += lambda q: q.join(subq)
if cond2:
if cond1:
def go():
result = bq(sess).all()
eq_([], result)
self.assert_sql_count(testing.db, go, 1)
else:
def go():
result = bq(sess).all()
eq_(assert_result[1:2], result)
self.assert_sql_count(testing.db, go, 2)
else:
if cond1:
def go():
result = bq(sess).all()
eq_(assert_result[0:1], result)
self.assert_sql_count(testing.db, go, 2)
else:
def go():
result = bq(sess).all()
eq_(assert_result[1:3], result)
self.assert_sql_count(testing.db, go, 3)
sess.close()
def test_baked_lazy_loading_m2o(self):
0
Source : test_baked.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_baked_lazy_loading_m2o(self):
User, Address = self._m2o_fixture()
base_bq = self.bakery(lambda s: s.query(Address))
base_bq += lambda q: q.options(lazyload(Address.user))
base_bq += lambda q: q.order_by(Address.id)
assert_result = self.static.address_user_result
for i in range(4):
for cond1 in (False, True):
bq = base_bq._clone()
sess = Session()
if cond1:
bq += lambda q: q.filter(
Address.email_address == "[email protected]"
)
else:
bq += lambda q: q.filter(
Address.email_address.like("ed@%")
)
if cond1:
def go():
result = bq(sess).all()
eq_(assert_result[0:1], result)
self.assert_sql_count(testing.db, go, 2)
else:
def go():
result = bq(sess).all()
eq_(assert_result[1:4], result)
self.assert_sql_count(testing.db, go, 2)
sess.close()
def test_useget_cancels_eager(self):
0
Source : test_default_strategies.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_joined_with_lazyload(self):
"""Mapper load strategy defaults can be upgraded with
joinedload('*') option, while explicit lazyload() option
is still honored"""
sess = self._upgrade_fixture()
users = []
# test joined all but 'keywords': upgraded to 1 sql
def go():
users[:] = (
sess.query(self.classes.User)
.options(sa.orm.lazyload("orders.items.keywords"))
.options(sa.orm.joinedload("*"))
.order_by(self.classes.User.id)
.all()
)
self.assert_sql_count(testing.db, go, 1)
# everything (but keywords) loaded ok
# (note self.static.user_all_result contains no keywords)
def go():
eq_(users, self.static.user_all_result)
self.assert_sql_count(testing.db, go, 0)
# verify the items were loaded, while item.keywords were not
def go():
# redundant with last test, but illustrative
users[0].orders[0].items[0]
self.assert_sql_count(testing.db, go, 0)
def go():
users[0].orders[0].items[0].keywords
self.assert_sql_count(testing.db, go, 1)
def test_joined_with_subqueryload(self):
0
Source : test_default_strategies.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_subquery_with_lazyload(self):
"""Mapper load strategy defaults can be upgraded with
subqueryload('*') option, while explicit lazyload() option
is still honored"""
sess = self._upgrade_fixture()
users = []
# test subquery all but 'keywords' (1 sql + 3 relationships = 4)
def go():
users[:] = (
sess.query(self.classes.User)
.options(sa.orm.lazyload("orders.items.keywords"))
.options(sa.orm.subqueryload("*"))
.order_by(self.classes.User.id)
.all()
)
self.assert_sql_count(testing.db, go, 4)
# no more sql
# (note self.static.user_all_result contains no keywords)
def go():
eq_(users, self.static.user_all_result)
self.assert_sql_count(testing.db, go, 0)
# verify the item.keywords were not loaded
def go():
users[0].orders[0].items[0]
self.assert_sql_count(testing.db, go, 0)
def go():
users[0].orders[0].items[0].keywords
self.assert_sql_count(testing.db, go, 1)
def test_subquery_with_joinedload(self):
0
Source : test_eager_relations.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_many_to_one_null(self):
"""test that a many-to-one eager load which loads None does
not later trigger a lazy load.
"""
Order, Address, addresses, orders = (
self.classes.Order,
self.classes.Address,
self.tables.addresses,
self.tables.orders,
)
# use a primaryjoin intended to defeat SA's usage of
# query.get() for a many-to-one lazyload
mapper(
Order,
orders,
properties=dict(
address=relationship(
mapper(Address, addresses),
primaryjoin=and_(
addresses.c.id == orders.c.address_id,
addresses.c.email_address != None, # noqa
),
lazy="joined",
)
),
)
sess = create_session()
def go():
o1 = (
sess.query(Order)
.options(lazyload("address"))
.filter(Order.id == 5)
.one()
)
eq_(o1.address, None)
self.assert_sql_count(testing.db, go, 2)
sess.expunge_all()
def go():
o1 = sess.query(Order).filter(Order.id == 5).one()
eq_(o1.address, None)
self.assert_sql_count(testing.db, go, 1)
def test_one_and_many(self):
0
Source : test_expire.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_state_noload_to_lazy(self):
"""Behavioral test to verify the current activity of loader callables
"""
users, Address, addresses, User = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User,
)
mapper(
User,
users,
properties={"addresses": relationship(Address, lazy="noload")},
)
mapper(Address, addresses)
sess = create_session()
u1 = sess.query(User).options(lazyload(User.addresses)).first()
assert isinstance(
attributes.instance_state(u1).callables["addresses"],
strategies.LoadLazyAttribute,
)
# expire, it stays
sess.expire(u1)
assert (
"addresses" not in attributes.instance_state(u1).expired_attributes
)
assert isinstance(
attributes.instance_state(u1).callables["addresses"],
strategies.LoadLazyAttribute,
)
# load over it. callable goes away.
sess.query(User).first()
assert (
"addresses" not in attributes.instance_state(u1).expired_attributes
)
assert "addresses" not in attributes.instance_state(u1).callables
sess.expunge_all()
u1 = sess.query(User).options(lazyload(User.addresses)).first()
sess.expire(u1, ["addresses"])
assert (
"addresses" not in attributes.instance_state(u1).expired_attributes
)
assert isinstance(
attributes.instance_state(u1).callables["addresses"],
strategies.LoadLazyAttribute,
)
# load the attr, goes away
u1.addresses
assert (
"addresses" not in attributes.instance_state(u1).expired_attributes
)
assert "addresses" not in attributes.instance_state(u1).callables
def test_deferred_expire_w_transient_to_detached(self):
0
Source : test_expire.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_refresh_with_lazy(self):
"""test that when a lazy loader is set as a trigger on an object's
attribute (at the attribute level, not the class level), a refresh()
operation doesn't fire the lazy loader or create any problems"""
User, Address, addresses, users = (
self.classes.User,
self.classes.Address,
self.tables.addresses,
self.tables.users,
)
s = create_session()
mapper(
User,
users,
properties={"addresses": relationship(mapper(Address, addresses))},
)
q = s.query(User).options(sa.orm.lazyload("addresses"))
u = q.filter(users.c.id == 8).first()
def go():
s.refresh(u)
self.assert_sql_count(testing.db, go, 1)
def test_refresh_with_eager(self):
0
Source : test_expire.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_refresh_maintains_deferred_options(self):
# testing a behavior that may have changed with
# [ticket:3822]
User, Address, Dingaling = self.classes("User", "Address", "Dingaling")
users, addresses, dingalings = self.tables(
"users", "addresses", "dingalings"
)
mapper(User, users, properties={"addresses": relationship(Address)})
mapper(
Address,
addresses,
properties={"dingalings": relationship(Dingaling)},
)
mapper(Dingaling, dingalings)
s = create_session()
q = (
s.query(User)
.filter_by(name="fred")
.options(sa.orm.lazyload("addresses").joinedload("dingalings"))
)
u1 = q.one()
# "addresses" is not present on u1, but when u1.addresses
# lazy loads, it should also joinedload dingalings. This is
# present in state.load_options and state.load_path. The
# refresh operation should not reset these attributes.
s.refresh(u1)
def go():
eq_(
u1.addresses,
[
Address(
email_address="[email protected]",
dingalings=[Dingaling(data="ding 2/5")],
)
],
)
self.assert_sql_count(testing.db, go, 1)
def test_refresh2(self):
0
Source : test_mapper.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_lazy_options_with_limit(self):
Address, addresses, users, User = (
self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User,
)
mapper(
User,
users,
properties=dict(
addresses=relationship(
mapper(Address, addresses), lazy="joined"
)
),
)
sess = create_session()
u = (
sess.query(User)
.options(sa.orm.lazyload("addresses"))
.filter_by(id=8)
).one()
def go():
eq_(u.id, 8)
eq_(len(u.addresses), 3)
self.sql_count_(1, go)
def test_eager_degrade(self):
0
Source : test_mapper.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_lazy_options(self):
"""An eager relationship can be upgraded to a lazy relationship."""
Address, addresses, users, User = (
self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User,
)
mapper(
User,
users,
properties=dict(
addresses=relationship(
mapper(Address, addresses), lazy="joined"
)
),
)
sess = create_session()
result = (
sess.query(User)
.order_by(User.id)
.options(sa.orm.lazyload("addresses"))
).all()
def go():
eq_(result, self.static.user_address_result)
self.sql_count_(4, go)
def test_option_propagate(self):
0
Source : test_mapper.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_upgrade_o2m_noload_lazyload_option(self):
Address, addresses, users, User = (
self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User,
)
m = mapper(
User,
users,
properties=dict(
addresses=relationship(
mapper(Address, addresses), lazy="noload"
)
),
)
q = create_session().query(m).options(sa.orm.lazyload("addresses"))
result = [None]
def go():
x = q.filter(User.id == 7).all()
x[0].addresses
result[0] = x
self.sql_count_(2, go)
self.assert_result(
result[0], User, {"id": 7, "addresses": (Address, [{"id": 1}])}
)
def test_m2o_noload_option(self):
0
Source : test_mapper.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_o2m_raiseload_lazyload_option(self):
Address, addresses, users, User = (
self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User,
)
mapper(Address, addresses)
mapper(
User,
users,
properties=dict(addresses=relationship(Address, lazy="raise")),
)
q = create_session().query(User).options(sa.orm.lazyload("addresses"))
result = [None]
def go():
x = q.filter(User.id == 7).all()
x[0].addresses
result[0] = x
self.sql_count_(2, go)
self.assert_result(
result[0], User, {"id": 7, "addresses": (Address, [{"id": 1}])}
)
def test_m2o_raiseload_option(self):
0
Source : test_pickled.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_instance_lazy_relation_loaders(self):
users, addresses = (self.tables.users, self.tables.addresses)
mapper(
User,
users,
properties={"addresses": relationship(Address, lazy="noload")},
)
mapper(Address, addresses)
sess = Session()
u1 = User(name="ed", addresses=[Address(email_address="[email protected]")])
sess.add(u1)
sess.commit()
sess.close()
u1 = sess.query(User).options(lazyload(User.addresses)).first()
u2 = pickle.loads(pickle.dumps(u1))
sess = Session()
sess.add(u2)
assert u2.addresses
def test_invalidated_flag_pickle(self):
0
Source : admin.py
with Mozilla Public License 2.0
from kronusdev
with Mozilla Public License 2.0
from kronusdev
def admin_removed(v):
page = int(request.args.get("page", 1))
ids = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=True).order_by(
Submission.id.desc()).offset(25 * (page - 1)).limit(26).all()
ids=[x[0] for x in ids]
next_exists = len(ids) == 26
ids = ids[:25]
posts = get_posts(ids, v=v)
return render_template("admin/removed_posts.html",
v=v,
listing=posts,
page=page,
next_exists=next_exists
)
@app.post("/admin/image_purge")
0
Source : admin.py
with Mozilla Public License 2.0
from kronusdev
with Mozilla Public License 2.0
from kronusdev
def admin_title_change(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
if user.admin_level != 0: abort(403)
new_name=request.form.get("title").strip()
user.customtitleplain=new_name
new_name = sanitize(new_name, linkgen=True)
user=g.db.query(User).with_for_update().options(lazyload('*')).filter_by(id=user.id).first()
user.customtitle=new_name
user.flairchanged = bool(request.form.get("locked"))
g.db.add(user)
if user.flairchanged: kind = "set_flair_locked"
else: kind = "set_flair_notlocked"
ma=ModAction(
kind=kind,
user_id=v.id,
target_user_id=user.id,
note=f'"{new_name}"'
)
g.db.add(ma)
return (redirect(user.url), user)
@app.post("/ban_user/ < user_id>")
0
Source : base.py
with BSD 3-Clause "New" or "Revised" License
from mononobi
with BSD 3-Clause "New" or "Revised" License
from mononobi
def _count(self, **options):
"""
returns the count of rows that the sql formed by this `Query` would return.
this method generates a single sql query like below:
select count(column, ...)
from table
where ...
if a single query could not be produced, an error may be raised.
:keyword CoreColumn column: column to be used in count function.
defaults to `*` if not provided.
:keyword bool distinct: specifies that count should
be executed on distinct select.
defaults to False if not provided.
note that `distinct` will only be
used if `column` is also provided.
:raises EfficientCountIsNotPossibleError: efficient count is not possible error.
:rtype: int
"""
old_statement = self.options(lazyload('*')).statement
if not old_statement.is_select or not old_statement.is_selectable:
raise EfficientCountIsNotPossibleError('The provided statement is not a select '
'statement and efficient count could not '
'be produced for it.')
if old_statement._group_by_clauses is not None \
and len(old_statement._group_by_clauses) > 0:
raise EfficientCountIsNotPossibleError('The provided statement has group by clause '
'and efficient count could not be produced '
'for it.')
column = options.get('column')
is_distinct = options.get('distinct', False)
func_count = None
if column is None:
column = self._get_appropriate_column(*old_statement.selected_columns)
if column is None:
raise EfficientCountIsNotPossibleError('The provided statement has complex '
'columns and efficient count could not '
'be produced for it.')
func_count = func.count(column)
else:
if is_distinct is True:
func_count = func.count(distinct(column))
else:
func_count = func.count(column)
statement = self._copy_statement(old_statement, func_count).order_by(None)
store = get_current_store()
result = store.execute(statement).scalar()
if result is None:
result = 0
return result
def count(self, **options):
0
Source : boards.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def idlist(self, sort=None, page=1, t=None,
hide_offensive=True, hide_bot=False, v=None, nsfw=False, **kwargs):
posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=False,
#is_pinned=False,
board_id=self.id
).filter(Submission.deleted_utc == 0)
if not nsfw:
posts = posts.filter_by(over_18=False)
if v and v.hide_offensive:
posts = posts.filter(
or_(
Submission.is_offensive==False,
Submission.author_id==v.id
)
)
if v and v.hide_bot and not self.has_mod(v, "content"):
posts = posts.filter_by(is_bot=False)
if v and not v.show_nsfl:
posts = posts.filter_by(is_nsfl=False)
if self.is_private:
if v and (self.can_view(v) or v.admin_level >= 4):
pass
elif v:
posts = posts.filter(or_(Submission.post_public == True,
Submission.author_id == v.id
)
)
else:
posts = posts.filter_by(post_public=True)
if v and not self.has_mod(v) and v.admin_level < = 3:
# blocks
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=v.id).subquery()
# blocked = g.db.query(
# UserBlock.user_id).filter_by(
# target_id=v.id).subquery()
posts = posts.filter(
Submission.author_id.notin_(blocking) #,
# Submission.author_id.notin_(blocked)
)
if t == None and v: t = v.defaulttime
if t:
now = int(time.time())
if t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
else:
cutoff = 0
posts = posts.filter(Submission.created_utc >= cutoff)
gt = kwargs.get("gt")
lt = kwargs.get("lt")
if gt:
posts = posts.filter(Submission.created_utc > gt)
if lt:
posts = posts.filter(Submission.created_utc < lt)
if sort == None:
if v: sort = v.defaultsorting
else: sort = "hot"
if sort != "new" and sort != "old": posts.filter_by(is_pinned=False)
if sort == "hot":
posts = posts.order_by(Submission.score_best.desc())
elif sort == "new":
posts = posts.order_by(Submission.created_utc.desc())
elif sort == "old":
posts = posts.order_by(Submission.created_utc.asc())
elif sort == "disputed":
posts = posts.order_by(Submission.score_disputed.desc())
elif sort == "top":
posts = posts.order_by(Submission.score_top.desc())
elif sort == "activity":
posts = posts.order_by(Submission.score_activity.desc())
else:
abort(422)
posts = [x[0] for x in posts.offset(25 * (page - 1)).limit(26).all()]
return posts
def has_mod(self, user, perm=None):
0
Source : boards.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def comment_idlist(self, page=1, v=None, nsfw=False, **kwargs):
posts = g.db.query(Submission).options(
lazyload('*')).filter_by(board_id=self.id)
if not nsfw:
posts = posts.filter_by(over_18=False)
if v and not v.show_nsfl:
posts = posts.filter_by(is_nsfl=False)
if self.is_private:
if v and (self.can_view(v) or v.admin_level >= 4):
pass
elif v:
posts = posts.filter(or_(Submission.post_public == True,
Submission.author_id == v.id
)
)
else:
posts = posts.filter_by(post_public=True)
posts = posts.subquery()
comments = g.db.query(Comment).options(lazyload('*'))
if v and v.hide_offensive:
comments = comments.filter_by(is_offensive=False)
if v and v.hide_bot and not self.has_mod(v, "content"):
comments = comments.filter_by(is_bot=False)
if v and not self.has_mod(v) and v.admin_level < = 3:
# blocks
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=v.id).subquery()
blocked = g.db.query(
UserBlock.user_id).filter_by(
target_id=v.id).subquery()
comments = comments.filter(
Comment.author_id.notin_(blocking),
Comment.author_id.notin_(blocked)
)
if not v or not v.admin_level >= 3:
comments = comments.filter_by(is_banned=False).filter(Comment.deleted_utc == 0)
comments = comments.join(
posts, Comment.parent_submission == posts.c.id)
comments = comments.order_by(Comment.created_utc.desc()).offset(
25 * (page - 1)).limit(26).all()
return [x.id for x in comments]
def user_guild_rep(self, user):
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def idlist(self, sort=None, page=1, t=None, filter_words="", **kwargs):
posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=False,
deleted_utc=0,
stickied=False
)
if not self.over_18:
posts = posts.filter_by(over_18=False)
if self.hide_offensive:
posts = posts.filter_by(is_offensive=False)
if self.hide_bot:
posts = posts.filter_by(is_bot=False)
if not self.show_nsfl:
posts = posts.filter_by(is_nsfl=False)
board_ids = g.db.query(
Subscription.board_id).filter_by(
user_id=self.id,
is_active=True).subquery()
user_ids = g.db.query(
Follow.user_id).filter_by(
user_id=self.id).join(
Follow.target).filter(
User.is_private == False,
User.is_nofollow == False).subquery()
posts = posts.filter(
or_(
Submission.board_id.in_(board_ids),
Submission.author_id.in_(user_ids)
)
)
if self.admin_level < 4:
# admins can see everything
m = g.db.query(
ModRelationship.board_id).filter_by(
user_id=self.id,
invite_rescinded=False).subquery()
c = g.db.query(
ContributorRelationship.board_id).filter_by(
user_id=self.id).subquery()
posts = posts.filter(
or_(
Submission.author_id == self.id,
Submission.post_public == True,
Submission.board_id.in_(m),
Submission.board_id.in_(c)
)
)
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=self.id).subquery()
# blocked = g.db.query(
# UserBlock.user_id).filter_by(
# target_id=self.id).subquery()
posts = posts.filter(
Submission.author_id.notin_(blocking) #,
#Submission.author_id.notin_(blocked)
).join(Submission.board).filter(Board.is_banned==False)
if filter_words:
posts=posts.join(Submission.submission_aux)
for word in filter_words:
#print(word)
posts=posts.filter(not_(SubmissionAux.title.ilike(f'%{word}%')))
if t:
now = int(time.time())
if t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
else:
cutoff = 0
posts = posts.filter(Submission.created_utc >= cutoff)
gt = kwargs.get("gt")
lt = kwargs.get("lt")
if gt:
posts = posts.filter(Submission.created_utc > gt)
if lt:
posts = posts.filter(Submission.created_utc < lt)
if sort == None:
sort= self.defaultsorting or "hot"
if sort == "hot":
posts = posts.order_by(Submission.score_best.desc())
elif sort == "new":
posts = posts.order_by(Submission.created_utc.desc())
elif sort == "old":
posts = posts.order_by(Submission.created_utc.asc())
elif sort == "disputed":
posts = posts.order_by(Submission.score_disputed.desc())
elif sort == "top":
posts = posts.order_by(Submission.score_top.desc())
elif sort == "activity":
posts = posts.order_by(Submission.score_activity.desc())
else:
abort(422)
return [x[0] for x in posts.offset(25 * (page - 1)).limit(26).all()]
@cache.memoize(300)
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def userpagelisting(self, v=None, page=1, sort="new", t="all"):
submissions = g.db.query(Submission.id).options(
lazyload('*')).filter_by(author_id=self.id)
if not (v and v.over_18):
submissions = submissions.filter_by(over_18=False)
if v and v.hide_offensive and v.id!=self.id:
submissions = submissions.filter_by(is_offensive=False)
if v and v.hide_bot:
submissions = submissions.filter_by(is_bot=False)
if not (v and (v.admin_level >= 3)):
submissions = submissions.filter_by(deleted_utc=0)
if not (v and (v.admin_level >= 3 or v.id == self.id)):
submissions = submissions.filter_by(is_banned=False).join(Submission.board).filter(Board.is_banned==False)
if v and v.admin_level >= 4:
pass
elif v:
m = g.db.query(
ModRelationship.board_id).filter_by(
user_id=v.id,
invite_rescinded=False).subquery()
c = g.db.query(
ContributorRelationship.board_id).filter_by(
user_id=v.id).subquery()
submissions = submissions.filter(
or_(
Submission.author_id == v.id,
Submission.post_public == True,
Submission.board_id.in_(m),
Submission.board_id.in_(c)
)
)
else:
submissions = submissions.filter(Submission.post_public == True)
if sort == "hot":
submissions = submissions.order_by(Submission.score_best.desc())
elif sort == "new":
submissions = submissions.order_by(Submission.created_utc.desc())
elif sort == "old":
submissions = submissions.order_by(Submission.created_utc.asc())
elif sort == "disputed":
submissions = submissions.order_by(Submission.score_disputed.desc())
elif sort == "top":
submissions = submissions.order_by(Submission.score_top.desc())
elif sort == "activity":
submissions = submissions.order_by(Submission.score_activity.desc())
now = int(time.time())
if t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
else:
cutoff = 0
submissions = submissions.filter(Submission.created_utc >= cutoff)
listing = [x[0] for x in submissions.offset(25 * (page - 1)).limit(26)]
return listing
@cache.memoize(300)
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def commentlisting(self, v=None, page=1, sort="new", t="all"):
comments = self.comments.options(
lazyload('*')).filter(Comment.parent_submission is not None).join(Comment.post)
if not (v and v.over_18):
comments = comments.filter(Submission.over_18 == False)
if v and v.hide_offensive and v.id != self.id:
comments = comments.filter(Comment.is_offensive == False)
if v and v.hide_bot:
comments = comments.filter(Comment.is_bot == False)
if v and not v.show_nsfl:
comments = comments.filter(Submission.is_nsfl == False)
if (not v) or v.admin_level < 3:
comments = comments.filter(Comment.deleted_utc == 0)
if not (v and (v.admin_level >= 3 or v.id == self.id)):
comments = comments.filter(Comment.is_banned == False)
if v and v.admin_level >= 4:
pass
elif v:
m = g.db.query(ModRelationship).filter_by(user_id=v.id, invite_rescinded=False).subquery()
c = v.contributes.subquery()
comments = comments.join(m,
m.c.board_id == Submission.board_id,
isouter=True
).join(c,
c.c.board_id == Submission.board_id,
isouter=True
).join(Board, Board.id == Submission.board_id)
comments = comments.filter(or_(Comment.author_id == v.id,
Submission.post_public == True,
Board.is_private == False,
m.c.board_id != None,
c.c.board_id != None),
Board.is_banned==False
)
else:
comments = comments.join(Board, Board.id == Submission.board_id).filter(
or_(Submission.post_public == True, Board.is_private == False), Board.is_banned==False)
comments = comments.options(contains_eager(Comment.post))
if sort == "hot":
comments = comments.order_by(Comment.score_hot.desc())
elif sort == "new":
comments = comments.order_by(Comment.created_utc.desc())
elif sort == "old":
comments = comments.order_by(Comment.created_utc.asc())
elif sort == "disputed":
comments = comments.order_by(Comment.score_disputed.desc())
elif sort == "top":
comments = comments.order_by(Comment.score_top.desc())
now = int(time.time())
if t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
else:
cutoff = 0
comments = comments.filter(Comment.created_utc >= cutoff)
comments = comments.offset(25 * (page - 1)).limit(26)
listing = [c.id for c in comments]
return listing
@property
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def has_report_queue(self):
board_ids = g.db.query(ModRelationship.board_id).options(lazyload('*')).filter(
ModRelationship.user_id==self.id,
ModRelationship.accepted==True,
or_(
ModRelationship.perm_full==True,
ModRelationship.perm_content==True
)
).subquery()
posts=g.db.query(Submission).options(lazyload('*')).filter(
Submission.board_id.in_(
board_ids
),
Submission.mod_approved == None,
Submission.is_banned == False,
Submission.deleted_utc==0
).join(Report, Report.post_id==Submission.id)
if not self.over_18:
posts=posts.filter(Submission.over_18==False)
return bool(posts.first())
@property
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def notification_commentlisting(self, page=1, all_=False, replies_only=False, mentions_only=False, system_only=False):
notifications = self.notifications.options(
lazyload('*'),
joinedload(Notification.comment).lazyload('*'),
joinedload(Notification.comment).joinedload(Comment.comment_aux)
).join(
Notification.comment
).filter(
Comment.is_banned == False,
Comment.deleted_utc == 0)
if replies_only:
cs=g.db.query(Comment.id).filter(Comment.author_id==self.id).subquery()
ps=g.db.query(Submission.id).filter(Submission.author_id==self.id).subquery()
notifications=notifications.filter(
or_(
Comment.parent_comment_id.in_(cs),
and_(
Comment.level==1,
Comment.parent_submission.in_(ps)
)
)
)
elif mentions_only:
cs=g.db.query(Comment.id).filter(Comment.author_id==self.id).subquery()
ps=g.db.query(Submission.id).filter(Submission.author_id==self.id).subquery()
notifications=notifications.filter(
and_(
Comment.parent_comment_id.notin_(cs),
or_(
Comment.level>1,
Comment.parent_submission.notin_(ps)
)
)
)
elif system_only:
notifications=notifications.filter(Comment.author_id==1)
elif not all_:
notifications = notifications.filter(Notification.read == False)
notifications = notifications.options(
contains_eager(Notification.comment)
)
notifications = notifications.order_by(
Notification.id.desc()).offset(25 * (page - 1)).limit(26)
output = []
for x in notifications[0:25]:
x.read = True
g.db.add(x)
output.append(x.comment_id)
g.db.commit()
return output
def notification_postlisting(self, all_=False, page=1):
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def mentions_count(self):
cs=g.db.query(Comment.id).filter(Comment.author_id==self.id).subquery()
ps=g.db.query(Submission.id).filter(Submission.author_id==self.id).subquery()
return self.notifications.options(
lazyload('*')
).join(
Notification.comment
).filter(
Notification.read==False,
Comment.is_banned == False,
Comment.deleted_utc == 0
).filter(
and_(
Comment.parent_comment_id.notin_(cs),
or_(
Comment.level>1,
Comment.parent_submission.notin_(ps)
)
)
).count()
@property
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def replies_count(self):
cs=g.db.query(Comment.id).filter(Comment.author_id==self.id).subquery()
ps=g.db.query(Submission.id).filter(Submission.author_id==self.id).subquery()
return self.notifications.options(
lazyload('*')
).join(
Notification.comment
).filter(
Comment.is_banned == False,
Comment.deleted_utc == 0
).filter(
Notification.read==False,
or_(
Comment.parent_comment_id.in_(cs),
and_(
Comment.level==1,
Comment.parent_submission.in_(ps)
)
)
).count()
@property
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def notifications_count(self):
return self.notifications.options(
lazyload('*')
).filter(
Notification.read==False
).join(Notification.comment, isouter=True
).join(Notification.post, isouter=True
).filter(
or_(
and_(
Comment.is_banned==False,
Comment.deleted_utc==0
),
and_(
Submission.is_banned==False,
Submission.deleted_utc==0
)
)
).count()
@property
0
Source : user.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def saved_idlist(self, page=1):
posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=False,
deleted_utc=0
)
if not self.over_18:
posts = posts.filter_by(over_18=False)
saved=g.db.query(SaveRelationship.submission_id).filter(SaveRelationship.user_id==self.id).subquery()
posts=posts.filter(Submission.id.in_(saved))
if self.admin_level < 4:
# admins can see everything
m = g.db.query(
ModRelationship.board_id).filter_by(
user_id=self.id,
invite_rescinded=False).subquery()
c = g.db.query(
ContributorRelationship.board_id).filter_by(
user_id=self.id).subquery()
posts = posts.filter(
or_(
Submission.author_id == self.id,
Submission.post_public == True,
Submission.board_id.in_(m),
Submission.board_id.in_(c)
)
)
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=self.id).subquery()
blocked = g.db.query(
UserBlock.user_id).filter_by(
target_id=self.id).subquery()
posts = posts.filter(
Submission.author_id.notin_(blocking),
Submission.author_id.notin_(blocked)
)
posts=posts.order_by(Submission.created_utc.desc())
return [x[0] for x in posts.offset(25 * (page - 1)).limit(26).all()]
def guild_rep(self, guild, recent=0):
0
Source : admin.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def admin_removed(v):
page = int(request.args.get("page", 1))
ids = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=True).order_by(
Submission.id.desc()).offset(25 * (page - 1)).limit(26).all()
ids=[x[0] for x in ids]
next_exists = len(ids) == 26
ids = ids[0:25]
posts = get_posts(ids, v=v)
return render_template("admin/removed_posts.html",
v=v,
listing=posts,
page=page,
next_exists=next_exists
)
@app.route("/admin/gm", methods=["GET"])
0
Source : admin.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def admin_siege_count(v):
board=get_guild(request.args.get("board"))
recent=int(request.args.get("days",0))
now=int(time.time())
cutoff=board.stored_subscriber_count//10 + min(recent, (now-board.created_utc)//(60*60*24))
uids=g.db.query(Subscription.user_id).filter_by(is_active=True, board_id=board.id).all()
uids=[x[0] for x in uids]
can_siege=0
total=0
for uid in uids:
posts=sum([x[0] for x in g.db.query(Submission.score_top).options(lazyload('*')).filter_by(author_id=uid).filter(Submission.created_utc>now-60*60*24*recent).all()])
comments=sum([x[0] for x in g.db.query(Comment.score_top).options(lazyload('*')).filter_by(author_id=uid).filter( Comment.created_utc>now-60*60*24*recent).all()])
rep=posts+comments
if rep>=cutoff:
can_siege+=1
total+=1
print(f"{can_siege}/{total}")
return jsonify(
{
"guild":f"+{board.name}",
"requirement":cutoff,
"eligible_users":can_siege
}
)
# @app.route('/admin/deploy', methods=["GET"])
# @admin_level_required(3)
# def admin_deploy(v):
# def reload_function():
# time.sleep(3)
# subprocess.run(". ~/go.sh", shell=True)
# thread=threading.Thread(target=reload_function, daemon=True)
# thread.start()
# return 'Reloading!'
# @app.route('/admin/test', methods=["GET"])
# @admin_level_required(3)
# def admin_test(v):
# return "1"
@app.route("/admin/purge_guild_images/ < boardname>", methods=["POST"])
0
Source : admin.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def admin_purge_guild_images(boardname, v):
#Iterates through all posts in guild with thumbnail, and nukes thumbnails and i.ruqqus uploads
board=get_guild(boardname)
if not board.is_banned:
return jsonify({"error":"This guild isn't banned"}), 409
if board.has_profile:
board.del_profile()
if board.has_banner:
board.del_banner()
posts = g.db.query(Submission).options(lazyload('*')).filter_by(board_id=board.id, has_thumb=True)
def del_function(post):
del_function
aws.delete_file(urlparse(post.thumb_url).path.lstrip('/'))
#post.has_thumb=False
if post.url and post.domain=="i.ruqqus.com":
aws.delete_file(urlparse(post.url).path.lstrip('/'))
i=0
threads=[]
for post in posts.all():
i+=1
threads.append(gevent.spawn(del_function, post))
post.has_thumb=False
g.db.add(post)
gevent.joinall(threads)
g.db.commit()
return redirect(board.permalink)
@app.route("/admin/image_ban", methods=["POST"])
0
Source : admin.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def admin_user_ipban(v):
#bans all non-Tor IPs associated with a given account
#only use for obvious proxys
target_user=get_user(request.values.get("username"))
targets=[target_user]+[alt for alt in target_user.alts]
ips=set()
for user in targets:
if user.creation_region != "T1":
ips.add(user.creation_ip)
for post in g.db.query(Submission).options(lazyload('*')).filter_by(author_id=user.id).all():
if post.creation_region != "T1":
ips.add(post.creation_ip)
for comment in g.db.query(Comment).options(lazyload('*')).filter_by(author_id=user.id).all():
if comment.creation_region != "T1":
ips.add()
for ip in ips:
new_ipban=IP(
banned_by=v.id,
addr=ip
)
g.db.add(new_ipban)
g.db.commit()
@app.route("/admin/get_ip", methods=["GET"])
0
Source : admin.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def admin_get_ip_info(v):
link=request.args.get("link","")
if not link:
return render_template(
"admin/ip_info.html",
v=v
)
thing=get_from_permalink(link)
if isinstance(thing, User):
if request.values.get("ipnuke"):
target_user=thing
targets=[target_user]+[alt for alt in target_user.alts]
ips=set()
for user in targets:
if user.creation_region != "T1":
ips.add(user.creation_ip)
for post in g.db.query(Submission).options(lazyload('*')).filter_by(author_id=user.id).all():
if post.creation_region != "T1":
ips.add(post.creation_ip)
for comment in g.db.query(Comment).options(lazyload('*')).filter_by(author_id=user.id).all():
if comment.creation_region != "T1":
ips.add()
for ip in ips:
if g.db.query(IP).filter_by(addr=ip).first():
continue
new_ipban=IP(
banned_by=v.id,
addr=ip
)
g.db.add(new_ipban)
g.db.commit()
return f"{len(ips)} ips banned"
return redirect(f"/admin/ip/{thing.creation_ip}")
def print_(*x):
0
Source : admin.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def admin_siege_guild(v):
now = int(time.time())
guild = request.form.get("guild")
user=get_user(request.form.get("username"))
guild = get_guild(guild)
if now-v.created_utc < 60*60*24*30:
return render_template("message.html",
v=v,
title=f"Siege on +{guild.name} Failed",
error=f"@{user.username}'s account is too new."
), 403
if v.is_suspended or v.is_deleted:
return render_template("message.html",
v=v,
title=f"Siege on +{guild.name} Failed",
error=f"@{user.username} is deleted/suspended."
), 403
# check time
#if user.last_siege_utc > now - (60 * 60 * 24 * 7):
# return render_template("message.html",
# v=v,
# title=f"Siege on +{guild.name} Failed",
# error=f"@{user.username} needs to wait 7 days between siege attempts."
# ), 403
# check guild count
if not user.can_join_gms and guild not in user.boards_modded:
return render_template("message.html",
v=v,
title=f"Siege on +{guild.name} Failed",
error=f"@{user.username} already leads the maximum number of guilds."
), 403
# Can't siege if exiled
if g.db.query(BanRelationship).filter_by(is_active=True, user_id=user.id, board_id=guild.id).first():
return render_template(
"message.html",
v=v,
title=f"Siege on +{guild.name} Failed",
error=f"@{user.username} is exiled from +{guild.name}."
), 403
# Cannot siege +general, +ruqqus, +ruqquspress, +ruqqusdmca
if not guild.is_siegable:
return render_template("message.html",
v=v,
title=f"Siege on +{guild.name} Failed",
error=f"+{guild.name} is an admin-controlled guild and is immune to siege."
), 403
#cannot be installed within 7 days of a successful siege
recent = g.db.query(ModAction).filter(
ModAction.target_user_id==user.id,
ModAction.kind=="add_mod",
ModAction.created_utc>int(time.time())-60*60*24*7
).first()
if recent:
return render_template("message.html",
v=v,
title=f"Siege on +{guild.name} Failed",
error=f"@{user.username} sieged +{recent.board.name} within the past 7 days.",
link=recent.permalink,
link_text="View mod log record"
), 403
# update siege date
user.last_siege_utc = now
g.db.add(user)
for alt in v.alts:
alt.last_siege_utc = now
g.db.add(user)
# check user activity
#if guild not in user.boards_modded and user.guild_rep(guild, recent=180) < guild.siege_rep_requirement and not guild.has_contributor(v):
# return render_template(
# "message.html",
# v=v,
# title=f"Siege against +{guild.name} Failed",
# error=f"@{user.username} does not have enough recent Reputation in +{guild.name} to siege it. +{guild.name} currently requires {guild.siege_rep_requirement} Rep within the last 180 days, and @{user.username} has {v.guild_rep(guild, recent=180)}."
# ), 403
# Assemble list of mod ids to check
# skip any user with a perm site-wide ban
# skip any deleted mod
#check mods above user
mods=[]
for x in guild.mods_list:
if (x.user.is_banned and not x.user.unban_utc) or x.user.is_deleted:
continue
if x.user_id==user.id:
break
mods.append(x)
# if no mods, skip straight to success
if mods and not request.values.get("activity_bypass"):
#if False:
ids = [x.user_id for x in mods]
# cutoff
cutoff = now - 60 * 60 * 24 * 60
# check mod actions
ma = g.db.query(ModAction).join(ModAction.user).filter(
User.is_deleted==False,
or_(
User.is_banned==0,
and_(
User.is_banned>0,
User.unban_utc>0
)
),
or_(
#option 1: mod action by user
and_(
ModAction.user_id.in_(tuple(ids)),
ModAction.board_id==guild.id
),
#option 2: ruqqus adds user as mod due to siege
and_(
ModAction.user_id==1,
ModAction.target_user_id.in_(tuple(ids)),
ModAction.kind=="add_mod",
ModAction.board_id==guild.id
)
),
ModAction.created_utc > cutoff
).options(contains_eager(ModAction.User)
).first()
if ma:
return render_template("message.html",
v=v,
title=f"Siege against +{guild.name} Failed",
error=f" One of the guildmasters has performed a mod action in +{guild.name} within the last 60 days. You may try again in 7 days.",
link=ma.permalink,
link_text="View mod log record"
), 403
#check submissions
post= g.db.query(Submission).filter(Submission.author_id.in_(tuple(ids)),
Submission.created_utc > cutoff,
Submission.original_board_id==guild.id,
Submission.deleted_utc==0,
Submission.is_banned==False).order_by(Submission.board_id==guild.id).first()
if post:
return render_template("message.html",
v=v,
title=f"Siege against +{guild.name} Failed",
error=f"One of the guildmasters created a post in +{guild.name} within the last 60 days. You may try again in 7 days.",
link=post.permalink,
link_text="View post"
), 403
# check comments
comment= g.db.query(Comment
).options(
lazyload('*')
).filter(
Comment.author_id.in_(tuple(ids)),
Comment.created_utc > cutoff,
Comment.original_board_id==guild.id,
Comment.deleted_utc==0,
Comment.is_banned==False
).join(
Comment.post
).order_by(
Submission.board_id==guild.id
).options(
contains_eager(Comment.post)
).first()
if comment:
return render_template("message.html",
v=v,
title=f"Siege against +{guild.name} Failed",
error=f"One of the guildmasters created a comment in +{guild.name} within the last 60 days. You may try again in 7 days.",
link=comment.permalink,
link_text="View comment"
), 403
#Siege is successful
#look up current mod record if one exists
m=guild.has_mod(user)
#remove current mods. If they are at or below existing mod, leave in place
for x in guild.moderators:
if m and x.id>=m.id and x.accepted:
continue
if x.accepted:
send_notification(x.user,
f"You have been overthrown from +{guild.name}.")
ma=ModAction(
kind="remove_mod",
user_id=v.id,
board_id=guild.id,
target_user_id=x.user_id,
note="siege"
)
g.db.add(ma)
else:
ma=ModAction(
kind="uninvite_mod",
user_id=v.id,
board_id=guild.id,
target_user_id=x.user_id,
note="siege"
)
g.db.add(ma)
g.db.delete(x)
if not m:
new_mod = ModRelationship(user_id=user.id,
board_id=guild.id,
created_utc=now,
accepted=True,
perm_full=True,
perm_access=True,
perm_appearance=True,
perm_content=True,
perm_config=True
)
g.db.add(new_mod)
ma=ModAction(
kind="add_mod",
user_id=v.id,
board_id=guild.id,
target_user_id=user.id,
note="siege"
)
g.db.add(ma)
send_notification(user, f"You have been added as a Guildmaster to +{guild.name}")
elif not m.perm_full:
m.perm_full=True
m.perm_access=True
m.perm_appearance=True
m.perm_config=True
m.perm_content=True
g.db.add(m)
ma=ModAction(
kind="change_perms",
user_id=v.id,
board_id=guild.id,
target_user_id=user.id,
note="siege"
)
g.db.add(ma)
return redirect(f"/+{guild.name}/mod/mods")
@app.get('/admin/email/ < email>')
0
Source : front.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def frontlist(v=None, sort=None, page=1, nsfw=False, nsfl=False,
t=None, categories=[], filter_words='', **kwargs):
# cutoff=int(time.time())-(60*60*24*30)
if sort == None:
if v: sort = v.defaultsorting
else: sort = "hot"
if sort == "hot":
sort_func = Submission.score_hot.desc
elif sort == "new":
sort_func = Submission.created_utc.desc
elif sort == "old":
sort_func = Submission.created_utc.asc
elif sort == "disputed":
sort_func = Submission.score_disputed.desc
elif sort == "top":
sort_func = Submission.score_top.desc
elif sort == "activity":
sort_func = Submission.score_activity.desc
else:
abort(400)
posts = g.db.query(
Submission
).options(
lazyload('*'),
Load(Board).lazyload('*')
).filter_by(
is_banned=False,
stickied=False
).filter(Submission.deleted_utc == 0)
if not nsfw:
posts = posts.filter_by(over_18=False)
if not nsfl:
posts = posts.filter_by(is_nsfl=False)
if (v and v.hide_offensive) or not v:
posts = posts.filter_by(is_offensive=False)
if v and v.hide_bot:
posts = posts.filter(Submission.is_bot==False)
if v and v.admin_level >= 4:
board_blocks = g.db.query(
BoardBlock.board_id).filter_by(
user_id=v.id).subquery()
posts = posts.filter(Submission.board_id.notin_(board_blocks))
elif v:
m = g.db.query(ModRelationship.board_id).filter_by(
user_id=v.id, invite_rescinded=False).subquery()
c = g.db.query(
ContributorRelationship.board_id).filter_by(
user_id=v.id).subquery()
posts = posts.filter(
or_(
Submission.author_id == v.id,
Submission.post_public == True,
Submission.board_id.in_(m),
Submission.board_id.in_(c)
)
)
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=v.id).subquery()
# blocked = g.db.query(
# UserBlock.user_id).filter_by(
# target_id=v.id).subquery()
posts = posts.filter(
Submission.author_id.notin_(blocking) #,
# Submission.author_id.notin_(blocked)
)
board_blocks = g.db.query(
BoardBlock.board_id).filter_by(
user_id=v.id).subquery()
posts = posts.filter(Submission.board_id.notin_(board_blocks))
else:
posts = posts.filter(Submission.post_public==True)
# board opt out of all
if v:
posts = posts.join(Submission.board).filter(
or_(
Board.all_opt_out == False,
Submission.board_id.in_(
g.db.query(
Subscription.board_id).filter_by(
user_id=v.id,
is_active=True).subquery()
)
)
)
else:
posts = posts.join(
Submission.board).filter_by(
all_opt_out=False)
if categories:
posts=posts.filter(Board.subcat_id.in_(tuple(categories)))
if (v and v.hide_offensive) or not v:
posts=posts.filter(
Board.subcat_id.notin_([44, 108])
)
posts=posts.filter(Submission.board_id!=1)
posts=posts.options(contains_eager(Submission.board))
#custom filter
#print(filter_words)
if v and filter_words:
posts=posts.join(Submission.submission_aux)
for word in filter_words:
#print(word)
posts=posts.filter(not_(SubmissionAux.title.ilike(f'%{word}%')))
if t == None and v: t = v.defaulttime
if t:
now = int(time.time())
if t == 'day':
cutoff = now - 86400
elif t == 'week':
cutoff = now - 604800
elif t == 'month':
cutoff = now - 2592000
elif t == 'year':
cutoff = now - 31536000
else:
cutoff = 0
posts = posts.filter(Submission.created_utc >= cutoff)
gt = kwargs.get("gt")
lt = kwargs.get("lt")
if gt:
posts = posts.filter(Submission.created_utc > gt)
if lt:
posts = posts.filter(Submission.created_utc < lt)
if sort == "hot":
posts = posts.order_by(Submission.score_best.desc())
elif sort == "new":
posts = posts.order_by(Submission.created_utc.desc())
elif sort == "old":
posts = posts.order_by(Submission.created_utc.asc())
elif sort == "disputed":
posts = posts.order_by(Submission.score_disputed.desc())
elif sort == "top":
posts = posts.order_by(Submission.score_top.desc())
elif sort == "activity":
posts = posts.order_by(Submission.score_activity.desc())
else:
abort(400)
return [x.id for x in posts.offset(25 * (page - 1)).limit(26).all()]
@app.route("/", methods=["GET"])
0
Source : front.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def random_post(v):
x = g.db.query(Submission).options(
lazyload('board')).filter_by(
is_banned=False,
).filter(Submission.deleted_utc == 0)
now = int(time.time())
cutoff = now - (60 * 60 * 24 * 180)
x = x.filter(Submission.created_utc >= cutoff)
if not (v and v.over_18):
x = x.filter_by(over_18=False)
if not (v and v.show_nsfl):
x = x.filter_by(is_nsfl=False)
if v and v.hide_offensive:
x = x.filter_by(is_offensive=False)
if v and v.hide_bot:
x = x.filter_by(is_bot=False)
if v:
bans = g.db.query(
BanRelationship.board_id).filter_by(
user_id=v.id).subquery()
x = x.filter(Submission.board_id.notin_(bans))
x=x.join(Submission.board).filter(Board.is_banned==False)
total = x.count()
n = random.randint(0, total - 1)
post = x.order_by(Submission.id.asc()).offset(n).limit(1).first()
return redirect(post.permalink)
@app.route("/random/guild", methods=["GET"])
0
Source : front.py
with GNU Lesser General Public License v2.1
from ruqqus
with GNU Lesser General Public License v2.1
from ruqqus
def comment_idlist(page=1, v=None, nsfw=False, **kwargs):
posts = g.db.query(Submission).options(
lazyload('*')).join(Submission.board)
if not nsfw:
posts = posts.filter_by(over_18=False)
if v and not v.show_nsfl:
posts = posts.filter_by(is_nsfl=False)
if v and v.admin_level >= 4:
pass
elif v:
m = g.db.query(ModRelationship.board_id).filter_by(
user_id=v.id, invite_rescinded=False).subquery()
c = g.db.query(
ContributorRelationship.board_id).filter_by(
user_id=v.id).subquery()
posts = posts.filter(
or_(
Submission.author_id == v.id,
Submission.post_public == True,
Submission.board_id.in_(m),
Submission.board_id.in_(c),
Board.is_private == False
)
)
else:
posts = posts.filter(or_(Submission.post_public ==
True, Board.is_private == False))
posts = posts.subquery()
comments = g.db.query(Comment).options(lazyload('*'))
if v and v.hide_offensive:
comments = comments.filter_by(is_offensive=False)
if v and v.hide_bot:
comments = comments.filter_by(is_bot=False)
if v and v.admin_level < = 3:
# blocks
blocking = g.db.query(
UserBlock.target_id).filter_by(
user_id=v.id).subquery()
blocked = g.db.query(
UserBlock.user_id).filter_by(
target_id=v.id).subquery()
comments = comments.filter(
Comment.author_id.notin_(blocking),
Comment.author_id.notin_(blocked)
)
if not v or not v.admin_level >= 3:
comments = comments.filter_by(is_banned=False).filter(Comment.deleted_utc == 0)
comments = comments.join(posts, Comment.parent_submission == posts.c.id)
comments = comments.order_by(Comment.created_utc.desc()).offset(
25 * (page - 1)).limit(26).all()
return [x.id for x in comments]
@app.route("/all/comments", methods=["GET"])
0
Source : test_default_strategies.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_upgrade_o2m_noload_lazyload_option(self):
Address, addresses, users, User = (
self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User,
)
m = self.mapper_registry.map_imperatively(
User,
users,
properties=dict(
addresses=relationship(
self.mapper_registry.map_imperatively(Address, addresses),
lazy="noload",
)
),
)
q = fixture_session().query(m).options(sa.orm.lazyload(User.addresses))
result = [None]
def go():
x = q.filter(User.id == 7).all()
x[0].addresses
result[0] = x
self.sql_count_(2, go)
self.assert_result(
result[0], User, {"id": 7, "addresses": (Address, [{"id": 1}])}
)
def test_m2o_noload_option(self):
0
Source : test_eager_relations.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_many_to_one_null(self):
"""test that a many-to-one eager load which loads None does
not later trigger a lazy load.
"""
Order, Address, addresses, orders = (
self.classes.Order,
self.classes.Address,
self.tables.addresses,
self.tables.orders,
)
# use a primaryjoin intended to defeat SA's usage of
# query.get() for a many-to-one lazyload
self.mapper_registry.map_imperatively(
Order,
orders,
properties=dict(
address=relationship(
self.mapper_registry.map_imperatively(Address, addresses),
primaryjoin=and_(
addresses.c.id == orders.c.address_id,
addresses.c.email_address != None, # noqa
),
lazy="joined",
)
),
)
sess = fixture_session()
def go():
o1 = (
sess.query(Order)
.options(lazyload(Order.address))
.filter(Order.id == 5)
.one()
)
eq_(o1.address, None)
self.assert_sql_count(testing.db, go, 2)
sess.expunge_all()
def go():
o1 = sess.query(Order).filter(Order.id == 5).one()
eq_(o1.address, None)
self.assert_sql_count(testing.db, go, 1)
def test_one_and_many(self):
0
Source : test_expire.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_state_noload_to_lazy(self):
"""Behavioral test to verify the current activity of
loader callables
"""
users, Address, addresses, User = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User,
)
self.mapper_registry.map_imperatively(
User,
users,
properties={"addresses": relationship(Address, lazy="noload")},
)
self.mapper_registry.map_imperatively(Address, addresses)
sess = fixture_session(autoflush=False)
u1 = sess.query(User).options(lazyload(User.addresses)).first()
assert isinstance(
attributes.instance_state(u1).callables["addresses"],
strategies.LoadLazyAttribute,
)
# expire, it goes away from callables as of 1.4 and is considered
# to be expired
sess.expire(u1)
assert "addresses" in attributes.instance_state(u1).expired_attributes
assert "addresses" not in attributes.instance_state(u1).callables
# load it
sess.query(User).first()
assert (
"addresses" not in attributes.instance_state(u1).expired_attributes
)
assert "addresses" not in attributes.instance_state(u1).callables
sess.expunge_all()
u1 = sess.query(User).options(lazyload(User.addresses)).first()
sess.expire(u1, ["addresses"])
assert (
"addresses" not in attributes.instance_state(u1).expired_attributes
)
assert isinstance(
attributes.instance_state(u1).callables["addresses"],
strategies.LoadLazyAttribute,
)
# load the attr, goes away
u1.addresses
assert (
"addresses" not in attributes.instance_state(u1).expired_attributes
)
assert "addresses" not in attributes.instance_state(u1).callables
def test_deferred_expire_w_transient_to_detached(self):
See More Examples