|
1 | 1 | import flask
|
2 |
| -from flask import Flask |
| 2 | +from flask import Flask, current_app |
3 | 3 | from flask.ext import restful
|
4 | 4 | from flask.ext.restful import \
|
5 | 5 | reqparse, fields, marshal_with, marshal, abort
|
|
10 | 10 |
|
11 | 11 | import datetime
|
12 | 12 |
|
| 13 | +try: |
| 14 | + from urlparse import urlparse |
| 15 | +except ImportError: |
| 16 | + from urllib.parse import urlparse |
| 17 | + |
13 | 18 | from . import models
|
14 | 19 | from .models import DB
|
15 | 20 | from .fields import \
|
16 | 21 | DateField, MarshallingException, \
|
17 | 22 | root_fields, event_fields, post_fields, person_fields, \
|
18 |
| - membership_fields, officership_fields |
| 23 | + membership_fields, officership_fields, database_fields |
19 | 24 | from .types import \
|
20 | 25 | datetime_type, date_type
|
21 | 26 | from .authentication import AUTH
|
@@ -661,6 +666,43 @@ def delete(self, officership_id):
|
661 | 666 |
|
662 | 667 | return {'message': 'delete successful'}
|
663 | 668 |
|
| 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 | + |
664 | 706 | API.add_resource(
|
665 | 707 | Root,
|
666 | 708 | '/',
|
@@ -698,3 +740,7 @@ def delete(self, officership_id):
|
698 | 740 | '/officerships/<int:officership_id>',
|
699 | 741 | endpoint='officerships')
|
700 | 742 |
|
| 743 | +API.add_resource( |
| 744 | + Database, |
| 745 | + '/database/', |
| 746 | + endpoint='database') |
0 commit comments