From 919b23d9e01a39834b7b403bdfdbb2c30d022378 Mon Sep 17 00:00:00 2001 From: yanyanho Date: Fri, 31 Jul 2020 23:11:24 +0800 Subject: [PATCH] fix the code structure --- db.sqlite3 | 0 manage.py | 21 +++++ zkclient/__init__.py | 0 zkclient/asgi.py | 16 ++++ zkclient/settings.py | 120 +++++++++++++++++++++++++++++ zkclient/urls.py | 24 ++++++ zkclient/wsgi.py | 16 ++++ zkclient/zkclient/__init__.py | 0 zkclient/zkclient/asgi.py | 16 ++++ zkclient/zkclient/settings.py | 120 +++++++++++++++++++++++++++++ zkclient/zkclient/urls.py | 21 +++++ zkclient/zkclient/wsgi.py | 16 ++++ zkclientapp/__init__.py | 0 zkclientapp/admin.py | 9 +++ zkclientapp/apps.py | 5 ++ zkclientapp/migrations/__init__.py | 0 zkclientapp/models.py | 8 ++ zkclientapp/routes.py | 66 ++++++++++++++++ zkclientapp/tests.py | 3 + zkclientapp/urls.py | 22 ++++++ zkclientapp/views.py | 31 ++++++++ 21 files changed, 514 insertions(+) create mode 100644 db.sqlite3 create mode 100755 manage.py create mode 100644 zkclient/__init__.py create mode 100644 zkclient/asgi.py create mode 100644 zkclient/settings.py create mode 100644 zkclient/urls.py create mode 100644 zkclient/wsgi.py create mode 100644 zkclient/zkclient/__init__.py create mode 100644 zkclient/zkclient/asgi.py create mode 100644 zkclient/zkclient/settings.py create mode 100644 zkclient/zkclient/urls.py create mode 100644 zkclient/zkclient/wsgi.py create mode 100644 zkclientapp/__init__.py create mode 100644 zkclientapp/admin.py create mode 100644 zkclientapp/apps.py create mode 100644 zkclientapp/migrations/__init__.py create mode 100644 zkclientapp/models.py create mode 100644 zkclientapp/routes.py create mode 100644 zkclientapp/tests.py create mode 100644 zkclientapp/urls.py create mode 100644 zkclientapp/views.py diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..e69de29 diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..3fcfbf1 --- /dev/null +++ b/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zkclient.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/zkclient/__init__.py b/zkclient/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zkclient/asgi.py b/zkclient/asgi.py new file mode 100644 index 0000000..ed99c8b --- /dev/null +++ b/zkclient/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for zkclient project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zkclient.settings') + +application = get_asgi_application() diff --git a/zkclient/settings.py b/zkclient/settings.py new file mode 100644 index 0000000..6b14814 --- /dev/null +++ b/zkclient/settings.py @@ -0,0 +1,120 @@ +""" +Django settings for zkclient project. + +Generated by 'django-admin startproject' using Django 3.0.8. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'yg!x=us*u&ja2*rfqz38v96nr$z(-^=c-&%)gvj+vaxq*csw6f' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'zkclient.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'zkclient.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.0/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/zkclient/urls.py b/zkclient/urls.py new file mode 100644 index 0000000..3f0cb49 --- /dev/null +++ b/zkclient/urls.py @@ -0,0 +1,24 @@ +"""zkclient URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path +from django.conf.urls import include,url + +urlpatterns = [ + path('admin/', admin.site.urls), + url(r'',include(('zkclientapp.urls','zkclientapp'),namespace='zkclientapp')), +] + diff --git a/zkclient/wsgi.py b/zkclient/wsgi.py new file mode 100644 index 0000000..9b88374 --- /dev/null +++ b/zkclient/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for zkclient project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zkclient.settings') + +application = get_wsgi_application() diff --git a/zkclient/zkclient/__init__.py b/zkclient/zkclient/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zkclient/zkclient/asgi.py b/zkclient/zkclient/asgi.py new file mode 100644 index 0000000..ed99c8b --- /dev/null +++ b/zkclient/zkclient/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for zkclient project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zkclient.settings') + +application = get_asgi_application() diff --git a/zkclient/zkclient/settings.py b/zkclient/zkclient/settings.py new file mode 100644 index 0000000..6b14814 --- /dev/null +++ b/zkclient/zkclient/settings.py @@ -0,0 +1,120 @@ +""" +Django settings for zkclient project. + +Generated by 'django-admin startproject' using Django 3.0.8. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'yg!x=us*u&ja2*rfqz38v96nr$z(-^=c-&%)gvj+vaxq*csw6f' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'zkclient.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'zkclient.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.0/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/zkclient/zkclient/urls.py b/zkclient/zkclient/urls.py new file mode 100644 index 0000000..17a4090 --- /dev/null +++ b/zkclient/zkclient/urls.py @@ -0,0 +1,21 @@ +"""zkclient URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/zkclient/zkclient/wsgi.py b/zkclient/zkclient/wsgi.py new file mode 100644 index 0000000..9b88374 --- /dev/null +++ b/zkclient/zkclient/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for zkclient project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zkclient.settings') + +application = get_wsgi_application() diff --git a/zkclientapp/__init__.py b/zkclientapp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zkclientapp/admin.py b/zkclientapp/admin.py new file mode 100644 index 0000000..8b89b49 --- /dev/null +++ b/zkclientapp/admin.py @@ -0,0 +1,9 @@ +from django.contrib import admin + +# Register your models here. +from .models import merkletree + +# Register your models here. + + +admin.site.register(merkletree) \ No newline at end of file diff --git a/zkclientapp/apps.py b/zkclientapp/apps.py new file mode 100644 index 0000000..9c541a2 --- /dev/null +++ b/zkclientapp/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ZkclientappConfig(AppConfig): + name = 'zkclientapp' diff --git a/zkclientapp/migrations/__init__.py b/zkclientapp/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zkclientapp/models.py b/zkclientapp/models.py new file mode 100644 index 0000000..a630891 --- /dev/null +++ b/zkclientapp/models.py @@ -0,0 +1,8 @@ +from django.db import models + +# Create your models here. +class merkletree(models.Model): + class Meta: + db_table = 'merkletree' + mid = models.AutoField(max_length=11,db_column='MID',primary_key=True) + tree_data = models.TextField(max_length=40000, db_column='tree_data', blank=False) \ No newline at end of file diff --git a/zkclientapp/routes.py b/zkclientapp/routes.py new file mode 100644 index 0000000..26508e8 --- /dev/null +++ b/zkclientapp/routes.py @@ -0,0 +1,66 @@ +from commands.zeth_gen_fisco_address import gen_fisco_address +from commands.zeth_gen_address import gen_address +from commands.event_sync import event_sync +from commands.zeth_deposit import deposit +from commands.zeth_token_approve import token_approve +from commands.zeth_mix import mix +from commands.zeth_ls_commits import ls_commits +from commands.zeth_ls_notes import ls_notes +from commands.zeth_deploy import deploy +from python_web3.eth_account.account import Account +from commands.constants import USER_DIR, FISCO_ADDRESS_FILE, WALLET_DIR_DEFAULT +from django.shortcuts import render +import json +from django.http import JsonResponse + +''' +The wallet of user is designed as that every wallet need to be specified a username and store the +reference accounts and assets data of that user. There two kinds of account in the wallet of a user, +including Fisco account (saved as keystore) and zbac account (saved as publickey file and privatekey file). +Note that different wallet must have different zbac account but any of them are allowed to have same Fisco +account. +''' + +''' +make wallet by generate a new Fisco account for user with username and password +''' +def genFiscoAddr(request) -> None: + result = {} + req = json.loads(request.body) + (address, publickey) = gen_fisco_address(req['username'], req['password']) + result['address'] = address + result['publickey'] = publickey + JsonResponse(result) + +''' +make wallet by import the Fisco account that the user want to use with privatekey, username and password +''' +def importFiscoAddr(request) -> None: + result = {} + req = json.loads(request.body) + account = Account.privateKeyToAccount(req['privatekey']) + keystore_file = "{}/{}/{}".format(USER_DIR, req['username'], FISCO_ADDRESS_FILE) + if exists(keystore_file): + raise ClickException(f"ZethAddress file {keystore_file} exists") + user_dir = "{}/{}/{}".format(USER_DIR, req['username'], WALLET_DIR_DEFAULT) + _ensure_dir(user_dir) + keytext = Account.encrypt(account.privateKey, req['password']) + with open(keystore_file, "w") as dump_f: + json.dump(keytext, dump_f) + print(f"{req['username']}'s address: {account.address}") + print(f"{req['username']}'s publickey: {account.publickey}") + print(f"fisco account keypair written to {keystore_file}") + result['address'] = account.address + result['publickey'] = account.publickey + JsonResponse(result) + + +''' +generate a new zbac account for user with username, return the zbac address used for recieving notes +''' +def genZbacAddr(request) -> None: + result = {} + req = json.loads(request.body) + zbac_addr = gen_address(req['username']) + result['address'] = zbac_addr + JsonResponse(result) \ No newline at end of file diff --git a/zkclientapp/tests.py b/zkclientapp/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/zkclientapp/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/zkclientapp/urls.py b/zkclientapp/urls.py new file mode 100644 index 0000000..9708077 --- /dev/null +++ b/zkclientapp/urls.py @@ -0,0 +1,22 @@ + +from django.conf.urls import url + +from . import views +from . import routes + + + + + +urlpatterns = [ + + #主页 + + url(r'^$',views.index,name='index'), + url(r'^get\.html$', views.get_html), + url(r'^get$', views.get), + url(r'^post\.html$', views.post_html), + url(r'^post$', views.post), + url(r'^genFiscoAddr$', routes.genFiscoAddr), + url(r'^genZbacAddr$', routes.genZbacAddr), +] diff --git a/zkclientapp/views.py b/zkclientapp/views.py new file mode 100644 index 0000000..aeecb2f --- /dev/null +++ b/zkclientapp/views.py @@ -0,0 +1,31 @@ +from django.shortcuts import render + +# Create your views here. + +def index(request): + + "学习笔记的主页" + + return render(request,'zkclientapp/template/index.html') + +def get_html(request): + return render(request, 'zkclientapp/template/get.html') + +def get(request): + context = {} + # 通过request.GET['name']形式获取get表单内容 + # result为重定向到的result.html所使用的变量 + context['result'] = f"你搜索的内容为:{request.GET['q']}" + return render(request, 'zkclientapp/template/result.html', context) + +def post_html(request): + # 不能和get一样使用render_to_response必须使用render进行重定向,不然服务端不会设置csrf_token + # return render_to_response('post.html') + return render(request, 'zkclientapp/template/post.html') + +def post(request): + context = {} + # 通过request.GET['name']形式获取post表单内容 + # result为重定向到的result.html所使用的变量 + context['result'] = f"你搜索的内容为:{request.POST['q']}" + return render(request, 'zkclientapp/template/result.html', context)