diff --git a/app/app/__init__.py b/app/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/app/__pycache__/__init__.cpython-37.pyc b/app/app/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..6ab9c59 Binary files /dev/null and b/app/app/__pycache__/__init__.cpython-37.pyc differ diff --git a/app/app/__pycache__/settings.cpython-37.pyc b/app/app/__pycache__/settings.cpython-37.pyc new file mode 100644 index 0000000..5047832 Binary files /dev/null and b/app/app/__pycache__/settings.cpython-37.pyc differ diff --git a/app/app/__pycache__/urls.cpython-37.pyc b/app/app/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000..07a4575 Binary files /dev/null and b/app/app/__pycache__/urls.cpython-37.pyc differ diff --git a/app/app/__pycache__/wsgi.cpython-37.pyc b/app/app/__pycache__/wsgi.cpython-37.pyc new file mode 100644 index 0000000..aab7d89 Binary files /dev/null and b/app/app/__pycache__/wsgi.cpython-37.pyc differ diff --git a/app/app/management/commands/run.py b/app/app/management/commands/run.py new file mode 100644 index 0000000..766a181 --- /dev/null +++ b/app/app/management/commands/run.py @@ -0,0 +1,9 @@ +from django.core.management.commands.runserver import Command as RunServer + +class Command(RunServer): + + def check(self, *args, **kwargs): + self.stdout.write(self.style.WARNING("SKIPPING SYSTEM CHECKS!\n")) + + def check_migrations(self, *args, **kwargs): + self.stdout.write(self.style.WARNING("SKIPPING MIGRATION CHECKS!\n")) diff --git a/app/app/settings.py b/app/app/settings.py new file mode 100644 index 0000000..7872365 --- /dev/null +++ b/app/app/settings.py @@ -0,0 +1,123 @@ +""" +Django settings for app project. + +Generated by 'django-admin startproject' using Django 2.2.3. + +For more information on this file, see +https://docs.djangoproject.com/en/2.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.2/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/2.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 's$%t4oz0c0^$-e1%ize(z^&@3$k6iuuump5%v-*0ivpph4^@p^' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + + +# Application definition + +INSTALLED_APPS = [ + 'home', + 'manufacturers', + 'logistics', + '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 = 'app.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 = 'app.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.2/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/2.2/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/2.2/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/2.2/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/app/app/urls.py b/app/app/urls.py new file mode 100644 index 0000000..287cfdb --- /dev/null +++ b/app/app/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import path , include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('home.urls')) +] diff --git a/app/app/wsgi.py b/app/app/wsgi.py new file mode 100644 index 0000000..5549b34 --- /dev/null +++ b/app/app/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for app 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/2.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings') + +application = get_wsgi_application() diff --git a/app/db.sqlite3 b/app/db.sqlite3 new file mode 100644 index 0000000..64e779e Binary files /dev/null and b/app/db.sqlite3 differ diff --git a/app/home/__init__.py b/app/home/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/home/__pycache__/__init__.cpython-35.pyc b/app/home/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..6787dcf Binary files /dev/null and b/app/home/__pycache__/__init__.cpython-35.pyc differ diff --git a/app/home/__pycache__/__init__.cpython-37.pyc b/app/home/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..769fadb Binary files /dev/null and b/app/home/__pycache__/__init__.cpython-37.pyc differ diff --git a/app/home/__pycache__/admin.cpython-35.pyc b/app/home/__pycache__/admin.cpython-35.pyc new file mode 100644 index 0000000..e74eeec Binary files /dev/null and b/app/home/__pycache__/admin.cpython-35.pyc differ diff --git a/app/home/__pycache__/admin.cpython-37.pyc b/app/home/__pycache__/admin.cpython-37.pyc new file mode 100644 index 0000000..2d68e25 Binary files /dev/null and b/app/home/__pycache__/admin.cpython-37.pyc differ diff --git a/app/home/__pycache__/models.cpython-35.pyc b/app/home/__pycache__/models.cpython-35.pyc new file mode 100644 index 0000000..3986db5 Binary files /dev/null and b/app/home/__pycache__/models.cpython-35.pyc differ diff --git a/app/home/__pycache__/models.cpython-37.pyc b/app/home/__pycache__/models.cpython-37.pyc new file mode 100644 index 0000000..143064f Binary files /dev/null and b/app/home/__pycache__/models.cpython-37.pyc differ diff --git a/app/home/__pycache__/urls.cpython-35.pyc b/app/home/__pycache__/urls.cpython-35.pyc new file mode 100644 index 0000000..eb16259 Binary files /dev/null and b/app/home/__pycache__/urls.cpython-35.pyc differ diff --git a/app/home/__pycache__/urls.cpython-37.pyc b/app/home/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000..a6f41a4 Binary files /dev/null and b/app/home/__pycache__/urls.cpython-37.pyc differ diff --git a/app/home/__pycache__/views.cpython-35.pyc b/app/home/__pycache__/views.cpython-35.pyc new file mode 100644 index 0000000..50fd66c Binary files /dev/null and b/app/home/__pycache__/views.cpython-35.pyc differ diff --git a/app/home/__pycache__/views.cpython-37.pyc b/app/home/__pycache__/views.cpython-37.pyc new file mode 100644 index 0000000..2f8ad42 Binary files /dev/null and b/app/home/__pycache__/views.cpython-37.pyc differ diff --git a/app/home/admin.py b/app/home/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/home/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/home/apps.py b/app/home/apps.py new file mode 100644 index 0000000..90dc713 --- /dev/null +++ b/app/home/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class HomeConfig(AppConfig): + name = 'home' diff --git a/app/home/migrations/__init__.py b/app/home/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/home/migrations/__pycache__/__init__.cpython-35.pyc b/app/home/migrations/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..b90c7bc Binary files /dev/null and b/app/home/migrations/__pycache__/__init__.cpython-35.pyc differ diff --git a/app/home/migrations/__pycache__/__init__.cpython-37.pyc b/app/home/migrations/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..a1c758c Binary files /dev/null and b/app/home/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/app/home/models.py b/app/home/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/home/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/home/templates/home/home.html b/app/home/templates/home/home.html new file mode 100644 index 0000000..30ec0d4 --- /dev/null +++ b/app/home/templates/home/home.html @@ -0,0 +1,10 @@ + + + HOME PAGE +
+ +

+
+
+ + \ No newline at end of file diff --git a/app/home/tests.py b/app/home/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/home/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/home/urls.py b/app/home/urls.py new file mode 100644 index 0000000..29a1e4f --- /dev/null +++ b/app/home/urls.py @@ -0,0 +1,8 @@ +from django.contrib import admin +from django.urls import path , include +from . import views +urlpatterns = [ + path('', views.home , name = 'home'), + path('manufacturers/', include('manufacturers.urls')), + path('logistics/', include('logistics.urls')), +] \ No newline at end of file diff --git a/app/home/views.py b/app/home/views.py new file mode 100644 index 0000000..6b0c678 --- /dev/null +++ b/app/home/views.py @@ -0,0 +1,4 @@ +from django.shortcuts import render + +def home(request): + return render(request, 'home/home.html') \ No newline at end of file diff --git a/app/logistics/__init__.py b/app/logistics/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/logistics/__pycache__/__init__.cpython-35.pyc b/app/logistics/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..5dc9aa1 Binary files /dev/null and b/app/logistics/__pycache__/__init__.cpython-35.pyc differ diff --git a/app/logistics/__pycache__/__init__.cpython-37.pyc b/app/logistics/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..1c77ae1 Binary files /dev/null and b/app/logistics/__pycache__/__init__.cpython-37.pyc differ diff --git a/app/logistics/__pycache__/admin.cpython-35.pyc b/app/logistics/__pycache__/admin.cpython-35.pyc new file mode 100644 index 0000000..15b27e6 Binary files /dev/null and b/app/logistics/__pycache__/admin.cpython-35.pyc differ diff --git a/app/logistics/__pycache__/admin.cpython-37.pyc b/app/logistics/__pycache__/admin.cpython-37.pyc new file mode 100644 index 0000000..6b8e4c6 Binary files /dev/null and b/app/logistics/__pycache__/admin.cpython-37.pyc differ diff --git a/app/logistics/__pycache__/models.cpython-35.pyc b/app/logistics/__pycache__/models.cpython-35.pyc new file mode 100644 index 0000000..d7556f5 Binary files /dev/null and b/app/logistics/__pycache__/models.cpython-35.pyc differ diff --git a/app/logistics/__pycache__/models.cpython-37.pyc b/app/logistics/__pycache__/models.cpython-37.pyc new file mode 100644 index 0000000..851866b Binary files /dev/null and b/app/logistics/__pycache__/models.cpython-37.pyc differ diff --git a/app/logistics/__pycache__/urls.cpython-35.pyc b/app/logistics/__pycache__/urls.cpython-35.pyc new file mode 100644 index 0000000..f01993f Binary files /dev/null and b/app/logistics/__pycache__/urls.cpython-35.pyc differ diff --git a/app/logistics/__pycache__/urls.cpython-37.pyc b/app/logistics/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000..e0da74d Binary files /dev/null and b/app/logistics/__pycache__/urls.cpython-37.pyc differ diff --git a/app/logistics/__pycache__/views.cpython-35.pyc b/app/logistics/__pycache__/views.cpython-35.pyc new file mode 100644 index 0000000..740a632 Binary files /dev/null and b/app/logistics/__pycache__/views.cpython-35.pyc differ diff --git a/app/logistics/__pycache__/views.cpython-37.pyc b/app/logistics/__pycache__/views.cpython-37.pyc new file mode 100644 index 0000000..f57af9c Binary files /dev/null and b/app/logistics/__pycache__/views.cpython-37.pyc differ diff --git a/app/logistics/admin.py b/app/logistics/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/logistics/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/logistics/apps.py b/app/logistics/apps.py new file mode 100644 index 0000000..812f267 --- /dev/null +++ b/app/logistics/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class LogisticsConfig(AppConfig): + name = 'logistics' diff --git a/app/logistics/migrations/__init__.py b/app/logistics/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/logistics/migrations/__pycache__/__init__.cpython-35.pyc b/app/logistics/migrations/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..07943fb Binary files /dev/null and b/app/logistics/migrations/__pycache__/__init__.cpython-35.pyc differ diff --git a/app/logistics/migrations/__pycache__/__init__.cpython-37.pyc b/app/logistics/migrations/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..f91ab43 Binary files /dev/null and b/app/logistics/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/app/logistics/models.py b/app/logistics/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/logistics/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/logistics/tests.py b/app/logistics/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/logistics/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/logistics/urls.py b/app/logistics/urls.py new file mode 100644 index 0000000..f8a36d5 --- /dev/null +++ b/app/logistics/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import path, include +from . import views + +urlpatterns = [ + path('', views.index, name = 'index') +] \ No newline at end of file diff --git a/app/logistics/views.py b/app/logistics/views.py new file mode 100644 index 0000000..1d60ba1 --- /dev/null +++ b/app/logistics/views.py @@ -0,0 +1,5 @@ +from django.shortcuts import render +from django.http import HttpResponse + +def index(request): + return HttpResponse('

LOGS

') diff --git a/app/manage.py b/app/manage.py new file mode 100755 index 0000000..a3f0719 --- /dev/null +++ b/app/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', 'app.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/app/management/commands/run.py b/app/management/commands/run.py new file mode 100755 index 0000000..766a181 --- /dev/null +++ b/app/management/commands/run.py @@ -0,0 +1,9 @@ +from django.core.management.commands.runserver import Command as RunServer + +class Command(RunServer): + + def check(self, *args, **kwargs): + self.stdout.write(self.style.WARNING("SKIPPING SYSTEM CHECKS!\n")) + + def check_migrations(self, *args, **kwargs): + self.stdout.write(self.style.WARNING("SKIPPING MIGRATION CHECKS!\n")) diff --git a/app/manufacturers/__init__.py b/app/manufacturers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/manufacturers/__pycache__/__init__.cpython-35.pyc b/app/manufacturers/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..12bcd8a Binary files /dev/null and b/app/manufacturers/__pycache__/__init__.cpython-35.pyc differ diff --git a/app/manufacturers/__pycache__/__init__.cpython-37.pyc b/app/manufacturers/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..251e7f0 Binary files /dev/null and b/app/manufacturers/__pycache__/__init__.cpython-37.pyc differ diff --git a/app/manufacturers/__pycache__/admin.cpython-35.pyc b/app/manufacturers/__pycache__/admin.cpython-35.pyc new file mode 100644 index 0000000..be21de8 Binary files /dev/null and b/app/manufacturers/__pycache__/admin.cpython-35.pyc differ diff --git a/app/manufacturers/__pycache__/admin.cpython-37.pyc b/app/manufacturers/__pycache__/admin.cpython-37.pyc new file mode 100644 index 0000000..2bc9d8f Binary files /dev/null and b/app/manufacturers/__pycache__/admin.cpython-37.pyc differ diff --git a/app/manufacturers/__pycache__/med_client.cpython-35.pyc b/app/manufacturers/__pycache__/med_client.cpython-35.pyc new file mode 100644 index 0000000..e432a0c Binary files /dev/null and b/app/manufacturers/__pycache__/med_client.cpython-35.pyc differ diff --git a/app/manufacturers/__pycache__/models.cpython-35.pyc b/app/manufacturers/__pycache__/models.cpython-35.pyc new file mode 100644 index 0000000..fdb0bcd Binary files /dev/null and b/app/manufacturers/__pycache__/models.cpython-35.pyc differ diff --git a/app/manufacturers/__pycache__/models.cpython-37.pyc b/app/manufacturers/__pycache__/models.cpython-37.pyc new file mode 100644 index 0000000..023ba19 Binary files /dev/null and b/app/manufacturers/__pycache__/models.cpython-37.pyc differ diff --git a/app/manufacturers/__pycache__/urls.cpython-35.pyc b/app/manufacturers/__pycache__/urls.cpython-35.pyc new file mode 100644 index 0000000..4d9e468 Binary files /dev/null and b/app/manufacturers/__pycache__/urls.cpython-35.pyc differ diff --git a/app/manufacturers/__pycache__/urls.cpython-37.pyc b/app/manufacturers/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000..976c168 Binary files /dev/null and b/app/manufacturers/__pycache__/urls.cpython-37.pyc differ diff --git a/app/manufacturers/__pycache__/views.cpython-35.pyc b/app/manufacturers/__pycache__/views.cpython-35.pyc new file mode 100644 index 0000000..27405ea Binary files /dev/null and b/app/manufacturers/__pycache__/views.cpython-35.pyc differ diff --git a/app/manufacturers/__pycache__/views.cpython-37.pyc b/app/manufacturers/__pycache__/views.cpython-37.pyc new file mode 100644 index 0000000..499f542 Binary files /dev/null and b/app/manufacturers/__pycache__/views.cpython-37.pyc differ diff --git a/app/manufacturers/admin.py b/app/manufacturers/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/manufacturers/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/manufacturers/apps.py b/app/manufacturers/apps.py new file mode 100644 index 0000000..40b1799 --- /dev/null +++ b/app/manufacturers/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ManufacturersConfig(AppConfig): + name = 'manufacturers' diff --git a/app/manufacturers/med_client.py b/app/manufacturers/med_client.py new file mode 100644 index 0000000..e23cbca --- /dev/null +++ b/app/manufacturers/med_client.py @@ -0,0 +1,319 @@ +import os +import hashlib +import base64 +from base64 import b64encode +import time +import random +import requests +import yaml +from hashlib import sha512 + +from sawtooth_signing import create_context +from sawtooth_signing import CryptoFactory +from sawtooth_signing import ParseError +from sawtooth_signing.secp256k1 import Secp256k1PrivateKey + +from sawtooth_sdk.protobuf.transaction_pb2 import TransactionHeader +from sawtooth_sdk.protobuf.transaction_pb2 import Transaction +from sawtooth_sdk.protobuf.batch_pb2 import Batch +from sawtooth_sdk.protobuf.batch_pb2 import BatchHeader +from sawtooth_sdk.protobuf.batch_pb2 import BatchList + + +from sawtooth_med.med_exceptions import MedException + +MED_NAMESPACE = hashlib.sha512('med'.encode("utf-8")).hexdigest()[0:6] + + +def _sha512(data): + return hashlib.sha512(data).hexdigest() + +class MedClient: + + def __init__(self, base_url , keyfile = None): + + self._base_url = base_url + + if keyfile is None: + self._signer = None + return + + try: + with open(keyfile) as f: + private_key_str = f.read().strip() + except OSError as err: + raise MedException('Failed to read private key {} : {}'.format(keyfile , str(err))) + + try: + private_key = Secp256k1PrivateKey.from_hex(private_key_str) + except ParseError as e: + raise MedException('Failed to load private key : {}'.format(str(err))) + + self._signer = CryptoFactory(create_context('secp256k1')).new_signer(private_key) + + def createMedicine(self, medicineName, medicineID, medicineKeyContent, medicineAllContents, manufactureDate, expiryDate , stock, wait = None , auth_user = None, auth_password = None): + return self._send_med_txn( + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock, + self._signer.get_public_key().as_hex(), + "createMedicine", + 'root', + wait, + auth_user, + auth_password + ) + + def updateMedicine(self, medicineName, medicineID, medicineKeyContent, medicineAllContents, manufactureDate, expiryDate, stock, wait = None , auth_user = None, auth_password = None): + return self._send_med_txn( + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock, + self._signer.get_public_key().as_hex(), + "updateMedicine", + 'root', + wait, + auth_user, + auth_password + ) + + def updateMedicineOwner(self, medicineName, medicineID, medicineKeyContent, medicineAllContents, manufactureDate, expiryDate, stock, wait = None , auth_user = None, auth_password = None): + return self._send_med_txn( + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock, + self._signer.get_public_key().as_hex(), + "updateMedicineOwner", + '', + wait, + auth_user, + auth_password + ) + + def produce(self, medicineName, medicineID, medicineKeyContent, medicineAllContents, manufactureDate, expiryDate, stock, wait = None , auth_user = None, auth_password = None): + return self._send_med_txn( + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock, + self._signer.get_public_key().as_hex(), + "produce", + 'root', + wait, + auth_user, + auth_password + ) + + def deleteMedicine(self , medicineName, medicineID, medicineKeyContent, medicineAllContents, manufactureDate, expiryDate, stock, wait = None , auth_user = None, auth_password = None): + return self._send_med_txn( + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock, + self._signer.get_public_key().as_hex(), + "deleteMedicine", + 'root', + wait, + auth_user, + auth_password + ) + + def list(self, auth_user=None, auth_password=None): + med_prefix = self._get_prefix() + + result = self._send_request( + "state?address={}".format(med_prefix), + auth_user=auth_user, + auth_password=auth_password) + + try: + encoded_entries = yaml.safe_load(result)["data"] + + return [ + base64.b64decode(entry["data"]) for entry in encoded_entries + ] + + except BaseException: + return None + + def show(self, name, auth_user=None, auth_password=None): + address = self._get_address(name) + + result = self._send_request( + "state/{}".format(address), + name=name, + auth_user=auth_user, + auth_password=auth_password) + try: + return base64.b64decode(yaml.safe_load(result)["data"]) + + except BaseException: + return None + + def _get_status(self, batch_id, wait, auth_user=None, auth_password=None): + try: + result = self._send_request( + 'batch_statuses?id={}&wait={}'.format(batch_id, wait), + auth_user=auth_user, + auth_password=auth_password) + return yaml.safe_load(result)['data'][0]['status'] + except BaseException as err: + raise MedException(err) + + + def _get_prefix(self): + return(_sha512('med'.encode('utf-8'))[0:6]) + + + def _get_address(self, name): + med_prefix = self._get_prefix() + med_address = _sha512(name.encode('utf-8'))[0:64] + return(med_prefix + med_address) + + def _send_request(self, + suffix, + data=None, + content_type=None, + name=None, + auth_user=None, + auth_password=None): + if self._base_url.startswith("http://"): + url = "{}/{}".format(self._base_url, suffix) + else: + url = "http://{}/{}".format(self._base_url, suffix) + + headers = {} + if auth_user is not None: + auth_string = "{}:{}".format(auth_user, auth_password) + b64_string = b64encode(auth_string.encode()).decode() + auth_header = 'Basic {}'.format(b64_string) + headers['Authorization'] = auth_header + + if content_type is not None: + headers['Content-Type'] = content_type + + try: + if data is not None: + result = requests.post(url, headers=headers, data=data) + else: + result = requests.get(url, headers=headers) + + if result.status_code == 404: + raise MedException("No such medicine: {}".format(name)) + + if not result.ok: + raise MedException("Error {}: {}".format(result.status_code, result.reason)) + + except requests.ConnectionError as err: + raise MedException( + 'Failed to connect to {}: {}'.format(url, str(err))) + + except BaseException as err: + raise MedException(err) + + return(result.text) + + + + def _send_med_txn(self, + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock, + manufacturerID, + action, + newOwner, + wait=None, + auth_user=None, + auth_password=None): + payload = ",".join([medicineName, medicineID, medicineKeyContent, medicineAllContents, str(manufactureDate), str(expiryDate), str(stock), str(manufacturerID) , action , newOwner]).encode() + + address = self._get_address(medicineName) + + header = TransactionHeader( + signer_public_key=self._signer.get_public_key().as_hex(), + family_name="med", + family_version="1.0", + inputs=[address], + outputs=[address], + dependencies=[], + payload_sha512=_sha512(payload), + batcher_public_key=self._signer.get_public_key().as_hex(), + nonce=hex(random.randint(0, 2**64)) + ).SerializeToString() + + signature = self._signer.sign(header) + + transaction = Transaction( + header=header, + payload=payload, + header_signature=signature + ) + + batch_list = self._create_batch_list([transaction]) + batch_id = batch_list.batches[0].header_signature + + if wait and wait > 0: + wait_time = 0 + start_time = time.time() + response = self._send_request( + "batches", batch_list.SerializeToString(), + 'application/octet-stream', + auth_user=auth_user, + auth_password=auth_password) + while wait_time < wait: + status = self._get_status( + batch_id, + wait - int(wait_time), + auth_user=auth_user, + auth_password=auth_password) + wait_time = time.time() - start_time + + if status != 'PENDING': + return response + + return response + + return self._send_request( + "batches", batch_list.SerializeToString(), + 'application/octet-stream', + auth_user=auth_user, + auth_password=auth_password) + + + def _create_batch_list(self, transactions): + transaction_signatures = [t.header_signature for t in transactions] + + header = BatchHeader( + signer_public_key=self._signer.get_public_key().as_hex(), + transaction_ids=transaction_signatures + ).SerializeToString() + + signature = self._signer.sign(header) + + batch = Batch( + header=header, + transactions=transactions, + header_signature=signature) + return BatchList(batches=[batch]) diff --git a/app/manufacturers/med_exceptions.py b/app/manufacturers/med_exceptions.py new file mode 100644 index 0000000..79501da --- /dev/null +++ b/app/manufacturers/med_exceptions.py @@ -0,0 +1,2 @@ +class MedException(Exception): + pass diff --git a/app/manufacturers/migrations/__init__.py b/app/manufacturers/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/manufacturers/migrations/__pycache__/__init__.cpython-35.pyc b/app/manufacturers/migrations/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..066a1ba Binary files /dev/null and b/app/manufacturers/migrations/__pycache__/__init__.cpython-35.pyc differ diff --git a/app/manufacturers/migrations/__pycache__/__init__.cpython-37.pyc b/app/manufacturers/migrations/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..2cf9f20 Binary files /dev/null and b/app/manufacturers/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/app/manufacturers/models.py b/app/manufacturers/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/manufacturers/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/manufacturers/templates/index.html b/app/manufacturers/templates/index.html new file mode 100644 index 0000000..f1a6ef6 --- /dev/null +++ b/app/manufacturers/templates/index.html @@ -0,0 +1,12 @@ + + +

SELECT

+
+
+ + +
+ \ No newline at end of file diff --git a/app/manufacturers/templates/med.html b/app/manufacturers/templates/med.html new file mode 100644 index 0000000..deeb707 --- /dev/null +++ b/app/manufacturers/templates/med.html @@ -0,0 +1,46 @@ + + +
+
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ + +
+ + diff --git a/app/manufacturers/templates/test.html b/app/manufacturers/templates/test.html new file mode 100644 index 0000000..9630053 --- /dev/null +++ b/app/manufacturers/templates/test.html @@ -0,0 +1,8 @@ + + +

NICE

+ + + {{ response }} + + \ No newline at end of file diff --git a/app/manufacturers/templates/transfer.html b/app/manufacturers/templates/transfer.html new file mode 100644 index 0000000..e69de29 diff --git a/app/manufacturers/tests.py b/app/manufacturers/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/manufacturers/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/manufacturers/urls.py b/app/manufacturers/urls.py new file mode 100644 index 0000000..6b54903 --- /dev/null +++ b/app/manufacturers/urls.py @@ -0,0 +1,9 @@ +from django.contrib import admin +from django.urls import path, include +from . import views + +urlpatterns = [ + path('', views.index , name = 'index'), + path('task', views.task , name = 'task'), + path('medtransact', views.medtransact, name = 'medtransact') +] \ No newline at end of file diff --git a/app/manufacturers/views.py b/app/manufacturers/views.py new file mode 100644 index 0000000..26a78e8 --- /dev/null +++ b/app/manufacturers/views.py @@ -0,0 +1,82 @@ +from django.shortcuts import render +from django.http import HttpResponse +import getpass +import datetime +import pkg_resources +import traceback +import os +import sys +sys.path.insert(0, '/home/harsh/project/med_python') + +# from sawtooth_med.med_client import MedClient +# from sawtooth_med.med_exceptions import MedException + + +DEFAULT_URL = 'http://127.0.0.1:8008' + +def _get_keyfile(args): + username = args + home = os.path.expanduser("~") + key_dir = os.path.join(home, ".sawtooth", "keys") + + return '{}/{}.priv'.format(key_dir, username) + +def index(request): + return render(request, 'index.html') + +def task(request): + options = request.POST['options'] + if options == 'med': + return render(request, 'med.html') + else: + return render(request, 'transfer.html') + +def medtransact(request): + func = request.POST['func'] + medicineName = request.POST['medicineName'] + medicineID = request.POST['medicineID'] + medicineKeyContent = request.POST['medicineKeyContent'] + medicineAllContents = request.POST['medicineAllContents'] + expiryMonths = request.POST['expiryMonths'] + uname = request.POST['uname'] + wait = request.POST['wait'] + + manufactureDate = datetime.date.today() + expiryDate = manufactureDate + datetime.timedelta(int(expiryMonths)*365/12) + + keyfile = _get_keyfile(uname) + url = DEFAULT_URL + + if func == 'createMedicine': + client = MedClient(base_url = url , keyfile= keyfile) + if wait and wait > 0: + response = client.createMedicine( + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock = 0, + wait = wait, + auth_user = None, + auth_password = None + ) + else: + response = client.createMedicine( + medicineName, + medicineID, + medicineKeyContent, + medicineAllContents, + manufactureDate, + expiryDate, + stock = 0, + auth_user = None, + auth_password = None + ) + print("Response : {}".format(response)) + + + return render(request, 'test.html', {'response': response}) + + diff --git a/djangodocker b/djangodocker new file mode 100644 index 0000000..aabe950 --- /dev/null +++ b/djangodocker @@ -0,0 +1,11 @@ +FROM python:3.7-alpine + +COPY ./requirements.txt /requirements.txt +RUN pip install -r /requirements.txt + +RUN mkdir /app +WORKDIR /app + +EXPOSE 8000 + +COPY ./app /app diff --git a/dock b/dock index 52277ac..31153eb 100755 --- a/dock +++ b/dock @@ -8,6 +8,10 @@ docker volume rm project_keyshare docker volume rm project_blocks -docker build -f transfer_python/Dockerfile -t harsh/transfer:v1 . +docker volume rm project_node + +# docker build -f transfer_python/Dockerfile -t harsh/transfer:v1 . + +docker build -f djangodocker -t harsh/djangoapp:v1 . echo "Done Shit!" diff --git a/docker-compose-NORMAL.yaml b/docker-compose-NORMAL.yaml index 0c4d925..084956f 100644 --- a/docker-compose-NORMAL.yaml +++ b/docker-compose-NORMAL.yaml @@ -2,129 +2,153 @@ version: '3.6' services: - settings-tp: - image: hyperledger/sawtooth-settings-tp:nightly - container_name: sawtooth-settings-tp - volumes: - - keyshare:/root - - blocks:/var/lib/sawtooth - depends_on: - - validator - command: | - bash -c " - settings-tp -vv -C tcp://validator:4004 - " - stop_signal: SIGKILL + # settings-tp: + # image: hyperledger/sawtooth-settings-tp:nightly + # container_name: sawtooth-settings-tp + # volumes: + # - keyshare:/root + # - blocks:/var/lib/sawtooth + # - node:/project + # depends_on: + # - validator + # command: | + # bash -c " + # settings-tp -vv -C tcp://validator:4004 + # " + # stop_signal: SIGKILL - med-tp-python: - image: harsh/med:v3 - container_name: sawtooth-med-tp-python - volumes: - - keyshare:/root - - blocks:/var/lib/sawtooth - depends_on: - - validator - command: | - bash -c " - med-tp-python -vv -C tcp://validator:4004 - " - stop_signal: SIGKILL + # med-tp-python: + # image: harsh/med:v3 + # container_name: sawtooth-med-tp-python + # volumes: + # - keyshare:/root + # - blocks:/var/lib/sawtooth + # - node:/project + # depends_on: + # - validator + # command: | + # bash -c " + # med-tp-python -vv -C tcp://validator:4004 + # " + # stop_signal: SIGKILL - transfer-tp-python: - image: harsh/transfer:v1 - container_name: sawtooth-transfer-tp-python - volumes: - - keyshare:/root - - blocks:/var/lib/sawtooth - depends_on: - - validator - command: | - bash -c " - transfer-tp-python -vv -C tcp://validator:4004 - " - stop_signal: SIGKILL + # transfer-tp-python: + # image: harsh/transfer:v1 + # container_name: sawtooth-transfer-tp-python + # volumes: + # - keyshare:/root + # - blocks:/var/lib/sawtooth + # - node:/project + # depends_on: + # - validator + # command: | + # bash -c " + # transfer-tp-python -vv -C tcp://validator:4004 + # " + # stop_signal: SIGKILL - client: - image: hyperledger/sawtooth-shell:nightly - container_name: sawtooth-shell - volumes: - - keyshare:/root - - blocks:/var/lib/sawtooth - depends_on: - - validator - command: | - bash -c " - sawtooth keygen && - tail -f /dev/null - " - stop_signal: SIGKILL + # client: + # image: hyperledger/sawtooth-shell:nightly + # container_name: sawtooth-shell + # volumes: + # - keyshare:/root + # - blocks:/var/lib/sawtooth + # - node:/project + # depends_on: + # - validator + # command: | + # bash -c " + # sawtooth keygen && + # tail -f /dev/null + # " + # stop_signal: SIGKILL - validator: - image: hyperledger/sawtooth-validator:nightly - container_name: sawtooth-validator - volumes: - - blocks:/var/lib/sawtooth - expose: - - 4004 - - 8800 - - 5050 - ports: - - "4004:4004" - # start the validator with an empty genesis batch - command: | - bash -c " - sawadm keygen - sawset genesis \ - -k /etc/sawtooth/keys/validator.priv \ - -o config-genesis.batch && \ - sawset proposal create \ - -k /etc/sawtooth/keys/validator.priv \ - sawtooth.consensus.algorithm.name=Devmode \ - sawtooth.consensus.algorithm.version=0.1 \ - -o config.batch && \ - sawadm genesis config-genesis.batch config.batch && \ - sawtooth-validator -vv \ - --endpoint tcp://validator:8800 \ - --bind component:tcp://eth0:4004 \ - --bind network:tcp://eth0:8800 \ - --bind consensus:tcp://eth0:5050 \ - " - stop_signal: SIGKILL + # validator: + # image: hyperledger/sawtooth-validator:nightly + # container_name: sawtooth-validator + # volumes: + # - blocks:/var/lib/sawtooth + # - node:/project + # expose: + # - 4004 + # - 8800 + # - 5050 + # ports: + # - "4004:4004" + # command: | + # bash -c " + # sawadm keygen + # sawset genesis \ + # -k /etc/sawtooth/keys/validator.priv \ + # -o config-genesis.batch && \ + # sawset proposal create \ + # -k /etc/sawtooth/keys/validator.priv \ + # sawtooth.consensus.algorithm.name=Devmode \ + # sawtooth.consensus.algorithm.version=0.1 \ + # -o config.batch && \ + # sawadm genesis config-genesis.batch config.batch && \ + # sawtooth-validator -vv \ + # --endpoint tcp://validator:8800 \ + # --bind component:tcp://eth0:4004 \ + # --bind network:tcp://eth0:8800 \ + # --bind consensus:tcp://eth0:5050 \ + # " + # stop_signal: SIGKILL - rest-api: - image: hyperledger/sawtooth-rest-api:nightly - container_name: sawtooth-rest-api - volumes: - - keyshare:/root - - blocks:/var/lib/sawtooth - ports: - - "8008:8008" - depends_on: - - validator - command: | - bash -c " - sawtooth-rest-api -v --connect tcp://validator:4004 --bind rest-api:8008 - " - stop_signal: SIGKILL + # rest-api: + # image: hyperledger/sawtooth-rest-api:nightly + # container_name: sawtooth-rest-api + # volumes: + # - keyshare:/root + # - blocks:/var/lib/sawtooth + # - node:/project + # ports: + # - "8008:8008" + # depends_on: + # - validator + # command: | + # bash -c " + # sawtooth-rest-api -v --connect tcp://validator:4004 --bind rest-api:8008 + # " + # stop_signal: SIGKILL - devmode-rust: - image: hyperledger/sawtooth-devmode-engine-rust:nightly - container_name: sawtooth-devmode-engine-rust + # devmode-rust: + # image: hyperledger/sawtooth-devmode-engine-rust:nightly + # container_name: sawtooth-devmode-engine-rust + # volumes: + # - keyshare:/root + # - blocks:/var/lib/sawtooth + # - node:/project + # depends_on: + # - validator + # command: | + # bash -c " + # devmode-engine-rust -v --connect tcp://validator:5050 + # " + # stop_signal: SIGKILL + + # dkr: + # image: docker + # privileged: true + # working_dir: "/dkr" + # volumes: + # - ".:/dkr" + # - "/var/run/docker.sock:/var/run/docker.sock" + # command: docker ps -a + + app: + image: harsh/djangoapp:v1 + ports: + - "8000:8000" volumes: - - keyshare:/root - - blocks:/var/lib/sawtooth - depends_on: - - validator - command: | - bash -c " - devmode-engine-rust -v --connect tcp://validator:5050 - " - stop_signal: SIGKILL + - ./app:/app + command: python3 manage.py runserver 0.0.0.0:8000 volumes: keyshare: blocks: + node: diff --git a/med_python/__init__.py b/med_python/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/med_python/sawtooth_med/__pycache__/med_client.cpython-35.pyc b/med_python/sawtooth_med/__pycache__/med_client.cpython-35.pyc index 97a5b74..ee93226 100644 Binary files a/med_python/sawtooth_med/__pycache__/med_client.cpython-35.pyc and b/med_python/sawtooth_med/__pycache__/med_client.cpython-35.pyc differ diff --git a/med_python/sawtooth_med/processor/handler.py b/med_python/sawtooth_med/processor/handler.py index 9ce0a44..d622657 100644 --- a/med_python/sawtooth_med/processor/handler.py +++ b/med_python/sawtooth_med/processor/handler.py @@ -114,10 +114,13 @@ def apply(self, transaction , context): if medicine is None: raise InvalidTransaction('Invalid action: Medicine DOES NOT exists: {}'.format(med_payload.medicineName)) - medicine.stock = med_payload.stock - med_state.set_medicine(med_payload.medicineName , medicine) + if medicine.medicineID == med_payload.medicineID: + medicine.stock = med_payload.stock + med_state.set_medicine(med_payload.medicineName , medicine) - _display('Medicine Info updated successfully by: {}'.format(signer[:6])) + _display('Medicine Info updated successfully by: {}'.format(signer[:6])) + else: + raise InvalidTransaction('Unhandled action: {}'.format(med_payload.action)) else: raise InvalidTransaction('Unhandled action: {}'.format(med_payload.action)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6b27890 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Django +djangorestframework +requests + diff --git a/transfer_python/sawtooth_transfer/processor/handler.py b/transfer_python/sawtooth_transfer/processor/handler.py index 9b221d5..da57e97 100644 --- a/transfer_python/sawtooth_transfer/processor/handler.py +++ b/transfer_python/sawtooth_transfer/processor/handler.py @@ -95,7 +95,7 @@ def apply(self, transaction, context): if transfer_state.get_shipment(transfer_payload.shipmentID) is not None: raise InvalidTransaction('Invalid action: Shipment already exists') - + shipment = Shipment( shipmentID = transfer_payload.shipmentID, logisticsID = transfer_payload.logisticsID, @@ -116,6 +116,11 @@ def apply(self, transaction, context): if shipment is None: raise InvalidTransaction('Invalid action : Shipment does not exists: {}'.format(transfer_payload.shipmentID)) + if(transfer_payload.shipmentStatus == 'DELIVERED'): + transfer_state.delete_shipment(transfer_payload.shipmentID) + _display('Shipment Deleted successfully deleted successfully by: {}'.format(signer[:6])) + + shipment.shipmentStatus = transfer_payload.shipmentStatus transfer_state.set_shipment(transfer_payload.shipmentID , shipment) _display('Logistics Company : {} updated Shipment'.format(signer[:6])) diff --git a/transfer_python/sawtooth_transfer/transfer_cli.py b/transfer_python/sawtooth_transfer/transfer_cli.py index 19ab0a1..6b60d6f 100644 --- a/transfer_python/sawtooth_transfer/transfer_cli.py +++ b/transfer_python/sawtooth_transfer/transfer_cli.py @@ -698,7 +698,11 @@ def do_list(args): ] if asset_list is not None: - print(asset_list) + print('-'*50) + for x in asset_list: + print(x) + print('x'*10) + print('-'*50) else: raise TransferException("Could not retireve List") @@ -715,9 +719,13 @@ def do_show(args): data = client.show(ID, auth_user=auth_user, auth_password=auth_password) if data is not None: - print(data) + print('-'*50) + for x in data: + print(x) + print('x'*10) + print('-'*50) else: - raise TransferException("Asset not found: {}".format(shipmentID)) + raise TransferException("Assets not found!") def main(prog_name = os.path.basename(sys.argv[0]), args = None): diff --git a/transfer_python/sawtooth_transfer/transfer_client.py b/transfer_python/sawtooth_transfer/transfer_client.py index 4581271..2451733 100644 --- a/transfer_python/sawtooth_transfer/transfer_client.py +++ b/transfer_python/sawtooth_transfer/transfer_client.py @@ -286,8 +286,8 @@ def _send_transfer_txn(self, signer_public_key=self._signer.get_public_key().as_hex(), family_name="transfer", family_version="1.0", - inputs=[addresss, addressb, address, med_address], - outputs=[addresss, addressb, address, med_address], + inputs=[addresss, addressb, address, med_address, '6d0f36e8b34cbf061ca4c58e89b40e797beefdb663b681a96e2d1ccd8f03f9b52eb4e0'], + outputs=[addresss, addressb, address, med_address, '6d0f36e8b34cbf061ca4c58e89b40e797beefdb663b681a96e2d1ccd8f03f9b52eb4e0'], dependencies=[], payload_sha512=_sha512(payload), batcher_public_key=self._signer.get_public_key().as_hex(),