Re: Best practice when testing form values in a template?

2011-08-05 Thread Joshua Russo
Ok, I apologize. Here is a fuller representation of what I'm doing. I had a 
hard time figuring out how much was enough versus too much. This is 
simplified, but I think it represents what I'm trying to do. For instance, 
don't pay too much attention to the save logic in the view, I haven't 
actually really worked on it much yet.

To restate my scenario, I want to carry information in hidden fields of a 
form that determine how the form is displayed and functions. I realize I do 
have access to the self.Initial values in the form itself but I don't think 
that there's easy access to those values in the template. The value that I 
want seems to be the value() method of the BoundField, but run through the 
to_python() method of the field. I actually solved this by adding a 
to_python() method to the BoundField that does just this, but I would like 
to solve this without modifying the Django code. The samples below do not 
reflect my added to_python() method.

# Form class (I use the self["multiLineInfo"] pattern instead of 
self.fields["multiLineInfo"] 
# because the former gives a BoundField which merges the Field and the 
post/initial data)
class OrganizationItemForm(ModelForm):
selected  = forms.BooleanField(required=False)  
extraRequired = forms.BooleanField(required=False, 
widget=forms.HiddenInput)
multiLineInfo = forms.BooleanField(required=False, 
widget=forms.HiddenInput)

def __init__(self, *args, **kwargs):
super(AuditModelForm, self).__init__(*args, **kwargs)
if self["multiLineInfo"].value():
self.fields["descr"].widget = 
forms.Textarea(attrs={"class":"descriptionEdit"})
else:
self.fields["descr"].widget = 
forms.TextInput(attrs={"class":"itemDescrEdit"}) 
self.fields["descr"].required = self["extraRequired"].value() and 
self["selected"].value()

class Meta:
model = OrganizationItem

# Model classes

class OrganizationItem(models.Model):
organization = models.ForeignKey(Organization)
item   = models.ForeignKey(ListItem)
descr  = models.TextField("Description", blank=True)

class ListItem(models.Model):
category = models.ForeignKey(ListCategory)
name = models.CharField("Name", max_length=200)
order= models.IntegerField("Order")
extraInfo = models.BooleanField("Requires extra information")
multiLineInfo = models.BooleanField("The display should use a multi-line 
edit box instead of a single line edit")
descrLabel= models.CharField("Extra information label", 
max_length=50,
blank=True)

class ListCategory(modelUtils.MyModelAudit):
catId = models.CharField("Category identifier", max_length=15,
primary_key=True)
name  = models.CharField("Name", max_length=100)
order = models.IntegerField("Order")
group = models.ForeignKey(ListGroup)
organization = models.BooleanField("Should this be associated with an 
organization?")
twoColumn= models.BooleanField("Should the current list span 2 
columns?")

class ListGroup(modelUtils.MyModelAudit):
name = models.CharField("Name", max_length=15)
descr = models.CharField("Description", max_length=100, blank=True)
order = models.IntegerField("Order")

# View example
def organizationAdd(request):

OrganizationItemFormSet = formset_factory(OrganizationItemForm)

if request.method == 'POST':
orgItemFormSet = OrganizationItemFormSet(request.POST)
orgItemFormSetIsValid = orgItemFormSet.is_valid()
if orgItemFormSetIsValid:
orgItemFormSet.save(commit=False)
else:
orgItemFormSet = OrganizationItemFormSet() 
listItems = CreateItemsSection(orgItemFormSet, "organization", 
category__organization=True)

context = {
'listItems': listItems, 
'listItemsManagementForm': orgItemFormSet.management_form}

return render_to_response('organizationEdit.html', context,
context_instance=RequestContext(request))


def CreateItemsSection(formset, idField="", objId=None, relation=None, 
**kwargs):
"""Construct the list of selectable items"""

qs = ListItem.objects.filter(**kwargs)

# Get the appliable list of groups
groups = qs.values(
"category__group", "category__group__descr"
).order_by(
"category__group__order"
).distinct()

# Convert the items relation into a dictionary of ListItem PK and 
XItems.descr
selectedData = {}
if relation is not None:
selectedData = dict([(item.item__id, item.descr) for item in 
relation])

retVal = []

# Track the current form to display when the formset is populated from a 
postback
formIndex = 0;
postedData = len(formset) > 0

# Assemble the information for each category group
for grp in groups:
# The current group's description
curGrpDescr = grp["category__group__descr"]
# 

Re: Best practice when testing form values in a template?

2011-08-03 Thread Shawn Milochik

On 08/03/2011 10:46 PM, Joshua Russo wrote:
Really? Nothing? Do you need more information? From the lack of 
response I feel like I'm completely off the mark and nobody wants to 
tell me. --


1: https://code.djangoproject.com/wiki/UsingTheMailingList

If you're not getting help then it's almost certainly because you 
haven't made it easy for the all-volunteer army to help you.


You posted code that shows a subclass of an unknown form, you have 
invalid code (it looks like self["multiLineInfo"] should be  
self.fields["multiLineInfo"]), and a template but no view to explain the 
context the template is processing.


What you should have done instead of pasting incomplete code is make the 
simplest working (or non-working, as the case may be) example that 
demonstrates your issue.


On top of that, replying with "Really? Nothing?" is a great way to get 
ignored. Nobody wants to help someone with that attitude.


So, to recap:
Read this, then re-post: 
https://code.djangoproject.com/wiki/UsingTheMailingList


Shawn




--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Best practice when testing form values in a template?

2011-08-03 Thread Joshua Russo
Really? Nothing? Do you need more information? From the lack of response I 
feel like I'm completely off the mark and nobody wants to tell me.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/uMI0ls6prlEJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Best practice when testing form values in a template?

2011-08-03 Thread Joshua Russo
Or am I not using these concepts as intended?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/UvoTGqPzDBoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.