Skip to content

Commit 32dfc79

Browse files
author
Cameron Brandon White
committed
Added database field and resource
1 parent bf43de3 commit 32dfc79

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

acmapi/fields.py

+9
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,12 @@ def output(self, obj, key):
103103
'person_id': Integer,
104104
'person': Url('people', absolute=True),
105105
}
106+
107+
database_fields = {
108+
'dilect': String,
109+
'host': String,
110+
'port': Integer,
111+
'database': String,
112+
'username': String,
113+
'password': String,
114+
}

acmapi/resources.py

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import flask
2-
from flask import Flask
2+
from flask import Flask, current_app
33
from flask.ext import restful
44
from flask.ext.restful import \
55
reqparse, fields, marshal_with, marshal, abort
@@ -10,12 +10,17 @@
1010

1111
import datetime
1212

13+
try:
14+
from urlparse import urlparse
15+
except ImportError:
16+
from urllib.parse import urlparse
17+
1318
from . import models
1419
from .models import DB
1520
from .fields import \
1621
DateField, MarshallingException, \
1722
root_fields, event_fields, post_fields, person_fields, \
18-
membership_fields, officership_fields
23+
membership_fields, officership_fields, database_fields
1924
from .types import \
2025
datetime_type, date_type
2126
from .authentication import AUTH
@@ -661,6 +666,43 @@ def delete(self, officership_id):
661666

662667
return {'message': 'delete successful'}
663668

669+
class Database(restful.Resource):
670+
671+
@AUTH.login_required
672+
def get(self):
673+
parsed_url = urlparse(current_app.config['SQLALCHEMY_DATABASE_URI'])
674+
675+
username, password, host, port = None, None, None, None
676+
677+
split_netloc = parsed_url.netloc.split('@')
678+
if len(split_netloc) == 2:
679+
host = split_netloc[1]
680+
username_password = split_netloc[0].split(':')
681+
if len(username_password) == 2:
682+
username = username_password[0]
683+
password = username_password[1]
684+
else:
685+
username = username_password[0]
686+
elif len(split_netloc) == 1:
687+
host = split_netloc[0]
688+
689+
split_host = host.split(':')
690+
if len(split_host) == 2:
691+
host = split_host[0]
692+
port = split_host[1]
693+
elif len(split_host) == 1:
694+
host = split_host[0]
695+
696+
database = {
697+
'dilect': parsed_url.scheme,
698+
'host': host,
699+
'port': port,
700+
'database': parsed_url.path.strip('/'),
701+
'username': username,
702+
'password': password}
703+
704+
return marshal(database, database_fields)
705+
664706
API.add_resource(
665707
Root,
666708
'/',
@@ -698,3 +740,7 @@ def delete(self, officership_id):
698740
'/officerships/<int:officership_id>',
699741
endpoint='officerships')
700742

743+
API.add_resource(
744+
Database,
745+
'/database/',
746+
endpoint='database')

0 commit comments

Comments
 (0)