From e886e4e95ca72ca1aa06234180835a75c139c5e7 Mon Sep 17 00:00:00 2001 From: Mustafa Yontar Date: Mon, 1 Feb 2021 02:44:13 +0300 Subject: [PATCH] add gitignore mongo_data_dir add requirements pillow create dockerfile and docker compose fix privkey file path add flask admin for developments. --- .gitignore | 1 + Dockerfile | 17 +++++++++++++++++ docker-compose.yaml | 29 +++++++++++++++++++++++++++++ internal_lib/EncryptedField.py | 6 +++--- main.py | 30 +++++++++++++++++++----------- models/EmbededDocuments.py | 3 +-- models/Union.py | 3 ++- mongo_data_dir/.gitkeep | 0 requirements.txt | 4 +++- 9 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 Dockerfile create mode 100644 mongo_data_dir/.gitkeep diff --git a/.gitignore b/.gitignore index 35b0d26..8ebc9f3 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ var/ *.pem *test.py trash +mongo_data_dir/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..df4b9d7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.9.1-buster +WORKDIR /code +ENV FLASK_APP=main.py +ENV FLASK_RUN_HOST=0.0.0.0 +RUN apt install gcc + +RUN apt-get update && apt-get install -y \ + python-dev python-pip python-setuptools \ + libffi-dev libxml2-dev libxslt1-dev \ + libtiff-dev libjpeg-dev zlib1g-dev \ + liblcms2-dev libwebp-dev python-tk + +COPY requirements.txt requirements.txt +RUN pip install -r requirements.txt +EXPOSE 5000 +COPY . . +CMD ["flask", "run"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index e69de29..97aea6d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -0,0 +1,29 @@ +version: "3.3" +services: + mongo: + image: mongo + restart: always + environment: + MONGO_INITDB_ROOT_USERNAME: xcoder + MONGO_INITDB_ROOT_PASSWORD: 4dun4710 + volumes: + - ./mongo_data_dir/:/data/db/ + mongo-express: + image: mongo-express + restart: always + ports: + - 8081:8081 + environment: + ME_CONFIG_MONGODB_ADMINUSERNAME: xcoder + ME_CONFIG_MONGODB_ADMINPASSWORD: 4dun4710 + ME_CONFIG_OPTIONS_EDITORTHEME: ambiance + ME_CONFIG_BASICAUTH_USERNAME: xcoder + ME_CONFIG_BASICAUTH_PASSWORD: 4dun4710 + web: + build: . + ports: + - "5000:5000" + environment: + ADUNATIO_PRIV_KEY: /code/privkey.pem + volumes: + - .:/code diff --git a/internal_lib/EncryptedField.py b/internal_lib/EncryptedField.py index e55c45c..04d0a54 100644 --- a/internal_lib/EncryptedField.py +++ b/internal_lib/EncryptedField.py @@ -1,6 +1,5 @@ import re -from flask import current_app from mongoengine.base import BaseField from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP @@ -11,8 +10,9 @@ class EncryptedStringField(BaseField): keyPair = None def __init__(self, **kwargs): - - self.keyPair = RSA.importKey(open(current_app.config.get('priv_key_path')).read()) + import os + priv_key = os.environ.get('ADUNATIO_PRIV_KEY') + self.keyPair = RSA.importKey(open(priv_key).read()) super().__init__(**kwargs) diff --git a/main.py b/main.py index 68aa93d..9b75b33 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,24 @@ -# This is a sample Python script. +from flask import Flask +from flask_admin.contrib.mongoengine import ModelView +from mongoengine import connect -# Press Shift+F10 to execute it or replace it with your code. -# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. +from models.Union import Union +from models.User import User +from restapi import MongoApi +from flask_admin import Admin +""" +Mongodb connection string +""" +connect('adunatio', host='mongo',username="xcoder",password="4dun4710", authentication_source='admin') -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press Ctrl+8 to toggle the breakpoint. +app = Flask(__name__) +app.secret_key = "secret_key+secret_key" +api = MongoApi(app) +api.register_model(User,uri="/api/user") +adm = Admin(app) +adm.add_view(ModelView(User)) +adm.add_view(ModelView(Union)) - -# Press the green button in the gutter to run the script. if __name__ == '__main__': - print_hi('PyCharm') - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/ + app.run(host="0.0.0.0",port=5000,debug=True) \ No newline at end of file diff --git a/models/EmbededDocuments.py b/models/EmbededDocuments.py index f665ff4..b350b72 100644 --- a/models/EmbededDocuments.py +++ b/models/EmbededDocuments.py @@ -2,7 +2,6 @@ from mongoengine import * class Descriptions(EmbeddedDocument): - from models.User import User text = StringField() - user = ReferenceField(User) + user = ReferenceField("User") date = DateTimeField() diff --git a/models/Union.py b/models/Union.py index 443a3e4..f8a2c71 100644 --- a/models/Union.py +++ b/models/Union.py @@ -19,13 +19,14 @@ class Union(Document): group = Group() group.union = self group.name = 'Root' - group.rights = ["*::*::{}}/*".format(self.id)] + group.rights = ["*::*::{}/*".format(self.id)] group.save() user = User() user.username = "{}@root".format(self.legal_registration_number) user.user_group = group user.union = self password = ''.join(choices(ascii_letters+digits, k=10)) + print("New union password : {}".format(password)) user.password = generate_password_hash(password) # TODO: send password via mail or sms or nothing user.save() diff --git a/mongo_data_dir/.gitkeep b/mongo_data_dir/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt index 0b6775f..84324bb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ flask pycryptodome flask_jwt_extended flask_views -flask_login \ No newline at end of file +flask_login +pillow +flask-admin \ No newline at end of file