Skip to content

Commit

Permalink
Muevo admins custom a un módulo aparte
Browse files Browse the repository at this point in the history
Overridea defaults de django_datajsonar para modelos Field y Distribution, particularmente
acciones custom relacionadas a la API de series (búsqueda e indexacion de datos)
  • Loading branch information
lucaslavandeira committed Dec 27, 2018
1 parent eaf2c96 commit 9dd6c8b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
1 change: 1 addition & 0 deletions conf/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def export_vars(_):
'series_tiempo_ar_api.apps.metadata.apps.MetadataConfig',
'series_tiempo_ar_api.apps.dump',
'series_tiempo_ar_api.libs.indexing',
'series_tiempo_ar_api.libs.custom_admins',
)

INSTALLED_APPS = DJANGO_BASE_APPS + VENDOR_APPS + APPS
Expand Down
43 changes: 1 addition & 42 deletions series_tiempo_ar_api/apps/metadata/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

from django.contrib import admin
from django.contrib import messages
from django_datajsonar.admin import FieldAdmin, DistributionAdmin
from django_datajsonar.models import Field, Distribution
from django_datajsonar.models import Field

from series_tiempo_ar_api.libs.singleton_admin import SingletonAdmin
from .models import IndexMetadataTask, CatalogAlias, Synonym, MetadataConfig
from .indexer.metadata_indexer import run_metadata_indexer
from .utils import delete_metadata


@admin.register(IndexMetadataTask)
Expand Down Expand Up @@ -45,45 +43,6 @@ class SynonymAdmin(admin.ModelAdmin):
list_display = ('terms', )


admin.site.unregister(Field)


@admin.register(Field)
class CustomFieldAdmin(FieldAdmin):
actions = ('delete_model',)

def get_actions(self, request):
# Borro la acción de borrado default
actions = super(CustomFieldAdmin, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions

def delete_model(self, _, queryset):
delete_metadata(list(queryset))
queryset.delete()


admin.site.unregister(Distribution)


@admin.register(Distribution)
class CustomDistributionAdmin(DistributionAdmin):
actions = ('delete_model',)

def get_actions(self, request):
# Borro la acción de borrado default
actions = super(CustomDistributionAdmin, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions

def delete_model(self, _, queryset):
fields = Field.objects.filter(distribution__identifier__in=queryset.values_list('identifier', flat=True))
delete_metadata(list(fields))
queryset.delete()


@admin.register(MetadataConfig)
class MetadataConfigAdmin(SingletonAdmin):
pass
Empty file.
48 changes: 48 additions & 0 deletions series_tiempo_ar_api/libs/custom_admins/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.contrib import admin

from django_datajsonar.admin import FieldAdmin, DistributionAdmin
from django_datajsonar.models import Field, Distribution

from series_tiempo_ar_api.apps.metadata.utils import delete_metadata
from .tasks import reindex_distribution

admin.site.unregister(Field)
admin.site.unregister(Distribution)


@admin.register(Field)
class CustomFieldAdmin(FieldAdmin):
actions = ('delete_model',)

def get_actions(self, request):
# Borro la acción de borrado default
actions = super(CustomFieldAdmin, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions

def delete_model(self, _, queryset):
delete_metadata(list(queryset))
queryset.delete()


@admin.register(Distribution)
class CustomDistributionAdmin(DistributionAdmin):
actions = ('delete_model', 'reindex')

def get_actions(self, request):
# Borro la acción de borrado default
actions = super(CustomDistributionAdmin, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions

def delete_model(self, _, queryset):
fields = Field.objects.filter(distribution__identifier__in=queryset.values_list('identifier', flat=True))
delete_metadata(list(fields))
queryset.delete()

def reindex(self, _, queryset):
for distribution in queryset:
reindex_distribution.delay(distribution)
reindex.short_description = "Refrescar datos"
10 changes: 10 additions & 0 deletions series_tiempo_ar_api/libs/custom_admins/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.conf import settings
from django_datajsonar.models import Distribution
from django_rq import job

from series_tiempo_ar_api.libs.indexing.indexer.distribution_indexer import DistributionIndexer


@job('indexing', timeout=settings.DISTRIBUTION_INDEX_JOB_TIMEOUT)
def reindex_distribution(distribution: Distribution):
DistributionIndexer(index=settings.TS_INDEX).reindex(distribution)

0 comments on commit 9dd6c8b

Please sign in to comment.