diff --git a/tests/test_api.py b/tests/test_api.py index 44a2a12f..360bfd64 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -606,7 +606,7 @@ def get_document(): the_scope.dump(dumped_data, accesskey) self.assertEqual(get_document(), the_scope.documentation) - the_scope.create_documentation() + the_scope.create_documentation_old() print(the_scope.documentation) self.assertEqual(get_document(), the_scope.documentation) @@ -637,7 +637,7 @@ def get_document(): def create_document(): data = {"scope": "onur.my_function", } - response = requests.post("http://localhost:7777" + create_document_of_scope_url, + response = requests.post("http://localhost:7777" + create_document_of_scope_url_old, auth=HTTPBasicAuth("", id), data=data) return response.json() @@ -794,7 +794,6 @@ def my_function(): cloudpickle.dumps(my_function)) the_scope.dump(dumped_data, AccessKey(id)) - self.assertEqual(the_scope.code, """def my_function():\n return "aaa"\n""") def get_document(): data = {"scope": id} @@ -804,7 +803,7 @@ def get_document(): get_document() self.assertEqual(the_scope.code, None) - self.assertEqual(the_scope.python, None) + self.assertEqual(the_scope.source, None) self.assertEqual(the_scope.type, None) self.assertEqual(the_scope.documentation, None) diff --git a/tests/test_utils.py b/tests/test_utils.py index dda41220..7cbf283e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -389,6 +389,7 @@ def my_function(): the_scope.dump(dumped_data, AccessKey(id)) + the_scope.set_type("function") self.assertEqual(the_scope.type, "function") storage_2.pop() @@ -405,9 +406,9 @@ def my_function(): cloudpickle.dumps(my_function)) the_scope.dump(dumped_data, AccessKey(id)) + the_scope.set_code("dsadasdadasdsa") - print(the_scope.code) - self.assertEqual(the_scope.code, """def my_function():\n return "aaa"\n""") + self.assertEqual(the_scope.code, "dsadasdadasdsa") storage_2.pop() @@ -424,7 +425,7 @@ def my_function(): the_scope.dump(dumped_data, AccessKey(id)) self.assertEqual(the_scope.documentation, None) - the_scope.create_documentation() + the_scope.create_documentation_old() print(the_scope.documentation) self.assertEqual(the_scope.documentation, "Returns a function instance for a my_function method .") @@ -560,7 +561,6 @@ def my_function(): cloudpickle.dumps(my_function)) the_scope.dump(dumped_data, AccessKey(id)) - self.assertEqual(the_scope.code, """def my_function():\n return "aaa"\n""") the_scope.delete() self.assertEqual(the_scope.code, None) diff --git a/upsonic_on_prem/api/__init__.py b/upsonic_on_prem/api/__init__.py index 27db47d8..aabedcae 100644 --- a/upsonic_on_prem/api/__init__.py +++ b/upsonic_on_prem/api/__init__.py @@ -44,7 +44,7 @@ @app.route(status_url, methods=["GET"]) def status(): - return jsonify(True) + return jsonify({"status": True, "result": True}) diff --git a/upsonic_on_prem/api/operations/user.py b/upsonic_on_prem/api/operations/user.py index 68989183..3c95abb5 100644 --- a/upsonic_on_prem/api/operations/user.py +++ b/upsonic_on_prem/api/operations/user.py @@ -14,9 +14,30 @@ def dump(): data = request.form.get("data") the_scope = Scope(scope) - the_scope.dump(data, AccessKey(request.authorization.password), pass_str=True) - return jsonify({"status": True}) + return jsonify( + {"status": True, "result": the_scope.dump(data, AccessKey(request.authorization.password), pass_str=True)}) + + +@app.route(dump_code_url, methods=["POST"]) +def dump_code(): + scope = request.form.get("scope") + code = request.form.get("code") + + the_scope = Scope(scope) + + return jsonify({"status": True, "result": the_scope.set_code(code)}) + + +@app.route(dump_type_url, methods=["POST"]) +def dump_type(): + scope = request.form.get("scope") + type = request.form.get("type") + + the_scope = Scope(scope) + + return jsonify({"status": True, "result": the_scope.set_type(type)}) + @app.route(load_url, methods=["POST"]) @@ -45,9 +66,15 @@ def get_document_of_scope(): @app.route(create_document_of_scope_url, methods=["POST"]) def create_document_of_scope(): scope = request.form.get("scope") - Scope(scope).create_documentation() - return jsonify({"status": True}) + return jsonify({"status": True, "result": Scope(scope).create_documentation()}) + + +@app.route(create_document_of_scope_url_old, methods=["POST"]) +def create_document_of_scope_old(): + scope = request.form.get("scope") + + return jsonify({"status": True, "result": Scope(scope).create_documentation_old()}) @app.route(get_type_of_scope_url, methods=["POST"]) def get_type_of_scope(): diff --git a/upsonic_on_prem/api/urls.py b/upsonic_on_prem/api/urls.py index 894da86a..37256fc1 100644 --- a/upsonic_on_prem/api/urls.py +++ b/upsonic_on_prem/api/urls.py @@ -2,6 +2,8 @@ dump_url = "/dump" +dump_code_url = "/dump_code" +dump_type_url = "/dump_type" load_url = "/load" get_admins_url = "/get_admins" @@ -50,6 +52,7 @@ get_document_of_scope_url = "/get_document_of_scope" create_document_of_scope_url = "/create_document_of_scope" +create_document_of_scope_url_old = "/create_document_of_scope_old" get_type_of_scope_url = "/get_type_of_scope" @@ -73,7 +76,9 @@ user_urs = [load_url, dump_url, get_read_scopes_of_me_url, get_write_scopes_of_me_url, get_document_of_scope_url, create_version_url, get_dump_history_url, load_specific_dump_url, get_all_scopes_name_prefix_url, - get_type_of_scope_url, create_document_of_scope_url, get_all_scopes_user_url, delete_scope_url] -user_write_urls = [dump_url, create_document_of_scope_url, delete_scope_url, create_version_url] + get_type_of_scope_url, create_document_of_scope_url, create_document_of_scope_url_old, + get_all_scopes_user_url, delete_scope_url, dump_code_url, dump_type_url] +user_write_urls = [dump_url, create_document_of_scope_url, create_document_of_scope_url_old, delete_scope_url, + create_version_url, dump_code_url, dump_type_url] user_read_urls = [load_url, get_document_of_scope_url, get_dump_history_url, get_type_of_scope_url, load_specific_dump_url] diff --git a/upsonic_on_prem/dash/app/adapter.py b/upsonic_on_prem/dash/app/adapter.py new file mode 100644 index 00000000..342fc5cd --- /dev/null +++ b/upsonic_on_prem/dash/app/adapter.py @@ -0,0 +1,11 @@ +from allauth.account.adapter import DefaultAccountAdapter +from allauth.account.utils import perform_login + + +class CustomAccountAdapter(DefaultAccountAdapter): + + def is_open_for_signup(self, request): + """ + Whether to allow sign ups. + """ + return False diff --git a/upsonic_on_prem/dash/app/api_integration.py b/upsonic_on_prem/dash/app/api_integration.py new file mode 100644 index 00000000..f34197c0 --- /dev/null +++ b/upsonic_on_prem/dash/app/api_integration.py @@ -0,0 +1,185 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +import json +import ast + +from rich.console import Console + +console = Console() + +from dotenv import load_dotenv +import os +from cryptography.fernet import Fernet + +load_dotenv(dotenv_path=".env") + +admin_key = os.environ.get("admin_key") + + +class Upsonic_Cloud_Utils: + def _log(self, message): + console.log(message) + + def __enter__(self): + return self # pragma: no cover + + def __exit__(self, exc_type, exc_val, exc_tb): + pass # pragma: no cover + + def create_database(self, name): + return ( + "DB_" + name + "_" + + (((Fernet.generate_key()).decode()).replace("-", "").replace("_", "").replace("=", ""))[ + :50 + ] + ) # pragma: no cover + + def create_access_key(self, ): + return ( + "ACK_" + (((Fernet.generate_key()).decode()).replace("-", "").replace("_", "").replace("=", ""))[ + :60 + ] + ) # pragma: no cover + + def create_read_only_access_key(self, ): + return ( + "R-ACK_" + (((Fernet.generate_key()).decode()).replace("-", "").replace("_", "").replace("=", ""))[ + :60 + ] + ) # pragma: no cover + + def __init__(self): + import requests + from requests.auth import HTTPBasicAuth + + self.verify = cloud_utils_ssl_verify + + from upsonic import console + + self.requests = requests + self.HTTPBasicAuth = HTTPBasicAuth + + self._log( + f"[bold white]Upsonic Cloud Utils[bold white] initializing...", + ) + + from upsonic import encrypt, decrypt + self.encrypt = encrypt + self.decrypt = decrypt + + self.api_url = cloud_utils_url + self.password = cloud_utils_access_key + + self._log( + f"[bold green]Upsonic Cloud[bold green] active", + ) + + def _send_request(self, method, endpoint, data=None, make_json=True): + try: + response = self.requests.request( + method, + self.api_url + endpoint, + data=data, + auth=self.HTTPBasicAuth("", self.password), + verify=self.verify + ) + try: + return response.text if not make_json else json.loads(response.text) + except: # pragma: no cover + print(f"Error on '{self.api_url + endpoint}': ", response.text) + return [None] # pragma: no cover + except: + print("Error: Remote is down") + return [None] + + def get_database(self, access_key): + data = {"access_key": access_key} + result = self._send_request("POST", "/get/databases", data) + return result[0] + + def get_content(self, database_name): + data = {"database_name": database_name} + result = self._send_request("POST", "/get/keys", data) + return result[0] + + def get_a_content(self, database_name, key): + data = {"database_name": database_name, "key": key} + result = self._send_request("POST", "/get/key", data) + return result[0] + + def count_content(self, database_name): + data = {"database_name": database_name} + result = self._send_request("POST", "/count/key", data) + return result[0] + + def edit_content(self, database_name, key, value): + data = {"database_name": database_name, "key": key, "value": value} + result = self._send_request("POST", "/edit/key", data) + return result[0] + + def delete_content(self, database_name, key, custom_user): + data = {"database_name": database_name, "key": key, "custom_user": custom_user} + result = self._send_request("POST", "/delete/key", data) + return result[0] + + def rename_database(self, database_name, new_database_name): + data = {"database_name": database_name, "new_database_name": new_database_name} + result = self._send_request("POST", "/rename/database", data) + return result[0] + + def delete_database(self, database_name, custom_user): + data = {"database_name": database_name, "custom_user": custom_user} + result = self._send_request("POST", "/delete/database", data) + return result[0] + + def pop_database(self, database_name): + data = {"database_name": database_name} + result = self._send_request("POST", "/pop/database", data) + return result[0] + + def get_access_keys(self, cloud_type): + data = {"cloud_type": cloud_type} + result = self._send_request("POST", "/get/access_key", data) + return result[0] + + def add_access_keys(self, cloud_type, access_key): + data = {"cloud_type": cloud_type, "access_key": access_key} + result = self._send_request("POST", "/add/access_key", data) + return result[0] + + def remove_access_keys(self, cloud_type, access_key): + data = {"cloud_type": cloud_type, "access_key": access_key} + result = self._send_request("POST", "/remove/access_key", data) + return result[0] + + def check_access_keys(self, cloud_type, access_key): + data = {"cloud_type": cloud_type, "access_key": access_key} + result = self._send_request("POST", "/check/access_key", data) + return result[0] + + def add_runner(self, uniq_name, database_name, encryption_key, access_key, cloud_type): + data = {"uniq_name": uniq_name, "database_name": database_name, "encryption_key": encryption_key, + "access_key": access_key, "cloud_type": cloud_type} + result = self._send_request("POST", "/add/runner", data) + return result[0] + + def log_runner(self, uniq_name, database_name): + data = {"uniq_name": uniq_name, "database_name": database_name} + result = self._send_request("POST", "/log/runner", data) + return result[0] + + def status_runner(self, uniq_name, database_name): + data = {"uniq_name": uniq_name, "database_name": database_name} + result = self._send_request("POST", "/status/runner", data) + return result[0] + + def get_runner(self, uniq_name, database_name): + data = {"uniq_name": uniq_name, "database_name": database_name} + result = self._send_request("POST", "/get/runner", data) + return result[0] + + def remove_runner(self, uniq_name, database_name): + data = {"uniq_name": uniq_name, "database_name": database_name} + result = self._send_request("POST", "/remove/runner", data) + return result[0] diff --git a/upsonic_on_prem/dash/app/forms.py b/upsonic_on_prem/dash/app/forms.py new file mode 100644 index 00000000..1054cdae --- /dev/null +++ b/upsonic_on_prem/dash/app/forms.py @@ -0,0 +1,50 @@ +from allauth.account.forms import LoginForm, AuthenticationMethod, get_username_max_length, app_settings, \ + set_form_field_order +from django import forms +from django.urls import reverse, NoReverseMatch +from django.utils.translation import pgettext, gettext_lazy as _ + +from django.utils.safestring import mark_safe + + +class MyCustomLoginForm(LoginForm): + + def __init__(self, *args, **kwargs): + self.request = kwargs.pop("request", None) + super(LoginForm, self).__init__(*args, **kwargs) + if app_settings.AUTHENTICATION_METHOD == AuthenticationMethod.EMAIL: + login_widget = forms.EmailInput( + attrs={ + "placeholder": _("Email address"), + "autocomplete": "email", + } + ) + login_field = forms.EmailField(label=_("Email"), widget=login_widget) + elif app_settings.AUTHENTICATION_METHOD == AuthenticationMethod.USERNAME: + login_widget = forms.TextInput( + attrs={"placeholder": _("Username"), "autocomplete": "username"} + ) + login_field = forms.CharField( + label=_("Username"), + widget=login_widget, + max_length=get_username_max_length(), + ) + else: + assert ( + app_settings.AUTHENTICATION_METHOD + == AuthenticationMethod.USERNAME_EMAIL + ) + login_widget = forms.TextInput( + attrs={"placeholder": _("Username or email"), "autocomplete": "email"} + ) + login_field = forms.CharField( + label=pgettext("field label", "Login"), widget=login_widget + ) + self.fields["login"] = login_field + set_form_field_order(self, ["login", "password", "remember"]) + if app_settings.SESSION_REMEMBER is not None: + del self.fields["remember"] + try: + reset_url = reverse("account_reset_password") + except NoReverseMatch: + pass diff --git a/upsonic_on_prem/dash/app/templates/account/base.html b/upsonic_on_prem/dash/app/templates/account/base.html index cbe8349f..869cc48a 100644 --- a/upsonic_on_prem/dash/app/templates/account/base.html +++ b/upsonic_on_prem/dash/app/templates/account/base.html @@ -11,30 +11,22 @@