Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor admin and add download bot data view #214

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 43 additions & 49 deletions bothub/common/admin.py
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,
]
10 changes: 10 additions & 0 deletions bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ def admins(self):
]
return list(set(admins))

def __str__(self):
return 'Repository {} - {}/{}'.format(
self.name,
self.owner.nickname,
self.slug,
)

def examples(self, language=None, exclude_deleted=True, queryset=None):
if queryset is None:
queryset = RepositoryExample.objects
Expand Down Expand Up @@ -371,6 +378,9 @@ def ready_for_train(self):
return True
return False

def __str__(self):
return 'Repository Update #{}'.format(self.id)

def start_training(self, by):
if self.trained_at:
raise RepositoryUpdateAlreadyTrained()
Expand Down
18 changes: 18 additions & 0 deletions bothub/common/views.py
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
5 changes: 5 additions & 0 deletions bothub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from bothub.api.routers import router as bothub_api_routers
from bothub.health.views import ping
from bothub.common.views import download_bot_data


urlpatterns = [
Expand All @@ -13,6 +14,10 @@
path('docs/', include_docs_urls(title='API Documentation')),
path('admin/', admin.site.urls),
path('ping/', ping, name='ping'),
path(
'downloadbotdata/<int:update_id>/',
download_bot_data,
name='download_bot_data')
]

if settings.DEBUG:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='bothub',
version='1.15.4',
version='1.15.5',
description='bothub',
packages=find_packages(),
install_requires=[
Expand Down