Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | from plstackapi.core.models import Site |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 2 | from plstackapi.core.models import * |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 3 | from django.contrib import admin |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 4 | from django import forms |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 5 | from django.utils.safestring import mark_safe |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 6 | from django.contrib.auth.admin import UserAdmin |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 7 | from django.contrib.admin.widgets import FilteredSelectMultiple |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 8 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 9 | |
| 10 | class ReadonlyTabularInline(admin.TabularInline): |
| 11 | can_delete = False |
| 12 | extra = 0 |
| 13 | editable_fields = [] |
| 14 | |
| 15 | def get_readonly_fields(self, request, obj=None): |
| 16 | fields = [] |
| 17 | for field in self.model._meta.get_all_field_names(): |
| 18 | if (not field == 'id'): |
| 19 | if (field not in self.editable_fields): |
| 20 | fields.append(field) |
| 21 | return fields |
| 22 | |
| 23 | def has_add_permission(self, request): |
| 24 | return False |
| 25 | |
| 26 | class SliverInline(admin.TabularInline): |
| 27 | model = Sliver |
| 28 | extra = 0 |
| 29 | |
| 30 | class SiteInline(admin.TabularInline): |
| 31 | model = Site |
| 32 | extra = 0 |
| 33 | |
| 34 | class NodeInline(admin.TabularInline): |
| 35 | model = Node |
| 36 | extra = 0 |
| 37 | |
| 38 | class PlanetStackBaseAdmin(admin.ModelAdmin): |
| 39 | save_on_top = False |
| 40 | |
| 41 | class DeploymentNetworkAdminForm(forms.ModelForm): |
| 42 | sites = forms.ModelMultipleChoiceField( |
| 43 | queryset=Site.objects.all(), |
| 44 | required=False, |
| 45 | widget=FilteredSelectMultiple( |
| 46 | verbose_name=('Sites'), is_stacked=False |
| 47 | ) |
| 48 | ) |
| 49 | class Meta: |
| 50 | model = DeploymentNetwork |
| 51 | |
| 52 | def __init__(self, *args, **kwargs): |
| 53 | super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs) |
| 54 | |
| 55 | if self.instance and self.instance.pk: |
| 56 | self.fields['sites'].initial = self.instance.sites.all() |
| 57 | |
| 58 | def save(self, commit=True): |
| 59 | deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False) |
| 60 | |
| 61 | if commit: |
| 62 | deploymentNetwork.save() |
| 63 | |
| 64 | if deploymentNetwork.pk: |
| 65 | deploymentNetwork.sites = self.cleaned_data['sites'] |
| 66 | self.save_m2m() |
| 67 | |
| 68 | return deploymentNetwork |
| 69 | |
| 70 | class DeploymentNetworkAdmin(PlanetStackBaseAdmin): |
| 71 | form = DeploymentNetworkAdminForm |
| 72 | inlines = [NodeInline,] |
| 73 | |
| 74 | class SiteAdmin(admin.ModelAdmin): |
| 75 | fieldsets = [ |
| 76 | (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}), |
| 77 | ('Location', {'fields': ['latitude', 'longitude']}), |
| 78 | ('Deployment Networks', {'fields': ['deployments']}) |
| 79 | ] |
| 80 | list_display = ('name', 'login_base','site_url', 'enabled') |
| 81 | filter_horizontal = ('deployments',) |
| 82 | inlines = [NodeInline,] |
| 83 | search_fields = ['name'] |
| 84 | |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 85 | class UserForm(forms.ModelForm): |
| 86 | class Meta: |
| 87 | password = forms.CharField(widget=forms.PasswordInput) |
| 88 | model = User |
| 89 | widgets = { |
| 90 | 'password': forms.PasswordInput(), |
| 91 | } |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 92 | |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 93 | class UserAdmin(admin.ModelAdmin): |
| 94 | form = UserForm |
| 95 | fieldsets = [ |
| 96 | ('User', {'fields': ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site']}) |
| 97 | ] |
| 98 | list_display = ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site'] |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 99 | search_fields = ['email'] |
| 100 | |
| 101 | class KeyAdmin(admin.ModelAdmin): |
| 102 | fieldsets = [ |
| 103 | ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']}) |
| 104 | ] |
| 105 | list_display = ['name', 'key', 'type', 'blacklisted', 'user'] |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 106 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 107 | class SliceAdmin(PlanetStackBaseAdmin): |
| 108 | fields = ['name', 'site', 'instantiation', 'description', 'slice_url'] |
| 109 | list_display = ('name', 'site','slice_url', 'instantiation') |
| 110 | inlines = [SliverInline] |
| 111 | |
| 112 | class SubnetAdmin(admin.ModelAdmin): |
| 113 | fields = ['cidr', 'ip_version', 'start', 'end', 'slice'] |
| 114 | |
| 115 | class ImageAdmin(admin.ModelAdmin): |
| 116 | fields = ['image_id', 'name', 'disk_format', 'container_format'] |
| 117 | |
| 118 | class NodeAdmin(admin.ModelAdmin): |
| 119 | list_display = ('name', 'site', 'deploymentNetwork') |
| 120 | list_filter = ('deploymentNetwork',) |
| 121 | |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 122 | class RoleAdmin(admin.ModelAdmin): |
Tony Mack | bc9cecf | 2013-04-14 13:56:38 -0400 | [diff] [blame] | 123 | fieldsets = [ |
| 124 | ('Role', {'fields': ['role_type']}) |
| 125 | ] |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 126 | list_display = ('role_type',) |
| 127 | |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 128 | class PlainTextWidget(forms.Widget): |
| 129 | def render(self, _name, value, attrs): |
| 130 | return mark_safe(value) if value is not None else '' |
| 131 | |
| 132 | class SliverForm(forms.ModelForm): |
| 133 | class Meta: |
| 134 | ip = forms.CharField(widget=PlainTextWidget) |
| 135 | model = Sliver |
| 136 | widgets = { |
| 137 | 'ip': PlainTextWidget(), |
| 138 | } |
| 139 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 140 | class SliverAdmin(admin.ModelAdmin): |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 141 | form = SliverForm |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 142 | fieldsets = [ |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 143 | ('Sliver', {'fields': ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']}) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 144 | ] |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 145 | list_display = ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork'] |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 146 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 147 | admin.site.register(Site, SiteAdmin) |
| 148 | admin.site.register(SitePrivilege) |
| 149 | admin.site.register(Slice, SliceAdmin) |
| 150 | admin.site.register(SliceMembership) |
| 151 | admin.site.register(Subnet, SubnetAdmin) |
| 152 | admin.site.register(Image, ImageAdmin) |
| 153 | admin.site.register(Node, NodeAdmin) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 154 | admin.site.register(Sliver, SliverAdmin) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 155 | admin.site.register(Flavor) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 156 | admin.site.register(Key, KeyAdmin) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 157 | admin.site.register(Role, RoleAdmin) |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 158 | admin.site.register(User, UserAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 159 | admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 160 | |