appengine KindError when accessing table outside of django - google-app-engine

I have a table called Mytable in home/models.py and using django aep
I reference is as Mytable.all().
It shows up in the Data Viewer as home_mytable
Now, for some urls within app.yaml I have a separate handler
that processes these requests. (This is in fact a google wave
robot handler).
Within this handler I want to reference the table home_mytable
I do so by calling db.GqlQuery("SELECT * from home_mytable")
However something strange happens. I receive a KindError
No implementation for kind home_mytable
I receive this sporadically though, sometimes It works just
fine, I suspect that happens right after I call up a url
that references this table from a django handler.
My questions are, how can I
a) ensure that this error doesnt occur and
b) programattically check what the available
'kinds' are so I can try and debug this

App Engine Patch monkeypatches your models to have different kind names. Don't ask me why, but that's what it does. To fix things, you need to override the kind() class method in your models to make sure they always have the 'fixed' kind names, like this:
class MyTable(db.Model):
#classmethod
def kind(cls):
return "home_mytable"

Related

Domain driven design database validation in model layer

I'm creating a design for a Twitter application to practice DDD. My domain model looks like this:
The user and tweet are marked blue to indicate them being a aggregate root. Between the user and the tweet I want a bounded context, each will run in their respective microservice (auth and tweet).
To reference which user has created a tweet, but not run into a self-referencing loop, I have created the UserInfo object. The UserInfo object is created via events when a new user is created. It stores only the information the Tweet microservice will need of the user.
When I create a tweet I only provide the userid and relevant fields to the tweet, with that user id I want to be able to retrieve the UserInfo object, via id reference, to use it in the various child objects, such as Mentions and Poster.
The issue I run into is the persistance, at first glance I thought "Just provide the UserInfo object in the tweet constructor and it's done, all the child aggregates have access to it". But it's a bit harder on the Mention class, since the Mention will contain a dynamic username like so: "#anyuser". To validate if anyuser exists as a UserInfo object I need to query the database. However, I don't know who is mentioned before the tweet's content has been parsed, and that logic resides in the domain model itself and is called as a result of using the tweets constructor. Without this logic, no mentions are extracted so nothing can "yet" be validated.
If I cannot validate it before creating the tweet, because I need the extraction logic, and I cannot use the database repository inside the domain model layer, how can I validate the mentions properly?
Whenever an AR needs to reach out of it's own boundary to gather data there's two main solutions:
You pass in a service to the AR's method which allows it to perform the resolution. The service interface is defined in the domain, but most likely implemented in the infrastructure layer.
e.g. someAr.someMethod(args, someServiceImpl)
Note that if the data is required at construction time you may want to introduce a factory that takes a dependency on the service interface, performs the validation and returns an instance of the AR.
e.g.
tweetFactory = new TweetFactory(new SqlUserInfoLookupService(...));
tweet = tweetFactory.create(...);
You resolve the dependencies in the application layer first, then pass the required data. Note that the application layer could take a dependency onto a domain service in order to perform some reverse resolutions first.
e.g.
If the application layer would like to resolve the UserInfo for all mentions, but can't because it doesn't know how to parse mentions within the text it could always rely on a domain service or value object to perform that task first, then resolve the UserInfo dependencies and provide them to the Tweet AR. Be cautious here not to leak too much logic in the application layer though. If the orchestration logic becomes intertwined with business logic you may want to extract such use case processing logic in a domain service.
Finally, note that any data validated outside the boundary of an AR is always considered stale. The #xyz user could currently exist, but not exist anymore (e.g. deactivated) 1ms after the tweet was sent.

ndb query on Google Cloud Platform intermittently returning nothing

I have a Python application deployed on Google Cloud Platform. There is a Google Cloud Datastore in the background, with two Kinds. I use NDB to pull the data into the application.
class AttEvent(ndb.Model):
event = ndb.StringProperty()
matchdate = ndb.DateTimeProperty()
class MainPage(webapp2.RequestHandler):
def get(self):
query = AttEvent.query().order(AttEvent.matchdate)
for q in query.fetch():
try:
# application code
One of the Kinds (AtEvent in the code above) is causing me trouble. The app will deploy and work as expected for hours / days, but then intermittently stop returning data. Debugging shows the q object is legitimate object of the type AttEvent, but for each of the items in the values collection, it says "(Object has no fields)". When the application code attempts to reference a property of the model (i.e. q.event), it fails.
The query will suddenly start working again, minutes / hours later, even if I take no action. I can't see any pattern or apparent cause. Obviously this isn't ideal from a user perspective.
The Kind that is causing trouble is static data and only actually contains 3 entities. The other Kind is transactional, contains thousands of records, but has never exhibited the same behaviour.
The intermittent nature of the fault leads me to believe this is something to do with caching, but I am fairly new to Python and GCP, so I am not exactly sure. I've tried doing a context.clear_cache() before the query, but it has no effect.
Am I missing something obvious?
I don't know why this is happening, but I have a possible work around. Since the data is static and the entities seem to be small, you could store them in instance memory instead of querying for them every time you need them.
Store the entities in a module level variable like this:
att_entities = AttEvent.query().order(AttEvent.matchdate).fetch()
class AttEvent(ndb.Model):
event = ndb.StringProperty()
matchdate = ndb.DateTimeProperty()
class MainPage(webapp2.RequestHandler):
def get(self):
for q in att_entities:
try:
# application code
You would get the entities only when a new instance is launched so as long as it works the first time you are all set. As a bonus, it will make the get call faster since you don't need to retrieve the data from the data store.
You might need to add extra logic to cause att_entities to be updated as needed.

Cakephp 3 process code after page response

One of the requirements of the project I am working on is that I log the connections made to the site, due to the amount of processing being done to get as much information as possible I would like to process this after the page has been sent back to the user.
At the moment I am running my code in the afterFilter of my appController:
public function afterFilter(Event $event){
$log_request = new RequestsController;
$log_request->log_request();
}
I am attempting to run this in afterRender of my appController:
public function afterRender(Event $event, $viewFile){
$log_request = new RequestsController;
$log_request->log_request();
}
But I can not seem to get the code to execute or if it does then I do not know how to find out what the error being thrown is.
If somebody can point me towards an example of this being done or a concurrent method of doing this (it needs to be logged within a second of the request) I would appreciate it.
$log_request = new RequestsController; you don't instantiate controllers inside controllers. You want to learn the MVC design pattern first when using a MVC based framework or you'll end up with a non maintainable piece of horrible spaghetti code. I recommend you to do the blog tutorial to get a basic understanding.
If somebody can point me towards an example of this being done or a concurrent method of doing this (it needs to be logged within a second of the request) I would appreciate it.
Read this chapter: CakePHP Logging Taken from there:
Logging data in CakePHP is easy - the log() function is provided by the LogTrait, which is the common ancestor for many CakePHP classes. If the context is a CakePHP class (Controller, Component, View,...), you can log your data. You can also use Log::write() directly.
Add the log trait to the AppController, pass the request to the log() method and configure the logging to log these requests to whatever you prefer either in afterRender() or if you want to do it really late, do it in __destruct().

Backbone/Laravel: How to post to a different table

Here's my dilemma. I have a series of tables with leads from our various contact forms. We also have tables for spam that comes in on our forms. But occasionally, a lead may get routed incorrectly to a spam table. So I need to move that lead from one table to the other, which means inserting it one and deleting it from the other.
As I understand it, when Backbone calls the save method, it checks to see if that id exists in the table. If it doesn't, it makes a POST request. If it does, it makes a PUT request. I need to be able to force Backbone to make a POST request, so that Laravel can call the right RESTful action.
See the problem is that if Backbone makes a PUT request to say, /send-message/52 (the 52 being the ID of the lead in the send-message-spam table), it will update/overwrite the existing lead with the ID of 52. I want to make a POST request to /send-message (obviously without an ID).
I can force Backbone to use a different urlRoot, but how do I force it to make a POST when I call save()?
I'm unsure if there is a configuration option to do this, but you could always override the save method in your Backbone model to have it perform the action you desire.

Modify contained models at runtime

I'm in the process of building a plugin that includes a behavior and several related models. My goal is to make this as easy as possible for the developer using the behavior. My perfect world has the dev simply attaching the behavior to any relevant models and configuring it.
The behavior interacts directly with one of the models and a hasOne association is being created on the fly, but the other models contain supporting data that is important. What I'd like to do is to have that model pull in its related data by modifying the Containable models.
In short:
MyModel (which actsAs the behavior) gets bound to top level model during the behavior's setup method.
The supporting models are directly associated to the top level model
In MyBehavior::beforeFind, I'd like to ensure that supporting model data is returned without the user having to know to ask for it when calling MyModel::find( ... ).
I haven't found the right keys that will allow me to modify these things at runtime. Maybe it's not even possible given that I want to essentially interact with another behavior (Containable).
Any thoughts would be appreciated.
This code automatically adds some contains to the find before it is run, you just have to make sure that your behavior is attached before the containable behavior or it will not work. The beforeFind callback for a behavior is only run once, so once containable has been called adding something like this does nothing. Took me a while to get it going because of that.
https://github.com/infinitas/infinitas/blob/dev/core/contents/models/behaviors/contentable.php#L65

Resources