I have this user 'u' when i try :
In [4]: u.get_all_permissions()
Out[4]: {u'jctracker.supervisor_dashboard'}
but when i try :
In [5]: u.has_perm(u,"jctracker.supervisor_dashboard")
Out[5]: False
what going wrong here? this problem is not letting in an user "u" to pass permission_required decorator
help please!!
has_perm receives permission as first argument, so you just need to remove 'u' from your function call:
u.has_perm("jctracker.supervisor_dashboard")
Related
I am testing my code in django and i have a case where i want to test if it is NOT equal. How can i do that ?
i am using the doc from django here :
https://docs.djangoproject.com/fr/2.2/topics/testing/tools/
Another example in react we have ".not" to say not equal.
I tried with '.not'
class SimpleTest(TestCase):
def test_details(self):
response = self.client.get('/customer/details/')
self.assertEqual(response.status_code, 200).not()
I expected to have one condition who test the no egality between my variable and my status code.
Thank for your help.
Solution from #DanielRoseman is working.
Used assertNotEqual fixed the problem
Doc: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertNotEqual
Here are two very basic examples of the problem that I am currently encountering in my work:
import re
type(re.search('(Bob)(.*)(Builder)', "Bob the Builder" , re.IGNORECASE))
returns _sre.SRE_Match and
import re
type(re.search('(Bob)(.*)(Builder)', "the Builder" , re.IGNORECASE))
returns NoneType. How can I test for the condition whether the data type is _sre.SRE_Match or NoneType? When I try implementing the code
import re
type(re.search('(Bob)(.*)(Builder)', "the Builder" , re.IGNORECASE)) is None
it returns False when it should be returning True. What am I doing incorrect here? Thank you!
type(None) is not None, it's NoneType. But you don't need type here, just use a bool:
if re.search(....):
print 'found!'
Actually, I was able to figure it out after attempting to search for solutions online:
import re
if re.search('(Bob)(.*)(Builder)', "the Builder" , re.IGNORECASE):
print 1
else:
print 2
I am writing a script to ask user to enter a date. If it is a date format, then return the entry; otherwise, continue. But my code doesn't stop even the user input is valid. Could somebody please share some insight? Thanks!
def date_input(prompt):
while True:
date_text = raw_input(prompt)
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
return date_text
except:
print('Invalid input.')
continue
You should never use except, always check for a specific exception:
except ValueError:
Then your real error should come through. I suspect you didn't import datetime.
In my urls.py file, I currently have the following resolver :
url(r'$', 'methodology_home')
url(r'^(?P<methodology_type\w+)/$', 'methodology_home')
I'd like the merge the two by saying that methodology_type is optional (and by specifying an argument with a default value in my view).
Is it feasible by simply adding a ? in the regexp ? I thought so at first but I was unable to make it work.
Does someone has an idea ?
Thanks.
You can write a regex to catch an optional url paramater
url(r'^(?:(?P<methodology_type>\w+)/?)?$', 'methodology_home')
One issue with this approach, though, is that your view will get None if no parameter is given. That's because the regex will in fact match and the value of the group is None:
import re
pattern = re.compile(r'^(?:(?P<methodology_type>\w+)/?)?$')
pattern.match('foo').groupdict()
>>> {'methodology_type': 'foo'}
pattern.match('').groupdict()
>>> {'methodology_type': None}
So you either handle it in your view code:
def my_view(request, methodology_type):
methodology_type = methodology_type or 'default'
...
or you stick with the two separated rules you already have.
I came across Django request.session; I know how to set and test it for a specific value.
request.session['name'] = "dummy"
and somewhere I check
if request.session['name'] == "dummy" :
#do something
But, now I have to check whether the session variable was even set in the first place? I mean how can I check whether there exists a value in the request.session['name'] is set?
Is there a way to check it?
Treat it as a Python dictionary:
if 'name' in request.session:
print request.session['name']
How to use sessions: Django documentation: How to use sessions
get will do all the work for you. Check if the key exists, if not then the value is None
if request.session.get('name', None) == "dummy":
print 'name is = dummy'
Another way of doing this is put it in try.
In this the third example code snippet
if you apply del operation in a session variable it which does not exist, so it throws KeyError.
so check it like this.
try:
print(request.session['name'])
except KeyError:
print('name variable is not set')