Django-Oscar: Adding slugs to url - python

I would like to be able to add a the following: [(?P<slug>[-\w]+)] after /dashboard/' between that and the rest of any possible url that comes after, i.e. '/dashboard/ (?P<slug>[-\w]+) /catalogue/. The point of this would be to make it 'partner-specific' for my particular project.
How would I go about doing this? I currently am working directly with Oscar code for several reasons, meaning that I do not have to 'fork' any apps - I just change the Oscar code within my project. The strategy I am trying to accomplish this URL-change is to go into app.py and adding it there after 'dashboard', or going into dashboard/app.py and adding it before each url defined there.
Whenever I change I keep getting 'NoReverseMatch at ---' and errors like:
Reverse for 'MY-URL-IS-HERE' with no arguments not found. 1 pattern(s) tried: ['dashboard/(?P<slug>[-\\w]+)/logout/$']
I am familiar with this error, but now seem unable to ever locate the exact location of the error message. And whenever I do get the page to load (was able to when adding it before 'reviews'), I still get an error something like the following in the terminal:
Invalid URL name dashboard:reviews-list Traceback (most recent call
last): File
"/Users/myname/Desktop/Developer/wybe-all/wybe/apps/oscar/dashboard/nav.py",
line 83, in default_access_fn
If someone that has done this before perhaps could point me in the right direction, that would be greatly appreciated!
Best regards,
William Karlsson

Related

"TypeError: make_staticdir() got an unexpected keyword argument 'document_root'" running Diazo through Proxy

I exactly followed the steps on the Diazo quickstart guide hosted at docs.plone.org (docs.diazo.org is out of date as I write this in April 2015) and received a strange error when I attempted to execute the final command bin/gearbox serve --reload -c proxy.ini. This error prevented me from continuing and did not indicate where it was coming from.
The precise error that I received was:
File "eggs/PasteDeploy-1.5.2-py2.7.egg/paste/deploy/util.py", line 55, in fix_call
val = callable(*args, **kw)
TypeError: make_staticdir() got an unexpected keyword argument 'document_root'
Does anyone know how I can resolve this issue in order to proceed in developing my Plone theme?
It turns out that this error is caused by a mistake in the proxy.ini file due to which a suppressed error is thrown in the webobentrypoints library. To correct this, please change the third section of proxy.ini entitled "app:static" to use "path" instead of the incorrect "document_root" such that it now contains the following:
[app:static]
use = egg:webobentrypoints#staticdir
path = %(here)s/theme
Once you fix this issue, however, you will still need to fix one more problem: the page that the quickstart guide attempts to proxy has changed such that it will redirect your browser and not display your theme. I had good luck switching the final section of proxy.ini to the latest version of the same document now hosted at plone.org:
[app:content]
use = egg:webobentrypoints#proxy
address = http://docs.plone.org/external/diazo/docs/index.html
suppress_http_headers = accept-encoding connection
To properly theme this site you must now further modify the rules.xml file to render the class ".content-column" instead of ".content." It should look like:
<rules
xmlns="http://namespaces.plone.org/diazo"
xmlns:css="http://namespaces.plone.org/diazo/css"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<theme href="theme/theme.html" />
<drop css:content="#indices-and-tables" />
<replace css:theme-children="#content" css:content-children=".content-column" />
</rules>
Hopefully this will fix your problem.
Note: I have already submitted these changes for inclusion in the Diazo documentation but, especially given how many versions of the document can be easily found when looking around for help, it seems prudent to document this possible error in a central location.

In interpreting Jinja2 templates, why does code object have a null filename?

I'm trying to pick apart Jinja2's TemplateSyntaxError to see why it doesn't tell me the exact file name in which a syntax error is found.
I'm actually introducing this error in a sub-template on purpose to try to better understand this templating system. Upon getting the syntax error, I see File "<unknown>", line 4, in template in my Flask preview server. The line number is correct, but the debugger appears confused about the file from which the problem originated, which is very annoying. I'm uncertain as of yet what the name of the code object, template represents.
As someone has pointed out, the <unknown> is used here as a throwaway when the filename value of the code object is null. After reading through a few references for code objects, I've not yet had luck wrapping my head around this weirdness.
Someone appears to get a similar error in this github issue.
May it be that it's just an arbitrary value provided by Jinja2 to some dynamically generated code?
>>> code = compile('print("test")', '<unknown>', 'exec')
>>> code
<code object <module> at 0x1064b6e30, file "<unknown>", line 1>
>>> exec code
test
>>> code.co_filename
'<unknown>'
And there it seems to be indeed in jinja2/debug.py - translate_syntax_error.
The explanation of why that is is rather straightforward. The whole machinery, starting with flask.render_template_string through jinja2.Environment.from_string down to Jinja2 exception handlers does not take any concern about the origin of the template string passed in.
While it would be possible to tunnel some more information top down, what would be the benefit of it anyway? In case of an exception you get a complete stack trace with appropriate local information available on each level of it, including from where you passed in the string and the line number in the template string that erred, e.g.:
File "jinja2-uknown-filename.py", line 7, in index
return flask.render_template_string("this is a \n \n {% test %}")
...
File "<unknown>", line 3, in template
TemplateSyntaxError: Encountered unknown tag 'test'.

Using isbntools with web2py

I'm using web2py to create a page where I search for books based on title/author/keyword/etc. and ISBN, and I can't seem to figure out how to use isbntools in the webapp. I'm sure it's something basic that I'm missing out on, but this is the first webapp that I've ever created, and it's for a class project. This is the related portion of my controller:
from isbntools import *
def index():
form=SQLFORM.factory(
Field('title',label='Try entering a title:'),
Field('author',label='Or an author:'),
Field('ISBN',label='Even better if you have the ISBN'),
Field('fromDate',label='When is the earliest the book might have come out?'),
Field('toDate',label='...and the latest?'))
if form.process().accepted:
titledata = isbn_goom form.vars.title bibtex
authordata = isbn_goom form.vars.author bibtex
isbndata = isbn_meta merge form.vars.ISBN bibtex
print(titledata)
print(authordata)
print(isbndata)
return dict(form=form)
This is a portion of the ticket information I'm getting back:
Error ticket for "Bibbly"
Ticket ID
96.255.27.81.2014-05-01.21-50-27.f66e0b53-b5bd-4621-8dbd-b6f30e8a6af1
invalid syntax (default.py, line 21)
Version
web2py™ Version 2.8.2-stable+timestamp.2013.11.28.13.54.07
Python Python 2.7.5+: /usr/local/bin/uwsgi (prefix: /usr)
Traceback
line 21
titledata = isbn_goom "form.vars.title" bibtex
^
SyntaxError: invalid syntax
isbm_goom is a command line script. Is that what you want?! (you cannot use it in your code like that!)
I suggests you use the most recent version of isbntools and adapt this snipet
from isbntools.contrib.modules.goom import goom
from isbntools.dev.fmt import fmtbib
...
titledata = goom.query(form.vars.title)
for r in titledata:
print((fmtbib('bibtex', r)))
Since python is complaining about a syntax error, it means it's about the literal code you've written. The Python interpreter can't grasp what you mean because you've specified something "impossible" in the language. In this case it's about the white-space after isbn_goom and form.vars.title. Since i don't know and don't see any declaration concerning isbn_goom i assume it's from the isbntools library. To try the library it may be best to learn about it in a separate console session on your own machine. See the pypi page for some examples.
On resolving syntax errors: You can try editing the code in any decent code editor and it will give you hints on these kinds of errors. The default python editor Idle that comes with any default installation would be great.

BioPython Pubmed Eutils url?

I'm trying to run some queries against Pubmed's Eutils service. If I run them on the website I get a certain number of records returned, in this case 13126 (link to pubmed).
A while ago I bodged together a python script to build a query to do much the same thing, and the resultant url returns the same number of hits (link to Eutils result).
Of course, not having any formal programming background, it was all a bit cludgy, so I'm trying to do the same thing using Biopython. I think the following code should do the same thing, but it returns a greater number of hits, 23303.
from Bio import Entrez
Entrez.email = "A.N.Other#example.com"
handle = Entrez.esearch(db="pubmed", term="stem+cell[All Fields]",datetype="pdat", mindate="2012", maxdate="2012")
record = Entrez.read(handle)
print(record["Count"])
I'm fairly sure it's just down to some subtlety in how the url is being generated, but I can't work out how to see what url is being generated by Biopython. Can anyone give me some pointers?
Thanks!
EDIT:
It's something to do with how the url is being generated, as I can get back the original number of hits by modifying the code to include double quotes around the search term, thus:
handle = Entrez.esearch(db='pubmed', term='"stem+cell"[ALL]', datetype='pdat', mindate='2012', maxdate='2012')
I'm still interested in knowing what url is being generated by Biopython as it'll help me work out how i have to structure the search term for when i want to do more complicated searches.
handle = Entrez.esearch(db="pubmed", term="stem+cell[All Fields]",datetype="pdat", mindate="2012", maxdate="2012")
print(handle.url)
You've solved this already (Entrez likes explicit double quoting round combined search terms), but currently the URL generated is not exposed via the API. The simplest trick would be to edit the Bio/Entrez/__init__.py file to add a print statement inside the _open function.
Update: Recent versions of Biopython now save the URL as an attribute of the returned handle, i.e. in this example try doing print(handle.url)

ValueError: invalid literal for int() with base 10: 'MSIE'

After I run my Python code on a big file of only HTTP headers, it gives me the above error. Any idea what that means?
Here is a piece of the code:
users = output.split(' ')[1]
accesses = output.split(' ')[3]
ave_accesses = int(accesses)/int(users)
Basically the 'users' are users who have accessed a website and 'accesses' are the total number of accesses by the users to that site. The 'ave_accesses' gives the number of accesses to that site by an average user. I hope this is enough to clear things, if not I can explain more.
thanks a lot, Adia.
It means that you are trying to convert a string to an integer, and the value of the string is 'MSIE'. The traceback will have a filename near this error and the line number (e.g., /my/module.py:123). Open the file and go to the line the error occurred, you should see a call to int() with a parameter. That parameter is probably supposed to be a number in string form, but it's not. You probably got your parsing code a little wrong, and fields were mixed up.
To track down the problem, use print statements around the code to see what is not working as expected. You can also use pdb.
I think, your header output is garbled. It is obviously looking for a number where it is find an string MSIE (which may be the value for User-Agent).

Categories