membership.create() checks for joinable and player limit; factory creates joinable lexicons by default #10
|
@ -5,6 +5,7 @@ Membership query interface
|
||||||
from sqlalchemy import select, func
|
from sqlalchemy import select, func
|
||||||
|
|
||||||
from amanuensis.db import DbContext, Membership
|
from amanuensis.db import DbContext, Membership
|
||||||
|
from amanuensis.db.models import Lexicon
|
||||||
from amanuensis.errors import ArgumentError
|
from amanuensis.errors import ArgumentError
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,6 +37,27 @@ def create(
|
||||||
):
|
):
|
||||||
raise ArgumentError("User is already a member of lexicon")
|
raise ArgumentError("User is already a member of lexicon")
|
||||||
|
|
||||||
|
# get reference to lexicon for next few checks
|
||||||
|
lex: Lexicon = db(
|
||||||
|
select(Lexicon)
|
||||||
|
.where(Lexicon.id == lexicon_id)
|
||||||
|
).scalar_one_or_none()
|
||||||
|
|
||||||
|
# Verify lexicon is joinable; current no Lexicons are joinable so this is commented out
|
||||||
|
if not lex.joinable:
|
||||||
|
raise ArgumentError("Can't join: Lexicon is not joinable")
|
||||||
|
|
||||||
|
# Verify lexicon is not full
|
||||||
|
if lex.player_limit:
|
||||||
|
if (
|
||||||
|
db(
|
||||||
|
select(func.count())
|
||||||
|
.where(Membership.lexicon_id == lexicon_id)
|
||||||
|
).scalar()
|
||||||
|
>= lex.player_limit
|
||||||
|
):
|
||||||
|
raise ArgumentError("Can't join: Lexicon is full")
|
||||||
|
|
||||||
new_membership = Membership(
|
new_membership = Membership(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
lexicon_id=lexicon_id,
|
lexicon_id=lexicon_id,
|
||||||
|
|
|
@ -49,7 +49,9 @@ def make_lexicon(db: DbContext):
|
||||||
}
|
}
|
||||||
state["nonce"] += 1
|
state["nonce"] += 1
|
||||||
updated_kwargs = {**default_kwargs, **kwargs}
|
updated_kwargs = {**default_kwargs, **kwargs}
|
||||||
return lexiq.create(db, **updated_kwargs)
|
lex = lexiq.create(db, **updated_kwargs)
|
||||||
|
lex.joinable = True
|
||||||
|
return lex
|
||||||
|
|
||||||
return lexicon_factory
|
return lexicon_factory
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue