Why "InvalidRequest" on 2nd hit of page 2 of pagination? - google-app-engine
I do pagination and a link that works to page 2 looks like this
/q?query=regionID%3D4700188&cursor=False:CqoECuYBCtgB_wDAQM8HgAD_AP8A_wD__wDAQCQA_wD_AP8A_wD_AP__AMBAHAD_AP8A_wD_AP8A___-9wllzNf_Z_-_jIGSkJGLnpCPjZCVmpyL_wB0baCgmYuMoKD_AF2ej4-akZiWkZr_AHN0bZaRm5qH_wBdnpuM_wBzdG2bkJyglpv_AF2emMaFmZLOiZ2RrZedzL2Gnc2Pk6bMrYa8iIysvpS5lLi0nsrOiLKy_wBzf56YxoWZks6JnZGtl53MvYadzY-TpsythryIjKy-lLmUuLSeys6IsrL_AP_-EDIh4o_3av1KJSkSDURvY3VtZW50SW5kZXganwEoQU5EIChJUyAiY3VzdG9tZXJfbmFtZSIgImFwcGVuZ2luZSIpIChJUyAiZ3JvdXBfbmFtZSIgInN-bW9udGFvcHJvamVjdCIpIChJUyAibmFtZXNwYWNlIiAiIikgKElTICJpbmRleF9uYW1lIiAiYWRzIikgKEVRIDQ3MDAxODguMDAwMDAwMDAwMDAgInNuYnJfcmVnaW9uSUQiKSk6GwoOKE4gc2RhdGVfZGF0ZSkQARkAAABzFPtzQjoaCg0oTiBzbmJyX2hvdXIpEAEZAAAAAAAAEEA6HAoPKE4gc25icl9taW51dGUpEAEZAAAAAAAAMkBKHAgBOhVzdDpidGlfZ2VuZXJpY19zY29yZXJAkE5SGQoMKE4gb3JkZXJfaWQpEAEZAAAAAAAA8P8
But every now and then, seemingly random, I get error message that doesn't seem to be a timeout but an error with the query. When I hit reload I got this error, is it a timeout error?
"GET /q?query=regionID%3D4700188&cursor=False:CqoECuYBCtgB_wDAQM8HgAD_AP8A_wD__wDAQCQA_wD_AP8A_wD_AP__AMBAHAD_AP8A_wD_AP8A___-9wllzNf_Z_-_jIGSkJGLnpCPjZCVmpyL_wB0baCgmYuMoKD_AF2ej4-akZiWkZr_AHN0bZaRm5qH_wBdnpuM_wBzdG2bkJyglpv_AF2emMaFmZLOiZ2RrZedzL2Gnc2Pk6bMrYa8iIysvpS5lLi0nsrOiLKy_wBzf56YxoWZks6JnZGtl53MvYadzY-TpsythryIjKy-lLmUuLSeys6IsrL_AP_-EDIh4o_3av1KJSkSDURvY3VtZW50SW5kZXganwEoQU5EIChJUyAiY3VzdG9tZXJfbmFtZSIgImFwcGVuZ2luZSIpIChJUyAiZ3JvdXBfbmFtZSIgInN-bW9udGFvcHJvamVjdCIpIChJUyAibmFtZXNwYWNlIiAiIikgKElTICJpbmRleF9uYW1lIiAiYWRzIikgKEVRIDQ3MDAxODguMDAwMDAwMDAwMDAgInNuYnJfcmVnaW9uSUQiKSk6GwoOKE4gc2RhdGVfZGF0ZSkQARkAAABzFPtzQjoaCg0oTiBzbmJyX2hvdXIpEAEZAAAAAAAAEEA6HAoPKE4gc25icl9taW51dGUpEAEZAAAAAAAAMkBKHAgBOhVzdDpidGlfZ2VuZXJpY19zY29yZXJAkE5SGQoMKE4gb3JkZXJfaWQpEAEZAAAAAAAA8P8 HTTP/1.1" 200 8611 "http://www.koolbusiness.com/q?query=regionID%3D4700188" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36" "www.koolbusiness.com" ms=133 cpu_ms=42 cpm_usd=0.060962 app_engine_release=1.8.1 instance=00c61b117cd1983617eb4b0968a84b71874563
D 2013-07-06 06:20:37.095
query regionID=4700188
E 2013-07-06 06:20:37.120
Search failed
Traceback (most recent call last):
File "/base/data/home/apps/s~montaoproject/2013e.368581150756737282/search_demo.py", line 87, in find_documents
return index.search(query)
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 2732, in search
_CheckStatus(response.status())
File "/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 413, in _CheckStatus
raise _ERROR_MAP[status.code()](status.error_detail())
InvalidRequest: Failed to execute search request "regionID=4700188"
Code
def find_documents(query_string, limit, cursor):
try:
date_desc = search.SortExpression(expression='date',
direction=search.SortExpression.DESCENDING,
default_value=datetime.now().date())
hr_desc = search.SortExpression(expression='hour',
direction=search.SortExpression.DESCENDING,
default_value=datetime.now().hour)
min_desc = search.SortExpression(expression='minute',
direction=search.SortExpression.DESCENDING,
default_value=datetime.now().minute)
# Sort up to 5000 matching results by subject in descending order
sort = search.SortOptions(expressions=[date_desc, hr_desc,
min_desc], limit=10000)
# Set query options
options = search.QueryOptions(limit=limit, cursor=cursor,
sort_options=sort, number_found_accuracy=10000) # the number of results to return
# returned_fields=['author', 'subject', 'summary'],
# snippeted_fields=['content']
query = search.Query(query_string=query_string, options=options)
index = search.Index(name=_INDEX_NAME)
# Execute the query
return index.search(query)
except search.Error:
logging.exception('Search failed')
return None
regions_in = [
('3', u'Entire India'),
('4703187', u'Andaman & Nicobar Islands'),
('4694186', u'Andhra Pradesh'),
('4699188', u'Arunachal Pradesh'),
('4692186', u'Assam'),
('4702186', u'Bihar'),
('4698185', u'Chandigarh'),
('4676188', u'Chhattisgarh'),
('4691190', u'Dadra & Nagar Haveli'),
('4704183', u'Daman & Diu'),
('4699183', u'Delhi'),
('4702187', u'Goa'),
('4691189', u'Gujarat'),
('4700186', u'Haryana'),
('4703185', u'Himachal Pradesh'),
('4694187', u'Jammu & Kashmir'),
('4699189', u'Jharkhand'),
('4701185', u'Karnataka'),
('4695189', u'Kerala'),
('4700189', u'Lakshadweep'),
('4697186', u'Madhya Pradesh'),
('4694184', u'Maharashtra'),
('4700187', u'Manipur'),
('4703186', u'Meghalaya'),
('4698184', u'Mizoram'),
('4692187', u'Nagaland'),
('4696185', u'Orissa'),
('4676189', u'Pondicherry'),
('4693185', u'Punjab'),
('4701186', u'Rajasthan'),
('4701187', u'Sikkim'),
('4701188', u'Tamil Nadu'),
('4697187', u'Tripura'),
('4699190', u'Uttaranchal'),
('4692188', u'Uttar Pradesh'),
('4700188', u'West Bengal'),
]
class RegionSearch(SearchBaseHandler):
"""Handles regional search requests."""
def get(self):
"""Handles a get request with a query."""
category = None
cityentity = None
next_cursor = None
country = ''
if self.request.host.find('hipheap') > -1: country = 'USA'
elif self.request.host.find('koolbusiness') > -1: country = 'India'
elif self.request.host.find('montao') > -1: country = 'Brasil'
number_returned = 0
regionname = None
cityname = None
regionentity = None
region = None
cursor = self.request.get('cursor')
uri = urlparse(self.request.uri)
query = ''
regionID = regionid = self.request.get('regionid', 0)
cityID = cityid = self.request.get('cityid', 0)
categoryID = categoryid = self.request.get('category', 0)
if uri.query:
query = parse_qs(uri.query)
try:
query = query['query'][0]
except KeyError, err:
query = ''
# logging.info('KeyError')
# Try find region ID and/or cityID and categoryID a.s.a.p.
if regionid or query.find('regionID') > -1:
regionID = re.sub("^regionID=(\d+).*", r'\1', query)
region = Region.get_by_id(long(regionID))
regionname = region.name
if regionid:
regionID = regionid
region = Region.get_by_id(long(regionID))
regionname = region.name
if cityid or query.find('cityID') > -1:
cityID = re.sub("^.*cityID=(\d+).*", r'\1', query)
if cityid: cityID = cityid
city = montaomodel.City.get_by_id(long(cityID))
cityID = city.key().id()
cityentity = city
cityname = city.name
region = Region.get_by_id(long(city.region.key().id()))
regionID = region.key().id()
if categoryid or query.find('category') > -1:
categoryID = re.sub("^.*category=(\d+).*", r'\1', query)
if categoryid: categoryID = categoryid
logging.debug('query %s', query)
if cursor: results = find_documents(query, 50, search.Cursor(cursor))
else: results = find_documents(query, 50, search.Cursor())
if results and results.cursor: next_cursor = results.cursor.web_safe_string
namedquery = query
query = query.replace(' and company_ad=0', ''
).replace(' and company_ad=1', ''
).replace(' and category:(6010 OR 6020 OR 6030 OR 6040 OR 6090)'
, ''
).replace(' and category:(6010 OR 6020 OR 6030 OR 6040 OR 6090)'
, ''
).replace(' and category:(1020 OR 1010 OR 1030 OR 1050 OR 1080 OR 1100 OR 1090)'
, ''
).replace(' and category:(2010 OR 2030 OR 2040 OR 2080 OR 2070)'
, ''
).replace(' and category:(3040 OR 3050 OR 3030 OR 3060)'
, ''
).replace(' and category:(4010 OR 4020 OR 4040 OR 4030 OR 4090 OR 4060 OR 4070)'
, '')
query = re.sub("regionID=\d+", '', query)
query = query.replace('category and', '')
query = query.replace('type=s', '')
query = query.replace('type=w', '')
query = query.replace('type=r', '')
query = query.replace('type=b', '')
query = query.replace('cityID and', '')
query = query.replace('and ', '')
query = query.replace(' and', '')
query = query.replace('regionID', '')
query = query.replace('=', '%3D')
namedquery = namedquery.replace('=', '%3D')
query = re.sub("cityID%3D\d+", '', query)
query = re.sub("category%3D\d+", '', query)
query = query.replace(' ', ' ')
# to do: make into a dictionary for O(1) access
if int(regionid) > 0: regionname = region_id_to_name[regionid]
#if regionID and query.find('cityID') < 1:
# region = Region.get_by_id(long(regionID))
form = SearchForm()
form.w.choices = [
('4703187', u'Andaman & Nicobar Islands'),
('4694186', u'Andhra Pradesh'),
('4699188', u'Arunachal Pradesh'),
('4692186', u'Assam'),
('4702186', u'Bihar'),
('4698185', u'Chandigarh'),
('4676188', u'Chhattisgarh'),
('4691190', u'Dadra & Nagar Haveli'),
('4704183', u'Daman & Diu'),
('4699183', u'Delhi'),
('4702187', u'Goa'),
('4691189', u'Gujarat'),
('4700186', u'Haryana'),
('4703185', u'Himachal Pradesh'),
('4694187', u'Jammu & Kashmir'),
('4699189', u'Jharkhand'),
('4701185', u'Karnataka'),
('4695189', u'Kerala'),
('4700189', u'Lakshadweep'),
('4697186', u'Madhya Pradesh'),
('4694184', u'Maharashtra'),
('4700187', u'Manipur'),
('4703186', u'Meghalaya'),
('4698184', u'Mizoram'),
('4692187', u'Nagaland'),
('4696185', u'Orissa'),
('4676189', u'Pondicherry'),
('4693185', u'Punjab'),
('4701186', u'Rajasthan'),
('4701187', u'Sikkim'),
('4701188', u'Tamil Nadu'),
('4697187', u'Tripura'),
('4699190', u'Uttaranchal'),
('4692188', u'Uttar Pradesh'),
('4700188', u'West Bengal'),
]
if region or cityentity:
# to do:use memcache
form.area.choices = [] # to do: use memcache for the list
for cityitem in City.all().filter('region =',
region.key()).order('-vieworder').order('name'
).fetch(99999):
form.area.choices.append([str(cityitem.key().id()),
cityitem.name])
if cityentity:
form.area.data = str(cityentity.key().id())
if self.request.host.find('hipheap') > -1:
if region and (str(region.key().id()), region.name) \
in form.w_us.choices:
form.w_us.choices.remove((str(region.key().id()),
region.name))
else:
if region and (str(region.key().id()), region.name) \
in form.w.choices:
form.w.choices.remove((str(region.key().id()),
region.name))
if region: regionname = region.name
if results: number_returned = len(results.results)
template_values = {
'results': results,'regionname':regionname,
'cursor': next_cursor,
'country': country,
'user': self.current_user,
'number_returned': number_returned,
'loggedin': self.logged_in,
'VERSION': VERSION,
'region': region,
'regionname': regionname,
'jobs_count': get_jobs_count(self, regionID, cityID),
'estate_count': get_estate_count(self, regionID, cityID),
'electronics_count': get_electronics_count(self, regionID,
cityID),
'home_count': get_home_count(self, regionID, cityID),
'leisure_count': get_leisure_count(self, regionID, cityID),
'vehicles_count': get_vehicles_count(self, regionID,
cityID),
'cityentity': cityentity,
'request': self.request,
'categoryID': categoryID,
'form': form,
'query': query,
'namedquery': namedquery,
'cityname': cityname,
'category': category,
}
self.render_template('q.htm', template_values)
class India(SearchBaseHandler):
def get(self):
"""Handles a get request with a query."""
regionname = None
country = 'India'
cursor = self.request.get('cursor')
region = None
if self.request.host.find('hipheap') > -1: country = 'USA'
elif self.request.host.find('koolbusiness') > -1: country = 'India'
elif self.request.host.find('montao') > -1: country = 'Brasil'
uri = urlparse(self.request.uri)
query = ''
if uri.query:
query = parse_qs(uri.query)
try:
query = query['query'][0]
except KeyError, err:
query = ''
# logging.info('KeyError')
if cursor:
results = find_documents(query, 50, search.Cursor(cursor))
else:
results = find_documents(query, 50, search.Cursor())
next_cursor = None
if results and results.cursor: next_cursor = results.cursor.web_safe_string
query = query.replace(' and company_ad=0', ''
).replace(' and company_ad=1', '')
regionname = 'Entire India'
regionID = 0
cityID = 0
form = SearchForm()
form.w.choices = region_id_to_name
template_values = {
'regions':region_id_to_name,
'form': form,
'results': results,
'cursor': next_cursor,
'region': region,
'country': country,
'number_returned': len(results.results),
'jobs_count': get_jobs_count_india(self, regionID, cityID),
'estate_count': get_estate_count_india(self, regionID,
cityID),
'electronics_count': get_electronics_count_india(self,
regionID, cityID),
'home_count': get_home_count_india(self, regionID, cityID),
'leisure_count': get_leisure_count_india(self, regionID,
cityID),
'vehicles_count': get_vehicles_count_india(self, regionID,
cityID),
'user': users.get_current_user(),
'loggedin': self.logged_in,
'region': region,
'regionname': regionname,
'city': '',
'cityentity': None,
'request': self.request,
'form': SearchForm(),
'query': query,
}
self.render_template('q.htm', template_values)
The log file where the first hit is OK and the reload fails is here.
Update
The bug is changing. Now it just didn't happen until the third hit of the 2nd page. It doesn't make sense at all not to encounter a pagination error until the third hit of the second page. This is absurd so tell me what is going on and how to investigate the trace? I changed the code to say more about the trace but python doesn't inform me what it is:
It's not only RegionSearch that's failing, it's also search for empty string on pages after page 2. Since it never happens on the first page I suspect this must have something to do with the cursorbut I've been totally unable to figure out what is wrong with the cursor. The exception doesn't tell me anything, it just says ´this is not working´and nothing about why. I changed handling but it didn't generate any more output about the error: `return
index.search(query)
except search.Error as e:
logging.exception('Search failed %s', e)`
It does occur randomöly without seeming to be a timeout and the exception does not inform about the problem, all it says is "search failed" and the query that fails, a query which works if I put it in the search field. So it doesnt make sense anyhow.
I can only guess that it's RegionSearch that's failing.
You'll need to handle exceptions on your queries.
See https://developers.google.com/appengine/docs/python/datastore/exceptions
The exception will tell you what the error is. Hard to guess.
EDIT:
try:
# do query
except search.PutError as e:
logging.exception('caught PutError %s', e)
except search.InternalError as e:
logging.exception('caught InternalError %s', e)
except search.DeleteError as e:
logging.exception('caught DeleteError %s', e)
except search.TransientError as e:
logging.exception('caught TransientError %s', e)
except search.InvalidRequest as e:
logging.exception('caught InvalidError %s', e)
except search.Error as e:
logging.exception('caught unknown error %s', e)
Try setting the SortExpression's default_value to None, that worked for me.
I was getting the same issue in my Test/QA instance however in my PROD instance worked fine, setting the default_value to None solved the problem in the Test/QA instance for good.
Related
Dynamic query to perform SOQL action not working
I'm trying to make a dynamic soql query with few parameters, but have not been able to execute it properly string knowledgeQuery = ''; string publishStatusOnline = 'Online'; // At the moment 'Knowledge__kav' only supports two languages 'is-is' and 'en-us' if(language == 'is-is'){ language = 'is'; knowledgeQuery = 'SELECT Id, Customer_Facing_Instruction__c FROM Knowledge__kav WHERE UrlName = :name AND PublishStatus = :publishStatusOnline AND language = :language ORDER BY VersionNumber DESC LIMIT 1'; } else { knowledgeQuery = 'SELECT Id, Customer_Facing_Instruction__c FROM Knowledge__kav WHERE UrlName = :name AND PublishStatus = :publishStatusOnline ORDER BY VersionNumber DESC LIMIT 1'; } System.debug(knowledgeQuery); Knowledge__kav article = Database.query(knowledgeQuery); return article; Tke knowledgeQuery string looks like this SELECT Id, Customer_Facing_Instruction__c FROM Knowledge__kav WHERE UrlName = :name AND PublishStatus = :publishStatusOnline ORDER BY VersionNumber DESC LIMIT 1
After checking the code, because there is really nothing dynamic, I was wondering the need for the dynamic query. I guess you could achieve basically the same by doing: Knowledge__kav article; String publishStatusOnline = 'Online'; // At the moment 'Knowledge__kav' only supports two languages 'is-is' and 'en-us' if(language == 'is-is'){ language = 'is'; article = [SELECT Id, Customer_Facing_Instruction__c FROM Knowledge__kav WHERE UrlName = :name AND PublishStatus = :publishStatusOnline AND language = :language ORDER BY VersionNumber DESC LIMIT 1]; } else { article = [SELECT Id, Customer_Facing_Instruction__c FROM Knowledge__kav WHERE UrlName = :name AND PublishStatus = :publishStatusOnline ORDER BY VersionNumber DESC LIMIT 1]; } return article;
Select ID from table2 insert into table1
I have two tables - table Kontrahent and table Orders. First, I insert data into the Kontrahent table, then into the Orders table. Inserting into Orders, in the IDAccount column I have to insert the value from the IDKontrahent column from the Kontrahent table. How? try { //Wstawianie nowego kontrahenta $query = "INSERT INTO dbo.Kontrahent (Nazwa,Odbiorca,Dostawca,NIP,Regon,Uwagi,KodPocztowy,Miejscowosc,UlicaLokal,AdresWWW,Email,Utworzono,Zmodyfikowano,Telefon,Fax,OsobaKontaktowa,Pracownik,IDKraju,NrKonta,SWIFT,NazwaBanku,IDPaymentType,Archiwalny,IDRodzajuTransportu,SupplyCity,UlicaDostawy,KodPocztowyDostawy,NazwaAdresuDostawy,OsobaKontaktowaDostawy,TelefonDostawy,IDPlatnikaVat,CzyFirma,CzyGlownaFirma,NazwaKonta,LimitKredytu,IDPriceList) VALUES ('$complete_billing_name',1,0,'','','','$billing_postcode','$billing_state','$billing_address','','$billing_email','$isoDate','$ModDate',$billing_phone,'',NULL,0,616,'','','',NULL,0,NULL,'','','','','','',NULL,NULL,NULL,NULL,NULL,NULL)"; $result = $conn->prepare($query); var_dump($result); unset($query); } catch (Exception $e) { die(print_r($e->getMessage())); } This not working : try{ $select = "SELECT IDENT_CURRENT(Kontrahent) "; $result22 = $conn->query($select); $result22 ->execute(); $query = "INSERT INTO dbo.Orders (IDOrderType, IDAccount, Number, IDOrderStatus, IDPaymentType, Remarks, IDUser, IDWarehouse, IDCurrency, IDCompany) VALUES (15,$result22,$customer_id,2,1,NULL,1,10,1,1)"; $result = $conn->query($query); var_dump($result); unset($result); } catch (Exception $e) { die(print_r($e->getMessage())); } EDIT: The current code looks like this: // Inserting data into Order: // IDOrder (broadcast automatically) // Set OrderTypeID = 15 for orders from customers (14 orders from suppliers, 16 are offers) // IDAccount - is the customer ID from the Contractor table // Number - is the document number, e.g. order number from presta // IDOrderStatus - is the current status of the order from the OrderStatus table (e.g. Open or you can add your own ..) // IDPaymentType - this is the payment method for the order (from the PaymentTypes table) // Remarks - Order notes // IDUser - what user creates this entry, e.g. 1 = Admin (Users table) // IDWarehouse - in which warehouse to create the order (ID from the Warehouse table) // IDCurrency = 1 for PLN // IDCompany = 1 // Get ID $query = "SELECT IDENT_CURRENT('dbo.Kontrahent') AS ID"; $stmt = $conn->prepare($query); if ($stmt->execute() === false) { die("Error executing query."); }; $result = $stmt->fetch(PDO::FETCH_ASSOC); $id = $result; var_dump($result); print_r($id); $stmt = null; // Insert into Orders $query = " INSERT INTO dbo.Orders (IDOrderType, IDAccount, Number, IDOrderStatus, IDPaymentType, Remarks, IDUser, IDWarehouse, IDCurrency, IDCompany) VALUES (15, ?, ?, 2, 1, NULL, 1, 10, 1, 1) "; $stmt = $conn->prepare($query); $stmt->bindParam(1, $id, PDO::PARAM_INT); $stmt->bindValue(2, $customer_id, PDO::PARAM_INT); if ($stmt->execute() === false) { die("Error executing query."); }; print_r($stmt); $stmt = null; // Get ID Order from Orders $query = "SELECT TOP 1 IDOrder FROM dbo.Orders ORDER BY IDOrder DESC"; //$query ="SELECT IDENT_CURRENT('dbo.Orders') AS IDTowaru"; $stmt = $conn->prepare($query); if ($stmt->execute() === false) { die("Error executing query."); }; $result = $stmt->fetch(PDO::FETCH_ASSOC); $idorder = $result; var_dump ($idorder, $result); $stmt = null; Search for IDTowaru by KodKreskowy. the KodKreskowy is only one identical in the store and application. $query = "SELECT IDTowaru FROM dbo.Towar WHERE KodKreskowy = '$product_sku' "; $stmt = $conn->prepare($query); if ($stmt->execute() === false) { die("Error executing query."); }; $result = $stmt->fetch(PDO::FETCH_ASSOC); $iditem = $result; var_dump($iditem); $stmt = null; Inserting OrderLines with IDItem from Towar table and IDOrder from Orders table // Inserting into OrderLines: // IDOrderLine (broadcast automatically) // IDOrder - Id of the order header from the Order table // IDItem -ID of the item from the Item or Current Status table // Quantity - Quantity of the item // PriceNet - net price // PriceGross - gross price including VAT // IDVat - VAT rate ID from the VATRates table // Remarks - any additional comments // IDUser - what user creates this entry, e.g. 1 = Admin (Users table) $query = " INSERT INTO dbo.OrderLines (IDItem, IDOrder, Quantity, PriceNet, PriceGross, IDVat, Remarks, IDUser) VALUES (?, ?, ?, ?, ?, 1, 1, 1) "; $stmt = $conn->prepare($query); $stmt->bindParam(1, $iditem, PDO::PARAM_INT); $stmt->bindParam(2, $idorder, PDO::PARAM_INT); $stmt->bindValue(3, $quantity, PDO::PARAM_INT); $stmt->bindValue(4, $product_price, PDO::PARAM_INT); $stmt->bindValue(5, $product_price, PDO::PARAM_INT); if ($stmt->execute() === false) { die("Error executing query."); }; $stmt = null; } catch (Exception $e) { die(print_r($e->getMessage())); } } This is error : SQLSTATE[HY000]: General error: 20018 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_OrderLines_Towar". The conflict occurred in database "greenmonkey", table "dbo.Towar", column 'IDTowaru'. [20018] (severity 16) [ INSERT INTO dbo.OrderLines (IDItem, IDOrder,Quantity, PriceNet, PriceGross, IDVat, Remarks, IDUser) VALUES (1, 1, 1, 42, 42, 1, 1, 1) ]1 Why IDAccount and Number is 1? object(PDOStatement)#14917 (1) { ["queryString"]=> string(225) " INSERT INTO dbo.Orders (IDOrderType, IDAccount, Number, IDOrderStatus, IDPaymentType, Remarks, IDUser, IDWarehouse, IDCurrency, IDCompany) VALUES (15, ?, ?, 2, 1, NULL, 1, 10, 1, 1) " } array(1) { ["IDTowaru"]=> float(99) } array(1) { ["IDTowaru"]=> int(825) } Next, why IDOrder and IDItem is 1? [ INSERT INTO dbo.OrderLines (IDItem, IDOrder,Quantity, PriceNet, PriceGross, IDVat, Remarks, IDUser) VALUES (1, 1, 1, 42, 42, 1, 1, 1) ]1
I think that you should consider at least the following: you need to prepare and execute your statements using PDO::prepare and PDOStatement::execute you need to use parameterized statements to prevent SQL injection issues and set each parameter value using PDOStatement::bindParam you need to fetch the result from your SELECT statement using PDOStatement::fetch The next example, based on your code, may help to find a solution to your problem: <?php try{ // Insert into Kontrahent $query = " INSERT INTO dbo.Kontrahent (Nazwa, Odbiorca, Dostawca, NIP, Regon, Uwagi, KodPocztowy, Miejscowosc, UlicaLokal, AdresWWW, Email, Utworzono, Zmodyfikowano, Telefon, Fax, OsobaKontaktowa, Pracownik, IDKraju, NrKonta, SWIFT, NazwaBanku, IDPaymentType, Archiwalny, IDRodzajuTransportu, SupplyCity, UlicaDostawy, KodPocztowyDostawy, NazwaAdresuDostawy, OsobaKontaktowaDostawy,TelefonDostawy, IDPlatnikaVat, CzyFirma, CzyGlownaFirma, NazwaKonta, LimitKredytu, IDPriceList) VALUES (?, 1, 0, '', '', '', ?, ?, ?, '', ?, ?, ?, ?, '', NULL, 0, 616, '', '', '', NULL, 0, NULL, '', '', '', '', '', '', NULL, NULL, NULL, NULL, NULL, NULL) "; $stmt = $conn->prepare($query); $stmt->bindParam(1, $complete_billing_name, PDO::PARAM_STR); $stmt->bindParam(2, $billing_postcode, PDO::PARAM_STR); $stmt->bindParam(3, $billing_state, PDO::PARAM_STR); $stmt->bindParam(4, $billing_address, PDO::PARAM_STR); $stmt->bindParam(5, $billing_email, PDO::PARAM_STR); $stmt->bindParam(6, $isoDate, PDO::PARAM_STR); $stmt->bindParam(7, $ModDate, PDO::PARAM_STR); $stmt->bindParam(8, $billing_phone, PDO::PARAM_STR); if ($stmt->execute() === false) { die("Error executing query."); }; $stmt = null; // Get ID $query = "SELECT IDENT_CURRENT('dbo.Kontrahent') AS ID"; $stmt = $conn->prepare($query); if ($stmt->execute() === false) { die("Error executing query."); }; $result = $stmt->fetch(PDO::FETCH_ASSOC); $id = $row['ID']; $stmt = null; // Insert into Orders $query = " INSERT INTO dbo.Orders (IDOrderType, IDAccount, Number, IDOrderStatus, IDPaymentType, Remarks, IDUser, IDWarehouse, IDCurrency, IDCompany) VALUES (15, ?, ?, 2, 1, NULL, 1, 10, 1, 1) "; $stmt = $conn->prepare($query); $stmt->bindParam(1, $id, PDO::PARAM_INT); $stmt->bindValue(2, $customer_id, PDO::PARAM_INT); if ($stmt->execute() === false) { die("Error executing query."); }; $stmt = null; } catch (Exception $e) { die(print_r($e->getMessage())); } ?>
How to create data to the database using views
I am trying to create order amount from the catalogue which works like a shopping cart but the amount returned is 1 for all orders made: views.py def get_user_pending_order(request): #get order from correct profile user_profile = get_object_or_404(Profile,user=request.user) order = Order.objects.filter(owner=user_profile,is_ordered=True) if order.exists(): #to get an order in the list of filtered orders return order[0] return 0 def add_to_catalogue(request,employee_id):#product_id,employee_id user_profile= get_object_or_404(Profile, user =request.user) order_to_purchase = get_user_pending_order(request) amount= self.order_to_purchase.get_catalogue_total(), employee = Employee.objects.get(pk=employee_id) if employee in request.user.profile.ebooks.all(): messages.info(request,'you already own this ebook') return redirect(reverse('freelance:employee_list')) order_task,status = OrderTask.objects.get_or_create(employee=employee) user_order,status = Order.objects.get_or_create(owner=user_profile, is_ordered=False,order_amount=amount)####TThis IS WHWERE TO EDIT TO PREVENT RE ORDERNG OF FREELANCING user_order.tasks.add(order_task) if status: user_order.ref_code = generate_order_id() user_order.save() messages.info(request,"task added to catalogue") return redirect(reverse('freelance:employee_list')) def get_user_pending_order(request): #get order from correct profile user_profile = get_object_or_404(Profile,user=request.user) order = Order.objects.filter(owner=user_profile,is_ordered=True) if order.exists(): #to get an order in the list of filtered orders return order[0] return 0 models.py class Order(models.Model): ref_code = models.CharField(max_length=15) owner = models.ForeignKey(Profile, on_delete=models.SET_NULL, null= True) is_ordered = models.BooleanField(default=False) tasks = models.ManyToManyField(OrderTask) date_ordered = models.DateTimeField(auto_now= True) order_amount = models.DecimalField(default=0.01, max_digits= 10, decimal_places=2) def order_tasks(self): return ','.join([str(c.employee) for c in self.tasks.all()]) def get_catalogue_tasks(self): return self.tasks.all() def get_catalogue_total(self): return sum([task.employee.pricing for task in self.tasks.all()]) def __str__(self): return '{0} - {1}'.format(self.owner, self.ref_code, self.order_amount) def tasks_summary(request): existing_order = get_user_pending_order(request) my_user_profile = Profile.objects.filter(user= request.user).first() my_orders = Order.objects.filter(is_ordered= True, owner= my_user_profile) order_to_purchase = get_user_pending_order(request) amount= order_to_purchase.get_catalogue_total(), order = Order.objects.filter(is_ordered= True) context = { 'my_orders':my_orders, 'order':order, 'amount':amount # 'total':total, } return render(request, 'freelance/tasks_summary.html',context)###Belongs to the admin sisde Output of the template I am getting this error when I try to add anything to the catalogue: AttributeError at /admin/tasks_summary/ 'int' object has no attribute 'get_catalogue_total'
Wagtail Snippets permissions per group
I have a Wagtail site where every group can work on a different page tree, with different images and documents permissions. That is a multisite setup where I am trying to keep sites really separate. Is that possible to limit the snippets permissions on a per-group basis? I would like my groups to see just a subset of the snippets.
I was facing something similar when I wanted to use Site settings. The only solution I found was to create a custom model and using ModelAdmin. Some ‘snippets’ to get you on the run: class SiteSettings(models.Model): base_form_class = SiteSettingsForm COMPANY_FORM_CHOICES = ( ('BED', 'Bedrijf'), ('ORG', 'Organisatie'), ('STI', 'Stichting'), ('VER', 'Vereniging'), ) site = models.OneToOneField( Site, unique = True, db_index = True, on_delete = models.CASCADE, verbose_name = _('site'), related_name = 'site_settings', help_text = _('The sites these setting belong to.') ) company_name = models.CharField( _('company name'), blank = True, max_length = 50, help_text = _('De naam van het bedrijf of de organisatie.') ) company_form = models.CharField( _('company form'), max_length = 3, blank = True, default = 'COM', choices = COMPANY_FORM_CHOICES ) ... class MyPermissionHelper(PermissionHelper): def user_can_edit_obj(self, user, obj): result = super().user_can_edit_obj(user, obj) if not user.is_superuser: user_site = get_user_site(user) result = user_site and user_site == obj.site return result class SiteSettingsAdmin(ThumbnailMixin, ModelAdmin): model = SiteSettings menu_label = _('Site settings') menu_icon = 'folder-open-inverse' add_to_settings_menu = True list_display = ['admin_thumb', 'company_name', 'get_categories'] list_select_related = True list_display_add_buttons = 'site' thumb_image_field_name = 'logo' thumb_col_header_text = _('logo') permission_helper_class = MyPermissionHelper create_view_class = CreateSiteSettingsView ... class CreateSiteSettingsView(SiteSettingsViewMixin, CreateView): #cached_property def sites_without_settings(self): sites = get_sites_without_settings() if not sites: messages.info( self.request, _('No sites without settings found.') ) return sites def dispatch(self, request, *args, **kwargs): if request.user.is_superuser and not self.sites_without_settings: return redirect(self.url_helper.get_action_url('index')) return super().dispatch(request, *args, **kwargs) def get_initial(self): initial = super().get_initial().copy() current_site = self.request.site initial.update({ 'company_name': current_site.site_name} ) if self.request.user.is_superuser: initial.update({ 'site': current_site} ) return initial def get_form(self): form = super().get_form() flds = form.fields if self.request.user.is_superuser: fld = form.fields['site'] fld.queryset = self.sites_without_settings.order_by( Lower('site_name') ) return form def form_valid(self, form): instance = form.save(commit=False) if not self.request.user.is_superuser: instance.site = self.request.site instance.save() messages.success( self.request, self.get_success_message(instance), buttons=self.get_success_message_buttons(instance) ) return redirect(self.get_success_url())
How to use cursors for search in gae?
When I RTFM, I can't understand how to specify paginated searches using the technique described in the manual. Here's my code: def find_documents(query_string, limit, cursor): try: subject_desc = search.SortExpression( expression='date', direction=search.SortExpression.DESCENDING, default_value=datetime.now().date()) # Sort up to 1000 matching results by subject in descending order sort = search.SortOptions(expressions=[subject_desc], limit=1000) # Set query options options = search.QueryOptions( limit=limit, # the number of results to return cursor=cursor, sort_options=sort, #returned_fields=['author', 'subject', 'summary'], #snippeted_fields=['content'] ) query = search.Query(query_string=query_string, options=options) index = search.Index(name=_INDEX_NAME) # Execute the query return index.search(query) except search.Error: logging.exception('Search failed') return None class MainAdvIndexedPage(SearchBaseHandler): """Handles search requests for comments.""" def get(self): """Handles a get request with a query.""" regionname = 'Delhi' region = Region.all().filter('name = ', regionname).get() uri = urlparse(self.request.uri) query = '' if uri.query: query = parse_qs(uri.query) query = query['query'][0] results = find_documents(query, 50, search.Cursor()) next_cursor = results.cursor template_values = { 'results': results,'next_cursor':next_cursor, 'number_returned': len(results.results), 'url': url, 'user' : users.get_current_user(), 'url_linktext': url_linktext, 'region' : region, 'city' : '', 'request' : self.request, 'form' : SearchForm(), 'query' : query } self.render_template('indexed.html', template_values) The code above works and does a search but it doesn't page the result. I wonder about the following code in the manual: next_cursor = results.cursor next_cursor_urlsafe = next_cursor.web_safe_string # save next_cursor_urlsafe ... # restore next_cursor_urlsafe results = find_documents(query_string, 20, search.Cursor(web_safe_string=next_cursor_urlsafe)) What is next_cursor used for? How do I save and what is the purpose of saving? How do I get a cursor in the first place? Should the code look something like this instead, using memcache to save an restore the cursor? class MainAdvIndexedPage(SearchBaseHandler): """Handles search requests for comments.""" def get(self): """Handles a get request with a query.""" regionname = 'Delhi' region = Region.all().filter('name = ', regionname).get() uri = urlparse(self.request.uri) query = '' if uri.query: query = parse_qs(uri.query) query = query['query'][0] # restore next_cursor_urlsafe next_cursor_urlsafe = memcache.get('results_cursor') if last_cursor: results = find_documents(query_string, 50, search.Cursor(web_safe_string=next_cursor_urlsafe)) results = find_documents(query, 50, search.Cursor()) next_cursor = results.cursor next_cursor_urlsafe = next_cursor.web_safe_string # save next_cursor_urlsafe memcache.set('results_cursor', results.cursor) template_values = { 'results': results,'next_cursor':next_cursor, 'number_returned': len(results.results), 'url': url, 'user' : users.get_current_user(), 'url_linktext': url_linktext, 'region' : region, 'city' : '', 'request' : self.request, 'form' : SearchForm(), 'query' : query } self.render_template('indexed.html', template_values) Update From what I see from the answer, I'm supposed to use an HTTP GET query string to save the cursor but I still don't know exactly how. Please tell me how. Update 2 This is my new effort. def get(self): """Handles a get request with a query.""" regionname = 'Delhi' region = Region.all().filter('name = ', regionname).get() cursor = self.request.get("cursor") uri = urlparse(self.request.uri) query = '' if uri.query: query = parse_qs(uri.query) query = query['query'][0] logging.info('search cursor: %s', search.Cursor()) if cursor: results = find_documents(query, 50, cursor) else: results = find_documents(query, 50, search.Cursor()) next_cursor = None if results and results.cursor: next_cursor = results.cursor.web_safe_string logging.info('next cursor: %s', str(next_cursor)) template_values = { 'results': results,'cursor':next_cursor, 'number_returned': len(results.results), 'user' : users.get_current_user(), 'region' : region, 'city' : '', 'request' : self.request, 'form' : SearchForm(), 'query' : query } I think that I've understood how it's supposed to work with the above, and it's outputting a cursor at the first hit so I can know how to get the cursor in the first place. This is clearly documented enough. But I get this error message: cursor must be a Cursor, got unicode
No, you should not use memcache for that, especially with a constant key like 'results_cursor' - that would mean that all users would get the same cursor, which would be bad. You are already passing the cursor to the template context (although you should be converting to the web_safe_string as you do in the second example). In the template, you should ensure that the cursor string is included in the GET parameters of your "next" button: then, back in the view, you should extract it from there and pass it into the find_documents call. Apart from the memcache issue, you're almost there with the second example, but you should obviously ensure that the second call to find_documents is inside an else block so it doesn't overwrite the cursor version.