Is there a way I can handle both the exceptions from different models and still pass none as context individually.
Views.py
class ProfilePage(DetailView):
model = models.UserCreation
context_object_name = 'profile'
def get_context_data(self, *args, **kwargs):
context = super(ProfilePage, self).get_context_data(*args, **kwargs)
user = User.objects.get(username=UserCreation.objects.get(id=self.kwargs.get('pk')))
print(self.request.user,user,self.kwargs.get('pk'))
try:
context['data'] = ProfileData.objects.get( user=user)
context['userdata'] = User.objects.get( username=user)
context['creationdata'] = UserCreation.objects.get(user=user)
context['friends'] = Friends.objects.get( user=self.request.user,added=user)
context['sorted'] = sorted(chain(AddStatus.objects.filter(user=user), ImageLib.objects.filter(user=user)),
key=lambda instance: instance.date, reverse=True)
except ((ProfileData.DoesNotExist as e) or (Friends.DoesNotExistas as f)) :
if e:
context['data']= None
elif f:
context['friends'] = None
return context
Yes, you should use two try-except scopes. In fact it is better not to write long try-except scopes, since then it is no longer clear what triggers the exception. So you can implement this like:
try:
context['data'] = ProfileData.objects.get( user=user)
except ProfileData.DoesNotExist:
context['data']= None
context['userdata'] = User.objects.get( username=user)
context['creationdata'] = UserCreation.objects.get( user=user)
try:
context['friends'] = Friends.objects.get( user=self.request.user,added=user)
except Friends.DoesNotExist:
context['friends'] = None
context['sorted'] = sorted(
chain(AddStatus.objects.filter(user=user),
ImageLib.objects.filter(user=user)),
key=lambda instance: instance.date, reverse=True
)
In case having multiple ProfileDatas, etc. is not a problem, you can make use of .first() instead that will return None if there is no row to return:
context['data'] = ProfileData.objects.filter(user=user).first()
context['userdata'] = User.objects.get(username=user)
context['creationdata'] = UserCreation.objects.get(user=user)
context['friends'] = Friends.objects.filter( user=self.request.user,added=user).first()
context['sorted'] = sorted(
chain(AddStatus.objects.filter(user=user),
ImageLib.objects.filter(user=user)),
key=lambda instance: instance.date, reverse=True
)
Related
We have migrated our Django project code base from Python 2.7 to 3.6 and suddenly what used to work stopped. Specifically, this:
map(functools.partial(self._assocUser, user=user), persistedGroupIds)
needed to be replaced with:
for group_id in persistedGroupIds:
self._assocUser(group_id, user)
and this:
persistedGroupIds = map(functools.partial(self._persistGroup, grp_mappings=attrAll.entitlements), saml_authorization_attributes)
needed to go to:
persistedGroupIds = []
for idp_group_name in saml_authorization_attributes:
persistedGroupIds.append(self._persistGroup(idp_group_name, attrAll.entitlements))
before the old functionality reappeared. Python 3's functools don't seem to work.
Here's the full listing of the code that works fine under Python 2:
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
import functools
from mappings import SAMLAttributesConfig
from django.conf import settings
import logging
log = logging.getLogger(__name__)
class SAMLServiceProviderBackend(object):
empty_entitlements_message="IdP supplied incorrect authorization entitlements. Please contact their support."
def _assocUser(self, group_id, user):
group = Group.objects.get(id=group_id)
group.user_set.add(user)
return None
def _persistGroup(self,idp_group_name, grp_mappings):
group_name = grp_mappings[idp_group_name]
try:
group = Group.objects.get(name=group_name)
except Group.DoesNotExist:
group = Group(name=group_name)
group.save()
return group.id
def _extract_grp_entitlements(self,saml_authentication_attributes,groups):
result = []
input_length = len(saml_authentication_attributes[groups])
if input_length == 0:
log.error(self.empty_entitlements_message)
raise RuntimeError(self.empty_entitlements_message)
if input_length == 1:
result = [t.strip() for t in saml_authentication_attributes[groups][0].split(',')]
elif input_length:
result = saml_authentication_attributes[groups]
return result
# return [t.strip() for t in saml_authentication_attributes[groups][0].split(',')] \
# if len(saml_authentication_attributes[groups]) == 1\
# else saml_authentication_attributes[groups]
def authenticate(self, saml_authentication=None):
if not saml_authentication: # Using another authentication method
return None
attrAll = SAMLAttributesConfig(mappings_file_name=settings.AUTH_MAPPINGS_FILE).get_config()
groups = attrAll.entitlements.containerName
if saml_authentication.is_authenticated():
saml_authentication_attributes = saml_authentication.get_attributes()
saml_authorization_attributes = self._extract_grp_entitlements(saml_authentication_attributes,groups)
persistedGroupIds = map(functools.partial(self._persistGroup, grp_mappings=attrAll.entitlements), saml_authorization_attributes)
try:
user = User.objects.get(username=saml_authentication.get_nameid())
except User.DoesNotExist:
user = User(username=saml_authentication.get_nameid())
user.set_unusable_password()
try:
user.first_name = saml_authentication_attributes['samlNameId'][0]
except KeyError:
pass
try:
setattr(user, "first_name", saml_authentication_attributes[attrAll.subject.first_name][0])
except KeyError:
pass
#user.last_name = attributes['Last name'][0]
user.save()
map(functools.partial(self._assocUser, user=user), persistedGroupIds)
user.save()
return user
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
The above code no longer works under Python 3 environment and only starts working to something like this, with a functools.partial() calls spelled out in a for loop:
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
import functools
from .mappings import SAMLAttributesConfig
from django.conf import settings
import logging
log = logging.getLogger(__name__)
class SAMLServiceProviderBackend(object):
empty_entitlements_message="IdP supplied incorrect authorization entitlements. Please contact their support."
def _assocUser(self, group_id, user):
group = Group.objects.get(id=group_id)
group.user_set.add(user)
return None
def _persistGroup(self,idp_group_name, grp_mappings):
group_name = grp_mappings[idp_group_name]
try:
group = Group.objects.get(name=group_name)
except Group.DoesNotExist:
group = Group(name=group_name)
group.save()
return group.id
def _extract_grp_entitlements(self,saml_authentication_attributes,groups):
result = []
input_length = len(saml_authentication_attributes[groups])
if input_length == 0:
log.error(self.empty_entitlements_message)
raise RuntimeError(self.empty_entitlements_message)
if input_length == 1:
result = [t.strip() for t in saml_authentication_attributes[groups][0].split(',')]
elif input_length:
result = saml_authentication_attributes[groups]
return result
# return [t.strip() for t in saml_authentication_attributes[groups][0].split(',')] \
# if len(saml_authentication_attributes[groups]) == 1\
# else saml_authentication_attributes[groups]
def authenticate(self, saml_authentication=None):
if not saml_authentication: # Using another authentication method
return None
attrAll = SAMLAttributesConfig(mappings_file_name=settings.AUTH_MAPPINGS_FILE).get_config()
groups = attrAll.entitlements.containerName
if saml_authentication.is_authenticated():
saml_authentication_attributes = saml_authentication.get_attributes()
saml_authorization_attributes = self._extract_grp_entitlements(saml_authentication_attributes,groups)
persistedGroupIds = map(functools.partial(self._persistGroup, grp_mappings=attrAll.entitlements), saml_authorization_attributes)
try:
user = User.objects.get(username=saml_authentication.get_nameid())
except User.DoesNotExist:
user = User(username=saml_authentication.get_nameid())
user.set_unusable_password()
try:
user.first_name = saml_authentication_attributes['samlNameId'][0]
except KeyError:
pass
try:
setattr(user, "first_name", saml_authentication_attributes[attrAll.subject.first_name][0])
except KeyError:
pass
#user.last_name = attributes['Last name'][0]
user.save()
for group_id in persistedGroupIds:
self._assocUser(user = user, group_id = group_id)
# map(functools.partial(self._assocUser, user=user), persistedGroupIds)
user.save()
return user
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
What can be wrong?
I'm using PyDev plugin in Eclipse. Here's how my Python interpreter is configured there:
Here's Eclipse's .pydevproject file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">venv3.6</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
<pydev_variables_property name="org.python.pydev.PROJECT_VARIABLE_SUBSTITUTION">
<key>DJANGO_SETTINGS_MODULE</key>
<value>reporting.settings</value>
<key>DJANGO_MANAGE_LOCATION</key>
<value>./manage.py</value>
<key>SAML_PLUGIN</key>
<value>/Users/sl/abc/venv3.6/lib/python3.6/site-packages/onelogin/saml2</value>
<key>PY</key>
<value>36</value>
</pydev_variables_property>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}</path>
</pydev_pathproperty>
<pydev_pathproperty name="org.python.pydev.PROJECT_EXTERNAL_SOURCE_PATH">
<path>${SAML_PLUGIN}</path>
</pydev_pathproperty>
</pydev_project>
In Python 3, the map function returns an iterator instead of a list.
This means that if you call map on a collection, the effects of the call are not materialised until you iterate over the resulting iterator.
Consider this class:
>>> class C:
... def __init__(self, x):
... self.x = x
... def double(self):
... self.x *= 2
... def __repr__(self):
... return '<C:{}>'.format(self.x)
...
Let's make a list of instances:
>>> cs = [C(x) for x in range(1, 4)]
>>> cs
[<C:1>, <C:2>, <C:3>]
Now use map to call each instance's double method:
>>> res = map(C.double, cs)
Note the result is not a list:
>>> res
<map object at 0x7ff276350470>
And the instances have not changed:
>>> cs
[<C:1>, <C:2>, <C:3>]
if we call next on the iterator, the instances are updated in turn.
>>> next(res)
>>> cs
[<C:2>, <C:2>, <C:3>]
>>> next(res)
>>> cs
[<C:2>, <C:4>, <C:3>]
>>> next(res)
>>> cs
[<C:2>, <C:4>, <C:6>]
In the code samples that you have provided, the result of calling map is not assigned to a variable, so map is being used for its side effects rather than its output. In Python 3 the correct way to do this is to loop over the iterable and call the function on each element:
>>> for c in cs:
c.double()
As the linked doc puts it:
Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).
I'm trying to get returned value from pipeline. I'm using yield generator to generate item.
And this is my code.
def get_or_create(model):
model_class = type(model)
created = False
try:
obj = model_class.objects.get(product_company=model.product_company, barcode=model.barcode)
except model_class.DoesNotExist:
created = True
obj = model # DjangoItem created a model for us.
obj.save()
return (obj, created)
def update_model(destination, source, commit=True):
pk = destination.pk
source_dict = model_to_dict(source)
for (key, value) in source_dict.items():
setattr(destination, key, value)
setattr(destination, 'pk', pk)
if commit:
destination.save()
return destination
class ProductItemPipeline(object):
def process_item(self, item, spider):
if isinstance(item, ProductItem):
item_model = item.instance
model, created = get_or_create(item_model)
item['cover_photo'] = item['files'][0]['path']
if created:
item.save()
for image in item['files']:
imageItem = ProductImageItem(image=image['path'], product=model)
imageItem.save()
for comment in item['comments']:
commentItem = CommentItem(comment=comment.comment, product=model)
commentItem.save()
return model
Also this is my spider.
item = ProductItem(name=name, price=price, barcode=barcode, file_urls=objectImages, product_url=response.url,product_company=company, comments = comments)
product = yield item
print type(product)
print "yield product"
And product type is returning nonetype
You don't understand python yield. You should use yield like return for return some function result. You can find more information about generators in this article
So your code will look like
item = ProductItem(name=name, price=price, barcode=barcode, file_urls=objectImages, product_url=response.url,product_company=company, comments = comments)
self.logger.debug(type(product))
self.logger.debug('yield product')
yield item
Here's the view function:
def bar(request):
...
record = get_record_from_model(model, **kwargs)
...
return JsonResponse(data_to_response)
and below there is the function used in view function:
def get_record_from_model(model, **kwargs):
try:
return model.objects.get(**kwargs)
except model.DoesNotExist:
error_data = copy.copy(settings.ERROR["NOT_EXIST_ERR"])
return JsonResponse(error_data)
Can I return JsonResponse(error_data) to the client in get_record_from_model function when exception occur?
Something like raise Http404
The short answer is that you can't do it there directly because the calling function still has to do something with the return value from get_record_for_model. That said, I would recommend that you do something like the below, which sends data as well as a found/not found boolean back to the calling function:
def get_record_from_model(model, **kwargs):
try:
return model.objects.get(**kwargs), True
except model.DoesNotExist:
error_data = copy.copy(settings.ERROR["NOT_EXIST_ERR"])
return error_data, False
...
def bar(request):
...
data, found = get_record_from_model(model, **kwargs)
if not found:
return JsonResponse(data, status=404)
...
return JsonResponse(response_data)
Use django's built-in shortcut get_object_or_404
from django.shortcuts import get_object_or_404
def bar(request):
...
record = get_object_or_404(model, **kwargs)
...
return JsonResponse(data_to_response)
I have the following code:
class GeonamesCountryViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = (AllowAny,)
serializer_class = GeonamesCountrySerializer
ordering = ('country_name',)
offset_limit = 'required'
def get_queryset(self):
country_geoname_ids = self.request.QUERY_PARAMS.get('country_geoname_ids', None)
name_prefix = self.request.QUERY_PARAMS.get('name_prefix', None)
if (country_geoname_ids is None) and (name_prefix is None):
raise exceptions.ParseError("Either 'country_geoname_id' or 'name_prefix' must be defined.")
if country_geoname_ids is not None:
country_geoname_ids = [param.strip() for param in country_geoname_id.split(',')]
queryset = GeonamesCountry.objects.filter(country_geoname_id__in = country_geoname_ids)
if name_prefix is not None:
if len(name_prefix) < 2:
raise exceptions.ParseError("'name_prefix' must be at least 2 characters long")
queryset = GeonamesCountry.objects.filter(country_name__istartswith = name_prefix)
paginator = Paginator(queryset, self.request.QUERY_PARAMS.get('limit', 10))
selected_page = self.request.QUERY_PARAMS.get('page')
try:
countries = paginator.page(selected_page)
except EmptyPage:
raise exceptions.ParseError("'Page Empty")
return queryset
Is it possible to default to page one instead of raise exceptions.ParseError("'Page Empty") when the EmptyPage exception is thrown?
After reading the documentation I see it is easily done when not using a ViewSet, but how can I do it from within a ViewSet?
I think you will be quite safe by doing this:
try:
countries = paginator.page(selected_page)
except InvalidPage:
countries = paginator.page(1)
Notice the InvalidPage exception, so you can cover non numbers as well.
-- UPDATE --
It seems like the the cleanest way would be to override pagination class, it's the only way that gives you control over returned page number:
from django.core.paginator import Paginator, InvalidPage
class MyPaginator(Paginator):
def validate_number(self, number):
try:
number = super(MyPaginator, self).validate_number(number)
except InvalidPage:
number = 1
return number
class GeonamesCountryViewSet(viewsets.ReadOnlyModelViewSet):
paginator_class = MyPaginator
...
I am getting an error "_reverse_with_prefix() argument after * must be a sequence, not int" when I try to reverse. I previously hardcoded the parameter in the view but am trying to make it dynamic. Any advice?
View:
def add_review(request, product_id):
p = get_object_or_404(Product, pk=product_id)
if request.method == 'POST':
form = ReviewForm(request.POST)
if form.is_valid():
form.save()
#HARDCODED: return HttpResponseRedirect('/products/1/reviews/')
return HttpResponseRedirect(reverse('view_reviews', args=(p.id)))
else:
form = ReviewForm()
variables = RequestContext(request, {'form': form})
return render_to_response('reserve/templates/create_review.html', variables)
def view_reviews(request, product_id):
product = get_object_or_404(Product, pk=product_id)
reviews = Review.objects.filter(product_id=product_id)
return render_to_response('reserve/templates/view_reviews.html', {'product':product, 'reviews':reviews},
context_instance=RequestContext(request))
urlpatterns = patterns('reserve.views',
url(r'^clubs/$', 'index'),
url(r'^products/(?P<product_id>\d+)/reviews/$', 'view_reviews'),
url(r'^products/(?P<product_id>\d+)/add_review/$', 'add_review'),
url(r'^admin/', include(admin.site.urls)),
)
Check args=(p.id) inside the reverse(), it must be args=(p.id,). The first form is treated as integer instead of a sequence.
Refs the doc and the tutorial:
A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses).
Also, use 'reserve.views.view_reviews' instead of merely 'view_reviews', thus:
reverse('reserve.views.view_reviews', args=(p.id,))
Check the doc of reverse
Since your pattern assigns a match to a variable, it is considered a keyword argument, so you should adjust the call to reverse.
return HttpResponseRedirect(reverse('view_reviews', kwargs={'product_id':p.id})
that's because the args need tuple, but when you use the args=(p.id), actually, the python will think is integer p.id, you can look the source code django 1.6 as follow:
def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None):
if urlconf is None:
urlconf = get_urlconf()
resolver = get_resolver(urlconf)
args = args or []
kwargs = kwargs or {}
if prefix is None:
prefix = get_script_prefix()
if not isinstance(viewname, six.string_types):
view = viewname
else:
parts = viewname.split(':')
parts.reverse()
view = parts[0]
path = parts[1:]
resolved_path = []
ns_pattern = ''
while path:
ns = path.pop()
# Lookup the name to see if it could be an app identifier
try:
app_list = resolver.app_dict[ns]
# Yes! Path part matches an app in the current Resolver
if current_app and current_app in app_list:
# If we are reversing for a particular app,
# use that namespace
ns = current_app
elif ns not in app_list:
# The name isn't shared by one of the instances
# (i.e., the default) so just pick the first instance
# as the default.
ns = app_list[0]
except KeyError:
pass
try:
extra, resolver = resolver.namespace_dict[ns]
resolved_path.append(ns)
ns_pattern = ns_pattern + extra
except KeyError as key:
if resolved_path:
raise NoReverseMatch(
"%s is not a registered namespace inside '%s'" %
(key, ':'.join(resolved_path)))
else:
raise NoReverseMatch("%s is not a registered namespace" %
key)
if ns_pattern:
resolver = get_ns_resolver(ns_pattern, resolver)
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
look this iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)), it use *args, so you should ensure args is a sequence,
according the docs, tuple with one item should add the comma to create, you code should be this:
return HttpResponseRedirect(reverse('view_reviews', args=(p.id,)))