-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reuse api keys and token as far as not expired
- Loading branch information
1 parent
662fffe
commit b7216ca
Showing
5 changed files
with
157 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,2 @@ | ||
[MESSAGES CONTROL] | ||
disable= | ||
locally-disabled, | ||
logging-format-interpolation, | ||
too-few-public-methods, | ||
too-many-instance-attributes, | ||
import-error | ||
[MASTER] | ||
ignore=openedx_search_api/migrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 5.0.7 on 2024-08-15 12:32 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='SearchApiKeyModel', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('uid', models.CharField(max_length=255)), | ||
('name', models.CharField(blank=True, max_length=255, null=True)), | ||
('actions', models.JSONField()), | ||
('indexes', models.JSONField()), | ||
('expires_at', models.DateTimeField()), | ||
('key', models.CharField(max_length=500)), | ||
('created_at', models.DateTimeField()), | ||
('updated_at', models.DateTimeField()), | ||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='SearchEngineToken', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('token', models.TextField()), | ||
('token_type', models.CharField(max_length=255)), | ||
('expires_at', models.DateTimeField()), | ||
('search_engine', models.CharField(max_length=255)), | ||
('index_search_rules', models.JSONField()), | ||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Define you models here | ||
""" | ||
from datetime import datetime | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.db import models | ||
|
||
User = get_user_model() | ||
|
||
|
||
class SearchEngineToken(models.Model): | ||
""" | ||
It is to store user specific token in database | ||
""" | ||
token = models.TextField() | ||
token_type = models.CharField(max_length=255) | ||
expires_at = models.DateTimeField() | ||
search_engine = models.CharField(max_length=255) | ||
index_search_rules = models.JSONField() | ||
user = models.OneToOneField(User, on_delete=models.CASCADE) | ||
|
||
objects = models.Manager() | ||
|
||
@classmethod | ||
def get_active_token(cls, user): | ||
""" | ||
Returns active token key object | ||
:param user: | ||
:return: | ||
""" | ||
return cls.objects.filter(user_id=user.id, expires_at__gt=datetime.now()).first() | ||
|
||
|
||
class SearchApiKeyModel(models.Model): | ||
""" | ||
It is to store user specific api key in database | ||
""" | ||
uid = models.CharField(max_length=255) | ||
name = models.CharField(max_length=255, blank=True, null=True) | ||
actions = models.JSONField() | ||
indexes = models.JSONField() | ||
expires_at = models.DateTimeField() | ||
key = models.CharField(max_length=500) | ||
created_at = models.DateTimeField() | ||
updated_at = models.DateTimeField() | ||
user = models.OneToOneField(User, on_delete=models.CASCADE) | ||
|
||
objects = models.Manager() | ||
|
||
@classmethod | ||
def get_active_api_key(cls, user): | ||
""" | ||
Returns active api key object | ||
:param user: | ||
:return: | ||
""" | ||
return cls.objects.filter(user_id=user.id, expires_at__gt=datetime.now()).first() |