Django

Code

Changeset 6627

Show
Ignore:
Timestamp:
10/29/07 18:52:17 (1 year ago)
Author:
gwilson
Message:

Style fixes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/models.py

    r5819 r6627  
    77from django.utils.encoding import smart_unicode 
    88 
    9  
    109from util import ValidationError 
    1110from forms import BaseForm, SortedDictFromList 
     
    1817) 
    1918 
    20 def save_instance(form, instance, fields=None, fail_message='saved', commit=True): 
     19def save_instance(form, instance, fields=None, fail_message='saved', 
     20                  commit=True): 
    2121    """ 
    2222    Saves bound Form ``form``'s cleaned_data into model instance ``instance``. 
     
    2828    opts = instance.__class__._meta 
    2929    if form.errors: 
    30         raise ValueError("The %s could not be %s because the data didn't validate." % (opts.object_name, fail_message)) 
     30        raise ValueError("The %s could not be %s because the data didn't" 
     31                         " validate." % (opts.object_name, fail_message)) 
    3132    cleaned_data = form.cleaned_data 
    3233    for f in opts.fields: 
    33         if not f.editable or isinstance(f, models.AutoField) or not f.name in cleaned_data: 
     34        if not f.editable or isinstance(f, models.AutoField) \ 
     35                or not f.name in cleaned_data: 
    3436            continue 
    3537        if fields and f.name not in fields: 
    3638            continue 
    37         f.save_form_data(instance, cleaned_data[f.name])         
    38     # Wrap up the saving of m2m data as a function 
     39        f.save_form_data(instance, cleaned_data[f.name]) 
     40    # Wrap up the saving of m2m data as a function. 
    3941    def save_m2m(): 
    4042        opts = instance.__class__._meta 
     
    4648                f.save_form_data(instance, cleaned_data[f.name]) 
    4749    if commit: 
    48         # If we are committing, save the instance and the m2m data immediately 
     50        # If we are committing, save the instance and the m2m data immediately. 
    4951        instance.save() 
    5052        save_m2m() 
    5153    else: 
    52         # We're not committing. Add a method to the form to allow deferred  
    53         # saving of m2m data 
     54        # We're not committing. Add a method to the form to allow deferred 
     55        # saving of m2m data. 
    5456        form.save_m2m = save_m2m 
    5557    return instance 
    5658 
    5759def make_model_save(model, fields, fail_message): 
    58     "Returns the save() method for a Form.
     60    """Returns the save() method for a Form.""
    5961    def save(self, commit=True): 
    6062        return save_instance(self, model(), fields, fail_message, commit) 
    6163    return save 
    62      
     64 
    6365def make_instance_save(instance, fields, fail_message): 
    64     "Returns the save() method for a Form.
     66    """Returns the save() method for a Form.""
    6567    def save(self, commit=True): 
    6668        return save_instance(self, instance, fields, fail_message, commit) 
    6769    return save 
    6870 
    69 def form_for_model(model, form=BaseForm, fields=None, formfield_callback=lambda f: f.formfield()): 
     71def form_for_model(model, form=BaseForm, fields=None, 
     72                   formfield_callback=lambda f: f.formfield()): 
    7073    """ 
    7174    Returns a Form class for the given Django model class. 
     
    8891            field_list.append((f.name, formfield)) 
    8992    base_fields = SortedDictFromList(field_list) 
    90     return type(opts.object_name + 'Form', (form,),  
    91         {'base_fields': base_fields, '_model': model, 'save': make_model_save(model, fields, 'created')}) 
    92  
    93 def form_for_instance(instance, form=BaseForm, fields=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): 
     93    return type(opts.object_name + 'Form', (form,), 
     94        {'base_fields': base_fields, '_model': model, 
     95         'save': make_model_save(model, fields, 'created')}) 
     96 
     97def form_for_instance(instance, form=BaseForm, fields=None, 
     98                      formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): 
    9499    """ 
    95100    Returns a Form class for the given Django model instance. 
     
    116121    base_fields = SortedDictFromList(field_list) 
    117122    return type(opts.object_name + 'InstanceForm', (form,), 
    118         {'base_fields': base_fields, '_model': model, 'save': make_instance_save(instance, fields, 'changed')}) 
     123        {'base_fields': base_fields, '_model': model, 
     124         'save': make_instance_save(instance, fields, 'changed')}) 
    119125 
    120126def form_for_fields(field_list): 
    121     "Returns a Form class for the given list of Django database field instances." 
    122     fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list if f.editable]) 
     127    """ 
     128    Returns a Form class for the given list of Django database field instances. 
     129    """ 
     130    fields = SortedDictFromList([(f.name, f.formfield()) 
     131                                 for f in field_list if f.editable]) 
    123132    return type('FormForFields', (BaseForm,), {'base_fields': fields}) 
    124133 
    125134class QuerySetIterator(object): 
    126135    def __init__(self, queryset, empty_label, cache_choices): 
    127         self.queryset, self.empty_label, self.cache_choices = queryset, empty_label, cache_choices 
     136        self.queryset = queryset 
     137        self.empty_label = empty_label 
     138        self.cache_choices = cache_choices 
    128139 
    129140    def __iter__(self): 
     
    137148 
    138149class ModelChoiceField(ChoiceField): 
    139     "A ChoiceField whose choices are a model QuerySet.
     150    """A ChoiceField whose choices are a model QuerySet.""
    140151    # This class is a subclass of ChoiceField for purity, but it doesn't 
    141152    # actually use any of ChoiceField's implementation. 
     153 
    142154    def __init__(self, queryset, empty_label=u"---------", cache_choices=False, 
    143             required=True, widget=Select, label=None, initial=None, help_text=None): 
     155                 required=True, widget=Select, label=None, initial=None, 
     156                 help_text=None): 
    144157        self.queryset = queryset 
    145158        self.empty_label = empty_label 
     
    161174        # self.choices is accessed) so that we can ensure the QuerySet has not 
    162175        # been consumed. 
    163         return QuerySetIterator(self.queryset, self.empty_label, self.cache_choices) 
     176        return QuerySetIterator(self.queryset, self.empty_label, 
     177                                self.cache_choices) 
    164178 
    165179    def _set_choices(self, value): 
     
    178192            value = self.queryset.model._default_manager.get(pk=value) 
    179193        except self.queryset.model.DoesNotExist: 
    180             raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.')) 
     194            raise ValidationError(ugettext(u'Select a valid choice. That' 
     195                                           u' choice is not one of the' 
     196                                           u' available choices.')) 
    181197        return value 
    182198 
    183199class ModelMultipleChoiceField(ModelChoiceField): 
    184     "A MultipleChoiceField whose choices are a model QuerySet.
     200    """A MultipleChoiceField whose choices are a model QuerySet.""
    185201    hidden_widget = MultipleHiddenInput 
     202 
    186203    def __init__(self, queryset, cache_choices=False, required=True, 
    187             widget=SelectMultiple, label=None, initial=None, help_text=None): 
    188         super(ModelMultipleChoiceField, self).__init__(queryset, None, cache_choices, 
    189             required, widget, label, initial, help_text) 
     204                 widget=SelectMultiple, label=None, initial=None, 
     205                 help_text=None): 
     206        super(ModelMultipleChoiceField, self).__init__(queryset, None, 
     207            cache_choices, required, widget, label, initial, help_text) 
    190208 
    191209    def clean(self, value): 
     
    201219                obj = self.queryset.model._default_manager.get(pk=val) 
    202220            except self.queryset.model.DoesNotExist: 
    203                 raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val) 
     221                raise ValidationError(ugettext(u'Select a valid choice. %s is' 
     222                                               u' not one of the available' 
     223                                               u' choices.') % val) 
    204224            else: 
    205225                final_values.append(obj)