crud-mysql 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- crud-mysql/__init__.py +1 -0
- crud-mysql/crud_object.py +75 -0
- crud-mysql/endpoints.py +79 -0
- crud-mysql/vars.py +9 -0
- crud_mysql-0.1.1.dist-info/METADATA +64 -0
- crud_mysql-0.1.1.dist-info/RECORD +9 -0
- crud_mysql-0.1.1.dist-info/WHEEL +5 -0
- crud_mysql-0.1.1.dist-info/licenses/LICENCE +2 -0
- crud_mysql-0.1.1.dist-info/top_level.txt +1 -0
crud-mysql/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .endpoints import crud
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
import json
|
|
4
|
+
from mysql_database import DatabaseCreds, Database
|
|
5
|
+
from vars import DATABASE_HOST, DATABASE_PORT, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME
|
|
6
|
+
|
|
7
|
+
class CrudObject:
|
|
8
|
+
|
|
9
|
+
def __init__(self, object_type):
|
|
10
|
+
try:
|
|
11
|
+
crud = self.get_crud_object(object_type)
|
|
12
|
+
except Exception as e:
|
|
13
|
+
raise(e)
|
|
14
|
+
self.object_type = object_type
|
|
15
|
+
self.allowd_methods = crud["allowd_methods"]
|
|
16
|
+
self.fetch_all = crud["fetch_all"] == "true"
|
|
17
|
+
|
|
18
|
+
schemas = self.get_schemas()
|
|
19
|
+
db_creds = DatabaseCreds(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_PORT)
|
|
20
|
+
self.db = Database(DATABASE_NAME, db_creds, schemas=schemas)
|
|
21
|
+
|
|
22
|
+
def get_object(self, object_id):
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
object = self.db.get_object_by_id(self.object_type, object_id, as_dict=True)
|
|
26
|
+
except:
|
|
27
|
+
raise Exception(f"cant find {self.object_type} with id: {object_id}")
|
|
28
|
+
return object
|
|
29
|
+
|
|
30
|
+
def get_objects(self, filter, include_columns=[], exclude_columns=[]):
|
|
31
|
+
|
|
32
|
+
object = self.db.get_filtered_list_of_objects(self.object_type, filter, include_columns, exclude_columns, as_dict=True)
|
|
33
|
+
return object
|
|
34
|
+
|
|
35
|
+
def create_object(self, object):
|
|
36
|
+
|
|
37
|
+
id = self.db.add_object(self.object_type, object)
|
|
38
|
+
return id
|
|
39
|
+
|
|
40
|
+
def update_object(self, object_id , object):
|
|
41
|
+
|
|
42
|
+
self.db.update_object(self.object_type, object_id, object)
|
|
43
|
+
|
|
44
|
+
def delete_object(self, object_id):
|
|
45
|
+
|
|
46
|
+
self.db.delete_object(self.object_type, object_id)
|
|
47
|
+
|
|
48
|
+
def check_object_in_schema(self, object_type):
|
|
49
|
+
is_valid = False
|
|
50
|
+
databases = os.listdir('schemas')
|
|
51
|
+
for database in databases:
|
|
52
|
+
with open(os.path.join('schemas', database), 'r') as f:
|
|
53
|
+
objects = json.loads(f.read())
|
|
54
|
+
for object in objects:
|
|
55
|
+
if object_type == object:
|
|
56
|
+
is_valid = True
|
|
57
|
+
return is_valid
|
|
58
|
+
|
|
59
|
+
def get_crud(self):
|
|
60
|
+
with open("crud.json", "r") as f:
|
|
61
|
+
crud = json.loads(f.read())
|
|
62
|
+
return crud
|
|
63
|
+
|
|
64
|
+
def get_crud_object(self, path):
|
|
65
|
+
crud = self.get_crud()
|
|
66
|
+
if path not in crud:
|
|
67
|
+
raise Exception('the path is not configured in crud.json')
|
|
68
|
+
return crud[path]
|
|
69
|
+
|
|
70
|
+
def get_schemas(self):
|
|
71
|
+
schema = {}
|
|
72
|
+
crud = self.get_crud()
|
|
73
|
+
for obj in crud:
|
|
74
|
+
schema[obj] = crud[obj]["schema"]
|
|
75
|
+
return schema
|
crud-mysql/endpoints.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from flask import Blueprint
|
|
2
|
+
from flask import request, jsonify
|
|
3
|
+
from crud_object import CrudObject
|
|
4
|
+
|
|
5
|
+
crud = Blueprint('crud', __name__, template_folder='templates')
|
|
6
|
+
|
|
7
|
+
@crud.route("/<object_type>", methods=["GET"])
|
|
8
|
+
def get_object(object_type):
|
|
9
|
+
try:
|
|
10
|
+
crud_object = CrudObject(object_type)
|
|
11
|
+
object_id = request.args.get(f"{object_type}_id")
|
|
12
|
+
if not object_id:
|
|
13
|
+
return(f"no {object_type} id was provided. (add {object_type}_id argument)"), 400
|
|
14
|
+
try:
|
|
15
|
+
object_obj = crud_object.get_object(object_id)
|
|
16
|
+
if object_obj:
|
|
17
|
+
return jsonify(object_obj), 200
|
|
18
|
+
else:
|
|
19
|
+
return jsonify("object not found"), 400
|
|
20
|
+
except Exception as e:
|
|
21
|
+
return jsonify("object not found"), 400
|
|
22
|
+
except Exception as e:
|
|
23
|
+
try:
|
|
24
|
+
crud_object = CrudObject(object_type[0:-1])
|
|
25
|
+
if object_type.endswith('s') and crud_object.fetch_all:
|
|
26
|
+
filter = request.args.get("filter", "")
|
|
27
|
+
objects = crud_object.get_objects(filter)
|
|
28
|
+
return jsonify(objects), 200
|
|
29
|
+
else:
|
|
30
|
+
raise
|
|
31
|
+
except Exception as e:
|
|
32
|
+
return jsonify(f"Invalid path: /{object_type}"), 404
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@crud.route("/<object_type>", methods=["POST"])
|
|
36
|
+
def create_object(object_type):
|
|
37
|
+
try:
|
|
38
|
+
crud_object = CrudObject(object_type)
|
|
39
|
+
except:
|
|
40
|
+
return jsonify(f"Invalid path: /{object_type}"), 404
|
|
41
|
+
if "POST" not in crud_object.allowd_methods:
|
|
42
|
+
return jsonify("method not allowed"), 400
|
|
43
|
+
data = request.json
|
|
44
|
+
if not object_type in data:
|
|
45
|
+
return jsonify(f"missing {object_type} info. (add {object_type} object in body)"), 400
|
|
46
|
+
object_info = data[object_type]
|
|
47
|
+
object_id = crud_object.create_object(object_info)
|
|
48
|
+
if object_id:
|
|
49
|
+
return jsonify({"objectId": object_id}), 201
|
|
50
|
+
else:
|
|
51
|
+
return jsonify("error occured while creating object"), 500
|
|
52
|
+
|
|
53
|
+
@crud.route("/<object_type>", methods=["PUT"])
|
|
54
|
+
def update_object(object_type):
|
|
55
|
+
try:
|
|
56
|
+
crud_object = CrudObject(object_type)
|
|
57
|
+
except:
|
|
58
|
+
return jsonify(f"Invalid path: /{object_type}"), 404
|
|
59
|
+
if "PUT" not in crud_object.allowd_methods:
|
|
60
|
+
return jsonify("method not allowed"), 400
|
|
61
|
+
object_id = request.args.get(f"{object_type}_id")
|
|
62
|
+
data = request.json
|
|
63
|
+
if not object_type in data:
|
|
64
|
+
return jsonify(f"missing {object_type} info to change. (add {object_type} object in body)"), 400
|
|
65
|
+
object_info = data[object_type]
|
|
66
|
+
crud_object.update_object(object_id, object_info)
|
|
67
|
+
return jsonify(f"Successfully updated {object_type} with id: {object_id}"), 200
|
|
68
|
+
|
|
69
|
+
@crud.route("/<object_type>", methods=["DELETE"])
|
|
70
|
+
def delete_object(object_type):
|
|
71
|
+
try:
|
|
72
|
+
crud_object = CrudObject(object_type)
|
|
73
|
+
except:
|
|
74
|
+
return jsonify(f"Invalid path: /{object_type}"), 404
|
|
75
|
+
if "DELETE" not in crud_object.allowd_methods:
|
|
76
|
+
return jsonify("method not allowed"), 400
|
|
77
|
+
object_id = request.args.get(f"{object_type}_id")
|
|
78
|
+
crud_object.delete_object(object_id)
|
|
79
|
+
return jsonify(f"Successfully deleted {object_type} with id: {object_id}"), 204
|
crud-mysql/vars.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: crud-mysql
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: crud defiend by json
|
|
5
|
+
Home-page: https://github.com/Ms-Shoshany/crud-mysql
|
|
6
|
+
Author: hanna
|
|
7
|
+
Author-email: channashosh@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENCE
|
|
14
|
+
Requires-Dist: mysql-connector-python
|
|
15
|
+
Requires-Dist: mysql-database
|
|
16
|
+
Requires-Dist: flask
|
|
17
|
+
Dynamic: author
|
|
18
|
+
Dynamic: author-email
|
|
19
|
+
Dynamic: classifier
|
|
20
|
+
Dynamic: description
|
|
21
|
+
Dynamic: description-content-type
|
|
22
|
+
Dynamic: home-page
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
Dynamic: requires-dist
|
|
25
|
+
Dynamic: requires-python
|
|
26
|
+
Dynamic: summary
|
|
27
|
+
|
|
28
|
+
main.py / app.py - example
|
|
29
|
+
|
|
30
|
+
from flask import Flask
|
|
31
|
+
from flask_cors import CORS
|
|
32
|
+
from crud-mysql import crud
|
|
33
|
+
|
|
34
|
+
app = Flask(__name__)
|
|
35
|
+
|
|
36
|
+
app.register_blueprint(crud)
|
|
37
|
+
|
|
38
|
+
if __name__ == "__main__":
|
|
39
|
+
app.run(host="0.0.0.0", debug=True)
|
|
40
|
+
|
|
41
|
+
cred.json - example
|
|
42
|
+
|
|
43
|
+
{
|
|
44
|
+
"user": {
|
|
45
|
+
"allowd_methods": ["GET", "POST", "DELETE", "PUT"],
|
|
46
|
+
"fetch_all": "true",
|
|
47
|
+
"schema": {
|
|
48
|
+
"name": "VARCHAR(100)",
|
|
49
|
+
"email": "VARCHAR(100)",
|
|
50
|
+
"encrypted_password": "VARCHAR(100)",
|
|
51
|
+
"account_id": "INT",
|
|
52
|
+
"is_active": "INT"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"account": {
|
|
56
|
+
"allowd_methods": ["GET", "POST", "DELETE"],
|
|
57
|
+
"fetch_all": "false",
|
|
58
|
+
"schema": {
|
|
59
|
+
"name": "VARCHAR(100)",
|
|
60
|
+
"subscription_type": "VARCHAR(100)",
|
|
61
|
+
"root_user": "INT"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
crud-mysql/__init__.py,sha256=eJA2HTYma6Udq8-rQziO3YYBjt6C2pe6YBO8V1JML-o,27
|
|
2
|
+
crud-mysql/crud_object.py,sha256=F1-sBOHS5x9tq2DMgy-CxTDIseCp4W8N18csb3sTBWQ,2550
|
|
3
|
+
crud-mysql/endpoints.py,sha256=ISa1Vj4Bi8tSHuJd6ExxoONw4VBFQSJo5OsdNpYdvlI,2850
|
|
4
|
+
crud-mysql/vars.py,sha256=Dx629hEZPt6uMWddftBqYzAR0D2VNlJo4NhiabBl0C8,243
|
|
5
|
+
crud_mysql-0.1.1.dist-info/licenses/LICENCE,sha256=6GSnt7dVNYlTiGLt8PqV1r8k0W7d83LX6GI-PQzPpsk,15
|
|
6
|
+
crud_mysql-0.1.1.dist-info/METADATA,sha256=LQshMb-gwSYxaU1pXzU6BJRkeNmKi7XKVByfVXsZ9x8,1622
|
|
7
|
+
crud_mysql-0.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
8
|
+
crud_mysql-0.1.1.dist-info/top_level.txt,sha256=DfjxBxTDBuGDhPo-MzGkksYw0UVyI7b50mrRAG4ppJQ,11
|
|
9
|
+
crud_mysql-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
crud-mysql
|