Example #1
0
 def _process(self):
     q = request.args['q'].lower()
     query = (Category.query.filter(Category.title_matches(q)).options(
         undefer('deep_children_count'), undefer('deep_events_count'),
         undefer('has_events'), undefer('has_children'),
         joinedload('acl_entries')))
     if session.user:
         # Prefer favorite categories
         query = query.order_by(
             Category.favorite_of.any(favorite_category_table.c.user_id ==
                                      session.user.id).desc())
     # Prefer exact matches and matches at the beginning, then order by category title and if
     # those are identical by the chain titles
     query = (query.order_by(
         (db.func.lower(Category.title) == q).desc(),
         db.func.lower(Category.title).startswith(q).desc(),
         db.func.lower(Category.title), Category.chain_titles))
     total_count = query.count()
     query = query.limit(10)
     return jsonify_data(categories=[
         serialize_category(c, with_favorite=True, with_path=True)
         for c in query
     ],
                         total_count=total_count,
                         flash=False)
Example #2
0
 def _process(self):
     q = request.args["q"].lower()
     query = Category.query.filter(Category.title_matches(q)).options(
         undefer("deep_children_count"),
         undefer("deep_events_count"),
         undefer("has_events"),
         joinedload("acl_entries"),
     )
     if session.user:
         # Prefer favorite categories
         query = query.order_by(
             Category.favorite_of.any(favorite_category_table.c.user_id == session.user.id).desc()
         )
     # Prefer exact matches and matches at the beginning, then order by category title and if
     # those are identical by the chain titles
     query = query.order_by(
         (db.func.lower(Category.title) == q).desc(),
         db.func.lower(Category.title).startswith(q).desc(),
         db.func.lower(Category.title),
         Category.chain_titles,
     )
     total_count = query.count()
     query = query.limit(10)
     return jsonify_data(
         categories=[serialize_category(c, with_favorite=True, with_path=True) for c in query],
         total_count=total_count,
         flash=False,
     )