Changeset 1599:21bd6a956601 in peach3-core


Ignore:
Timestamp:
01/19/2012 05:07:30 PM (4 months ago)
Author:
eriks
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.
Message:

Merge with 65ac081ba73e5b2d3ff2a09f46993ff6063923c4

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/peach3/admin/cluster.py

    r1594 r1599  
    44""" 
    55from django.contrib import admin 
     6from django.core.exceptions import ValidationError 
    67from django import forms 
    78from django.forms.formsets import DELETION_FIELD_NAME 
    8 from django.forms.models import modelform_factory, ModelForm 
     9from django.forms.models import BaseInlineFormSet 
    910from django.utils.translation import ugettext_lazy as _ 
    1011 
    1112from peach3.admin.i18n import I18NModelForm 
    1213from peach3.admin.course import CourseEditionListFilter 
     14from peach3.admin.forms import ImmutableUserModelForm 
    1315from peach3.admin.formsets import BaseInlineFormSetWithParentInstance 
    1416from peach3.admin.news import NewsItemInline 
    1517from peach3.admin.realm import CourseEditionRealmsListFilter 
    16 from peach3.admin.widgets import ImmutableUserWidget 
    1718 
    1819from peach3.models.cluster import * #@UnusedWildImport 
     
    2021__all__ = ('ClusterAdmin',) 
    2122 
    22 class ClusterMemberInlineForm(ModelForm): 
     23class ClusterMemberInlineForm(ImmutableUserModelForm): 
    2324    def __init__(self, parent_instance, *args, **kwargs): 
    2425        from peach3.models.realm import Realm 
     
    2627        super(ClusterMemberInlineForm, self).__init__(*args, **kwargs) 
    2728 
    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() 
    3137 
    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 
    4141 
    4242class ClusterMembersInline(admin.TabularInline): 
     
    4949    extra = 0 
    5050 
    51 class ClusterStaffInlineFormSet(BaseInlineFormSetWithParentInstance): 
     51class ClusterStaffInlineFormSet(BaseInlineFormSet): 
    5252    def add_fields(self, form, index): 
    5353        super(ClusterStaffInlineFormSet, self).add_fields(form, index) 
    5454 
    5555        if index is not None and index<len(self.queryset): 
    56             # Make the user field Immutable for existing objects 
    57             form.fields['user'].widget = ImmutableUserWidget() 
    58  
    5956            # Prevent mutation or deletion of ClusterStaff records of managers 
    6057            # Need to do this in the formset instead of in the form itself 
     
    6663                    form.fields[fn].required = False 
    6764 
    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 field 
    74         if self.instance and self.instance.pk and self.instance.user: 
    75             return self.instance.user 
    76  
    77         return self.cleaned_data['user'] 
    78  
    7965class ClusterStaffInline(admin.TabularInline): 
    8066    model = ClusterStaff 
    81     form = ClusterStaffInlineForm 
    8267    formset = ClusterStaffInlineFormSet 
     68    form = ImmutableUserModelForm 
    8369 
    8470    fields = 'user', 'level', 'role', 'extend_deadline', 
     
    8773    extra = 0 
    8874 
     75class 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 
    8994class ClusterAdmin(admin.ModelAdmin): 
    90     form = modelform_factory(Cluster, I18NModelForm) 
     95    form = ClusterAdminModelForm 
    9196    fieldsets = ( 
    9297        (None, { 
     
    102107    ) 
    103108    raw_id_fields = 'courseedition', 
     109    readonly_fields = 'admin_cluster', 
    104110    filter_horizontal = 'realms', 
    105111    inlines = ClusterMembersInline, ClusterStaffInline, NewsItemInline, 
Note: See TracChangeset for help on using the changeset viewer.