How to get latest and adjacent records from the datastore? - google-app-engine

I have movies datastore, each record there has its own id as key name like below:
12345
32453
12154
78873
34543
I would like to allow user to browse movies one by one. Firstly, the latest movie should be shown (database has field added with date and time). How to get that from the datastore?
Upd. I can do it like below:
movies = Movies.query()
movies.order(-Movies.added)
for movie in movies.fetch(1):
self.response.out.write(movie.key.id())
But I don't like it - in order to get key I request the whole record.
Secondly, if some other movie is shown (for ex., 12154), user should be able to go to previous movie (id 32453) and next movie (id 78873). Of course, if last movie is shown, there will not be next movie; and if first movie is shown, there will not be previous movie. So, the question is how to get key names of next and previous movies?
Upd. If current movie shown is 12154, then I should generate links like example.com/movie/32453 for previous movie and example.com/movie/78873 for the next one.
Upd. I've tried something like below:
next_movie = Movies.query(Movies.added < movie.added)
next_movie = next_movie.order(-Movies.added)
next_movie = next_movie.get()
if next_movie:
next_url = next_movie.key.id()
else:
next_url = ''
prev_movie = Movies.query(Movies.added > movie.added)
prev_movie = prev_movie.order(-Movies.added)
prev_movie = prev_movie.get()
if prev_movie:
prev_url = prev_movie.key.id()
else:
prev_url = ''
But it doesn't work well... next_url seems to be OK, but prev_url always the same. Here is my test database content (-Movies.added order):
id added
503035: 2012-08-05 19:49:51.259000
475537: 2012-08-05 19:49:51.238000
677539: 2012-08-05 19:49:51.218000
566355: 2012-08-05 19:49:51.197000
557850: 2012-08-05 19:49:51.176000
670146: 2012-08-05 19:49:51.155000
581030: 2012-08-05 19:49:51.135000
464561: 2012-08-05 19:49:51.114000
507817: 2012-08-05 19:49:51.092000

First you need a property on your movie entity that would represent "lateness", e.g. a date filed when movie was inserted in database.
Then you should use query with descending sort on this field.
To skip to next/previous you should use Query Cursors.

the following codes works well:
next_movie = Movies.query(Movies.added < movie.added)
next_movie = next_movie.order(-Movies.added)
next_movie = next_movie.get(keys_only = True)
if next_movie:
next_url = next_movie.id()
else:
next_url = ''
prev_movie = Movies.query(Movies.added > movie.added)
prev_movie = prev_movie.order(Movies.added)
prev_movie = prev_movie.get(keys_only = True)
if prev_movie:
prev_url = prev_movie.id()
else:
prev_url = ''
return next_url, prev_url

Related

How can update field by overriding save method which is in another app models

I have two models Bill and Payment each with 3 fields. Here I want to update field last_price directly when user pay bill. If user pay complete amount then it would be 0. or if user not pay complete amount then remaining amount want to be save in last_price. So here I want to update amount of last_bill directly when user pay bill.
Note: Both models are in separate app
My Fields are:
BillApp/models
Bill(model.Model):
bill_no = models.IntegerField(max_length = 100,primary_key=True)
last_price = models.IntegerField()
Name = models.CharField(max_length = 20)
PaymentApp/models
Payment(model.Model):
id = models.CharField(max_length = 100,primary_key=True)
bill_no = models.ForeignKey(Bill, on_delete = SET_NULL,null=True)
total_amount = models.CharField(max_length = 10)
def save(...):
Update value of Bill.last_price
How do I update value of Bill.last_price in the save method
I tried this for update field last_price
def save(self,*args, **kwargs):
new_last_price = self.total_amount - self.bill_no.last_price
print("new_last_price : ",new_last_price)
bill_detail = Bill.objects.filter(bill_no=self.bill_no).first()
print("bill_detail : ",bill_detail)
try:
with transaction.atomic():
updated_field = bill_detail.save(update_fields = ['last_price'])
print("updated_field : ", updated_field)
super().save(*args, **kwargs)
print(Bill.objects.filter(bill_no=self.bill_no).first().last_price)
except IntegrityError:
print('Exception in save')
I getting correct output of new_last_price and bill_detail..
but updated_field display None ..
How Can I save new value in Bill?
Your save method will save the data and refresh the object instance but will not return the object instance. Use directly show last price.
bill_detail.save(update_fields = ['last_price'])
print(bill_detail.last_price)

Google App Engine - Get from repeated StructuredProperty

I have the following structures:
class UserOther(ndb.Model):
other_type = ndb.StringProperty(indexed = True)
other_data = ndb.StringProperty(indexed = False)
class User(ndb.Model):
name = ndb.StringProperty(default = "NULL", indexed = False)
email = ndb.StringProperty(default = "NULL", indexed = False)
active = ndb.BooleanProperty(default = True)
others = ndb.StructuredProperty(UserOther, repeated = True)
updated_at = ndb.DateTimeProperty(auto_now = True)
How can I use an User key id and a string for other_type(like "job") to get and be able to edit that information. I tried using the ancestor parameter, but perhaps I didn't do that correctly.
user_key = ndb.Key("User", user_id)
user = user_key.get()
other = UserOther.query(UserOther.other_type == "job", ancestor = user_key).get()
So if i print my user looks like this :
1425436064.0User(key=Key('User', 5171003185430528), active=True, email=u'NULL', name=u'NULL', others=[UserOther(other_data=u'0', other_type=u'job'), UserOther(other_data=u'0', other_type=u'times_worked'), UserOther(other_data=u'0', other_type=u'times_opened')], updated_at=datetime.datetime(2015, 3, 6, 10, 35, 24, 838078))
But if I print the job variable it is
1425436759.0None
You've misunderstood how to query for structured properties. The UserOther entity doesn't live on its own, it's part of the relevant User entity, so that's what you need to query.
The documentation explains exactly how to do this, but in summary you would do:
job = User.query(User.others.other_type == "job").get()
What I would do is get the user (by id) and then filter the 'others' in code:
user = User.get_by_id(user_key_id)
for other in user.others:
if other.other_type == 'job':
print other.other_data # do edits

The best way to store data or query data on google app engine

I want to be able to store some data in app engine and I am looking for some help on the best way to store the data or retrieve the data through a query. I have a list of users and want them to be able to add cars they want to sell and enter a lower and upper limit they would accept. When a user is searching for a car and enters a price, if this price is between the lower and upper limits, it will return the car:
class User(ndb.Model):
username = ndb.StringProperty()
cars = ndb.StructuredProperty(Car, repeated = True)
datecreated = ndb.DateTimeProperty(auto_now_add = True)
date_last_modified = ndb.DateTimeProperty(auto_now = True)
class Car(ndb.Model):
lowerprice = ndb.IntegerProperty()
maxprice = ndb.IntegerProperty()
make = ndb.StringProperty()
model = ndb.StringProperty()
datecreated = ndb.DateTimeProperty(auto_now_add = True)
date_last_modified = ndb.DateTimeProperty(auto_now = True)
I can't filter on:
c = User.query(User.car.lowerprice >= int(self.carprice), User.car.maxprice < int(self.carprice)).get())
As it returns BadRequestError: Only one inequality filter per query is supported.
Should I structure or store the data differently to allow filtering on one inequality or should I try and use and and / or query?
Is there anything else you would recommend?
Try something like this:
holder = User.query(User.car.lowerprice >= int(self.carprice)).get())
results = filter(lambda x: x.car.maxprice < int(self.carprice), holder)
It boils down to having to programmably handling the second filter.

Apex Lookup Value copy to second Object

I have a trigger that moves the values from one object to another, but am stuck on how to move the values of the lookup fields from one to the other. what is the syntax? If you could show me the Company and the Chair_Rep ones that would be great!
<Lead> newLeadsList= new List<Lead>();
for (integer i=0; i<newContacts.size(); i++) {
if (newContacts[i].createlead__c == TRUE && oldContacts[i].createlead__c == FALSE ) {
newLeadsList.add(new Lead(
firstName = newContacts[i].firstName,
lastName = newContacts[i].lastName,
***Company = newContacts[i].account.name,***
Status = 'identified',
LeadSource = newContacts[i].leadsource ,
Product_Interest__c = 'CE',
//ContactLink__c = newContacts[i].ID,
Title = newContacts[i].title,
Email = newContacts[i].email,
//***Chair_Rep__c = newContacts[i].Chair_Rep__c***
Phone = newContacts[i].Phone,
MobilePhone = newContacts[i].MobilePhone,
// Address = newContacts[i].MailingAddress,
//Website = newContacts[i].Website,
nickname__c = newContacts[i].Nickname__c
Lookup fields should contain references (IDs) on records.
Is 'Company' a standard Lead field in your code?
***Company = newContacts[i].account.name,***
If so, then it's a Text(255) type field, which cannot be used as lookup.
If you need to make a lookup on a Contact's account record, then you can create a custom Lookup field on Lead with reference to Account. And then you could try this code (assuming ContactCompany is that custom lookup field :
ContactCompany__c = newContacts[i].AccountId
or
ContactCompany__c = newContacts[i].Account.Id
Chair_Rep__c and newContacts.Chair_Rep__c should be lookup fields on same object. Then this
Chair_Rep__c = newContacts[i].Chair_Rep__c
or this should work
Chair_Rep__c = newContacts[i].Chair_Rep__r.Id

Lua - How to check if list contains element

I need some help with my lua script for a game. I need to check if my inventory in the game contains any id from a list.
Here's a piece of my list:
local Game_Items = {
{id = 7436, name = "angelic axe", value = 5000},
{id = 3567, name = "blue robe", value = 10000},
{id = 3418, name = "bonelord shield", value = 1200},
{id = 3079, name = "boots of haste", value = 30000},
{id = 7412, name = "butcher's axe", value = 18000},
{id = 3381, name = "crown armor", value = 12000}
}
The following code might look a bit weird since you don't know what it's for, but it's basically this: the list above is a list of items in my game, and inside the game theres an inventory where you can keep items and stuff. Now I want to check if my inventory contains any of those IDs.
I tried adding 2 of the id's manually and it worked, but my list of items contains over 500 items in total and I don't want to write them all out. Is there a way to put the whole list and check if it's in there somehow?
if not table.contains({ 3035, 3043, Game_Items[id] }, tempItemCounter.id) then
This is what I tried so far. Those two first id's work 3035 and 3043, then I tried all my whole list and only check the Ids. but I dont know how to do that. That code does not work. Could anyone just help me include the whole list of id's in the table.contains ?
Basically wanna include my whole list in that line, without typing out all IDs manually.
Shouldn't Game_Items[id] work? Doesn't that mean all the "id" inside "Game_Items"?
Thanks!
No it doesn't mean that. If foo is a table, then foo[id] looks for a field in foo that is called whatever id refers to, such as a string (so if id is 1 you will get foo[1], if id is "bar" you will get foo.bar, etc).
You can't do it in one line, but you can create a function that will allow you to write your if condition. I'm not sure what tempItemCounter is but assuming that your inventory is a map of keys to entries of the form
inventory = {
[1234] = {....},
[1235] = {....},
...
}
where each integer key is unique, and assuming you want true only if all items are in inventory, then you could do this:
function isAllInInventory(items, inventory)
for i,item in ipairs(items) do
if inventory[item.id] == nil
return false
end
end
return true
end
if isAllInInventory(Game_Items, inventory) then
...
end

Resources