This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimally write new accounts to the db
- Loading branch information
1 parent
1c3db55
commit 52920d0
Showing
4 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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,7 +1,8 @@ | ||
BEGIN; | ||
|
||
CREATE TABLE accounts | ||
( | ||
( number varchar(32) unique not null | ||
, name varchar(32) unique not null | ||
); | ||
|
||
END; |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import re | ||
from aspen import Response | ||
from psycopg2 import IntegrityError | ||
|
||
number_re = re.compile(r'^[0-9-]{1,32}$') | ||
name_re = re.compile(r'^[A-Za-z0-9 ~-]{1,32}$') | ||
[---] | ||
if user.ANON: raise Response(401) | ||
if not user.ADMIN: raise Response(403) | ||
|
||
request.allow('GET', 'PUT') | ||
|
||
number = request.path['number'] | ||
if not number_re.match(number): | ||
raise Response(404) | ||
|
||
if request.method == 'PUT': | ||
name = request.body.get('name') | ||
if not name_re.match(name): | ||
raise Response(400, 'Invalid name.') | ||
with website.db.get_cursor() as c: | ||
try: | ||
website.db.run("INSERT INTO accounts (number, name) VALUES (%s, %s)", (number, name)) | ||
except IntegrityError: | ||
website.db.run("UPDATE accounts SET name=%s WHERE number=%s") | ||
|
||
out = website.db.one("SELECT * FROM accounts WHERE number=%s", (number,)) | ||
if not out: | ||
raise Response(404) | ||
[---] application/json via json_dump | ||
out |