Hi, Ryan.
 

> I'm working on a server project for a petrol station company.
> I have this station class.
>
> class Station(modelpy.Model):
>     shifts = ndb.JsonProperty()
>
> The Station's key's id is the name of a station.
> Here are the names of the actual stations: "West Coast", "Woodlands", 
> "South View".
> I use these station names in get_by_id() to get the station entity.
> As you can see, Station consists of a JsonProperty called "shifts".
> The "shifts" consists of the data as follows (shown in a json format):
> {
>   "South View": {
>     "shifts": {
>       "Full Day": {
>         "start": "7am",
>         "end": "7pm"
>       }
>     }
>   },
>   "West Coast": {
>     "shifts": {
>       "Morning Shift": {
>         "start": "10am",
>         "end": "10pm"
>       },
>       "Night Shift": {
>         "start": "10pm",
>         "end": "10am"
>       }
>     }
>   },
>   "Woodlands": {
>     "shifts": {
>       "Morning Shift": {
>         "start": "8am",
>         "end": "8pm"
>       },
>       "Night Shift": {
>         "start": "8pm",
>         "end": "8am"
>       }
>     }
>   }
> }
> I need a solution for these 3:
> 1) Ndb must not be allowed to store more than 1 shift (e.g. key.put() 
> replaces any (if existing) entity with equal id).
> 2) Ndb must contain the "start" and "end" required property for every 
> shift (e.g. required=True).
> 3) Most IMPORTANT: A MAINTAINTABLE solution that does not need to be 
> re-implemented for other similar examples. (e.g. NOT write custom assertion 
> every single time!)
>
>
Maybe, something like this?

class Shifts(modelpy.Model):
    start = ndb.StringProperty(default='')
    end = ndb.StringProperty(default='')


def convert_json_to_ndb():
    # assume, that Station contain small amount of records
    for station in Station.query():
        for key, props in station.shifts.items():

            Shifts(
              id=key,
              parent=station.key,
              start=props['start'],
              end=props['end'],
            ).put()


Then, access to shifts:

west_coast_shifts = Station.query(ancestor=ndb.Key(Station, 'West Coast'))

for item in west_coast_shifts:
    print "name", item.key.id(), "start", item.start, "end", item.end


WBR, Vitaly

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/8d21a7b6-1cae-42b0-b85b-00311845cbe8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to