django haystack custom form - django-haystack

I'm trying to make a custom search form using django haystack, i just modify from haystack's documentation :
forms.py
from django import forms
from haystack.forms import SearchForm
class DateRangeSearchForm(SearchForm):
start_date = forms.DateField(required=False)
end_date = forms.DateField(required=False)
def search(self):
# First, store the SearchQuerySet received from other processing.
sqs = super(DateRangeSearchForm, self).search()
# Check to see if a start_date was chosen.
if self.cleaned_data['start_date']:
sqs = sqs.filter(pub_date__gte=self.cleaned_data['start_date'])
# Check to see if an end_date was chosen.
if self.cleaned_data['end_date']:
sqs = sqs.filter(pub_date__lte=self.cleaned_data['end_date'])
return sqs
to :
from django import forms
from haystack.forms import HighlightedModelSearchForm
class CustomSearchForm(HighlightedModelSearchForm):
title = forms.CharField(max_length = 100, required = False)
content = forms.CharField(max_length = 100, required = False)
date_added = forms.DateField(required = False)
post_by = forms.CharField(max_length = 100, required = False)
def search(self):
sqs = super(CustomSearchForm, self).search()
if self.cleaned_data['post_by']:
sqs = sqs.filter(content = self.cleaned_data['post_by'])
if self.cleaned_data['title']:
sqs = sqs.filter(content = self.cleaned_data['title'])
if self.cleaned_data['content']:
sqs = sqs.filter(content = self.cleaned_data['content'])
if self.cleaned_data['date_added']:
sqs = sqs.filter(content = self.cleaned_data['date_added'])
return sqs
haystack .urls :
urlpatterns = patterns('haystack.views',
url(r'^$', search_view_factory(view_class = SearchView, form_class = CustomSearchForm), name='haystack_search'),
)
when i go to the url, it says : AttributeError at /search/
'CustomSearchForm' object has no attribute 'cleaned_data'
can you guys help me? thx
Then i try to comment the search method, but when i submit a word into the custom field, the result is always nothing, only when i submit a word to non-custom field it can gimme the result i want, already tried to understand this all day long, pls help

I know this is a bit old question, but to help others who may be viewing this and wondering the same thing, this is how I got it working in the same situation.
Something along these lines:
...
def search(self)
sqs=super(MyFooSearchForm, self).search()
if self.is_valid() and self.cleaned_data['foo']:
sqs = sqs.filter(foostuff__exact=self.cleaned_data['foo'])
return sqs
Basically, I added 'self.is_valid and' before self.cleaned_data['']
this got rid of the error for me. Hope this helps.
So,
def search(self):
sqs = super(CustomSearchForm, self).search()
if self.cleaned_data['post_by']:
sqs = sqs.filter(content = self.cleaned_data['post_by'])
if self.cleaned_data['title']:
sqs = sqs.filter(content = self.cleaned_data['title'])
if self.cleaned_data['content']:
sqs = sqs.filter(content = self.cleaned_data['content'])
if self.cleaned_data['date_added']:
sqs = sqs.filter(content = self.cleaned_data['date_added'])
return sqs
would become:
def search(self):
sqs = super(CustomSearchForm, self).search()
if self.is_valid() and self.cleaned_data['post_by']:
sqs = sqs.filter(content = self.cleaned_data['post_by'])
if self.is_valid() and self.cleaned_data['title']:
sqs = sqs.filter(content = self.cleaned_data['title'])
if self.is_valid() and self.cleaned_data['content']:
sqs = sqs.filter(content = self.cleaned_data['content'])
if self.is_valid() and self.cleaned_data['date_added']:
sqs = sqs.filter(content = self.cleaned_data['date_added'])
return sqs
There may be a better way to do this, and I'm a relative beginner for django/python but it worked for me.

the thing is that firstly form.is_valid() is called. data will not be in cleaned state until isvalid() method is called. therefore while validating all fields, it put all fields one by one in its cleaned_data attribute if they are valid as per form definition.. if any field makes is_valid() false then also cleaned_data exist and contains those data which was validated previously.

Related

How mock a method with parameters and return an iterable in python

I need to test this function with a unit test:
def nlp_extraction(texts, nlp=None):
extr = []
for doc in nlp.pipe([texts]):
extr.append(list([ent.label_, ent.text]) for ent in doc.ents)
extracao = [list(extr[i]) for i in range(len(extr))]
extracao = list(chain.from_iterable(extracao))
extracao = " ".join([item[1] for item in extracao])
return [texts, extracao]
I wrote, inicialy, this test and worked:
def test_nlp_extraction_entrada_correta():
nlp = loadModel('ner_extract_ingredients')
result_reference = ['xilitol', 'xilitol']
texts = 'xilitol'
result = nlp_extraction(texts, nlp)
assert result == result_reference
But in this test I need to load the model. As this is an unit test, I would like to mock the responses, thus load an external model can be disable. I am trying something like this (and a combination of the lines commented in the code):
def test_nlp_extraction_entrada_correta():
texts = 'xilitol'
doc = Mock(name="DOC")
ents = Mock(name="ENTS", label_='xilitol', text="xilitol")
doc.ents = [ents]
from nextmock import Mock
nlp = Mock()
nlp_mock = Mock()
nlp.with_args([texts]).returns([doc])
nlp_mock.pipe = nlp([texts])
# nlp_mock.pipe.with_args([texts]).returns(doc)
# nlp_mock.pipe = [Mock(return_value=doc)]
result = nlp_extraction(texts, nlp=nlp_mock)
assert result == result_reference
But an error always raise, saying that nlp.pipe([texts]) mock object is not iterable. So, I need to mock this part nlp.pipe([texts]) and return the doc object. How I can do this? Something I am missing in the proccess, can someone help me.
As Cpt.Hook said in comments, the solution was achieved using nlp.pipe.return_value = [doc].

ValueError: Cannot assign "(<User: testuser>,)": "Message.user" must be a "User" instance

Trying to create a simple real-time chat application with multiple chat rooms. When trying to add new message in the message model, getting ValueError.
The model.py:
class Message(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
room = models.ForeignKey(Room, on_delete=models.CASCADE)
body = models.TextField()
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
The view.py:
def addMessage(request):
user = User.objects.get(username = request.POST['username']),
room = Room.objects.get(id = request.POST['room_id']),
message = request.POST['message'],
Message.objects.create(
user = user,
room = room,
body = message
)
return HttpResponse("Message Sent Sucessfully!")
I have seen many similar questions, but none of the solutions worked.
Thanks.
Since you added trailing commas in the following lines
user = User.objects.get(username = request.POST['username']),
room = Room.objects.get(id = request.POST['room_id']),
message = request.POST['message'],
These variables become a python tuple and result in the error since the values are wrong types.
Remove those trailing commas should fix the issue.
user = User.objects.get(username = request.POST['username'])
room = Room.objects.get(id = request.POST['room_id'])
message = request.POST['message']

Not able to add a project ID/name to a list in Jira/Groovy/Scriptrunner

The following question I present is the following: I'm doing a code that is been tested in the Scriptrunner console and tried into Jira. What I want to reach is the following: I have an issue in any project, at any moment a workflow transition allows me to copy the current issue and all the linked issues in a new project.
The cfProjectPicker is a custom field Project Picker that allows me to select a project.
What I'm having trouble right now on the code is the following:
1- projectKey is returning null valor but projectPicker is returning the correct project selected in the custom field.
2- createNewIssue is not allowing me to recognize the "params" field as a mutable issue, and it is a mutable (if I replace with newIssue it does take it)
3- the list "issuesCopied" is not allowing me to issuesCopied.id.toString() but I'm guessing is giving error due to the point 2.
Any help regarding the matter would be much appreciated because I'm not understanding what I'm doing wrong at moment.
def issueMgr = ComponentAccessor.getIssueManager();
def issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def projectMgr = ComponentAccessor.getProjectManager();
def groupManager = ComponentAccessor.getGroupManager();
def issueFactory = ComponentAccessor.getIssueFactory();
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();
def cfProjectPicker = customFieldManager.getCustomFieldObjectByName("Seleccionar Proyecto") //cf project picker
def issue = issueMgr.getIssueObject("MART1-1")
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() //User Admin
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def currentIssue = issueFactory.getIssue()
//def issue = issueMgr.getIssueObject()
def newIssue = issueFactory.getIssue()
def exit = false
// Array list of the issues created to save them
List<String> issuesCopied = new ArrayList<>();
// ***** CREATE ISSUE *****
if (issuesCopied.isEmpty()){
String projectPicker = issue.getCustomFieldValue(cfProjectPicker)
log.error("Gengar")
log.error(projectPicker)
def projectKey = projectMgr.getProjectObjByKey(projectPicker)
log.error(projectKey)
newIssue.setProjectObject(projectKey)
newIssue.setIssueType(issue.getIssueType())
newIssue.setSummary(issue.getSummary())
newIssue.setDescription(issue.getDescription())
newIssue.setReporter(currentUser)
def params = ["issue":newIssue]
log.error(params)
def createNewIssue = issueMgr.createIssueObject(user,params);
issuesCopied.add(createNewIssue.id.toString())
}

Sum felds with with sqlalchemy and marshmallow

I'm using the python's framework pyramid, sqlalchemy for the data and marshmallow to serialize.
I'm want to send a sum of value to the client
I have Challenge and player whom use this challenge, each actions of the player is an event and each events has a duration.
For each challenge, i want to send the sum's duration for each player and also send this value only for one player. I try to use hybrid attribute or pre_dump function but i didn't succeeded
First method : with hybrid attribute
__tablename__ = "Challenge"
id = Column(Integer, primary_key=True)
name = Column(String(255), unique=True, nullable=False)
description = Column(TEXT(length=65535))
map_url = Column(String(255))
end_date = Column(DateTime(timezone=False))
alone_only = Column(Integer)
level = Column(String(255))
scalling = Column(Integer)
draft = Column(Boolean, server_default=text("0"))
admin_id = Column(Integer, ForeignKey("User.id"))
admin = relationship("User", backref="challenge_manager")
event_sum_user = relationship("Events")```
#hybrid_property
def event_sum(self):
return sum(Events.duration for Events in self.event_sum_user)
But i have sum for all user, not by user or for one user
Second method :with pre_dump method
id = fields.Int()
name = fields.Str()
description = fields.Str()
end_date = fields.DateTime()
alone_only = fields.Int()
level = fields.Str()
scalling = fields.Int()
draft = fields.Bool()
admin = fields.Nested(UserSchema)
admin_id = fields.Int(load_only=True)
event_sum = fields.Int(dump_only=True)
#pre_dump
def get_eventsum(self,data, **kwargs):
data["event_sum"] = DBSession.query(func.sum(Events.duration)).filter(Events.challenge_id==data["id"]).filter(Events.user_id==1).first()
return data```
With this method, i've have an error TypeError: 'Challenge' object is not subscriptable'
The purpose of this is to send with each challenge the total duration realise by a user or for each user on the challenge : id_user and total duration.
Thanks for your help
Marilyn

Flask Assign different ID to different website visitor at the same time and write their input information into json file

I am using FLASK to create a series of websites for doing a survey, I want to run the server once and invite multiple participants to help me fill the form on the webpage.
1.
I am aiming for assigning a unique ID (currently using python UUID) to different visitors and save their input info into different JSON files. (eg,. A's id is 00001, after he submiting the form, the info will be saved in the file named 00001.json, B's info will be saved in 00002.json). For now, I only create a UUID when the server start running, every visitor's information will be rewritten in the same file, (what I want is n visitors will produce n files).
2.
I want to show different survey questions to different visitors.
I am quite confused about if it is related to Multi-thread? Session? Cookie? Flask-login.
Hope someone can guide me on this. Thanks a lot!
assignmentID = str(uuid.uuid1())
jsonFileName = "upload/" + assignmentID + ".json"
jsonSurvey = "upload/" + assignmentID + "_survey.json"
#it ideally should randomly select different info/question no. to different user
Idx = random.sample(range(0,10), 5)
#app.route("/")
def index():
return render_template("surveyPage.html", data=Idx)
# write input information from webpage to JSON files, each visitor ideally should have their own JSON file.
#app.route("/record")
def recordData():
if request.method == 'POST':
print("READING FORM")
with open(jsonSurvey, 'w') as f:
json.dump(request.form, f)
f.close()
if __name__ == "__main__":
app.config['JSON_SORT_KEYS'] = False
app.run(debug = True)
# app.config["TEMPLATES_AUTO_RELOAD"] = True
You are defining assignmentID, jsonFileName, jsonSurvey and Idx outside of the request handlers which means they will be set once: when the server starts. jsonSurvey will therefore have the same value for each request which means that every new survey you store will overwrite the previous one.
The solution is to move the definition into the request handlers so that you genereate new values on each request.
#app.route("/")
def index():
Idx = random.sample(range(0,10), 5)
return render_template("surveyPage.html", data=Idx)
#app.route("/record")
def recordData():
if request.method == 'POST':
assignmentID = str(uuid.uuid1())
jsonFileName = "upload/" + assignmentID + ".json"
jsonSurvey = "upload/" + assignmentID + "_survey.json"
print("READING FORM")
with open(jsonSurvey, 'w') as f:
json.dump(request.form, f)
f.close()

Resources