Sorting objects and ListProperty filtering without breaking indexes

I am developing a simple Blogging / Bookmarking platform, and I am trying to add the lร  delicious function to the explorer / drill-down tag so that users can filter messages indicating a list of specific tags.

Something like that: enter image description here

Messages are presented in the data warehouse with this simplified model:

class Post(db.Model):
    title = db.StringProperty(required = True)
    link = db.LinkProperty(required = True)
    description = db.StringProperty(required = True)
    tags = db.ListProperty(str)
    created = db.DateTimeProperty(required = True, auto_now_add = True)

Email tags are stored in ListProperty, and to get a list of messages tagged with a specific tag list, the Post model provides the following static method:

@staticmethod
def get_posts(limit, offset, tags_filter = []):
        posts = Post.all()
        for tag in tags_filter:
          if tag:
              posts.filter('tags', tag)
        return posts.fetch(limit = limit, offset = offset)

This works well, although I did not particularly emphasize it.

, get_posts, , "-created":

@staticmethod
def get_posts(limit, offset, tags_filter = []):
        posts = Post.all()
        for tag in tags_filter:
          if tag:
              posts.filter('tags', tag)
        posts.order("-created")
        return posts.fetch(limit = limit, offset = offset)

, .
, , , get_posts .

- // / ?

+5
4

, , . , , : , Entity.KEY_RESERVED_PROPERTY_key_ .

, :

class Post(db.Model):
    title = db.StringProperty(required = True)
    link = db.LinkProperty(required = True)
    description = db.StringProperty(required = True)
    tags = db.ListProperty(str)
    created = db.DateTimeProperty(required = True, auto_now_add = True)

    @classmethod
    def create(*args, **kw):
         kw.update(dict(key_name=inverse_millisecond_str() + disambig_chars()))
         return Post(*args, **kw)

...

def inverse_microsecond_str(): #gives string of 8 characters from ascii 23 to 'z' which sorts in reverse temporal order
    t = datetime.datetime.now()
    inv_us = int(1e16 - (time.mktime(t.timetuple()) * 1e6 + t.microsecond)) #no y2k for >100 yrs
    base_100_chars = []
    while inv_us:
        digit, inv_us = inv_us % 100, inv_us / 100
        base_100_str = [chr(23 + digit)] + base_100_chars
    return "".join(base_100_chars)

, .

:

  • , "create" .
  • .
  • , ; base-100 .
  • 100% - . disambig_chars , , 10 , 1/100 000. , - , 1/1000. , disambig_chars; 100% - , , , , , save().
+3

, ? .

class Tag(db.Model):
  tag = db.StringProperty()
  posts = db.ListProperty(db.Key, indexed=False)

, tags = Tag.all().filter('tag IN', ['python','blog','async'])

3 , , . post_union = set(tags[0].posts).intersection(tags[1].posts, tags[2].posts), , .

, ( ). Posts.all().filter('__key__ IN', post_union).order("-created")

: , , .

: @Yasser , IN- < 30 .

. , , Posts.get(sorted_posts).

, / .

Edit2: , .

+3

:

Robert Kluin , , " ", / Google.

# Model definitions
class Article(db.Model):
  title = db.StringProperty()
  content = db.StringProperty()

class TagIndex(db.Model):
  tags = db.StringListProperty()

# Tags are child entities of Articles
article1 = Article(title="foo", content="foo content")
article1.put()
TagIndex(parent=article1, tags=["hop"]).put()

# Get all articles for a given tag
tags = db.GqlQuery("SELECT __key__ FROM Tag where tags = :1", "hop")
keys = (t.parent() for t in tags)
articles = db.get(keys)

, , Article key_name

StringListProperty Robert Kluin Wooble #appengine IRC.

+2

:

| StringProperty . tag_filter, StringProperty . , AND, OR, , .

EDIT: , , , .

EDIT: , Post , . b1, b2, b3 .. , , . blog = b1, python = b2, async = b3 . , - True.

, tag_filter, , .

Post.all().filter("b1",True).filter("b2",True).order('-created')

python blog.

0

All Articles