-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from Ilhasoft/refactor-admin
Refactor admin and add download bot data view
- Loading branch information
Showing
5 changed files
with
77 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,56 @@ | ||
from django.contrib import admin | ||
from django.utils.html import format_html | ||
from django.shortcuts import reverse | ||
|
||
from .models import RepositoryCategory | ||
from .models import Repository | ||
from .models import RepositoryUpdate | ||
from .models import RepositoryExample | ||
from .models import RepositoryExampleEntity | ||
from .models import RepositoryTranslatedExample | ||
from .models import RepositoryTranslatedExampleEntity | ||
from .models import RepositoryAuthorization | ||
from bothub.common.models import Repository | ||
from bothub.common.models import RepositoryUpdate | ||
|
||
|
||
@admin.register(RepositoryCategory) | ||
class RepositoryCategoryAdmin(admin.ModelAdmin): | ||
pass | ||
class RepositoryUpdateInline(admin.TabularInline): | ||
model = RepositoryUpdate | ||
extra = 0 | ||
can_delete = False | ||
|
||
|
||
@admin.register(Repository) | ||
class RepositoryAdmin(admin.ModelAdmin): | ||
pass | ||
|
||
|
||
@admin.register(RepositoryUpdate) | ||
class RepositoryUpdateAdmin(admin.ModelAdmin): | ||
list_display_links = [ | ||
'id', | ||
'__str__', | ||
] | ||
list_display = [ | ||
'id', | ||
'__str__', | ||
'repository', | ||
fields = [ | ||
'language', | ||
'created_at', | ||
'by', | ||
'training_started_at', | ||
'trained_at', | ||
'failed_at', | ||
'download_bot_data', | ||
] | ||
readonly_fields = fields | ||
|
||
|
||
@admin.register(RepositoryExample) | ||
class RepositoryExampleAdmin(admin.ModelAdmin): | ||
pass | ||
|
||
|
||
@admin.register(RepositoryExampleEntity) | ||
class RepositoryExampleEntityAdmin(admin.ModelAdmin): | ||
pass | ||
|
||
|
||
@admin.register(RepositoryTranslatedExample) | ||
class RepositoryTranslatedExampleAdmin(admin.ModelAdmin): | ||
pass | ||
def download_bot_data(self, obj): | ||
if not obj.trained_at: | ||
return '-' | ||
return format_html(""" | ||
<a href="{}">Download Bot Data</a> | ||
""".format(reverse('download_bot_data', kwargs={'update_id': obj.id}))) | ||
|
||
|
||
@admin.register(RepositoryTranslatedExampleEntity) | ||
class RepositoryTranslatedExampleEntity(admin.ModelAdmin): | ||
pass | ||
|
||
|
||
@admin.register(RepositoryAuthorization) | ||
class RepositoryAuthorizationAdmin(admin.ModelAdmin): | ||
pass | ||
@admin.register(Repository) | ||
class RepositoryAdmin(admin.ModelAdmin): | ||
list_display = [ | ||
'__str__', | ||
'uuid', | ||
'language', | ||
'is_private', | ||
'created_at', | ||
] | ||
search_fields = [ | ||
'name', | ||
'uuid', | ||
'language', | ||
'owner__nickname', | ||
'slug', | ||
] | ||
list_filter = [ | ||
'is_private', | ||
'language', | ||
'categories', | ||
] | ||
inlines = [ | ||
RepositoryUpdateInline, | ||
] |
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,18 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from django.http import HttpResponse | ||
from django.core.exceptions import ValidationError | ||
from django.contrib.admin.views.decorators import staff_member_required | ||
from .models import RepositoryUpdate | ||
|
||
|
||
@staff_member_required | ||
def download_bot_data(self, update_id): | ||
update = get_object_or_404(RepositoryUpdate, id=update_id) | ||
if not update.trained_at: | ||
raise ValidationError('Update #{} not trained at.'.format(update.id)) | ||
response = HttpResponse( | ||
update.get_bot_data(), | ||
content_type='application/gzip') | ||
response['Content-Disposition'] = 'inline; filename={}.tar.gz'.format( | ||
update.id) | ||
return response |
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