mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-23 07:14:21 +00:00
✨ Add sync_flows to the admin page
This commit is contained in:
parent
f5d1c50e2c
commit
93f28b9dad
5 changed files with 75 additions and 9 deletions
|
@ -19,6 +19,17 @@ class ProjectAdmin(CoreAdmin):
|
||||||
"name",
|
"name",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ordering = (
|
||||||
|
"slug",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@admin.action(description="Syncronize DataFlows (slow, be patient!)")
|
||||||
|
def sync_flows_admin(modeladmin, request, queryset):
|
||||||
|
for datasource in queryset:
|
||||||
|
datasource: models.DataSource
|
||||||
|
datasource.sync_flows()
|
||||||
|
|
||||||
|
|
||||||
@admin.register(models.DataSource)
|
@admin.register(models.DataSource)
|
||||||
class DataSourceAdmin(CoreAdmin):
|
class DataSourceAdmin(CoreAdmin):
|
||||||
|
@ -33,6 +44,14 @@ class DataSourceAdmin(CoreAdmin):
|
||||||
"last_sync",
|
"last_sync",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ordering = (
|
||||||
|
"last_sync",
|
||||||
|
)
|
||||||
|
|
||||||
|
actions = (
|
||||||
|
sync_flows_admin,
|
||||||
|
)
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(
|
(
|
||||||
None, {
|
None, {
|
||||||
|
@ -94,5 +113,11 @@ class DataFlowAdmin(CoreAdmin):
|
||||||
|
|
||||||
list_display = (
|
list_display = (
|
||||||
"datasource",
|
"datasource",
|
||||||
"id",
|
"sdmx_id",
|
||||||
|
"description",
|
||||||
|
)
|
||||||
|
|
||||||
|
ordering = (
|
||||||
|
"datasource",
|
||||||
|
"sdmx_id",
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.2 on 2021-04-18 17:51
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('core', '0002_auto_20210415_1453'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='dataflow',
|
||||||
|
old_name='id',
|
||||||
|
new_name='sdmx_id',
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.2 on 2021-04-18 17:52
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('core', '0003_rename_id_dataflow_sdmx_id'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='dataflow',
|
||||||
|
name='surrogate_id',
|
||||||
|
field=models.BigAutoField(help_text='Internal id used by Django to identify this DataFlow.', primary_key=True, serialize=False, verbose_name='Surrogate id'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -9,6 +9,7 @@ import json
|
||||||
import abc
|
import abc
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
|
import pytz
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -278,18 +279,22 @@ class DataSource(models.Model):
|
||||||
log.info(f"Syncing DataFlows of {self!r}...")
|
log.info(f"Syncing DataFlows of {self!r}...")
|
||||||
for description, sdmx_id in zip(flows, flows.index):
|
for description, sdmx_id in zip(flows, flows.index):
|
||||||
|
|
||||||
db_flow = DataFlow.objects.update_or_create(
|
db_flow, _created = DataFlow.objects.update_or_create(
|
||||||
**{
|
**{
|
||||||
"datasource": self,
|
"datasource": self,
|
||||||
"id": sdmx_id,
|
"sdmx_id": sdmx_id,
|
||||||
},
|
},
|
||||||
defaults={
|
defaults={
|
||||||
"last_update": datetime.datetime.now(),
|
|
||||||
"description": description,
|
"description": description,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
db_flow.save()
|
db_flow.save()
|
||||||
log.info(f"Synced {db_flow}!")
|
log.debug(f"Synced {db_flow}!")
|
||||||
|
|
||||||
|
log.debug(f"Updating last_sync value of {self!r}")
|
||||||
|
self.last_sync = datetime.datetime.now()
|
||||||
|
self.save()
|
||||||
|
log.info(f"Finished syncing DataFlows of {self!r}")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.id
|
return self.id
|
||||||
|
@ -302,7 +307,7 @@ class DataFlow(models.Model):
|
||||||
See `this page <https://ec.europa.eu/eurostat/online-help/redisstat-admin/en/TECH_A_main/>`_ for more details.
|
See `this page <https://ec.europa.eu/eurostat/online-help/redisstat-admin/en/TECH_A_main/>`_ for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
surrogate_id = models.IntegerField(
|
surrogate_id = models.BigAutoField(
|
||||||
"Surrogate id",
|
"Surrogate id",
|
||||||
help_text="Internal id used by Django to identify this DataFlow.",
|
help_text="Internal id used by Django to identify this DataFlow.",
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
|
@ -314,7 +319,7 @@ class DataFlow(models.Model):
|
||||||
on_delete=models.RESTRICT,
|
on_delete=models.RESTRICT,
|
||||||
)
|
)
|
||||||
|
|
||||||
id = models.CharField(
|
sdmx_id = models.CharField(
|
||||||
"SDMX id",
|
"SDMX id",
|
||||||
help_text="Internal string used in SDMX communication to identify the DataFlow.",
|
help_text="Internal string used in SDMX communication to identify the DataFlow.",
|
||||||
max_length=64,
|
max_length=64,
|
||||||
|
@ -327,7 +332,7 @@ class DataFlow(models.Model):
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"[{self.datasource}] {self.id}"
|
return f"[{self.datasource}] {self.sdmx_id}"
|
||||||
|
|
||||||
|
|
||||||
class Project(models.Model):
|
class Project(models.Model):
|
||||||
|
|
|
@ -46,7 +46,7 @@ class DataFlowSerializer(serializers.ModelSerializer):
|
||||||
fields = [
|
fields = [
|
||||||
"surrogate_id",
|
"surrogate_id",
|
||||||
"datasource",
|
"datasource",
|
||||||
"id",
|
"sdmx_id",
|
||||||
"description",
|
"description",
|
||||||
]
|
]
|
||||||
read_only_fields = [
|
read_only_fields = [
|
||||||
|
|
Loading…
Reference in a new issue