Skip to content

Commit

Permalink
adding function making function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Aylard committed May 9, 2016
1 parent a339eb7 commit 7e43aa9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
24 changes: 21 additions & 3 deletions metameta/cars/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .models import BaseCar, HondaCar, FordCar


# Zoom using cmd+= or cmd+-
class BaseCarForm(ModelForm):

def clean_colour(self):
Expand All @@ -29,6 +30,8 @@ class HondaForm(BaseCarForm):
def clean_cupholders(self):
cupholders = self.cleaned_data['cupholders']

# this a typical client request...
# and you can't say no because they pay you
if cupholders % 3 == 0:
raise ValidationError('What? No!')

Expand All @@ -41,6 +44,7 @@ class Meta:

# We can change the Meta fields and model dynamically

# First replicate the above inner Meta class
parent_inner_meta = BaseCarForm.Meta
inner_meta_attrs = {
'model': HondaCar,
Expand All @@ -52,11 +56,25 @@ class Meta:
inner_meta_attrs
)


def clean_function_generator(field_name):
def clean_field_name(self):
field_val = self.cleaned_data[field_name]
if field_val % 3 == 0:
raise ValidationError('What? No!')
return field_val

return clean_field_name

clean_cupholders = clean_function_generator('cupholders')

# Then create a form using that
FirstCrazyForm = type(
'FirstCrazyForm',
(BaseCarForm, ),
{
'Meta': new_inner_meta
'Meta': new_inner_meta,
'clean_cupholders': clean_cupholders,
}
)

Expand All @@ -72,6 +90,7 @@ def run_validators(self, value):

class CustomMeta(ModelFormMetaclass):
def __new__(cls, name, parents, dct):
# lets make a standard ModelForm, then make a new form, inheriting from it
plain_modelform = super(CustomMeta, cls).__new__(cls, name, parents, dct)
model_fields = plain_modelform._meta.model._meta.get_fields()
integer_fields = [
Expand All @@ -97,8 +116,6 @@ class Meta:

class SecondCrazyForm(ModelForm):

# you can dynamically create clean_{field} methods
# but in python it's hacky to find out the name of the current function
def clean(self, *args, **kwargs):
cleaned_data = super(SecondCrazyForm, self).clean(*args, **kwargs)

Expand All @@ -115,6 +132,7 @@ def clean(self, *args, **kwargs):
clean_value = cleaned_data.get(integer_field.name)
if clean_value and clean_value % 3 == 0:
# add_error added in 1.7 :)
# should've been there forever but nvm
self.add_error(integer_field.name, 'What? No!')
# raise ValidationError('What? No!')
# Filter get_fields with these flags (True/False):
Expand Down
43 changes: 21 additions & 22 deletions metameta/cars/views.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
from django.shortcuts import render

from .forms import BaseCarForm, FirstCrazyForm, SecondCrazyForm, CustomMetaForm
from .forms import BaseCarForm, HondaForm, FirstCrazyForm, SecondCrazyForm, CustomMetaForm


def cars(request):
# import pdb; pdb.set_trace()
if request.method == 'POST':
form = CustomMetaForm(data=request.POST)
form = HondaForm(data=request.POST)
else:
form = CustomMetaForm()
"""
if request.method == 'POST':
form = SecondCrazyForm(data=request.POST)
else:
form = SecondCrazyForm()
"""
form = HondaForm()

"""
if request.method == 'POST':
form = FirstCrazyForm(data=request.POST)
else:
form = FirstCrazyForm()
"""
# if request.method == 'POST':
# form = FirstCrazyForm(data=request.POST)
# else:
# form = FirstCrazyForm()

"""
if request.method == 'POST':
form = BaseCarForm(data=request.POST)
else:
form = BaseCarForm()
"""
# if request.method == 'POST':
# form = CustomMetaForm(data=request.POST)
# else:
# form = CustomMetaForm()

# if request.method == 'POST':
# form = SecondCrazyForm(data=request.POST)
# else:
# form = SecondCrazyForm()

# if request.method == 'POST':
# form = BaseCarForm(data=request.POST)
# else:
# form = BaseCarForm()

return render(request, 'cars/cars.html', {'form': form})

0 comments on commit 7e43aa9

Please sign in to comment.