Translate a python dict into a Solr query string - python

I'm just getting started with Python, and I'm stuck on the syntax that I need to convert a set of request.POST parameters to Solr's query syntax.
The use case is a form defined like this:
class SearchForm(forms.Form):
text = forms.CharField()
metadata = forms.CharField()
figures = forms.CharField()
Upon submission, the form needs to generate a url-encoded string to pass to a URL client that looks like this:
text:myKeyword1+AND+metadata:myKeyword2+AND+figures:myKeyword3
Seems like there should be a simple way to do this. The closest I can get is
for f, v in request.POST.iteritems():
if v:
qstring += u'%s\u003A%s+and+' % (f,v)
However in that case the colon (\u003A) generates a Solr error because it is not getting encoded properly.
What is the clean way to do this?
Thanks!

I would recommend creating a function in the form to handle this rather than in the view. So after you call form.isValid() to ensure the data is acceptable. You can invoke a form.generateSolrQuery() (or whatever name you would like).
class SearchForm(forms.Form):
text = forms.CharField()
metadata = forms.CharField()
figures = forms.CharField()
def generateSolrQuery(self):
return "+and+".join(["%s\u003A%s" % (f,v) for f,v in self.cleaned_data.items()])

Isn't u'\u003A' simply the same as u':', which is escaped in URLs as %3A ?
qstring = '+AND+'.join(
[ u'%s%%3A%s'%(f,v) for f,v in request.POST.iteritems() if f]
)

Use django-haystack. It takes care of all the interfacing with Solr - both indexing and querying.

Related

Django autocomplete from json

I'm trying to add some autocomplete field in Django Admin (Django 2.0.1).
I managed to get it to work (both with the autocomplete included in Django and with Select2) because in those cases I loaded the dropdown options from a ForeignKey field.
Now I need to have autocomplete on a simple CharField but the choices need to be taken from a remote API that return a json response. I can decide how to structure the json response. Any way to do this?
The returned json responde doesn't represent objects of model, just simple text options.
Not sure if this fits your needs, but here was a solution to a similar problem (remote API, JSON, autocomplete text input). Select portions of the code:
HTML
<label>Which student? (search by last name.)</label>
<input type="text" name="studentname" id="student_name">
JS
// Build list of all students - hit API.
var ajax = new XMLHttpRequest();
ajax.open("GET", "example.com/api/student/?format=json", true);
ajax.onload = function() {
students = JSON.parse(ajax.responseText);
list = students.map(function(i) {
name = i.last_name + ', ' + i.first_name;
return name;
});
var input = document.getElementById("student_name");
new Awesomplete(input, { list: list });
};
ajax.send();
All this of course requires the Awesomplete JS library.
This is a solution when working outside the Django admin, but I think could be adapted to work within the admin setting without too much difficulty?
Perhaps something like this in your ModelAdmin?
def special_field(self, obj):
return render_to_string('special.html')
special_field.allow_tags = True
Then throw the aforementioned HTML/JS in special.html.
Finally you'll need to remove the old field from your ModelAdmin, add your new custom field, and likely override your ModelForm - something like this:
def save(self, commit=True):
extra_input = self.cleaned_data.get('studentname', None)
self.instance.full_name = extra_input # the model instance to save to
return super(NameOfYourForm, self).save(commit=commit)

Form field with underscore doesn't work

I'm using a form to obtain the data from a post request and I noticed some behavior I'm not familiar with. I have two versions of my form shown below. The only variation is that in the second version of the form, I have an underscore in my variable names.
class EditTitleForm(Form):
newTitle = StringField('newTitle')
currentTitle = StringField('currentTitle')
class EditTitleForm(Form):
new_title = StringField('newTitle')
current_title = StringField('currentTitle')
However, when I try print(form.new_title.data) and print(form.current_title.data) I get an empty line, but when I switch the form to the version without the underscores, everything prints out fine. Why is that?
#auth.route('/edit_title', methods=['POST'])
def edit_title():
data = MultiDict(mapping=request.json)
print(data)
form = EditTitleForm(data)
print(form.newTitle.data)
print(form.currentTitle.data)
The names of the fields correspond to the keys in the data being read. If you change the field names, you need to change the keys in the JSON data as well. Setting the label (the first argument to a field) doesn't effect this, that's only used when rendering the fields as HTML.
# if attribute name is new_title
new_title = StringField()
# then data key must be new_title as well
data = {
"new_title": "Stack Overflow"
}
WTForms can work with JSON, but if you want something more suited to that, try Marshmallow or another serialization library.

How to get a filed from self.data in model_formset_factory in clean method

I am using modelformset_factory to edit multiple images on my interface.
I have following fields in each image.
Name
User
City
I have allowed user to select new user that is currently not in the system, (for that case I should get a text "Jack" in my
def clean_user(self) instead of ID.
But using model_formseta_factory, I am getting some wired names in my self.data. and when I try to get self.data.get('user'), I get nothing, obviously there is no key with this name,
the key is formed like form_0_user etc.
fields = ['city', 'name']
note, i do not have user in my fields. if I do, it fails the validation.
def clean(self):
data = self.cleaned_data
data['name'] = data.get('name', '').strip()
return data
Works fine
pic_credits = self.data.get('user')
This does not.
pic_credits = self.data.get('form-0-name')
This works fine too.
Please help.
If you want to use self.data instead of self.cleaned_data, you can construct the "composite prefix" using the fields auto_id and prefix (or at least when the form has been instanced by a formset).
See _construct_form() https://docs.djangoproject.com/es/1.9/_modules/django/forms/formsets/
Your method will look like this:
def clean(self):
# ...
form_prefix_and_autoid = "%s-%d-" % (self.prefix, self.auto_id)
pic_credits = self.data.get(form_prefix_and_autoid + 'name')
# ...
Update:
A lot simpler is calling the method self.add_prefix
pic_credits = self.data.get(self.add_prefix('name'))

Not a Valid Choice for Dynamic Select Field WTFORMS

I currently am creating a dynamic select field using WTFORMS, however it never submits and fails the validation with the following error.
Not a valid choice
My Field is created like this:
area = SelectField()
and in the view, i am grabbing the options from the db like so:
form = MytestForm()
form.area.choices = [(a.id, a.name) for a in Area.objects.all()]
It works however if i create static options.
My guess is that Area.id is a int - when data comes back from the client it is treated as a string by WTForms unless a callable is passed to the coerce keyword argument of the wtforms.fields.SelectField constructor:
area = SelectField(coerce=int)
Alternately, if you are using SQLAlchemy you could use wtforms.ext.sqlalchemy.fields.QuerySelectField (wtforms_sqlalchemy if you are using WTForms 3+):
area = QuerySelectField(query_factory=Area.objects.all,
get_pk=lambda a: a.id,
get_label=lambda a: a.name)
Here is how you can solve it without QuerySelectField.
Here is how I did:
years = [(str(y), y) for y in reversed(range(1950, 2013))]
years.insert(0, ('','year'))
year = wt.SelectField(choices=years)
a 3.01 Pullrequest adds the necessary argument:
validate_choice=True
To switch off the feature which more often than not seems to be buggy given that 33k views are on this question already and 52 upvotest as of 2022-01.
SelectField(coerce=int, validate_choice=False)
made me a happy camper
Remember to put also the available choices when setting up the Form class.
class MyForm(FlaskForm):
sel = SelectField(coerce=int, label="MyLabel", choices=my_choices)
....
where choices can be something like this:
my_choices = [x.id for x in get_products()]
The exception is arised in function pre_validate in script /wtforms/fields/core.py when the validate_on_submit() functions is called

Is there a way to capture URL parameters with django in urls.py?

I am trying to write something elegant where I am not relying on Request object in my code. All the examples are using:
(r'^hello/(?P.*)$', 'foobar.views.hello')
but it doesn't seem like you can post to a URL like that very easily with a form. Is there a way to make that URL respond to ..../hello?name=smith
Absolutely. If your url is mapped to a function, in this case foobar.views.hello, then that function might look like this for a GET request:
def hello(request):
if request.method == "GET":
name_detail = request.GET.get("name", None)
if name_detail:
# got details
else:
# error handling if required.
Data in encoded forms, i.e. POST parameters, is available if you HTTP POST from request.POST.
You can also construct these yourself if you want, say, query parameters on a POST request. Just do this:
PARAMS = dict()
raw_qs = request.META.get('QUERY_STRING', '') # this will be the raw query string
if raw_qs:
for line in raw_qs.split("&"):
key,arg = line.split("=")
PARAMS[key] = arg
And likewise for form-encoded parameters in non POST requests, do this:
FORM_PARAMS = QueryDict(request.raw_post_data)
However, if you're trying to use forms with Django, you should definitely look at django.forms. The whole forms library will just generally make your life easier; I've never written a html form by hand using Django because this part of Django takes all the work out of it. As a quick summary, you do this:
forms.py:
class FooForm(forms.Form):
name = fields.CharField(max_length=200)
# other properties
or even this:
class FooForm(forms.ModelForm):
class Meta:
model = model_name
Then in your request, you can pass a form out to the template:
def pagewithforminit(request):
myform = FooForm()
return render_to_response('sometemplate.html', {'nameintemplate': myform},
context_instance=RequestContext(request))
And in the view that receives it:
def pagepostingto(request):
myform = FooForm(request.POST)
if myform.is_valid(): # check the fields for you:
# do something with results. if a model form, this:
myform.save()
# creates a model for you.
See also model forms. In short, I strongly recommend django.forms.
You can't catch GET parameters in a URL pattern. As you can see in django.core.handlers.base.BaseHandler.get_response, only the part of the URL that ends up in request.path_info is used to resolve an URL:
callback, callback_args, callback_kwargs = resolver.resolve(
request.path_info)
request.path_info does not contain the GET parameters. For handling those, see Ninefingers answer.

Categories

Resources