add redis cache, Acquisition & django-esi
This commit is contained in:
@@ -5,6 +5,7 @@ ESI_USER_AGENT=
|
|||||||
|
|
||||||
REDIS_URL=
|
REDIS_URL=
|
||||||
REDIS_PORT=
|
REDIS_PORT=
|
||||||
|
REDIS_DB=
|
||||||
REDIS_PASSWORD=
|
REDIS_PASSWORD=
|
||||||
|
|
||||||
SQLITE_DB_PATH=
|
SQLITE_DB_PATH=
|
||||||
|
|||||||
31
api/migrations/0001_initial.py
Normal file
31
api/migrations/0001_initial.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Generated by Django 4.2.6 on 2023-10-29 11:10
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('sde', '0001_initial'),
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Acquisition',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('quantity', models.IntegerField()),
|
||||||
|
('remaining', models.IntegerField()),
|
||||||
|
('price', models.FloatField()),
|
||||||
|
('date', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('source', models.CharField(max_length=255)),
|
||||||
|
('type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='acquisitions', to='sde.sdetype')),
|
||||||
|
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='acquisitions', to=settings.AUTH_USER_MODEL)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -1,2 +1,15 @@
|
|||||||
|
from django.db import models
|
||||||
|
from sde.models import SDEType
|
||||||
|
|
||||||
# models
|
|
||||||
|
class Acquisition(models.Model):
|
||||||
|
type = models.ForeignKey(SDEType, related_name="acquisitions", on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
quantity = models.IntegerField()
|
||||||
|
remaining = models.IntegerField()
|
||||||
|
|
||||||
|
price = models.FloatField()
|
||||||
|
date = models.DateTimeField(auto_now_add=True)
|
||||||
|
source = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
user = models.ForeignKey("auth.User", related_name="acquisitions", on_delete=models.CASCADE)
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth import models as auth_models
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from sde.models import SDEType
|
from sde.models import SDEType
|
||||||
|
from api import models
|
||||||
|
|
||||||
|
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = auth_models.User
|
||||||
fields = ['url', 'username', 'email', 'groups']
|
fields = ['id', 'username', 'email', 'groups']
|
||||||
|
|
||||||
|
|
||||||
class GroupSerializer(serializers.ModelSerializer):
|
class GroupSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Group
|
model = auth_models.Group
|
||||||
fields = ['url', 'name']
|
fields = ['id', 'name']
|
||||||
|
|
||||||
|
|
||||||
|
class AcquisitionSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = models.Acquisition
|
||||||
|
fields = '__all__'
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from rest_framework import routers
|
|||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
router.register(r'users', views.UserViewSet)
|
router.register(r'users', views.UserViewSet)
|
||||||
router.register(r'groups', views.GroupViewSet)
|
router.register(r'groups', views.GroupViewSet)
|
||||||
|
router.register(r'acquisitions', views.AcquisitionViewSet)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include(router.urls)),
|
path('', include(router.urls)),
|
||||||
|
|||||||
28
api/views.py
28
api/views.py
@@ -1,21 +1,20 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.contrib.auth.models import User, Group
|
from django.http import JsonResponse
|
||||||
|
from django.db.models import Q
|
||||||
|
from django.contrib.auth import models as auth_models
|
||||||
from rest_framework import viewsets, permissions, settings
|
from rest_framework import viewsets, permissions, settings
|
||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from api import serializers
|
|
||||||
from sde import serializers as sde_serializers
|
|
||||||
from django.http import JsonResponse
|
|
||||||
from django.db.models import Q
|
|
||||||
|
|
||||||
from sde.models import SDEType
|
from api import serializers, models
|
||||||
|
from sde import serializers as sde_serializers, models as sde_models
|
||||||
|
|
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
"""
|
"""
|
||||||
API endpoint that allows users to be viewed or edited.
|
API endpoint that allows users to be viewed or edited.
|
||||||
"""
|
"""
|
||||||
queryset = User.objects.all().order_by('-date_joined')
|
queryset = auth_models.User.objects.all().order_by('-date_joined')
|
||||||
serializer_class = serializers.UserSerializer
|
serializer_class = serializers.UserSerializer
|
||||||
permission_classes = [permissions.IsAuthenticated]
|
permission_classes = [permissions.IsAuthenticated]
|
||||||
|
|
||||||
@@ -24,11 +23,20 @@ class GroupViewSet(viewsets.ModelViewSet):
|
|||||||
"""
|
"""
|
||||||
API endpoint that allows groups to be viewed or edited.
|
API endpoint that allows groups to be viewed or edited.
|
||||||
"""
|
"""
|
||||||
queryset = Group.objects.all()
|
queryset = auth_models.Group.objects.all()
|
||||||
serializer_class = serializers.GroupSerializer
|
serializer_class = serializers.GroupSerializer
|
||||||
permission_classes = [permissions.IsAuthenticated]
|
permission_classes = [permissions.IsAuthenticated]
|
||||||
|
|
||||||
|
|
||||||
|
class AcquisitionViewSet(viewsets.ModelViewSet):
|
||||||
|
"""
|
||||||
|
API endpoint that allows acquisitions to be viewed or edited.
|
||||||
|
"""
|
||||||
|
queryset = models.Acquisition.objects.all().order_by('-date')
|
||||||
|
serializer_class = serializers.AcquisitionSerializer
|
||||||
|
permission_classes = [permissions.IsAuthenticated]
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
def custom_types_search(request):
|
def custom_types_search(request):
|
||||||
items = []
|
items = []
|
||||||
@@ -51,7 +59,7 @@ def custom_types_search(request):
|
|||||||
if "not" in mods:
|
if "not" in mods:
|
||||||
condition = ~condition
|
condition = ~condition
|
||||||
|
|
||||||
items.extend(SDEType.objects.filter(condition))
|
items.extend(sde_models.SDEType.objects.filter(condition))
|
||||||
|
|
||||||
paginator = settings.api_settings.DEFAULT_PAGINATION_CLASS()
|
paginator = settings.api_settings.DEFAULT_PAGINATION_CLASS()
|
||||||
result_page = paginator.paginate_queryset(items, request)
|
result_page = paginator.paginate_queryset(items, request)
|
||||||
@@ -69,7 +77,7 @@ def reprocess_eval(request):
|
|||||||
|
|
||||||
item_reprocess = []
|
item_reprocess = []
|
||||||
for rawitem in ep_items["items"]:
|
for rawitem in ep_items["items"]:
|
||||||
item = SDEType.objects.get(id=rawitem["typeID"])
|
item = sde_models.SDEType.objects.get(id=rawitem["typeID"])
|
||||||
buy_reprocess = sell_reprocess = 0.0
|
buy_reprocess = sell_reprocess = 0.0
|
||||||
for mat in item.typematerials.all():
|
for mat in item.typematerials.all():
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
import datetime
|
|
||||||
import os
|
|
||||||
import redis
|
|
||||||
import pickle
|
|
||||||
from esy.client import ESIClient
|
|
||||||
from esy.auth import ESIAuthenticator
|
|
||||||
|
|
||||||
|
|
||||||
class ESICache(object):
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
self._r = redis.Redis(**kwargs)
|
|
||||||
# self._r = redis.StrictRedis(host=redis_url, port=redis_port, db=db)
|
|
||||||
|
|
||||||
def get(self, key):
|
|
||||||
# return pickle.loads(self._r[key])
|
|
||||||
return pickle.loads(self._r.get(key))
|
|
||||||
|
|
||||||
def set(self, key, data, cached_until: datetime.datetime):
|
|
||||||
self._r.set(key, pickle.dumps(data), ex=cached_until - datetime.datetime.now(datetime.timezone.utc))
|
|
||||||
|
|
||||||
def __contains__(self, item):
|
|
||||||
# return item in self._r
|
|
||||||
return self._r.exists(item)
|
|
||||||
|
|
||||||
|
|
||||||
esi_client_id = os.getenv('ESI_CLIENT_ID')
|
|
||||||
esi_secret_key = os.getenv('ESI_SECRET_KEY')
|
|
||||||
|
|
||||||
esi_cache = ESICache(host=os.getenv("REDIS_URL"), port=int(os.getenv("REDIS_PORT")), db="0",
|
|
||||||
password=os.getenv("REDIS_PASSWD"))
|
|
||||||
|
|
||||||
esi_client = ESIClient.get_client(user_agent=os.getenv('ESI_USER_AGENT'), cache=esi_cache)
|
|
||||||
esi_auth = ESIAuthenticator()
|
|
||||||
@@ -46,6 +46,7 @@ REST_FRAMEWORK = {
|
|||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'api',
|
'api',
|
||||||
'sde',
|
'sde',
|
||||||
|
'esi',
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
@@ -87,6 +88,13 @@ TEMPLATES = [
|
|||||||
WSGI_APPLICATION = 'mabras.wsgi.application'
|
WSGI_APPLICATION = 'mabras.wsgi.application'
|
||||||
|
|
||||||
|
|
||||||
|
CACHES = {
|
||||||
|
"default": {
|
||||||
|
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
||||||
|
"LOCATION": f"redis://{os.getenv('REDIS_URL')}:{os.getenv('REDIS_PORT')}/{os.getenv('REDIS_DB')}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
|
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
|
||||||
|
|
||||||
@@ -142,3 +150,9 @@ STATIC_URL = 'static/'
|
|||||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
|
ESI_SSO_CLIENT_ID = os.getenv("ESI_CLIENT_ID")
|
||||||
|
ESI_SSO_CLIENT_SECRET = os.getenv("ESI_SECRET_KEY")
|
||||||
|
ESI_SSO_CALLBACK_URL = os.getenv("ESI_CALLBACK_URL")
|
||||||
|
ESI_USER_AGENT = os.getenv("ESI_USER_AGENT")
|
||||||
|
ESI_USER_CONTACT_EMAIL = os.getenv("ESI_USER_AGENT")
|
||||||
|
|||||||
@@ -17,14 +17,13 @@ Including another URLconf
|
|||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
from rest_framework.schemas import get_schema_view
|
from rest_framework.schemas import get_schema_view
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
from sde.urls import urlpatterns as sde_urls
|
|
||||||
from api.urls import urlpatterns as api_urls
|
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/', include(api_urls)),
|
path('api/', include("api.urls")),
|
||||||
path('sde/', include(sde_urls)),
|
path('sde/', include("sde.urls")),
|
||||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||||
|
path('sso/', include('esi.urls', namespace='esi')),
|
||||||
path('openapi/', get_schema_view(
|
path('openapi/', get_schema_view(
|
||||||
title="Mabras",
|
title="Mabras",
|
||||||
description="API for EvEal",
|
description="API for EvEal",
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ markdown
|
|||||||
uvicorn[standard]
|
uvicorn[standard]
|
||||||
psycopg[binary]
|
psycopg[binary]
|
||||||
redis
|
redis
|
||||||
esy
|
|
||||||
pyyaml
|
pyyaml
|
||||||
uritemplate
|
uritemplate
|
||||||
|
django-esi
|
||||||
|
|||||||
Reference in New Issue
Block a user