diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d2d22f --- /dev/null +++ b/.gitignore @@ -0,0 +1,164 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +env + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +dropped.py +req.txt diff --git a/Config/__init__.py b/Config/__init__.py new file mode 100644 index 0000000..cd04264 --- /dev/null +++ b/Config/__init__.py @@ -0,0 +1,3 @@ +from .celery import app as celery_app + +__all__ = ['celery_app'] diff --git a/Config/asgi.py b/Config/asgi.py new file mode 100644 index 0000000..dfa0ff9 --- /dev/null +++ b/Config/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for Config 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/4.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Config.settings') + +application = get_asgi_application() diff --git a/Config/celery.py b/Config/celery.py new file mode 100644 index 0000000..4d6f81e --- /dev/null +++ b/Config/celery.py @@ -0,0 +1,22 @@ +import os + +from celery import Celery + +# Set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Config.settings') + +app = Celery('Config') + +# Using a string here means the worker doesn't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object('django.conf:settings', namespace='CELERY') + +# Load task modules from all registered Django apps. +app.autodiscover_tasks() + + +@app.task(bind=True) +def debug_task(self): + print(f'Request: {self.request!r}') \ No newline at end of file diff --git a/Config/settings.py b/Config/settings.py new file mode 100644 index 0000000..d0633e4 --- /dev/null +++ b/Config/settings.py @@ -0,0 +1,206 @@ +""" +Django settings for Config project. + +Generated by 'django-admin startproject' using Django 4.2.1. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.2/ref/settings/ +""" + +from pathlib import Path +from datetime import timedelta + +# 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/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-5urf$zue#=!d5er1k1h!oyjg()shna+2hu))%a8#!a7z!qv+9)' + +# Fernet key used for encrypting user secrects +ENCRYPTION_KEY = b'8TpVNaD5OCy01Fz3gpZ3k9m2jhl51kMhHCn7ARCEiF4=' + +# 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', + + # Third-party apps + 'rest_framework', + + # Apps + 'users.apps.UsersConfig', + 'readers.apps.ReadersConfig', +] + +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 = 'Config.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 = 'Config.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.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/4.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +# Custom user model +AUTH_USER_MODEL = "users.User" + +# Rest framework +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework_simplejwt.authentication.JWTAuthentication', + ) +} + +# Simple-JWT +SIMPLE_JWT = { + "ACCESS_TOKEN_LIFETIME": timedelta(days=30), + "REFRESH_TOKEN_LIFETIME": timedelta(days=30), + # "ROTATE_REFRESH_TOKENS": False, + # "BLACKLIST_AFTER_ROTATION": False, + # "UPDATE_LAST_LOGIN": False, + + "ALGORITHM": "HS256", + "SIGNING_KEY": SECRET_KEY, + # "VERIFYING_KEY": "", + # "AUDIENCE": None, + # "ISSUER": None, + # "JSON_ENCODER": None, + # "JWK_URL": None, + # "LEEWAY": 0, + + "AUTH_HEADER_TYPES": ("JWT",), + "AUTH_HEADER_NAME": "HTTP_AUTHORIZATION", + # "USER_ID_FIELD": "id", + # "USER_ID_CLAIM": "user_id", + # "USER_AUTHENTICATION_RULE": "rest_framework_simplejwt.authentication.default_user_authentication_rule", + + # "AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",), + # "TOKEN_TYPE_CLAIM": "token_type", + # "TOKEN_USER_CLASS": "rest_framework_simplejwt.models.TokenUser", + + # "JTI_CLAIM": "jti", + + # "SLIDING_TOKEN_REFRESH_EXP_CLAIM": "refresh_exp", + # "SLIDING_TOKEN_LIFETIME": timedelta(minutes=5), + # "SLIDING_TOKEN_REFRESH_LIFETIME": timedelta(days=1), + + # "TOKEN_OBTAIN_SERIALIZER": "rest_framework_simplejwt.serializers.TokenObtainPairSerializer", + # "TOKEN_REFRESH_SERIALIZER": "rest_framework_simplejwt.serializers.TokenRefreshSerializer", + # "TOKEN_VERIFY_SERIALIZER": "rest_framework_simplejwt.serializers.TokenVerifySerializer", + # "TOKEN_BLACKLIST_SERIALIZER": "rest_framework_simplejwt.serializers.TokenBlacklistSerializer", + # "SLIDING_TOKEN_OBTAIN_SERIALIZER": "rest_framework_simplejwt.serializers.TokenObtainSlidingSerializer", + # "SLIDING_TOKEN_REFRESH_SERIALIZER": "rest_framework_simplejwt.serializers.TokenRefreshSlidingSerializer", +} + +# Celery configs +CELERY_BROKER_URL = 'redis://localhost:6379' +CELERY_RESULT_BACKEND = 'redis://localhost:6379' +CELERY_ACCEPT_CONTENT = ['application/json'] +CELERY_TASK_SERIALIZER = 'json' +CELERY_ENABLE_UTC = True +CELERY_IMPORTS = [ + 'readers.tasks', +] + +# Celery settings +CELERY_BEAT_SCHEDULE = { + 'every-30-sec': { + 'task': 'readers.tasks.cache_positions_all_users', + 'schedule': 30.0 + } +} + +# Thread pool number for celery worker threads +THREAD_POOL = 4 \ No newline at end of file diff --git a/Config/urls.py b/Config/urls.py new file mode 100644 index 0000000..8b0dd85 --- /dev/null +++ b/Config/urls.py @@ -0,0 +1,24 @@ +""" +URL configuration for Config project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.2/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, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/user/', include('users.api.urls')), # User actions + path('api/', include('readers.api.urls')), # Reader actions +] diff --git a/Config/wsgi.py b/Config/wsgi.py new file mode 100644 index 0000000..a5e7fc9 --- /dev/null +++ b/Config/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for Config 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/4.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Config.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..3051634 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Config.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/readers/__init__.py b/readers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/readers/admin.py b/readers/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/readers/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/readers/api/router.py b/readers/api/router.py new file mode 100644 index 0000000..a0c948a --- /dev/null +++ b/readers/api/router.py @@ -0,0 +1,5 @@ +from rest_framework import routers +from .views import AccountViewSet + +router = routers.SimpleRouter() +router.register('positions', AccountViewSet, basename='api-positions') \ No newline at end of file diff --git a/readers/api/serializers.py b/readers/api/serializers.py new file mode 100644 index 0000000..b564cab --- /dev/null +++ b/readers/api/serializers.py @@ -0,0 +1,14 @@ +from rest_framework import serializers +from readers.models import Account + +class AccountSerializer(serializers.ModelSerializer): + class Meta: + model = Account + fields = [ + 'id', + 'currency', + 'type', + 'balance', + 'available', + 'holds' + ] \ No newline at end of file diff --git a/readers/api/urls.py b/readers/api/urls.py new file mode 100644 index 0000000..e132236 --- /dev/null +++ b/readers/api/urls.py @@ -0,0 +1,5 @@ +from django.urls import path, include +from .router import router + +urlpatterns = [] +urlpatterns += router.urls \ No newline at end of file diff --git a/readers/api/views.py b/readers/api/views.py new file mode 100644 index 0000000..aa76330 --- /dev/null +++ b/readers/api/views.py @@ -0,0 +1,26 @@ +from rest_framework import viewsets, permissions, mixins +from rest_framework.response import Response +from readers.models import Account +from .serializers import AccountSerializer +from utils.cache import read_positions_from_cache + +class AccountViewSet(viewsets.GenericViewSet, mixins.ListModelMixin): + serializer_class = AccountSerializer + permission_classes = [permissions.IsAuthenticated,] + + def list(self, request, *args, **kwargs): + """ + List of request.user positions. + This endpoint tries to populate AccountSerializer first using with redis cache. + If redis service was down, it will populate serializer from DB. + """ + accounts = read_positions_from_cache(user=request.user) + if accounts == 'DB': + qs = request.user.accounts.all() + serializer = self.get_serializer(qs, many=True) + return Response(serializer.data) + qs = [] + for account in accounts: + qs.append(Account(**account)) + serializer = self.get_serializer(qs, many=True) + return Response(serializer.data) diff --git a/readers/apps.py b/readers/apps.py new file mode 100644 index 0000000..f0e93f5 --- /dev/null +++ b/readers/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ReadersConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'readers' diff --git a/readers/migrations/0001_initial.py b/readers/migrations/0001_initial.py new file mode 100644 index 0000000..168531d --- /dev/null +++ b/readers/migrations/0001_initial.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2.1 on 2023-05-16 10:16 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Account', + fields=[ + ('id', models.CharField(max_length=512)), + ('currency', models.CharField(max_length=10)), + ('type', models.CharField(max_length=6)), + ('balance', models.DecimalField(decimal_places=6, max_digits=20)), + ('available', models.DecimalField(decimal_places=6, max_digits=20)), + ('holds', models.DecimalField(decimal_places=6, max_digits=20)), + ('id_field', models.AutoField(default=79, primary_key=True, serialize=False)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='accounts', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'unique_together': {('user', 'id')}, + }, + ), + ] diff --git a/readers/migrations/0002_alter_account_id_field.py b/readers/migrations/0002_alter_account_id_field.py new file mode 100644 index 0000000..e6a1fde --- /dev/null +++ b/readers/migrations/0002_alter_account_id_field.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.1 on 2023-05-16 10:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('readers', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='id_field', + field=models.AutoField(primary_key=True, serialize=False), + ), + ] diff --git a/readers/migrations/0003_alter_account_id.py b/readers/migrations/0003_alter_account_id.py new file mode 100644 index 0000000..4bcf60a --- /dev/null +++ b/readers/migrations/0003_alter_account_id.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.1 on 2023-05-17 08:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('readers', '0002_alter_account_id_field'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='id', + field=models.CharField(max_length=512, unique=True), + ), + ] diff --git a/readers/migrations/__init__.py b/readers/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/readers/models.py b/readers/models.py new file mode 100644 index 0000000..17495e2 --- /dev/null +++ b/readers/models.py @@ -0,0 +1,21 @@ +from django.db import models +from django.contrib.auth import get_user_model +import random +User = get_user_model() + +class Account(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='accounts') + id = models.CharField(max_length=512, null=False, blank=False, unique=True) + currency = models.CharField(max_length=10, null=False, blank=False) + type = models.CharField(max_length=6, null=False, blank=False) + balance = models.DecimalField(max_digits=20, decimal_places=6) + available = models.DecimalField(max_digits=20, decimal_places=6) + holds = models.DecimalField(max_digits=20, decimal_places=6) + + id_field = models.AutoField(primary_key=True) + + class Meta: + unique_together = ('user', 'id',) + + def __str__(self) -> str: + return f'{self.id} - user:{self.user.id}' \ No newline at end of file diff --git a/readers/tasks.py b/readers/tasks.py new file mode 100644 index 0000000..664e50a --- /dev/null +++ b/readers/tasks.py @@ -0,0 +1,22 @@ +from celery.utils.log import get_task_logger +from celery import shared_task +from django.contrib.auth import get_user_model +from multiprocessing.dummy import Pool as ThreadPool +from django.conf import settings + +from utils.cache import write_positions_in_cache + +logger = get_task_logger(__name__) +User = get_user_model() + +@shared_task +def cache_positions_all_users(): + """ + Caches all Users positions in redis if service is active. + Function implements multi-thread processing to boost speed of caching process. + """ + users = User.objects.all() + pool = ThreadPool(settings.THREAD_POOL) + params = list(users) + results = pool.map(write_positions_in_cache, params) + \ No newline at end of file diff --git a/readers/tests.py b/readers/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/readers/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/readers/views.py b/readers/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/readers/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7ddf8c0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +django==4.2.1 +djangorestframework==3.14.0 +kucoin-python +djangorestframework-simplejwt +django-filter +cryptography +celery==4.4.2 diff --git a/users/__init__.py b/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/users/admin.py b/users/admin.py new file mode 100644 index 0000000..bca5cc5 --- /dev/null +++ b/users/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from django.contrib.auth import get_user_model + +User = get_user_model() +admin.site.register(User) diff --git a/users/api/routers.py b/users/api/routers.py new file mode 100644 index 0000000..e3188f3 --- /dev/null +++ b/users/api/routers.py @@ -0,0 +1,6 @@ +from rest_framework import routers +from .views import UserCreateView, UserGetView + +router = routers.SimpleRouter() +router.register('signup', UserCreateView, basename='api-user-signup') +router.register('', UserGetView, basename='api-account') \ No newline at end of file diff --git a/users/api/serializers.py b/users/api/serializers.py new file mode 100644 index 0000000..baefd42 --- /dev/null +++ b/users/api/serializers.py @@ -0,0 +1,45 @@ +from rest_framework import serializers +from django.contrib.auth.hashers import make_password +from django.contrib.auth import get_user_model + +from utils.encryption import encrypt, decrypt + +User = get_user_model() + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = [ + 'id', + 'username', + 'first_name', + 'last_name', + 'api_key', + 'api_secret', + 'api_passphrase', + 'password', + ] + extra_kwargs = { + 'password': {'write_only': True}, + 'id': {'read_only': True}, + } + + def create(self, validated_data): + """ + Hash password and encrypt api details for safety. + """ + validated_data['password'] = make_password(validated_data['password']) + validated_data['api_key'] = encrypt(validated_data['api_key']) + validated_data['api_secret'] = encrypt(validated_data['api_secret']) + validated_data['api_passphrase'] = encrypt(validated_data['api_passphrase']) + return User.objects.create(**validated_data) + + def to_representation(self, instance): + """ + Decrypt api details to show to user. + """ + ret = super().to_representation(instance) + ret['api_key'] = decrypt(ret['api_key']) + ret['api_secret'] = decrypt(ret['api_secret']) + ret['api_passphrase'] = decrypt(ret['api_passphrase']) + return ret diff --git a/users/api/urls.py b/users/api/urls.py new file mode 100644 index 0000000..e13e1b1 --- /dev/null +++ b/users/api/urls.py @@ -0,0 +1,14 @@ +from django.urls import path, include +from rest_framework_simplejwt.views import ( + TokenObtainPairView, + TokenRefreshView, +) +from .routers import router + + +urlpatterns = [ + path('login/token/', TokenObtainPairView.as_view(), name='api-token_obtain'), + path('login/token/refresh/', TokenRefreshView.as_view(), name='api-token_refresh'), +] + +urlpatterns += router.urls \ No newline at end of file diff --git a/users/api/views.py b/users/api/views.py new file mode 100644 index 0000000..4ccae74 --- /dev/null +++ b/users/api/views.py @@ -0,0 +1,22 @@ +from rest_framework import mixins, viewsets, permissions +from django.contrib.auth import get_user_model + +from .serializers import UserSerializer + +User = get_user_model() + +class UserCreateView(viewsets.GenericViewSet, mixins.CreateModelMixin): + serializer_class = UserSerializer + authentication_classes = [] + permission_classes = [] + queryset = User.objects.all() + +class UserGetView(viewsets.GenericViewSet, mixins.RetrieveModelMixin): + serializer_class = UserSerializer + permission_classes = [permissions.IsAuthenticated,] + + def get_queryset(self): + if self.request.user.is_staff: + return User.objects.all() + else: + return User.objects.filter(pk=self.request.user.id) \ No newline at end of file diff --git a/users/apps.py b/users/apps.py new file mode 100644 index 0000000..72b1401 --- /dev/null +++ b/users/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'users' diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py new file mode 100644 index 0000000..d4b98f1 --- /dev/null +++ b/users/migrations/0001_initial.py @@ -0,0 +1,47 @@ +# Generated by Django 4.2.1 on 2023-05-13 19:12 + +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), + ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), + ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), + ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('api_key', models.CharField(max_length=255)), + ('api_secret', models.CharField(max_length=255)), + ('api_passphrase', models.CharField(max_length=255)), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), + ], + options={ + 'verbose_name': 'user', + 'verbose_name_plural': 'users', + 'abstract': False, + }, + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/users/migrations/0002_alter_user_api_key_alter_user_api_passphrase_and_more.py b/users/migrations/0002_alter_user_api_key_alter_user_api_passphrase_and_more.py new file mode 100644 index 0000000..d5e598d --- /dev/null +++ b/users/migrations/0002_alter_user_api_key_alter_user_api_passphrase_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.1 on 2023-05-14 12:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='api_key', + field=models.CharField(max_length=512), + ), + migrations.AlterField( + model_name='user', + name='api_passphrase', + field=models.CharField(max_length=512), + ), + migrations.AlterField( + model_name='user', + name='api_secret', + field=models.CharField(max_length=512), + ), + ] diff --git a/users/migrations/__init__.py b/users/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/users/models.py b/users/models.py new file mode 100644 index 0000000..a69a476 --- /dev/null +++ b/users/models.py @@ -0,0 +1,11 @@ +from django.db import models + +from django.contrib.auth.models import AbstractUser + +class User(AbstractUser): + api_key = models.CharField(max_length=512, null=False, blank=False) + api_secret = models.CharField(max_length=512, null=False, blank=False) + api_passphrase = models.CharField(max_length=512, null=False, blank=False) + + def __str__(self) -> str: + return f'{self.id}. {self.first_name} {self.last_name}' diff --git a/users/tests.py b/users/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/users/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/users/views.py b/users/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/users/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/cache.py b/utils/cache.py new file mode 100644 index 0000000..0b3c69c --- /dev/null +++ b/utils/cache.py @@ -0,0 +1,60 @@ +from readers.models import Account +from django.contrib.auth import get_user_model + +import redis +import pickle + +from utils.request import get_account_for_user + +User = get_user_model() + +def write_positions_in_cache(user:User) -> None: + """ + Handles caching positions in redis + If redis was down it will call another function to store data in DB. + params: + user : user + """ + account_list = get_account_for_user(user) + try: + client = redis.Redis('localhost', port=6379, db=0) + client.set(f'user_{user.id}', pickle.dumps(account_list)) + except redis.exceptions.ConnectionError: # Rides down store in DB + write_to_db(user, account_list) + +def read_positions_from_cache(user:User) -> list: + """ + Reads all positions in account of user + params: + - user : user + """ + try: + client = redis.Redis('localhost', port=6379, db=0) + account_list = client.get(f'user_{user.id}') # returns pickled + if account_list: + return pickle.loads(account_list) + else: + return list() + except redis.exceptions.ConnectionError: # Rides down store in DB + return 'DB' # View handles reading from DB if necessary + + +def write_to_db(user:User, account_list:list) -> None: + """ + Function to replace storing positions in DB + params: + user : user + account_list : list of position (returned by Kucoin) + """ + bulk_accounts = [] + for item in account_list: + print(item) + account = Account(**item) + account.user = user + bulk_accounts.append(account) + # Won't work on Oracle and SQLite < 3.24 because of update_conflicts feature + Account.objects.bulk_create( + bulk_accounts, update_conflicts=True, + update_fields=['type', 'balance', 'available', 'holds'], + unique_fields=['id'] + ) diff --git a/utils/encryption.py b/utils/encryption.py new file mode 100644 index 0000000..7eda583 --- /dev/null +++ b/utils/encryption.py @@ -0,0 +1,47 @@ +from typing import Optional +from django.conf import settings +from cryptography.fernet import Fernet +import base64 +import logging +import traceback + +# get the key from settings +f = Fernet(settings.ENCRYPTION_KEY) + +def encrypt(raw_txt:str) -> Optional[str]: + """ + Function to encrypt raw text using Fernet class + params: + - raw_txt : raw string to be ecrypted + """ + try: + # convert integer etc to string first + txt = str(raw_txt) + #input should be byte, so convert the text to byte + encrypted_text = f.encrypt(txt.encode('ascii')) + # encode to urlsafe base64 format + encrypted_text = base64.urlsafe_b64encode(encrypted_text).decode("ascii") + return encrypted_text + except Exception as e: + # log the error if any + logging.getLogger("error_logger").error(traceback.format_exc()) + return None + + +def decrypt(encrypted_txt:str) -> Optional[str]: + """ + Function to decrypt encrypted text using Fernet class + params: + - encrypted_txt : ecrypted text to be decrypted + """ + try: + # base64 decode + txt = base64.urlsafe_b64decode(encrypted_txt) + # decryption + decoded_text = f.decrypt(txt).decode("ascii") + return decoded_text + except Exception as e: + # log the error + logging.getLogger("error_logger").error(traceback.format_exc()) + return None + diff --git a/utils/request.py b/utils/request.py new file mode 100644 index 0000000..be5ce53 --- /dev/null +++ b/utils/request.py @@ -0,0 +1,20 @@ +from kucoin.client import User as KuUser +from django.contrib.auth import get_user_model + +from .encryption import decrypt + +User = get_user_model() + +def get_account_for_user(user:User) -> list: + """ + Function to handle Kucoin's account requests + params: + - user : user + """ + client = KuUser(decrypt(user.api_key), decrypt(user.api_secret), decrypt(user.api_passphrase)) + # Request for accounts using Kucoin SDK + account_list = client.get_account_list() + # Transaction success + if isinstance(account_list, list): + return account_list + return []