diff --git a/beat/web/databases/admin.py b/beat/web/databases/admin.py index 35d43c911b2badfeda484110d7b92d487ee200a3..a8467029eb99a6d32445971c0590a5b28f94da58 100755 --- a/beat/web/databases/admin.py +++ b/beat/web/databases/admin.py @@ -38,8 +38,12 @@ from .models import DatabaseSetTemplate as DatabaseSetTemplateModel from .models import DatabaseSetTemplateOutput as DatabaseSetTemplateOutputModel from .models import validate_database -from ..ui.forms import CodeMirrorJSONFileField, CodeMirrorRSTFileField, \ - CodeMirrorPythonFileField, NameField +from ..ui.forms import ( + CodeMirrorJSONFileField, + CodeMirrorRSTFileField, + CodeMirrorPythonFileField, + NameField, +) from ..common.texts import Messages from ..common.admin import notify_by_email @@ -48,52 +52,45 @@ from ..common.models import Shareable import simplejson as json -#------------------------------------------------ +# ------------------------------------------------ class DatabaseModelForm(forms.ModelForm): name = NameField( - widget=forms.TextInput(attrs=dict(size=80)), - help_text=Messages['name'], + widget=forms.TextInput(attrs=dict(size=80)), help_text=Messages["name"] ) declaration_file = CodeMirrorJSONFileField( - label='Declaration', - help_text=Messages['json'], + label="Declaration", help_text=Messages["json"] ) source_code_file = CodeMirrorPythonFileField( - label='Source code', - help_text=Messages['code'], + label="Source code", help_text=Messages["code"] ) description_file = CodeMirrorRSTFileField( - label='Description', + label="Description", required=False, allow_empty_file=True, - help_text=Messages['description'], + help_text=Messages["description"], ) class Meta: model = DatabaseModel exclude = [] - widgets = { - 'short_description': forms.TextInput( - attrs=dict(size=100), - ), - } + widgets = {"short_description": forms.TextInput(attrs=dict(size=100))} def clean_declaration_file(self): """Cleans-up the declaration_file data, make sure it is really new""" - new_declaration = self.cleaned_data['declaration_file'].read() - old_declaration = '' + new_declaration = self.cleaned_data["declaration_file"].read() + old_declaration = "" if self.instance and self.instance.declaration_file.name is not None: old_declaration = self.instance.declaration_string if new_declaration == old_declaration: - self.changed_data.remove('declaration_file') + self.changed_data.remove("declaration_file") content_file = ContentFile(old_declaration) content_file.name = self.instance.declaration_file.name return content_file @@ -109,206 +106,195 @@ class DatabaseModelForm(forms.ModelForm): raise forms.ValidationError(all_errors) # if that works out, then we return the passed file - self.cleaned_data['declaration_file'].seek(0) #reset ContentFile readout - return self.cleaned_data['declaration_file'] + self.cleaned_data["declaration_file"].seek(0) # reset ContentFile readout + return self.cleaned_data["declaration_file"] def clean(self): """Cleans-up the input data, make sure it overall validates""" - if 'declaration_file' in self.data and \ - isinstance(self.data['declaration_file'], six.string_types): + if "declaration_file" in self.data and isinstance( + self.data["declaration_file"], six.string_types + ): mutable_data = self.data.copy() - mutable_data['declaration_file'] = ContentFile(self.data['declaration_file'], name='unsaved') + mutable_data["declaration_file"] = ContentFile( + self.data["declaration_file"], name="unsaved" + ) self.data = mutable_data - -#---------------------------------------------------------- +# ---------------------------------------------------------- class DatabaseProtocolInline(admin.TabularInline): - model = DatabaseProtocolModel - can_delete = False - extra = 0 - max_num = 0 - readonly_fields = ('name',) - ordering = ('name',) + model = DatabaseProtocolModel + can_delete = False + extra = 0 + max_num = 0 + readonly_fields = ("name",) + ordering = ("name",) -#---------------------------------------------------------- +# ---------------------------------------------------------- class Database(admin.ModelAdmin): list_display = ( - 'id', - 'name', - 'version', - 'short_description', - 'creation_date', - 'previous_version', - 'sharing', + "id", + "name", + "version", + "short_description", + "creation_date", + "previous_version", + "sharing", ) search_fields = [ - 'name', - 'short_description', - 'previous_version__author__username', - 'previous_version__name' + "name", + "short_description", + "previous_version__author__username", + "previous_version__name", ] - list_display_links = ( - 'id', - 'name', - ) + list_display_links = ("id", "name") - readonly_fields = ('short_description',) + readonly_fields = ("short_description",) form = DatabaseModelForm - inlines = [ - DatabaseProtocolInline, - ] - - filter_horizontal = [ - 'shared_with', - 'shared_with_team' - ] + inlines = [DatabaseProtocolInline] + filter_horizontal = ["shared_with", "shared_with_team"] def new_version(self, request, queryset): - """Creates a new version of a specific database""" - - count = 0 - for olddb in queryset: - - newdb = DatabaseModel() - newdb.name = olddb.name - newdb.version = olddb.version + 1 - newdb.sharing = Shareable.PRIVATE - newdb.declaration = olddb.declaration - newdb.source_code = olddb.source_code - newdb.description = olddb.description - newdb.previous_version = olddb - newdb.save() - count += 1 - - self.message_user(request, "Created %s new version(s) of selected " \ - "databases." % count) - new_version.short_description = 'Create new version' - - actions = [ - notify_by_email('database_notifications_enabled'), - 'new_version', - ] + """Creates a new version of a specific database""" + + count = 0 + for olddb in queryset: + + newdb = DatabaseModel() + newdb.name = olddb.name + newdb.version = olddb.version + 1 + newdb.sharing = Shareable.PRIVATE + newdb.declaration = olddb.declaration + newdb.source_code = olddb.source_code + newdb.description = olddb.description + newdb.previous_version = olddb + newdb.save() + count += 1 + + self.message_user( + request, "Created %s new version(s) of selected " "databases." % count + ) + + new_version.short_description = "Create new version" + + actions = [notify_by_email("database_notifications_enabled"), "new_version"] fieldsets = ( - (None, - dict( - fields=('name',), - ), - ), - ('Documentation', - dict( - classes=('collapse',), - fields=('short_description', 'description_file'), - ), - ), - ('Versioning', - dict( - classes=('collapse',), - fields=('version', 'previous_version'), - ), - ), - ('Sharing', - dict( - classes=('collapse',), - fields=('sharing', 'shared_with', 'shared_with_team'), - ), - ), - ('Source code', - dict( - fields=('declaration_file', 'source_code_file'), - ), - ), + (None, dict(fields=("name",))), + ( + "Documentation", + dict( + classes=("collapse",), fields=("short_description", "description_file") + ), + ), + ( + "Accessibility", + dict( + classes=("collapse",), + fields=("accessibility_start_date", "accessibility_end_date"), + ), + ), + ( + "Versioning", + dict(classes=("collapse",), fields=("version", "previous_version")), + ), + ( + "Sharing", + dict( + classes=("collapse",), + fields=("sharing", "shared_with", "shared_with_team"), + ), + ), + ("Source code", dict(fields=("declaration_file", "source_code_file"))), ) + admin.site.register(DatabaseModel, Database) -#------------------------------------------------ +# ------------------------------------------------ class DatabaseSetTemplateOutputInline(admin.TabularInline): - model = DatabaseSetTemplateOutputModel - extra = 0 - ordering = ('name',) - readonly_fields = ('name', 'dataformat') + model = DatabaseSetTemplateOutputModel + extra = 0 + ordering = ("name",) + readonly_fields = ("name", "dataformat") def has_delete_permission(self, request, obj=None): return False def has_add_permission(self, request): - return False + return False class DatabaseSetTemplate(admin.ModelAdmin): - list_display = ('id', 'name') - search_fields = ['name'] - list_display_links = ('id', 'name') - readonly_fields = ('name',) + list_display = ("id", "name") + search_fields = ["name"] + list_display_links = ("id", "name") + readonly_fields = ("name",) - inlines = [ - DatabaseSetTemplateOutputInline, - ] + inlines = [DatabaseSetTemplateOutputInline] def has_delete_permission(self, request, obj=None): return False def has_add_permission(self, request): - return False + return False admin.site.register(DatabaseSetTemplateModel, DatabaseSetTemplate) -#------------------------------------------------ +# ------------------------------------------------ class DatabaseSetOutputInline(admin.TabularInline): - model = DatabaseSetOutputModel - extra = 0 - ordering = ('template__name',) - readonly_fields = ('template',) + model = DatabaseSetOutputModel + extra = 0 + ordering = ("template__name",) + readonly_fields = ("template",) def has_delete_permission(self, request, obj=None): return False def has_add_permission(self, request): - return False + return False class DatabaseSet(admin.ModelAdmin): - list_display = ('id', 'protocol', 'name', 'template', 'hash') - search_fields = ['name', - 'template__name', - 'protocol__database__name', - 'protocol__name'] - list_display_links = ('id', 'name') - readonly_fields = ('name', 'template', 'protocol', 'hash') - - inlines = [ - DatabaseSetOutputInline, + list_display = ("id", "protocol", "name", "template", "hash") + search_fields = [ + "name", + "template__name", + "protocol__database__name", + "protocol__name", ] + list_display_links = ("id", "name") + readonly_fields = ("name", "template", "protocol", "hash") + + inlines = [DatabaseSetOutputInline] def has_delete_permission(self, request, obj=None): return False def has_add_permission(self, request): - return False + return False admin.site.register(DatabaseSetModel, DatabaseSet)