How can I ignore a field validation in flask-wtf? - flask

I have a form to add an item to my database, which includes two buttons: Cancel and Submit. The problem I have is that when I press the Cancel button with an empty form, I get a Please fill out this field. error instead of returning to my home page (see views.py for logic). So how can I get my app to ignore the DataRequired validators when I press the Cancel button?
forms.py:
class ItemForm(FlaskForm):
id = StringField('id', validators=[DataRequired()]
name = StringField('Name', validators=[DataRequired()]
cancel = SubmitField('Cancel')
submit = SubmitField('Submit')
views.py:
def add_item()
form = ItemForm()
if form.validate_on_submit():
if form.submit.data:
# Code to add item to db, removed for brevity.
elif form.cancel.data:
flash('Add operation cancelled')
return redirect(url_for('home.homepage'))

Your cancel button doesn't really need to be a submit button. You can simply have a normal button which takes the user back to the home page (using a href or capturing the onclick event).
If you still want the cancel button to be a WTForms field, one option would be to override the validate method in the form and remove the DataRequired validators on id and name. The below is untested but may give you a starting point to work from.
class ItemForm(FlaskForm):
id = StringField('id')
name = StringField('Name')
cancel = SubmitField('Cancel')
submit = SubmitField('Submit')
def validate(self):
rv = Form.validate(self)
if not rv:
return False
if self.cancel.data
return True
if self.id.data is None or self.name.data is None:
return False
return True

Related

django htmx change hx-get url after user input from radio buttons

This is forms.py:
class OrderForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.attrs = {
"hx_get": reverse("planner:detail", kwargs={"slug": self.instance.venue}),
"hx_target": "#services",
}
class Meta:
model = Order
fields = ["venue"]
widgets = {"venue": forms.RadioSelect()}
HTML:
<div id="venue-options" class="p-3">
{% crispy form %}
</div>
This is the error when you click one of the radio buttons: The current path, planner/detail/None/, matched the last one. It says None/ because when you join the page none of the radio buttons are clicked so there is no slug to replace it. I need a way to replace None with the slug / id of the object chosen by the user. I need the url to be changed everytime the choise is changed.
I am open to using apline.js but i would like a way of doing it in htmx/django.
Thank you so much.
Basically you are changing a single attribute (hx_get) based on a click. HTMX would make this a roundtrip to the server which seems inefficient as all the data is there on the page (assuming the value of the radiobuttons is the slug you are after). You can do this with pure javascript which would make the process much faster
<script>
//get the form
const venueForm = document.getElementById('venue-options')
//when implementing, replace [name='radio'] with name of radio input fields
const venueRadioButtons = document.querySelectorAll("input[name='radio']")
//get the default venue URL for later alteration
const venueURL = venueForm.getAttribute('hx_get')
// create a clickhandler to change the URL to selected radiobutton
const clickHandler = () => {
//get the slug value
let radioButtonSlug=document.querySelector("input[name='radio']:checked").value
//use reguylar expression to replace last '/word/' value with slug value
let newVenueUrl = venueURL.replace(/\/\w+\/$/, "/" + radioButtonSlug + "/")
//reset the the hx_get value
venueForm.setAttribute('hx_get', newVenueUrl)
};
// Assign the handler to the radiobuttons
venueRadioButtons.forEach(i => i.onchange = () => clickHandler());
</script>

How can I called a view within another view django

Currently, I have a view that essentially closes a lead, meaning that it simply copies the information from one table (leads) to another (deals), now what I really would like to do is that after clicking close, the user is redirected to another page where the user can update some entries (sales forecast), I have a view that updates the lead, so I thought that I can do something like below:
#login_required
def close_lead(request):
id = request.GET.get('project_id', '')
keys = Leads.objects.select_related().get(project_id=id)
form_dict = {'project_id': keys.project_id,
'agent': keys.agent,
'client': keys.point_of_contact,
'company': keys.company,
'service': keys.services,
'licenses': keys.expected_licenses,
'country_d': keys.country
}
deal_form = NewDealForm(request.POST or None,initial=form_dict)
if request.method == 'POST':
if deal_form.is_valid():
deal_form.save()
obj = Leads.objects.get(project_id=id)
obj.status = "Closed"
obj.save(update_fields=['status'])
## Changing the Forecast Table Entry
forecast = LeadEntry.objects.filter(lead_id=id)
for i in forecast:
m = i
m.stage = "Deal"
m.save(update_fields=['stage'])
messages.success(request, 'You have successfully updated the status from open to Close')
update_forecast(request,id)
else:
messages.error(request, 'Error updating your Form')
return render(request,
"account/close_lead.html",
{'form': deal_form})
This view provides the formset that I want to update after closing the lead
#login_required
def update_forecast(request,lead_id):
# Gets the lead queryset
lead = get_object_or_404(Leads,pk=lead_id)
#Create an inline formset using Leads the parent model and LeadEntry the child model
FormSet = inlineformset_factory(Leads,LeadEntry,form=LeadUpdateForm,extra=0)
if request.method == "POST":
formset = FormSet(request.POST,instance=lead)
if formset.is_valid():
formset.save()
return redirect('forecast_lead_update',lead_id=lead.project_id)
else:
formset = FormSet(instance=lead)
context = {
'formset':formset
}
return render(request,"account/leadentry_update.html",context)
As you can see I’m calling this function update_forecast(request,id) after validating the data in the form, and I would have expected to be somehow redirected to the HTML page specified on that function, however, after clicking submit, the form from the first view is validated but then nothing happens, so I'm the function doesn't render the HTML page
My question how can I leverage existing functions in my views?, obviously, I will imagine that following the DRY principles you can do that in Django, so what am I doing wrong ?, how can I call an existing function within another function in views?
A view returns a response object. In your current code, you're calling a second view but not doing anything with its response. If you just wanted to display static content (not a form that might lead to an action that cares about the current URL) you could return the response object from the second view - return update_forecast(request, id).
But since your second view is displaying a form, you care about what the action for the view from the second form is. The typical Django idiom is to have forms submit to the current page's URL - that wouldn't work if you just call it and return its response object. You could customize the action in the second view, say adding an optional parameter to the view, but the usual idiom for form processing is to redirect to the view you want to show on success. Just as you do in the update_forecast view. Something like this:
messages.success(request, 'You have successfully updated the status from open to Close')
return redirect('update_forecast', lead_id=id)

Django not displaying correct URL after reverse

There's lots of documentation about Django and the reverse() method. I can't seem to locate my exact problem. Suppose I have two urlconfs like this:
url(r'ParentLists/$', main_page, name = "main_page"),
url(r'ParentLists/(?P<grade_level>.+)/$', foo, name = "foo")
and the two corresponding views like this:
def main_page(request):
if request.method == 'POST':
grade_level = request.POST['grade_picked']
return HttpResponseRedirect(reverse('foo', args = (grade_level,)))
else:
return render(request, 'index.html', context = {'grade_list' : grade_list})
def foo(request, grade_level):
grade_level = request.POST['grade_picked']
parent_list = # get stuff from database
emails = # get stuff from database
return render(request, 'list.html', context = {'grade_list' : grade_list, 'parent_list' : parent_list})
Here, list.html just extends my base template index.html, which contains a drop down box with grade levels. When the user goes to /ParentLists, the main_page view renders index.html with the drop down box as it should.
When the user picks a grade level from the drop down box (say 5th Grade), the template does a form submit, and main_page once again executes - but this time the POST branch runs and the HttpResponseRedirect takes the user to /ParentLists/05. This simply results in an HTML table pertaining to grade 5 being displayed below the drop down box.
The problem is, when the user now selects say 10th Grade, the table updates to show the grade 10 content, but the URL displayed is still /ParentLists/05. I want it to be /ParentLists/10.
Clearly, after the first selection, the main_page view never executes again. Only foo does...and so the HttpResponseRedirect never gets called. How should I reorganize this to get what I'm looking for? Thanks in advance!
As you correctly mentioned you will never redirect to foo() from foo().
So the simple way to fix this is just add similar code as in main_page() view:
def foo(request, grade_level):
if request.method == 'POST':
grade_level = request.POST['grade_picked']
return HttpResponseRedirect(reverse('foo', args = (grade_level,)))
else:
parent_list = # get stuff from database
emails = # get stuff from database
return render(request, 'list.html', context = {'grade_list' : grade_list, 'parent_list' : parent_list})
Please note that I remove grade_level = request.POST['grade_picked'] because as Nagkumar Arkalgud correctly said it is excessively.
Also instead of combination of HttpResponseRedirect and reverse you can use shortcut redirect which probably little easy to code:
from django.shortcuts redirect
...
return redirect('foo', grade_level=grade_level)
I would suggest you to use kwargs instead of args.
The right way to use the view is:
your_url = reverse("<view_name>", kwargs={"<key>": "<value>"})
Ex:
return HttpResponseRedirect(reverse('foo', kwargs={"grade_level": grade_level}))
Also, you are sending "grade_level" to your view foo using the URL and not a POST value. I would remove the line:
grade_level = request.POST['grade_picked']
as you will override the grade_level sent to the method from the url.

django-activity-stream actions not displaying

I've just set django-activity-stream up but can't get it to display my actions when I goto the built in template mysite.com/activity/. Yet if I check the admin site I can see the actions have been saved as expected. I am using django-allauth for authentication/authorization
myapp/Settings.py
ACTSTREAM_SETTINGS = {
'MODELS': ('auth.user', 'auth.group'),
'MANAGER': 'actstream.managers.ActionManager',
'FETCH_RELATIONS': True,
'USE_PREFETCH': True,
'USE_JSONFIELD': True,
'GFK_FETCH_DEPTH': 0,
}
myapp/receivers.py
from actstream import action
#receiver(user_logged_in)
def handle_user_logged_in(sender, **kwargs):
request = kwargs.get("request")
user = kwargs['user']
action.send(user, verb='logged in')
In the django-activity-stream views.py it seems models.user_stream(request.user) is returning empty. But I have no idea why.
actstream/views.py
#login_required
def stream(request):
"""
Index page for authenticated user's activity stream. (Eg: Your feed at
github.com)
"""
return render_to_response(('actstream/actor.html', 'activity/actor.html'), {
'ctype': ContentType.objects.get_for_model(User),
'actor': request.user, 'action_list': models.user_stream(request.user)
}, context_instance=RequestContext(request))
Debugging from models.userstream(request.user) it seems I've found where it's returning no results:
actstream/managers.py
#stream
def user(self, object, **kwargs):
"""
Stream of most recent actions by objects that the passed User object is
following.
"""
q = Q()
qs = self.filter(public=True)
actors_by_content_type = defaultdict(lambda: [])
others_by_content_type = defaultdict(lambda: [])
follow_gfks = get_model('actstream', 'follow').objects.filter(
user=object).values_list('content_type_id',
'object_id', 'actor_only')
if not follow_gfks:
return qs.none()
When I check the value at q = self.filter I can actually see all the correct "logged in" activities for the user I passed, however when it gets to follow_gfks = get_model because the user in question isn't following anyone else follow_gfks ends up being None and the query set qs gets deleted on the last line.
Why this works this way when im just trying to view my own users activity feed I have no idea.
Here's what a row from my actstream_action table looks like:
id 1
actor_content_type_id [fk]3
actor_object_id 2
verb logged in
description NULL
target_content_type_id NULL
target_object_id NULL
action_object_content_type_id NULL
action_object_object_id NULL
timestamp 2013-09-28 12:58:41.499694+00
public TRUE
data NULL
I've managed to get the action/activity list of the current logged in user by passing user to actor_stream() instead of user_stream(). But I have no idea why user_stream doesn't work as intended
If it's your user that you want to show the actions for, you need to pass with_user_activity=True to user_stream; if it's for another user, you need to follow them first.

How can I test an unchecked checkbox in Django?

So I present a checkbox to the user, as you can see inside my forms.py file:
class AddFooToBarForm(forms.Form):
to_original = forms.BooleanField(initial=True,
error_messages={'required':
"Oh no! How do I fix?"})
...
Depending on whether the user checks or unchecks that checkbox, I do something different, as you can see inside my views.py file:
def add_foo_to_bar(request, id):
...
try:
bar = Bar.objects.get(pk=id)
if request.method == 'POST': # handle submitted data
form = AddFooToBarForm(bar, request.POST)
if form.is_valid(): # good, now create Foo and add to bar
...
to_original = form.cleaned_data['to_original']
print 'to original is {0}'.format(to_original) # for debugging
if to_original:
# do A
else:
# do B
...
So I want to test that my site does indeed perform the correct actions, depending on whether the user checks the checkbox or not, inside my tests.py file:
class FooTest(TestCase):
...
def test_submit_add_to_Bar(self):
form_data = {
...
'to_original': True,
}
response = self.client.post(reverse('add_foo_to_bar', args=(self.bar.id,)),
form_data, follow=True)
self.assertContains(...) # works
...
form_data['to_original'] = None
response = self.client.post(reverse('add_foo_to_bar', args=(self.bar.id,)),
form_data, follow=True)
print response # for debugging purposes
self.assertContains(...) # doesn't work
I've tried
del form_data['to_original'] -- this gives me the "Oh no! How do I fix?" error message
form_data['to_original'] = None -- in my view function, I get True, so A is done instead of B
form_data['to_original'] = False -- this gives me the "Oh no! How do I fix?" error message once again
So how should I test the user not checking the checkbox in Django? (I'm using the latest version, 1.4.3)
When checkbox is not checked, its not present in submitted form. Also, when submitted value is 'on'.
If you want to make BooleanField optional have required=False while defining the form field.
Documentation BooleanField.