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 | |
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 | } |
| 92 | class UserAdmin(admin.ModelAdmin): |
| 93 | form = UserForm |
| 94 | fieldsets = [ |
| 95 | ('User', {'fields': ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site']}) |
| 96 | ] |
| 97 | list_display = ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site'] |
| 98 | search_fields = ['email'] |
| 99 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 100 | class SliceAdmin(PlanetStackBaseAdmin): |
| 101 | fields = ['name', 'site', 'instantiation', 'description', 'slice_url'] |
| 102 | list_display = ('name', 'site','slice_url', 'instantiation') |
| 103 | inlines = [SliverInline] |
| 104 | |
| 105 | class SubnetAdmin(admin.ModelAdmin): |
| 106 | fields = ['cidr', 'ip_version', 'start', 'end', 'slice'] |
| 107 | |
| 108 | class ImageAdmin(admin.ModelAdmin): |
| 109 | fields = ['image_id', 'name', 'disk_format', 'container_format'] |
| 110 | |
| 111 | class NodeAdmin(admin.ModelAdmin): |
| 112 | list_display = ('name', 'site', 'deploymentNetwork') |
| 113 | list_filter = ('deploymentNetwork',) |
| 114 | |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 115 | |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 116 | class RoleAdmin(admin.ModelAdmin): |
Tony Mack | bc9cecf | 2013-04-14 13:56:38 -0400 | [diff] [blame] | 117 | fieldsets = [ |
| 118 | ('Role', {'fields': ['role_type']}) |
| 119 | ] |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 120 | list_display = ('role_type',) |
| 121 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 122 | admin.site.register(Site, SiteAdmin) |
| 123 | admin.site.register(SitePrivilege) |
| 124 | admin.site.register(Slice, SliceAdmin) |
| 125 | admin.site.register(SliceMembership) |
| 126 | admin.site.register(Subnet, SubnetAdmin) |
| 127 | admin.site.register(Image, ImageAdmin) |
| 128 | admin.site.register(Node, NodeAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 129 | admin.site.register(Sliver) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 130 | admin.site.register(Flavor) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 131 | admin.site.register(Key) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 132 | admin.site.register(Role, RoleAdmin) |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame^] | 133 | admin.site.register(User, UserAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 134 | admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 135 | |