-
Notifications
You must be signed in to change notification settings - Fork 12
Move to ORM for database access #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c0f17da
orbit: db: generate schema from peewee orm classes
charliemirabile d375f96
orbit: radius: use peewee for session queries
charliemirabile 6752ddc
orbit: hyperspace: use peewee for session queries
charliemirabile aab610d
orbit: db: remove functions/queries for old session table interface
charliemirabile 8712ceb
orbit: radius: use peewee for registration queries
charliemirabile a61f26e
orbit: hyperspace: use peewee for registration queries
charliemirabile 353e178
orbit: db: remove functions/queries for old registration table interface
charliemirabile 33f1b8b
orbit: radius: factor credential checking logic into helper
charliemirabile d07d086
orbit: radius: use peewee for user queries
charliemirabile 4416ffa
orbit: hyperspace: use peewee for user queries
charliemirabile a474334
orbit: db: remove functions/queries for old user table interface
charliemirabile 10bb0c1
orbit: db: remove old code and imports
charliemirabile 14ecdce
orbit: db: remove old database names
charliemirabile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,5 +8,3 @@ | |
|
|
||
| # duration of authentication token validity period | ||
| minutes_each_session_token_is_valid = 180 | ||
|
|
||
| sql_verbose = False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,146 +1,33 @@ | ||
| import sqlite3 | ||
| #!/usr/bin/env python3 | ||
| import peewee | ||
| import config | ||
| # nickname table name | ||
| # USR => users | ||
| # SES => sessions | ||
| # REG => newusers | ||
| import sys | ||
|
|
||
| DB = peewee.SqliteDatabase(config.database) | ||
|
|
||
| def _do(cmd, reps=(), set_=False, get_=False): | ||
| if config.sql_verbose: | ||
| print("SQL", cmd, file=sys.stderr) | ||
| reps = (lambda x: x if type(x) is tuple else (x,))(reps) | ||
| dat = None | ||
| con = sqlite3.connect(config.database) | ||
| new = con.cursor() | ||
| ret = new.execute(cmd, reps) | ||
| if get_: | ||
| dat = ret.fetchall() | ||
| if len(dat) < 1: | ||
| dat = [None] | ||
| if config.sql_verbose: | ||
| print("SQLRET", dat, file=sys.stderr) | ||
| # works when get lookup fails | ||
| if set_: | ||
| ret.execute("COMMIT;") | ||
| con.close() | ||
| return dat | ||
|
|
||
| class BaseModel(peewee.Model): | ||
| class Meta: | ||
| database = DB | ||
| strict_tables = True | ||
|
|
||
| def _set(cmd, reps=()): return _do(cmd, reps, set_=True, get_=True) | ||
| def _get(cmd, reps=()): return _do(cmd, reps, get_=True) | ||
|
|
||
| class User(BaseModel): | ||
| username = peewee.TextField(unique=True) | ||
| pwdhash = peewee.TextField() | ||
| student_id = peewee.TextField(unique=True, null=True) | ||
|
|
||
| # session table interface | ||
|
|
||
| SES_GETBY_TOKEN = """ | ||
| SELECT token, username, expiry | ||
| FROM sessions | ||
| WHERE token = ?; | ||
| """.strip() | ||
| def ses_getby_token(tok): return _get(SES_GETBY_TOKEN, tok) | ||
| class Session(BaseModel): | ||
| token = peewee.TextField(primary_key=True) | ||
| username = peewee.TextField(unique=True) | ||
| expiry = peewee.FloatField() | ||
|
|
||
|
|
||
| SES_GETBY_USERNAME = """ | ||
| SELECT token, username, expiry | ||
| FROM sessions | ||
| WHERE username = ?; | ||
| """.strip() | ||
| def ses_getby_username(usn): return _get(SES_GETBY_USERNAME, usn) | ||
| class Registration(BaseModel): | ||
| student_id = peewee.TextField(unique=True) | ||
| username = peewee.TextField(unique=True) | ||
| password = peewee.TextField() | ||
|
|
||
|
|
||
| SES_INS = """ | ||
| INSERT INTO sessions (token, username, expiry) | ||
| VALUES (?, ?, ?) | ||
| RETURNING username; | ||
| """.strip() | ||
| def ses_ins(tpl): return _set(SES_INS, tpl) | ||
|
|
||
|
|
||
| SES_DELBY_TOKEN = """ | ||
| DELETE FROM sessions | ||
| WHERE token = ? | ||
| RETURNING username; | ||
| """.strip() | ||
| def ses_delby_token(tok): return _set(SES_DELBY_TOKEN, tok) | ||
|
|
||
|
|
||
| SES_DELBY_USERNAME = """ | ||
| DELETE FROM sessions | ||
| WHERE username = ? | ||
| RETURNING username; | ||
| """.strip() | ||
| def ses_delby_username(usn): return _set(SES_DELBY_USERNAME, usn) | ||
|
|
||
|
|
||
| SES_GET = """ | ||
| SELECT token, username, expiry | ||
| FROM sessions; | ||
| """.strip() | ||
| def ses_get(): return _get(SES_GET) | ||
|
|
||
|
|
||
| # users table interface | ||
|
|
||
| USR_PWDHASHFOR_USERNAME = """ | ||
| SELECT pwdhash | ||
| FROM users | ||
| WHERE username = ?; | ||
| """.strip() | ||
| def usr_pwdhashfor_username(usn): return _get(USR_PWDHASHFOR_USERNAME, usn) | ||
|
|
||
|
|
||
| USR_INS = """ | ||
| INSERT INTO users (username, pwdhash, student_id) | ||
| VALUES (?, ?, ?); | ||
| """.strip() | ||
| def usr_ins(usr): return _set(USR_INS, usr) | ||
|
|
||
|
|
||
| USR_DELBY_USERNAME = """ | ||
| DELETE FROM users | ||
| WHERE username = ? | ||
| RETURNING username; | ||
| """.strip() | ||
| def usr_delby_username(usn): return _set(USR_DELBY_USERNAME, usn) | ||
|
|
||
|
|
||
| USR_SETPWDHASH_USERNAME = """ | ||
| UPDATE users | ||
| SET pwdhash = ? | ||
| WHERE username = ?; | ||
| """.strip() | ||
| def usr_setpwdhash_username(usr): return _set(USR_SETPWDHASH_USERNAME, usr) | ||
|
|
||
|
|
||
| USR_GET = """ | ||
| SELECT username, pwdhash, student_id | ||
| FROM users; | ||
| """.strip() | ||
| def usr_get(): return _get(USR_GET) | ||
|
|
||
|
|
||
| USR_GETBY_USERNAME = """ | ||
| SELECT username, pwdhash, student_id | ||
| FROM users | ||
| WHERE username = ?; | ||
| """.strip() | ||
| def usr_getby_username(usn): return _get(USR_GETBY_USERNAME, usn) | ||
|
|
||
|
|
||
| # registration table inferface | ||
|
|
||
| REG_INS = """ | ||
| INSERT INTO newusers (username, password, student_id) | ||
| VALUES (?,?,?); | ||
| """.strip() | ||
| def reg_ins(tpl): return _set(REG_INS, tpl) | ||
|
|
||
|
|
||
| REG_GETDEL_BY_STUID = """ | ||
| DELETE FROM newusers | ||
| WHERE student_id = ? | ||
| RETURNING username, password; | ||
| """ | ||
| def reg_get_and_del_by_stuid(sid): return _set(REG_GETDEL_BY_STUID, sid) | ||
| if __name__ == '__main__': | ||
| DB.create_tables(BaseModel.__subclasses__()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.