def get(self, collection): print "Get waypoints" parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) parser.add_argument('size', type=int, default=10) parser.add_argument('noTotal', type=ArgumentValidator.create('boolTrue'),default=False) parser.add_argument('newer', default=None) parser.add_argument('offset', type=int, default=None) # newer date - offset (in seconds) #parser.add_argument('only_approved',default=True) #parser.add_argument('collection',default='') #parser.add_argument('toplevel',default='') #parser.add_argument('count_greater',type=int,default=0) args = parser.parse_args() args.total = not args.noTotal #try: #toplevel_key=ndb.Key(urlsafe=args.toplevel) #except: #toplevel_key=None #try: #collection_key=ndb.Key(urlsafe=args.collection) #except: #return make_bad_request_exception("Wrong collection key") query = model.WayPoint.qry(collection=g.col_key,compare_date='>modified',date=args.newer, time_offset=args.offset) wps_future = query.fetch_page_async(args.size, start_cursor=args.cursor) if args.total: total_count_future = query.count_async(keys_only=True) wps, next_cursor, more = wps_future.get_result() wps = [u.to_dict(include=model.WayPoint.get_public_properties()) for u in wps] if args.total: total_count = total_count_future.get_result() else: total_count = None return make_list_response(wps, next_cursor, more, total_count)
def get(self): print "Get tags" parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) parser.add_argument('size', type=int, default=10) parser.add_argument('total', type=ArgumentValidator.create('boolTrue'),default=True) parser.add_argument('only_approved',default=True) parser.add_argument('collection',default='') parser.add_argument('toplevel',default='') parser.add_argument('count_greater',type=int,default=0) args = parser.parse_args() try: toplevel_key=ndb.Key(urlsafe=args.toplevel) except: toplevel_key=None try: collection_key=ndb.Key(urlsafe=args.collection) except: collection_key=None query = model.Tag.qry(toplevel=toplevel_key, collection=collection_key, only_approved=args.only_approved, count_greater=args.count_greater) tags_future = query.fetch_page_async(args.size, start_cursor=args.cursor) if args.total: total_count_future = query.count_async(keys_only=True) tags, next_cursor, more = tags_future.get_result() tags = [u.to_dict(include=model.Tag.get_public_properties()) for u in tags] #print next_cursor if args.total: total_count = total_count_future.get_result() else: total_count = None return make_list_response(tags, next_cursor, more, total_count)
def get(self, username): parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) parser.add_argument('q', type=str,default='') parser.add_argument('size', type=int,default=10) args = parser.parse_args() # current user key user_key = g.user_db.key if auth.is_admin() or user_db.key == auth.current_user_key(): query = model.CollectionUser.qry(user=user_key) collection_keys_future = query.fetch_async(limit=args.size*4,keys_only=True) # do something else here? collection_keys = collection_keys_future.get_result() collection_keys = [key.parent() for key in collection_keys] # Search for users in this collections users = []; for key in collection_keys: query = model.CollectionUser.query(ancestor=key) if len(args.q) > 2: query = query \ .filter(model.CollectionUser.user.username >= args.q) \ .filter(model.CollectionUser.user.username <= unicode(args.q) + u"\ufffd")\ .order(model.CollectionUser.user.username) else: query = query.order(model.CollectionUser.modified) users_future = query.fetch_async( limit=args.size, projection=['modified','user.username','user.email','user.avatar_url','user_key']) users = users + users_future.get_result() #users = users + [{'key':u.key,'modified':u.modified} for u in users_future.get_result()] # check if a user with this name or email exists: user_db = model.User.get_by_email_or_username(args.q) if len(args.q)>2 else None if user_db: # create temp user with modified now -> first in list user = model.CollectionUser( user_key= user_db.key, user = model.User(username = user_db.username, email = user_db.email, avatar_url = user_db.avatar_url), name = "Just Temp User", modified = datetime.datetime.now(), created = datetime.datetime.now() ) users.append(user) # sort users after its last modification users = util.sort_uniq(users,'modified','user_key',reverse=False) #users = [u.urlsafe() for u in users] users = [u.to_dict(include=['modified','user','user_key']) for u in users] total = len(users) if total > args.size: users = users[:args.size] return make_list_response(users, None, total > args.size, total)
def get(self): parser = reqparse.RequestParser() parser.add_argument("cursor", type=ArgumentValidator.create("cursor")) args = parser.parse_args() users_future = User.query().order(-User.created).fetch_page_async(10, start_cursor=args.cursor) total_count_future = User.query().count_async(keys_only=True) users, next_cursor, more = users_future.get_result() users = [u.to_dict(include=User.get_public_properties()) for u in users] return make_list_response(users, next_cursor, more, total_count_future.get_result())
def post(self): """Sends feedback email to admin""" if not config.CONFIG_DB.feedback_email: return abort(418) parser = reqparse.RequestParser() parser.add_argument('message', type=ArgumentValidator.create('feedback'), required=True) parser.add_argument('email', type=UserValidator.create('email', required=False)) args = parser.parse_args() body = '%s\n\n%s' % (args.message, args.email) kwargs = {'reply_to': args.email} if args.email else {} task.send_mail_notification('%s...' % body[:48].strip(), body, **kwargs) return make_empty_ok_response()
def inject_validators(): """Injects 'validators' variable into jinja template, so it can be passed into angular. See base.html Model validators are passed to angular so it can be used for frontend input validation as well This prevents code repetition, as we e.g we change property of UserValidator.name to [5, 20] and the same validation of user's name (length between 5-20 characters) will be performed in frontend as well as in backend """ return { 'validators': { 'arg': ArgumentValidator.to_dict(), 'user': UserValidator.to_dict() } }
def get(self): parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) args = parser.parse_args() positions_future = Position.query() \ .order(-Position.created) \ .fetch_page_async(10, start_cursor=args.cursor) total_count_future = Position.query().count_async(keys_only=True) positions, next_cursor, more = positions_future.get_result() positions = [u.to_dict(include=Position.get_public_properties()) for u in positions] return make_list_response(positions, next_cursor, more, total_count_future.get_result())
def get(self): parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) args = parser.parse_args() items_future = Hackathon.query() \ .order(-Hackathon.modified) \ .fetch_page_async(10, start_cursor=args.cursor) total_count_future = Hackathon.query().count_async(keys_only=True) items, next_cursor, more = items_future.get_result() items = [u.to_dict(include=Hackathon.get_public_properties()) for u in items] return make_list_response(items, next_cursor, more, total_count_future.get_result())
def get(self): parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) parser.add_argument('page') parser.add_argument('keyword') parser.add_argument('location') args = parser.parse_args() keyw = args.get('keyword') loc = args.get('location') page = args.get('page') if not keyw: keyw = "" if not loc: loc = "" if not page: page=0 else: page=int(page) positions_future = Position.query() \ .order(-Position.created) \ .fetch_page_async(10000, start_cursor=args.get('cursor')) total_count_future = Position.query().count_async(keys_only=True) positions, next_cursor, more = positions_future.get_result() positions = [p.to_dict(include=Position.get_public_properties()) for p in positions] pos = [] pos = [elem for elem in positions if (elem["title"].find(keyw) != -1 or elem["description"].find(keyw) != -1) and elem["location"].find(loc) != -1] ipage=10 until=((page+1)*ipage) if until > len(pos): until=len(pos) npos = pos[page*ipage:until] pages=math.ceil(len(pos)/float(ipage)) # for x in range(0,len(positions)): # if positions[x]["title"].find(keyw) != -1: # pos.append(positions[x]) return make_new_list_response(npos, pages, len(npos), len(pos), page*ipage)
def get(self): parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) parser.add_argument('description') parser.add_argument('location') args = parser.parse_args() positions_future = Position.query() \ .order(-Position.created) \ .fetch_page_async(10, start_cursor=args.cursor) total_count_future = Position.query().count_async(keys_only=True) positions, next_cursor, more = positions_future.get_result() positions = [p.to_json() for p in positions] positions = [p for p in positions if args.description.lower() in p["description"].lower()] positions = [p for p in positions if args.location.lower() in p["location"].lower()] return make_list_response(positions, next_cursor, more, total_count_future.get_result())
def get(self, key): parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) args = parser.parse_args() user_id = g.model_db.id() if auth.current_user_db().id() != user_id: # not allowed to access other registrations return ApiException.error(403) items_future = Hackathon.query(Hackathon.user_id==user_id) \ .order(-Hackathon.modified) \ .fetch_page_async(10, start_cursor=args.cursor) total_count_future = Hackathon.query(Hackathon.user_id==user_id).count_async(keys_only=True) items, next_cursor, more = items_future.get_result() items = [u.to_dict(include=Hackathon.get_public_properties()) for u in items] return make_list_response(items, next_cursor, more, total_count_future.get_result())
def get(self, collection,query): parser = reqparse.RequestParser() parser.add_argument('cursor', type=ArgumentValidator.create('cursor')) #parser.add_argument('q', type=str,default='') parser.add_argument('size', type=int,default=30) parser.add_argument('only_names', type=ArgumentValidator.create('boolTrue'),default=False) args = parser.parse_args() tags = [] try: collection = ndb.Key(urlsafe=collection) except: collection = model.Collection.top_key() # Check if the user has read permission for the collection TODO if auth.is_admin(): # check if name is already a full tag key = model.Tag.tag_to_key(query,collection) try: db = key.get() except: key = model.Tag.tag_to_key(query) try: db = key.get() except: db = False if db: dbs_related, more = db.related(char_limit=200) #tags = [db.name] + db.related() tags = [r.related_to for r in dbs_related] tags = [db.name] + tags else: gae_qry = model.Tag.query(model.Tag.collection == collection) if len(query) > 1: gae_qry = gae_qry \ .filter(model.Tag.name >= query) \ .filter(model.Tag.name <= unicode(query) + u"\ufffd")\ .order(model.Tag.name) else: gae_qry = gae_qry.order(model.Tag.cnt) tags_future = gae_qry.fetch_async( limit=args.size, projection=['name']) tags = tags_future.get_result() tags = [r.name for r in tags] rel_tags = [] for tag in tags: key = model.Tag.tag_to_key(tag,collection) try: db = key.get() if db: dbs_related, more = db.related(char_limit=100) rel_tags = [r.related_to for r in dbs_related] tags = tags+rel_tags #[:5] except: pass if len(tags) > args.size+5: break #tags = list(set(tags)) tags = list(OrderedDict.fromkeys(tags)) total = len(tags) tags = tags[:args.size] if not args.only_names: tags = model.Tag.get_tag_infos(tags,collection) return make_list_response(tags, False, total > len(tags), total)
def get(self): parser = default_parser() parser.add_argument('expenses', type=ArgumentValidator.create('boolTrue'),default=False) parser.add_argument('travelers', type=ArgumentValidator.create('boolTrue'),default=False) parser.add_argument('locations', type=ArgumentValidator.create('boolTrue'),default=False) args = parser.parse_args() compare ,date = to_compare_date(args.newer, args.older, args.orderBy) query = model.Trip.qry(order_by_date=args.orderBy, \ compare_date = compare, date = date, time_offset=args.offset, \ ancestor=auth.current_user_key()) dbs_future = query.fetch_page_async(args.size, start_cursor=args.cursor) # Count how many results are available (only of prameter total is given!) total_count_future = query.count_async(keys_only=True) if args.total else False # Get result, this is needed to do the other things. dbs, next_cursor, more = dbs_future.get_result() # Create already a dictionary, this is used to add data instead of keys dbs = [db.to_dict(include=model.Trip.get_public_properties()) for db in dbs] # get locations async (only if not only keys are requested) if (args.locations): locs_future = [] for db in dbs: if db['locations']: locs_future.append(ndb.get_multi_async([ndb.Key(urlsafe=key) for key in db['locations']])) else: locs_future.append(None) i=0 for loc_future in locs_future: if loc_future: # is a locatio given? locs = [f.get_result() for f in loc_future] locs = [db.to_dict(include=model.Location.get_public_properties()) for db in locs] u=0 if (args.expenses or args.travelers or True): for loc in locs: for trans in ['trans_start','trans_end']: wayPt = locs[u][trans].get('waypoints',[]) locs[u][trans]['geo'] = wayPt[-1] if len(wayPt) > 0 else None if args.travelers: locs[u]['fellow_travelers'] = [ndb.Key(urlsafe=db).get().to_dict(include= model.FellowTraveler.get_public_properties()) for db in loc['fellow_travelers']] if args.expenses: #print "Get expenses" #print loc['expenses'] locs[u]['expenses'] = [{"amount": e['amount'] if 'amount' in e else 0, "note": e['note'] if 'note' in e else "", "type":ndb.Key(urlsafe=e['type']).get(). to_dict(include=model.ExpenseType.get_public_properties()) if 'type' in e and e['type'] else None} for e in loc['expenses']] u+=1 dbs[i]['locations'] = locs else: # empty locations dbs[i]['locations'] = [] i += 1 #dbs = [db.to_dict(include=model.Trip.get_public_properties()) for db in dbs] total_count = total_count_future.get_result() if total_count_future else False return make_list_response(dbs, next_cursor, more, total_count)