Commit e799ec74 authored by Volkov Gherman's avatar Volkov Gherman

Create first version of docusign app

parent d69e365c
from datetime import datetime, timedelta
import jwt
from django.core.mail import send_mail
from docusign_esign import ApiClient, EnvelopesApi
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from api.serializers import DocumentNDASerializer
from core.settings import DOCUSIGN_PRIVATE_KEY_PATH, DOCUSIGN_INTEGRATION_KEY, DOCUSIGN_AUTH_SERVER, DOCUSIGN_API_SERVER
class IndexView(APIView):
......@@ -15,7 +21,77 @@ class IndexView(APIView):
def post(self, request):
serializer = DocumentNDASerializer(data=request.data)
if serializer.is_valid():
serializer.save()
# отправка в докусайн
document = serializer.save()
send_docusign_email(request, document)
return Response(status=200, data=serializer.data)
return Response({'error': 'Bad Request'}, status=400)
def gen_jwt_token(email):
with open(DOCUSIGN_PRIVATE_KEY_PATH, 'rb') as rsa_key:
private_key = rsa_key.read()
integration_key = DOCUSIGN_INTEGRATION_KEY
user_email = email
cur_time = datetime.utcnow()
exp_time = cur_time + timedelta(hours=1)
payload = {
'iss': integration_key,
'sub': user_email,
'aud': 'account-d.docusign.com',
'iat': cur_time,
'exp': exp_time,
'scope': 'signature impersonation'
}
return jwt.encode(payload, private_key, algorithm='RS256')
def send_docusign_email(request, user_data):
user = user_data
api_client = ApiClient()
api_client.host = DOCUSIGN_AUTH_SERVER
jwt_token = gen_jwt_token(user.owner_email)
api_client.set_default_header('Authorization', 'Bearer ' + jwt_token)
base_url = DOCUSIGN_API_SERVER
envelope_api = EnvelopesApi(api_client)
envelope_definition = {
'email_subject': 'Подписать документ',
'email_blurb': 'Пожалуйста, подпишите документ.',
'status': 'sent',
'recipients': {
'signers': [{
'email': user.owner_email,
'name': user.owner_name,
'recipientId': '1',
'clientUserId': user.pk,
'tabs': {
'textTabs': [{
'tabLabel': 'Name',
'value': user.owner_name
}]
}
}]
},
'documents': [{
'documentId': '1',
'name': 'document.docx',
'fileExtension': 'docx',
'documentBase64': 'BASE64_ENCODED_DOCUMENT'
}]
}
envelope_summary = envelope_api.create_envelope('1', envelope_definition=envelope_definition)
sign_url = f'{base_url}/envelopes/{envelope_summary.envelope_id}/views/recipient'
send_mail(
'Подписать документ',
f'Пожалуйста, подпишите документ по ссылке: {sign_url}',
'отправитель@example.com',
[user.email],
fail_silently=False,
)
return {'status': 'success'}
......@@ -14,12 +14,12 @@ import os
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
......@@ -31,7 +31,6 @@ DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
......@@ -83,7 +82,6 @@ REST_FRAMEWORK = {
WSGI_APPLICATION = 'core.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
......@@ -98,7 +96,6 @@ DATABASES = {
},
}
# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
......@@ -123,7 +120,6 @@ CORS_ORIGIN_WHITELIST = [
'http://127.0.0.1:8000/api/v1/'
]
# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/
......@@ -135,7 +131,6 @@ USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
......@@ -148,3 +143,9 @@ MEDIA_URL = '/demo/'
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DOCUSIGN_INTEGRATION_KEY = os.getenv('DOCUSIGN_INTEGRATION_KEY')
DOCUSIGN_PRIVATE_KEY_PATH = os.path.join(BASE_DIR, 'docusign_test_app-python/app/private.key')
print(DOCUSIGN_PRIVATE_KEY_PATH)
DOCUSIGN_AUTH_SERVER = 'https://account-d.docusign.com'
DOCUSIGN_API_SERVER = 'https://api-d.docusign.com'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment