EndPointsModel query by ancestor - google-app-engine

how can I do a query by ancestor according to the user_id that I get from the token ?
This is my code:
class MyUserClass(EndPointModel):
user_object = ndb.UserProperty(required=True, indexed=False)
class MyModel(EndPointModel):
...
#MyModel.method(user_required=True, name="model.add", path="model")
def add_mymodel(self, my_model):
gplus_user_id = auth_util.get_google_plus_user_id()
if gplus_user_id is None:
raise endpoints.ForbiddenException(NidAppUser.NO_GPLUS_ID)
user = MyUserClass.get_or_insert(gplus_user_id, user_object=endpoints.get_current_user())
my_model.parent = user.key.string_id()
my_model.put()
return my_model
#MyModel.query_method(user_required=True, name="list.mymodel", path="models",
query_fields=('order','attr1',))
def list_models(self, query):
gplus_user_id = auth_util.get_google_plus_user_id()
if gplus_user_id is None:
raise endpoints.ForbiddenException(NidAppUser.NO_GPLUS_ID)
## Here or in thsi decorator function I want to do something like
## query.ancestor.(ndb.Key(MyClassUser, gplus_user_id)) (such as the DB Datastore)
## to return only the query of MyModels that belong to the current user
return query
and I need to create a query ancestor by the key of MyUserClass key. I don't want to use the solution to add a field owner in my model because query ancestor is more fast than filters, and it really help me to organize my db.
Thank you

You can construct a query based on ancestor like this:
myancestorkey = ndb.Key(MyUserClass, gplus_user_id)
myquery = MyModel.query(ancestor=myancestorkey)

With endpoints_proto_datastore you can do something like this:
#MyModel.query_method(user_required=True, name="list.mymodel",
path="models", query_fields=('order','attr1',))
def list_models(self, query):
# Returns only MyModels that belong to the current g+ user
return query.filter(MyModel.parent == auth_util.
get_google_plus_user_id())

Related

Creating object with ManyToMany field via DRF ViewSet's perform_create

I have a simple model:
class Item(models.Model):
user = ForeignKey(User, related_name="user_items")
users = ManyToManyField(User, related_name="users_items")
I want it so that when a user creates an Item via ViewSet, that user is automatically assigned to the user and users fields.
I typically do this for ForeignKey's via the ViewSet's perform_create:
class ItemViewSet(viewsets.ModelViewSet):
...
def perform_create(self, serializer):
if self.request.user.is_authenticated:
serializer.save(user=self.request.user)
else:
serializer.save()
When I try to do it for the ManyToManyField too, I get this error:
{'users': [ErrorDetail(string='This list may not be empty.', code='empty')]}
I've tried the following in perform_create():
# 1
serializer.save(user=self.request.user, users=self.request.user)
# 2
serializer.save(user=self.request.user, users=[self.request.user])
# 2
serializer.save(user=self.request.user, users=[self.request.user.id])
How can I update a ManyToManyField via a ViewSet's perform_create?
Edit:
I guess the following works:
obj = serializer.save(user=self.request.user)
obj.users.add(self.request.user)
Is there no way to have the M2M field when the object is initially created though?
when you want set a list to m2m field one of the things you can do is this:
item_obj.users.set(user_list)
probably you need first get your item_obj.
for this you can get your object id from item_id = serializer.data.get('id') , and then item_obj = Item.objects.get(id = item_id)

google appengine endpoint proto datastore simple get id example return not found

I am trying the simple get at endpoint proto datastore for few days.
http://endpoints-proto-datastore.appspot.com/examples/simple_get.html
It works, but it always return not found.
Here is the get by id api.
https://pttbuying.appspot.com/_ah/api/pttbuying/v1/items/4504690549063680
Here is my model code.
class Item(EndpointsModel):
_message_fields_schema = ('id', 'item_title', 'item_link', 'item_price', 'item_description_strip', 'datetime')
item_title = ndb.StringProperty(indexed=False)
item_author_name = ndb.StringProperty(indexed=False)
item_link = ndb.StringProperty(indexed=True)
item_description_strip = ndb.StringProperty(indexed=False)
item_price = ndb.StringProperty(indexed=False)
datetime = ndb.DateTimeProperty(auto_now_add=True)
Here is my api code
#endpoints.api(name='pttbuying', version='v1',
allowed_client_ids=[WEB_CLIENT_ID, ANDROID_CLIENT_ID,
IOS_CLIENT_ID, endpoints.API_EXPLORER_CLIENT_ID],
audiences=[ANDROID_AUDIENCE],
scopes=[endpoints.EMAIL_SCOPE])
class PttBuyingApi(remote.Service):
"""PttBuying API v1."""
#Item.method(request_fields=('id',),
path='items/{id}', http_method='GET', name='item.MyModelGet')
def MyModelGet(self, my_item):
if not my_item.from_datastore:
raise endpoints.NotFoundException('Item not found.')
return my_item
#Item.query_method(query_fields=('limit', 'order', 'pageToken'), path='items', name='item.list')
def MyModelList(self, query):
return query
Am i missing something?
Thanks for advice.
I did your example here and it works nicely with some changes:
#Item.query_method(query_fields=('limit', 'pageToken',),
path='items',
http_method='GET',
name='items.list')
def ItemsList(self, query):
return query
#Item.method(request_fields=('id',),
path='item',
http_method='GET',
name='item.get')
def ItemGet(self, item):
if not item.from_datastore:
raise endpoints.NotFoundException('item not found')
return item
#Item.method(path='item',
http_method='POST',
name='item.post')
def ItemPost(self, item):
item.put()
return item
I didn't change a thing regarding the model, just the api methods.
Just perform an item insertion, get the ID of the item that was just inserted and then perform the ItemGet with the ID provided.
For the get i prefer this way (not using the /{id}, but requiring the user to do a GET Query - i.e, ah/api/path?id=__ , which seems more correct for me). If you have any questions, ask bellow.

How to edit information submitted using Django forms in Google App Engine

Thank you for the tips. Maybe I can edit this to explain it a little better...
I'm writing an application for Google App Engine where I would like to do a GQL query to get all the evaluations of each type that have already been submitted. I want to allow the user to click on a link to edit each individual evaluation and then submit the change. This is the best way I've found to do it...
class EvaluationApproval(webapp.RequestHandler):
def get(self):
#search for unapproved general evaluations
query = db.GqlQuery("SELECT * FROM GeneralAssessmentReport WHERE Approved = False")
if query.count() != 0:
for item in query:
#create a link to edit that item
self.response.out.write('%s' % (item.key(), item.Name))
#do this for each type of evaluation...
query = db.GqlQuery("SELECT * FROM HeadNeck WHERE Approved = False")
if query.count() != 0:
for item in query:
self.response.out.write('%s' % (item.key(), item.Name))
query = db.GqlQuery("SELECT * FROM lowerExtremity WHERE Approved = False")
if query.count() != 0:
for item in query:
self.response.out.write('%s' % (item.key(), item.Name))
So, when the user clicks on the link they are directed to...
class GeneralFormApprove(webapp.RequestHandler):
def get(self):
# get the correct evaluation to approve
key = self.request.get('key')
item = GeneralAssessmentReport.get(key)
#write out the form with key in the form action!
self.response.out.write('<form method="POST" action="/generalFormApprove?key=%s">' % key)
self.response.out.write(GeneralAssessmentReportForm(instance=item))
def post(self):
# get the key once more
key = self.request.get('key')
data = GeneralAssessmentReportForm(data=self.request.POST,instance=GeneralAssessmentReport.get(key))
if data.is_valid():
# save the edited evaluation
entity = data.save(commit=False)
entity.put()
and then have a WhateverFormApprove class for each type of evaluation. Does this make sense and do you have any other ideas to accomplish this?
for editing existing entities, a good way is to use a url pattern that includes the id of the entity. Upon post, you can use the id to load the entity and then use it in the instance argument for the form.
the url pattern is something like:
r'^evaluationapproval/(?P<item_id>[\d]+)/$'
def post(self,item_id=None):
item = None
if item_id:
item = db.get(db.Key.from_path('MyModelKind',int(item_id)))
#...

How can I mimic 'select_related' using google-appengine and django-nonrel?

django nonrel's documentation states: "you have to manually write code for merging the results of multiple queries (JOINs, select_related(), etc.)".
Can someone point me to any snippets that manually add the related data? #nickjohnson has an excellent post showing how to do this with the straight AppEngine models, but I'm using django-nonrel.
For my particular use I'm trying to get the UserProfiles with their related User models. This should be just two simple queries, then match the data.
However, using django-nonrel, a new query gets fired off for each result in the queryset. How can I get access to the related items in a 'select_related' sort of way?
I've tried this, but it doesn't seem to work as I'd expect. Looking at the rpc stats, it still seems to be firing a query for each item displayed.
all_profiles = UserProfile.objects.all()
user_pks = set()
for profile in all_profiles:
user_pks.add(profile.user_id) # a way to access the pk without triggering the query
users = User.objects.filter(pk__in=user_pks)
for profile in all_profiles:
profile.user = get_matching_model(profile.user_id, users)
def get_matching_model(key, queryset):
"""Generator expression to get the next match for a given key"""
try:
return (model for model in queryset if model.pk == key).next()
except StopIteration:
return None
UPDATE:
Ick... I figured out what my issue was.
I was trying to improve the efficiency of the changelist_view in the django admin. It seemed that the select_related logic above was still producing additional queries for each row in the results set when a foreign key was in my 'display_list'. However, I traced it down to something different. The above logic does not produce multiple queries (but if you more closely mimic Nick Johnson's way it will look a lot prettier).
The issue is that in django.contrib.admin.views.main on line 117 inside the ChangeList method there is the following code: result_list = self.query_set._clone(). So, even though I was properly overriding the queryset in the admin and selecting the related stuff, this method was triggering a clone of the queryset which does NOT keep the attributes on the model that I had added for my 'select related', resulting in an even more inefficient page load than when I started.
Not sure what to do about it yet, but the code that selects related stuff is just fine.
I don't like answering my own question, but the answer might help others.
Here is my solution that will get related items on a queryset based entirely on Nick Johnson's solution linked above.
from collections import defaultdict
def get_with_related(queryset, *attrs):
"""
Adds related attributes to a queryset in a more efficient way
than simply triggering the new query on access at runtime.
attrs must be valid either foreign keys or one to one fields on the queryset model
"""
# Makes a list of the entity and related attribute to grab for all possibilities
fields = [(model, attr) for model in queryset for attr in attrs]
# we'll need to make one query for each related attribute because
# I don't know how to get everything at once. So, we make a list
# of the attribute to fetch and pks to fetch.
ref_keys = defaultdict(list)
for model, attr in fields:
ref_keys[attr].append(get_value_for_datastore(model, attr))
# now make the actual queries for each attribute and store the results
# in a dict of {pk: model} for easy matching later
ref_models = {}
for attr, pk_vals in ref_keys.items():
related_queryset = queryset.model._meta.get_field(attr).rel.to.objects.filter(pk__in=set(pk_vals))
ref_models[attr] = dict((x.pk, x) for x in related_queryset)
# Finally put related items on their models
for model, attr in fields:
setattr(model, attr, ref_models[attr].get(get_value_for_datastore(model, attr)))
return queryset
def get_value_for_datastore(model, attr):
"""
Django's foreign key fields all have attributes 'field_id' where
you can access the pk of the related field without grabbing the
actual value.
"""
return getattr(model, attr + '_id')
To be able to modify the queryset on the admin to make use of the select related we have to jump through a couple hoops. Here is what I've done. The only thing changed on the 'get_results' method of the 'AppEngineRelatedChangeList' is that I removed the self.query_set._clone() and just used self.query_set instead.
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('username', 'user', 'paid')
select_related_fields = ['user']
def get_changelist(self, request, **kwargs):
return AppEngineRelatedChangeList
class AppEngineRelatedChangeList(ChangeList):
def get_query_set(self):
qs = super(AppEngineRelatedChangeList, self).get_query_set()
related_fields = getattr(self.model_admin, 'select_related_fields', [])
return get_with_related(qs, *related_fields)
def get_results(self, request):
paginator = self.model_admin.get_paginator(request, self.query_set, self.list_per_page)
# Get the number of objects, with admin filters applied.
result_count = paginator.count
# Get the total number of objects, with no admin filters applied.
# Perform a slight optimization: Check to see whether any filters were
# given. If not, use paginator.hits to calculate the number of objects,
# because we've already done paginator.hits and the value is cached.
if not self.query_set.query.where:
full_result_count = result_count
else:
full_result_count = self.root_query_set.count()
can_show_all = result_count self.list_per_page
# Get the list of objects to display on this page.
if (self.show_all and can_show_all) or not multi_page:
result_list = self.query_set
else:
try:
result_list = paginator.page(self.page_num+1).object_list
except InvalidPage:
raise IncorrectLookupParameters
self.result_count = result_count
self.full_result_count = full_result_count
self.result_list = result_list
self.can_show_all = can_show_all
self.multi_page = multi_page
self.paginator = paginator

Use a db.StringProperty() as unique identifier in Google App Engine

I just have a hunch about this. But if feels like I'm doing it the wrong way. What I want to do is to have a db.StringProperty() as a unique identifier. I have a simple db.Model, with property name and file. If I add another entry with the same "name" as one already in the db.Model I want to update this.
As of know I look it up with:
template = Templates.all().filter('name = ', name)
Check if it's one entry already:
if template.count() > 0:
Then add it or update it. But from what I've read .count() is every expensive in CPU usage.
Is there away to set the "name" property to be unique and the datastore will automatic update it or another better way to do this?
..fredrik
You can't make a property unique in the App Engine datastore. What you can do instead is to specify a key name for your model, which is guaranteed to be unique - see the docs for details.
I was having the same problem and came up with the following answer as the simplest one :
class Car(db.Model):
name = db.StringProperty(required=True)
def __init__(self,*args, **kwargs):
super(Car, self).__init__(*args, **kwargs)
loadingAnExistingCar = ("key" in kwargs.keys() or "key_name" in kwargs.keys())
if not loadingAnExistingCar:
self.__makeSureTheCarsNameIsUnique(kwargs['name'])
def __makeSureTheCarsNameIsUnique(self, name):
existingCarWithTheSameName = Car.GetByName(name)
if existingCarWithTheSameName:
raise UniqueConstraintValidationException("Car should be unique by name")
#staticmethod
def GetByName(name):
return Car.all().filter("name", name).get()
It's important to not that I first check if we are loading an existing entity first.
For the complete solution : http://nicholaslemay.blogspot.com/2010/07/app-engine-unique-constraint.html
You can just try to get your entity and edit it, and if not found create a new one:
template = Templates.gql('WHERE name = :1', name)
if template is None:
template = Templates()
# do your thing to set the entity's properties
template.put()
That way it will insert a new entry when it wasn't found, and if it was found it will update the existing entry with the changes you made (see documentation here).
An alternative solution is to create a model to store the unique values, and store it transationally using a combination of Model.property_name.value as key. Only if that value is created you save your actual model. This solution is described (with code) here:
http://squeeville.com/2009/01/30/add-a-unique-constraint-to-google-app-engine/
I agree with Nick. But, if you do ever want to check for model/entity existence based on a property, the get() method is handy:
template = Templates.all().filter('name = ', name).get()
if template is None:
# doesn't exist
else:
# exists
I wrote some code to do this. The idea for it is to be pretty easy to use. So you can do this:
if register_property_value('User', 'username', 'sexy_bbw_vixen'):
return 'Successfully registered sexy_bbw_vixen as your username!'
else:
return 'The username sexy_bbw_vixen is already in use.'
This is the code. There are a lot of comments, but its actually only a few lines:
# This entity type is a registry. It doesn't hold any data, but
# each entity is keyed to an Entity_type-Property_name-Property-value
# this allows for a transaction to 'register' a property value. It returns
# 'False' if the property value is already in use, and thus cannot be used
# again. Or 'True' if the property value was not in use and was successfully
# 'registered'
class M_Property_Value_Register(db.Expando):
pass
# This is the transaction. It returns 'False' if the value is already
# in use, or 'True' if the property value was successfully registered.
def _register_property_value_txn(in_key_name):
entity = M_Property_Value_Register.get_by_key_name(in_key_name)
if entity is not None:
return False
entity = M_Property_Value_Register(key_name=in_key_name)
entity.put()
return True
# This is the function that is called by your code, it constructs a key value
# from your Model-Property-Property-value trio and then runs a transaction
# that attempts to register the new property value. It returns 'True' if the
# value was successfully registered. Or 'False' if the value was already in use.
def register_property_value(model_name, property_name, property_value):
key_name = model_name + '_' + property_name + '_' + property_value
return db.run_in_transaction(_register_property_value_txn, key_name )

Resources