Commit c37dc1704e9b88b4cded220f2c0c58bf914a9203
1 parent
4393fbe509
Exists in
master
add adm directory
Showing 18 changed files with 315 additions and 0 deletions Inline Diff
- components/adm/README.md
- components/adm/__pycache__/__init__.cpython-310.pyc
- components/adm/__pycache__/__init__.cpython-311.pyc
- components/adm/adm/.idea/.gitignore
- components/adm/adm/.idea/adm.iml
- components/adm/adm/.idea/inspectionProfiles/Project_Default.xml
- components/adm/adm/.idea/inspectionProfiles/profiles_settings.xml
- components/adm/adm/.idea/misc.xml
- components/adm/adm/.idea/modules.xml
- components/adm/adm/.idea/vcs.xml
- components/adm/adm/__pycache__/__init__.cpython-310.pyc
- components/adm/adm/__pycache__/__init__.cpython-311.pyc
- components/adm/adm/__pycache__/models.cpython-310.pyc
- components/adm/adm/__pycache__/views.cpython-310.pyc
- components/adm/adm/__pycache__/views.cpython-311.pyc
- components/adm/adm/models.py
- components/adm/adm/views.py
- components/adm/setup.py
components/adm/README.md
| File was created | 1 | # main.py | |
| 2 | |||
| 3 | from components.adm.adm.views import adm | ||
| 4 | from components.test.test.views import test | ||
| 5 | |||
| 6 | app.register_blueprint(adm, url_prefix='/adm') | ||
| 7 | |||
| 8 | cd components/adm | ||
| 9 | pip install -r requirements.txt | ||
| 10 | |||
| 11 | |||
| 12 | endpoint "http://127.0.0.1:5000/adm/register" | ||
| 13 | dictionary to register in such way | ||
| 14 | { | ||
| 15 | "login": "xxx", | ||
| 16 | "password": "xxx", | ||
| 17 | "name": "xxx", | ||
| 18 | "email": "xxx" | ||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | endpoint "http://127.0.0.1:5000/adm/login" | ||
| 23 | dictionary to login in such way | ||
| 24 | { | ||
| 25 | "login":"xxx", | ||
| 26 | "password":"xxx" | ||
| 27 | } | ||
| 28 | |||
| 29 | endpoint http://127.0.0.1:5000/adm/changePassword | ||
| 30 | dictionary to changePassword in such way | ||
| 31 | { | ||
| 32 | "old_password":"xxx", | ||
| 33 | "new_password": "xxx", | ||
| 34 | "re_new_password": "xxx" | ||
| 35 | } | ||
| 36 | endpoint http://127.0.0.1:5000/adm/newUser | ||
| 37 | { | ||
| 38 | "login": "xxx", | ||
| 39 | "password": "xxx", | ||
| 40 | "name": "xxx", | ||
| 41 | "email": "xxx", | ||
| 42 | "phone": "xxx", | ||
| 43 | "repassword": "xxx" | ||
| 44 | |||
| 45 | } |
components/adm/__pycache__/__init__.cpython-310.pyc
No preview for this file type
components/adm/__pycache__/__init__.cpython-311.pyc
No preview for this file type
components/adm/adm/.idea/.gitignore
| File was created | 1 | # Default ignored files | |
| 2 | /shelf/ | ||
| 3 | /workspace.xml | ||
| 4 | # Editor-based HTTP Client requests | ||
| 5 | /httpRequests/ | ||
| 6 | # Datasource local storage ignored files | ||
| 7 | /dataSources/ | ||
| 8 | /dataSources.local.xml | ||
| 9 |
components/adm/adm/.idea/adm.iml
| File was created | 1 | <?xml version="1.0" encoding="UTF-8"?> | |
| 2 | <module type="PYTHON_MODULE" version="4"> | ||
| 3 | <component name="NewModuleRootManager"> | ||
| 4 | <content url="file://$MODULE_DIR$" /> | ||
| 5 | <orderEntry type="inheritedJdk" /> | ||
| 6 | <orderEntry type="sourceFolder" forTests="false" /> | ||
| 7 | </component> | ||
| 8 | <component name="TemplatesService"> | ||
| 9 | <option name="TEMPLATE_CONFIGURATION" value="Jinja2" /> | ||
| 10 | <option name="TEMPLATE_FOLDERS"> | ||
| 11 | <list> | ||
| 12 | <option value="$MODULE_DIR$/templates" /> | ||
| 13 | </list> | ||
| 14 | </option> | ||
| 15 | </component> | ||
| 16 | </module> |
components/adm/adm/.idea/inspectionProfiles/Project_Default.xml
| File was created | 1 | <component name="InspectionProjectProfileManager"> | |
| 2 | <profile version="1.0"> | ||
| 3 | <option name="myName" value="Project Default" /> | ||
| 4 | <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" /> | ||
| 5 | <inspection_tool class="JSHint" enabled="true" level="ERROR" enabled_by_default="true" /> | ||
| 6 | <inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true"> | ||
| 7 | <option name="ignoredPackages"> | ||
| 8 | <value> | ||
| 9 | <list size="13"> | ||
| 10 | <item index="0" class="java.lang.String" itemvalue="blinker" /> | ||
| 11 | <item index="1" class="java.lang.String" itemvalue="greenlet" /> | ||
| 12 | <item index="2" class="java.lang.String" itemvalue="Werkzeug" /> | ||
| 13 | <item index="3" class="java.lang.String" itemvalue="Flask-WTF" /> | ||
| 14 | <item index="4" class="java.lang.String" itemvalue="SQLAlchemy" /> | ||
| 15 | <item index="5" class="java.lang.String" itemvalue="MarkupSafe" /> | ||
| 16 | <item index="6" class="java.lang.String" itemvalue="click" /> | ||
| 17 | <item index="7" class="java.lang.String" itemvalue="Flask-SQLAlchemy" /> | ||
| 18 | <item index="8" class="java.lang.String" itemvalue="Jinja2" /> | ||
| 19 | <item index="9" class="java.lang.String" itemvalue="Flask-Login" /> | ||
| 20 | <item index="10" class="java.lang.String" itemvalue="WTForms" /> | ||
| 21 | <item index="11" class="java.lang.String" itemvalue="itsdangerous" /> | ||
| 22 | <item index="12" class="java.lang.String" itemvalue="Flask" /> | ||
| 23 | </list> | ||
| 24 | </value> | ||
| 25 | </option> | ||
| 26 | </inspection_tool> | ||
| 27 | </profile> | ||
| 28 | </component> |
components/adm/adm/.idea/inspectionProfiles/profiles_settings.xml
| File was created | 1 | <component name="InspectionProjectProfileManager"> | |
| 2 | <settings> | ||
| 3 | <option name="USE_PROJECT_PROFILE" value="false" /> | ||
| 4 | <version value="1.0" /> | ||
| 5 | </settings> | ||
| 6 | </component> |
components/adm/adm/.idea/misc.xml
| File was created | 1 | <?xml version="1.0" encoding="UTF-8"?> | |
| 2 | <project version="4"> | ||
| 3 | <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" /> | ||
| 4 | </project> |
components/adm/adm/.idea/modules.xml
| File was created | 1 | <?xml version="1.0" encoding="UTF-8"?> | |
| 2 | <project version="4"> | ||
| 3 | <component name="ProjectModuleManager"> | ||
| 4 | <modules> | ||
| 5 | <module fileurl="file://$PROJECT_DIR$/.idea/adm.iml" filepath="$PROJECT_DIR$/.idea/adm.iml" /> | ||
| 6 | </modules> | ||
| 7 | </component> | ||
| 8 | </project> |
components/adm/adm/.idea/vcs.xml
| File was created | 1 | <?xml version="1.0" encoding="UTF-8"?> | |
| 2 | <project version="4"> | ||
| 3 | <component name="VcsDirectoryMappings"> | ||
| 4 | <mapping directory="$PROJECT_DIR$/.." vcs="Git" /> | ||
| 5 | </component> | ||
| 6 | </project> |
components/adm/adm/__pycache__/__init__.cpython-310.pyc
No preview for this file type
components/adm/adm/__pycache__/__init__.cpython-311.pyc
No preview for this file type
components/adm/adm/__pycache__/models.cpython-310.pyc
No preview for this file type
components/adm/adm/__pycache__/views.cpython-310.pyc
No preview for this file type
components/adm/adm/__pycache__/views.cpython-311.pyc
No preview for this file type
components/adm/adm/models.py
| File was created | 1 | import uuid | |
| 2 | from sqlalchemy import Column, Integer, String | ||
| 3 | from sqlalchemy.ext.declarative import declarative_base | ||
| 4 | from flask_jwt_extended import create_access_token | ||
| 5 | from datetime import timedelta | ||
| 6 | import hashlib | ||
| 7 | from flask import current_app as k2 | ||
| 8 | |||
| 9 | |||
| 10 | Base = declarative_base() | ||
| 11 | |||
| 12 | class k2users(Base): | ||
| 13 | __tablename__ = 'k2users' | ||
| 14 | user_id = Column(Integer, primary_key=True) | ||
| 15 | login = Column(String(50)) | ||
| 16 | name = Column(String(50)) | ||
| 17 | email = Column(String(150)) | ||
| 18 | password = Column(String(50)) | ||
| 19 | phone = Column(String(20)) | ||
| 20 | |||
| 21 | def __init__(self, **kwargs): | ||
| 22 | self.user_id = hashlib.md5(str(uuid.uuid4()).encode()).hexdigest() | ||
| 23 | self.login = kwargs.get('login') | ||
| 24 | self.password = hashlib.md5(kwargs.get('password').encode()).hexdigest() | ||
| 25 | self.name = kwargs.get('name') | ||
| 26 | self.email = kwargs.get('email') | ||
| 27 | self.phone = kwargs.get('phone') | ||
| 28 | |||
| 29 | def get_token(self, expire_time=24): | ||
| 30 | expires_delta = timedelta(expire_time) | ||
| 31 | token = create_access_token(identity=self.user_id, expires_delta=expires_delta) | ||
| 32 | return token | ||
| 33 | |||
| 34 | @classmethod | ||
| 35 | def authenticate(cls, login, password): | ||
| 36 | session = k2.extensions['sqlalchemy'].db.session | ||
| 37 | user = '' | ||
| 38 | try: | ||
| 39 | user = session.query(cls).filter(cls.login == login).filter(cls.password == hashlib.md5(password.encode()).hexdigest()).one() | ||
| 40 | except: | ||
| 41 | user = None | ||
| 42 | |||
| 43 | return user |
components/adm/adm/views.py
| File was created | 1 | import hashlib | |
| 2 | import uuid | ||
| 3 | from flask import Blueprint, request, jsonify | ||
| 4 | from flask import current_app as k2 | ||
| 5 | from flask_jwt_extended import jwt_required, get_jwt_identity | ||
| 6 | # from flask_sqlalchemy import session | ||
| 7 | # from sqlalchemy.orm import sessionmaker | ||
| 8 | # from k2.k2inst import Component | ||
| 9 | |||
| 10 | from .models import k2users | ||
| 11 | |||
| 12 | |||
| 13 | adm = Blueprint('adm', __name__, template_folder='templates', static_folder='static', static_url_path='/adm') | ||
| 14 | |||
| 15 | |||
| 16 | @adm.route('/register', methods=["POST"]) | ||
| 17 | def register(): | ||
| 18 | params = request.json | ||
| 19 | session = k2.extensions['sqlalchemy'].db.session | ||
| 20 | try: | ||
| 21 | user = k2users(**params) | ||
| 22 | session.add(user) | ||
| 23 | session.commit() | ||
| 24 | token = user.get_token() | ||
| 25 | return {'access_token': token} | ||
| 26 | except: | ||
| 27 | return jsonify({'error': 'Unauthorized'}), 401 | ||
| 28 | |||
| 29 | |||
| 30 | @adm.route('/login', methods=["POST"]) | ||
| 31 | def login(): | ||
| 32 | params = request.json | ||
| 33 | login = params.get('login') | ||
| 34 | password = params.get('password') | ||
| 35 | user = k2users.authenticate(login, password) | ||
| 36 | if user: | ||
| 37 | token = user.get_token() | ||
| 38 | return {'access_token': token} | ||
| 39 | else: | ||
| 40 | return jsonify({'error': 'Unauthorized'}), 401 | ||
| 41 | |||
| 42 | |||
| 43 | @adm.route('/logout', methods=["GET"]) | ||
| 44 | @jwt_required() | ||
| 45 | def logout(): | ||
| 46 | response = { | ||
| 47 | 'message': 'Logged out successfully' | ||
| 48 | } | ||
| 49 | return jsonify(response) | ||
| 50 | |||
| 51 | |||
| 52 | @adm.route('/users', methods=["GET"]) | ||
| 53 | @jwt_required() | ||
| 54 | def grid_users(): | ||
| 55 | return jsonify({"data": "users"}) | ||
| 56 | |||
| 57 | |||
| 58 | @adm.route('/roles', methods=["GET"]) | ||
| 59 | @jwt_required() | ||
| 60 | def grid_roles(): | ||
| 61 | return jsonify({"data": "roles"}) | ||
| 62 | |||
| 63 | |||
| 64 | @adm.route('/usersclients', methods=["GET"]) | ||
| 65 | @jwt_required() | ||
| 66 | def users_clients(): | ||
| 67 | return jsonify({"data": "roles"}) | ||
| 68 | |||
| 69 | @adm.route('/menuaaccess', methods=["GET"]) | ||
| 70 | @jwt_required() | ||
| 71 | def menu_aaccess(): | ||
| 72 | return jsonify({"data": "roles"}) | ||
| 73 | |||
| 74 | |||
| 75 | @adm.route('/newUser', methods=["GET", "POST"]) | ||
| 76 | @jwt_required() | ||
| 77 | def newUser(): | ||
| 78 | session = k2.extensions['sqlalchemy'].db.session | ||
| 79 | exist = session.query(k2users).filter_by(login=request.json['login']).first() | ||
| 80 | if not exist: | ||
| 81 | res = k2users( | ||
| 82 | user_id=hashlib.md5(str(uuid.uuid4()).encode()).hexdigest(), | ||
| 83 | login=request.json['login'], | ||
| 84 | name=request.json['name'], | ||
| 85 | email=request.json['email'], | ||
| 86 | password=hashlib.md5(request.json['password'].encode()).hexdigest(), | ||
| 87 | phone=request.json['phone']) | ||
| 88 | if res: | ||
| 89 | session.add(res) | ||
| 90 | session.commit() | ||
| 91 | return jsonify({'status': 'ok'}) | ||
| 92 | else: | ||
| 93 | return jsonify({'error': 'Such user exist'}), 404 | ||
| 94 | |||
| 95 | |||
| 96 | @adm.route('/changePassword', methods=["POST"]) | ||
| 97 | @jwt_required() | ||
| 98 | def changePassword(): | ||
| 99 | session = k2.extensions['sqlalchemy'].db.session | ||
| 100 | current_user = get_jwt_identity() | ||
| 101 | user = session.query(k2users).filter_by(user_id=current_user).first() | ||
| 102 | if user.password == hashlib.md5(request.json['old_password'].encode()).hexdigest(): | ||
| 103 | if request.json['new_password'] == request.json['re_new_password']: | ||
| 104 | user.password = hashlib.md5(request.json['new_password'].encode()).hexdigest() | ||
| 105 | session.commit() | ||
| 106 | else: | ||
| 107 | return jsonify({'error': 'Пароль не найден в базе'}), 404 | ||
| 108 | else: | ||
| 109 | return jsonify({'error': 'Пароль не совпвдает'}), 404 | ||
| 110 | return jsonify({'status': 'ok'}) | ||
| 111 | |||
| 112 | |||
| 113 | @adm.route('/menu-items') | ||
| 114 | def menu_items(): | ||
| 115 | menu = [ | ||
| 116 | {"heading": 'APPS & PAGES'}, | ||
| 117 | { | ||
| 118 | "title": 'Адміністрування сайту', | ||
| 119 | |||
| 120 | "icon": {"icon": 'mdi mdi-database'}, | ||
| 121 | "children": [ | ||
| 122 | {'title': 'Зміна паролю', 'to': 'adm-changePassword'}, | ||
| 123 | {'title': 'Новий користувач', 'to': 'adm-newUser'}, | ||
| 124 | {'title': 'Користувачі', 'to': 'adm-users'}, | ||
| 125 | {'title': 'Користувачі клієнти', 'to': 'adm-usersclients'}, | ||
| 126 | {'title': 'Права', 'to': 'adm-roles'}, | ||
| 127 | {'title': 'Доступ до меню', 'to': 'adm-menuaaccess'}, | ||
| 128 | ], | ||
| 129 | }] | ||
| 130 | |||
| 131 | return menu | ||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 |
components/adm/setup.py
| File was created | 1 | from setuptools import setup | |
| 2 | |||
| 3 | setup( | ||
| 4 | name='adm', | ||
| 5 | version='1.0.0', | ||
| 6 | description='Description of my project', | ||
| 7 | author='Your Name', | ||
| 8 | author_email='yourname@example.com', | ||
| 9 | keywords="adm, k2", | ||
| 10 | python_requires=">=3.7, <=3.11.2", | ||
| 11 | packages=['adm'], | ||
| 12 | package_data={ # Optional | ||
| 13 | }, | ||
| 14 | install_requires=[""], | ||
| 15 | ) |