-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'license-activation' into 'main'
License activation See merge request reportcreator/reportcreator!819
- Loading branch information
Showing
20 changed files
with
294 additions
and
107 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
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
30 changes: 30 additions & 0 deletions
30
api/src/reportcreator_api/tasks/migrations/0002_licenseactivationinfo.py
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,30 @@ | ||
# Generated by Django 5.1.4 on 2025-01-09 09:24 | ||
|
||
import reportcreator_api.utils.models | ||
import uuid | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tasks', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='LicenseActivationInfo', | ||
fields=[ | ||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), | ||
('created', models.DateTimeField(default=reportcreator_api.utils.models.now, editable=False)), | ||
('updated', models.DateTimeField(auto_now=True)), | ||
('license_type', models.CharField(choices=[('community', 'Community'), ('professional', 'Professional')], max_length=255)), | ||
('license_hash', models.CharField(max_length=255, null=True)), | ||
('last_activation_time', models.DateTimeField(blank=True, null=True)), | ||
], | ||
options={ | ||
'ordering': ['-created'], | ||
'abstract': False, | ||
}, | ||
), | ||
] |
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
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,10 +1,47 @@ | ||
import json | ||
import logging | ||
from datetime import timedelta | ||
|
||
import httpx | ||
from asgiref.sync import sync_to_async | ||
from django.core.management import call_command | ||
from django.core.serializers.json import DjangoJSONEncoder | ||
from django.utils import dateparse | ||
|
||
from reportcreator_api.tasks.models import periodic_task | ||
from reportcreator_api.tasks.models import LicenseActivationInfo, TaskStatus, periodic_task | ||
from reportcreator_api.utils import license | ||
|
||
|
||
@periodic_task(id='clear_sessions', schedule=timedelta(days=1)) | ||
def clear_sessions(task_info): | ||
call_command('clearsessions') | ||
|
||
|
||
async def activate_license_request(license_info): | ||
async with httpx.AsyncClient(timeout=10) as client: | ||
res = await client.post( | ||
url='https://portal.sysreptor.com/api/v1/licenses/activate/', | ||
headers={'Content-Type': 'application/json'}, | ||
data=json.dumps(license_info, cls=DjangoJSONEncoder), | ||
) | ||
res.raise_for_status() | ||
return res.json() | ||
|
||
|
||
@periodic_task(id='activate_license', schedule=timedelta(days=1)) | ||
async def activate_license(task_info): | ||
activation_info = await sync_to_async(LicenseActivationInfo.objects.current)() | ||
if not await license.ais_professional(): | ||
return TaskStatus.SUCCESS | ||
|
||
try: | ||
res = await activate_license_request(await license.aget_license_info()) | ||
if res.get('status') == 'ok': | ||
try: | ||
activation_info.last_activation_time = dateparse.parse_datetime(res.get('license_info', {}).get('last_activation_time')) | ||
except (TypeError, ValueError): | ||
activation_info.last_activation_time = None | ||
await activation_info.asave() | ||
except httpx.TransportError as ex: | ||
logging.warning(f'Failed to activate license: {ex}. Check your internet connection.') | ||
return TaskStatus.FAILED |
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
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
Oops, something went wrong.