remove some app and db params/kwargs from auth functions

This commit is contained in:
Luna 2019-10-25 08:56:07 -03:00
parent b0ef3c4d0c
commit a3f793a211
2 changed files with 26 additions and 30 deletions

View File

@ -152,15 +152,14 @@ async def hash_data(data: str, loop=None) -> str:
return hashed.decode() return hashed.decode()
async def check_username_usage(username: str, db=None): async def check_username_usage(username: str):
"""Raise an error if too many people are with the same username.""" """Raise an error if too many people are with the same username."""
db = db or app.db same_username = await app.db.fetchval(
same_username = await db.fetchval(
""" """
SELECT COUNT(*) SELECT COUNT(*)
FROM users FROM users
WHERE username = $1 WHERE username = $1
""", """,
username, username,
) )
@ -175,23 +174,21 @@ async def check_username_usage(username: str, db=None):
def _raw_discrim() -> str: def _raw_discrim() -> str:
new_discrim = randint(1, 9999) discrim_number = randint(1, 9999)
new_discrim = "%04d" % new_discrim return "%04d" % discrim_number
return new_discrim
async def roll_discrim(username: str, *, db=None) -> Optional[str]: async def roll_discrim(username: str) -> Optional[str]:
"""Roll a discriminator for a DiscordTag. """Roll a discriminator for a DiscordTag.
Tries to generate one 10 times. Tries to generate one 10 times.
Calls check_username_usage. Calls check_username_usage.
""" """
db = db or app.db
# we shouldn't roll discrims for usernames # we shouldn't roll discrims for usernames
# that have been used too much. # that have been used too much.
await check_username_usage(username, db) await check_username_usage(username)
# max 10 times for a reroll # max 10 times for a reroll
for _ in range(10): for _ in range(10):
@ -199,12 +196,12 @@ async def roll_discrim(username: str, *, db=None) -> Optional[str]:
discrim = _raw_discrim() discrim = _raw_discrim()
# check if anyone is with it # check if anyone is with it
res = await db.fetchval( res = await app.db.fetchval(
""" """
SELECT id SELECT id
FROM users FROM users
WHERE username = $1 AND discriminator = $2 WHERE username = $1 AND discriminator = $2
""", """,
username, username,
discrim, discrim,
) )
@ -217,19 +214,17 @@ async def roll_discrim(username: str, *, db=None) -> Optional[str]:
return None return None
async def create_user( async def create_user(username: str, email: str, password: str) -> Tuple[int, str]:
username: str, email: str, password: str, db=None, loop=None
) -> Tuple[int, str]:
"""Create a single user. """Create a single user.
Generates a distriminator and other information. You can fetch the user Generates a distriminator and other information. You can fetch the user
data back with :meth:`Storage.get_user`. data back with :meth:`Storage.get_user`.
""" """
db = db or app.db db = app.db
loop = loop or app.loop loop = app.loop
new_id = get_snowflake() new_id = get_snowflake()
new_discrim = await roll_discrim(username, db=db) new_discrim = await roll_discrim(username)
if new_discrim is None: if new_discrim is None:
raise BadRequest( raise BadRequest(
@ -242,11 +237,11 @@ async def create_user(
try: try:
await db.execute( await db.execute(
""" """
INSERT INTO users INSERT INTO users
(id, email, username, discriminator, password_hash) (id, email, username, discriminator, password_hash)
VALUES VALUES
($1, $2, $3, $4, $5) ($1, $2, $3, $4, $5)
""", """,
new_id, new_id,
email, email,
username, username,

View File

@ -17,7 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from litecord.blueprints.auth import create_user, make_token from litecord.auth import create_user
from litecord.blueprints.auth import make_token
from litecord.blueprints.users import delete_user from litecord.blueprints.users import delete_user
from litecord.enums import UserFlags from litecord.enums import UserFlags