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 | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 5 | |
| 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 | |
| 85 | class SliceAdmin(PlanetStackBaseAdmin): |
| 86 | fields = ['name', 'site', 'instantiation', 'description', 'slice_url'] |
| 87 | list_display = ('name', 'site','slice_url', 'instantiation') |
| 88 | inlines = [SliverInline] |
| 89 | |
| 90 | class SubnetAdmin(admin.ModelAdmin): |
| 91 | fields = ['cidr', 'ip_version', 'start', 'end', 'slice'] |
| 92 | |
| 93 | class ImageAdmin(admin.ModelAdmin): |
| 94 | fields = ['image_id', 'name', 'disk_format', 'container_format'] |
| 95 | |
| 96 | class NodeAdmin(admin.ModelAdmin): |
| 97 | list_display = ('name', 'site', 'deploymentNetwork') |
| 98 | list_filter = ('deploymentNetwork',) |
| 99 | |
| 100 | admin.site.register(Site, SiteAdmin) |
| 101 | admin.site.register(SitePrivilege) |
| 102 | admin.site.register(Slice, SliceAdmin) |
| 103 | admin.site.register(SliceMembership) |
| 104 | admin.site.register(Subnet, SubnetAdmin) |
| 105 | admin.site.register(Image, ImageAdmin) |
| 106 | admin.site.register(Node, NodeAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 107 | admin.site.register(Sliver) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 108 | admin.site.register(Flavor) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 109 | admin.site.register(Key) |
| 110 | admin.site.register(Role) |
| 111 | admin.site.register(User) |
| 112 | admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 113 | |