Changeset 1599:21bd6a956601 in peach3-core
- Timestamp:
- 01/19/2012 05:07:30 PM (4 months ago)
- Branch:
- core-split
- Parents:
- 1598:84036a8c30c1 (diff), 1594:65ac081ba73e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
src/peach3/admin/cluster.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/peach3/admin/cluster.py
r1594 r1599 4 4 """ 5 5 from django.contrib import admin 6 from django.core.exceptions import ValidationError 6 7 from django import forms 7 8 from django.forms.formsets import DELETION_FIELD_NAME 8 from django.forms.models import modelform_factory, ModelForm9 from django.forms.models import BaseInlineFormSet 9 10 from django.utils.translation import ugettext_lazy as _ 10 11 11 12 from peach3.admin.i18n import I18NModelForm 12 13 from peach3.admin.course import CourseEditionListFilter 14 from peach3.admin.forms import ImmutableUserModelForm 13 15 from peach3.admin.formsets import BaseInlineFormSetWithParentInstance 14 16 from peach3.admin.news import NewsItemInline 15 17 from peach3.admin.realm import CourseEditionRealmsListFilter 16 from peach3.admin.widgets import ImmutableUserWidget17 18 18 19 from peach3.models.cluster import * #@UnusedWildImport … … 20 21 __all__ = ('ClusterAdmin',) 21 22 22 class ClusterMemberInlineForm( ModelForm):23 class ClusterMemberInlineForm(ImmutableUserModelForm): 23 24 def __init__(self, parent_instance, *args, **kwargs): 24 25 from peach3.models.realm import Realm … … 26 27 super(ClusterMemberInlineForm, self).__init__(*args, **kwargs) 27 28 28 # Make the user field Immutable for existing objects 29 if self.instance and self.instance.pk: 30 self.fields['user'].widget = ImmutableUserWidget() 29 admin_cluster = parent_instance.admin_cluster 30 realms_queryset = Realm.objects 31 if admin_cluster: 32 # Admin cluster doesn't need realms 33 realms_queryset = realms_queryset.none() 34 else: 35 # Limit the available Realms for a ClusterMember to the Realms of the CourseEdition 36 realms_queryset = realms_queryset.filter(cluster=parent_instance).distinct() 31 37 32 # Limit the available Realms for a ClusterMember to the Realms of the CourseEdition 33 self.fields['realm'].queryset = Realm.objects.filter(cluster=parent_instance).distinct() 34 35 def clean_user(self): 36 # Make sure the user was not modified by hacking the hidden field 37 if self.instance and self.instance.pk and self.instance.user: 38 return self.instance.user 39 40 return self.cleaned_data['user'] 38 realm_field = self.fields['realm'] 39 realm_field.queryset = realms_queryset 40 realm_field.required = not admin_cluster 41 41 42 42 class ClusterMembersInline(admin.TabularInline): … … 49 49 extra = 0 50 50 51 class ClusterStaffInlineFormSet(BaseInlineFormSet WithParentInstance):51 class ClusterStaffInlineFormSet(BaseInlineFormSet): 52 52 def add_fields(self, form, index): 53 53 super(ClusterStaffInlineFormSet, self).add_fields(form, index) 54 54 55 55 if index is not None and index<len(self.queryset): 56 # Make the user field Immutable for existing objects57 form.fields['user'].widget = ImmutableUserWidget()58 59 56 # Prevent mutation or deletion of ClusterStaff records of managers 60 57 # Need to do this in the formset instead of in the form itself … … 66 63 form.fields[fn].required = False 67 64 68 class ClusterStaffInlineForm(ModelForm):69 def __init__(self, parent_instance, *args, **kwargs):70 super(ClusterStaffInlineForm, self).__init__(*args, **kwargs)71 72 def clean_user(self):73 # Make sure the user was not modified by hacking the hidden field74 if self.instance and self.instance.pk and self.instance.user:75 return self.instance.user76 77 return self.cleaned_data['user']78 79 65 class ClusterStaffInline(admin.TabularInline): 80 66 model = ClusterStaff 81 form = ClusterStaffInlineForm82 67 formset = ClusterStaffInlineFormSet 68 form = ImmutableUserModelForm 83 69 84 70 fields = 'user', 'level', 'role', 'extend_deadline', … … 87 73 extra = 0 88 74 75 class ClusterAdminModelForm(I18NModelForm): 76 # For admin clusters, raise a ValidationError if realms are defined 77 # For non-admin clusters, raise ValidationError if no realms are defined 78 # TODO: Figure out a way to completely hide the realms field for admin clusters 79 80 def __init__(self, *arg, **kwargs): 81 super(ClusterAdminModelForm, self).__init__(*arg, **kwargs) 82 self.fields['realms'].required = not self.instance.admin_cluster 83 84 def clean_realms(self): 85 realms = self.cleaned_data['realms'] 86 if self.instance.admin_cluster and realms: 87 raise ValidationError, _("Admin cluster cannot have realms") 88 89 return realms 90 91 class Meta: 92 model = Cluster 93 89 94 class ClusterAdmin(admin.ModelAdmin): 90 form = modelform_factory(Cluster, I18NModelForm)95 form = ClusterAdminModelForm 91 96 fieldsets = ( 92 97 (None, { … … 102 107 ) 103 108 raw_id_fields = 'courseedition', 109 readonly_fields = 'admin_cluster', 104 110 filter_horizontal = 'realms', 105 111 inlines = ClusterMembersInline, ClusterStaffInline, NewsItemInline,
Note: See TracChangeset
for help on using the changeset viewer.

