Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
[admin] Added way to easily get UUID #14
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
nemesifier committed Feb 1, 2016
1 parent 31ead77 commit 39c3b54
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
26 changes: 22 additions & 4 deletions django_netjsonconfig/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Media:
css = {'all': ('css/admin/django-netjsonconfig.css',)}
js = (
'js/admin/preview.js',
'js/admin/unsaved_changes.js'
'js/admin/unsaved_changes.js',
'js/admin/uuid.js'
)

def get_extra_context(self, pk=None):
Expand Down Expand Up @@ -135,20 +136,37 @@ class ConfigAdmin(BaseConfigAdmin):
list_display = ('name', 'backend', 'status', 'last_ip', 'created', 'modified')
list_filter = ('backend', 'status', 'created',)
search_fields = ('id', 'name', 'key', 'last_ip')
readonly_fields = ('status', 'last_ip')
fields = ('name',
readonly_fields = ['id', 'status', 'last_ip']
fields = ['name',
'key',
'id',
'status',
'last_ip',
'templates',
'backend',
'config',
'created',
'modified')
'modified']
actions_on_bottom = True
save_on_top = True
form = ConfigForm

def _get_fields(self, fields, request, obj=None):
"""
removes "id" field in add view
"""
if obj:
return fields
new_fields = fields[:]
if 'id' in new_fields:
new_fields.remove('id')
return new_fields

def get_fields(self, request, obj=None):
return self._get_fields(self.fields, request, obj)

def get_readonly_fields(self, request, obj=None):
return self._get_fields(self.readonly_fields, request, obj)

admin.site.register(Template, TemplateAdmin)
admin.site.register(Config, ConfigAdmin)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
line-height: 22px;
}

.readonly{
border: 1px solid rgba(0, 0, 0, 0.05) !important;
background-color: rgba(0, 0, 0, 0.070);
}

.djnjc-overlay {
display: none;
position: fixed;
Expand Down
9 changes: 9 additions & 0 deletions django_netjsonconfig/static/js/admin/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
django.jQuery(function($) {
var p = $('.field-id p'),
value = p.text();
p.html('<input readonly id="id_id" type="text" class="vTextField readonly" value="'+ value +'">');
var id = $('#id_id');
id.click(function(){
$(this).select()
});
});
16 changes: 16 additions & 0 deletions django_netjsonconfig/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,19 @@ def test_preview_template(self):
self.assertContains(response, '<pre class="djnjc-preformatted')
self.assertNotContains(response, 'system')
self.assertNotContains(response, 'hostname')

def test_uuid_field_not_in_add(self):
path = reverse('admin:django_netjsonconfig_config_add')
response = self.client.get(path)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'field-id')

def test_uuid_field_in_change(self):
t = Template.objects.first()
c = Config(name='test', backend=t.backend, config=t.config, key=self.TEST_KEY)
c.full_clean()
c.save()
path = reverse('admin:django_netjsonconfig_config_change', args=[c.pk])
response = self.client.get(path)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'field-id')

0 comments on commit 39c3b54

Please sign in to comment.