def _UpdateEntities(kind, master, bot, start_cursor=None): """Update `internal_only` flags of `kind` entities to match their Bot. """ datastore_hooks.SetPrivilegedRequest() internal_only = graph_data.Bot.GetInternalOnlySync(master, bot) query = ndb.Query(kind=kind).filter( ndb.GenericProperty('master_name') == master, ndb.GenericProperty('bot_name') == bot, ndb.GenericProperty('internal_only') == (not internal_only)) entities, next_cursor, more = query.fetch_page(PAGE_LIMIT, start_cursor=start_cursor) for entity in entities: entity.internal_only = internal_only ndb.put_multi(entities) logging.info('updated %d entities', len(entities)) if more and next_cursor: logging.info('continuing') deferred.defer(_UpdateEntities, kind, master, bot, next_cursor, _queue=QUEUE_NAME) else: logging.info('complete')
def get(self, **kwargs): check = self.request.get('check', default_value=None) uName = self.request.get('username', default_value=None) pWord = self.request.get('password', default_value=None) #Checks for JSON if 'application/json' not in self.request.accept: self.response.status = 406 self.response.status_message = "Error, this API requires JSON objects" return #Writes the contents of that user id to the page if 'id' in kwargs: out = ndb.Key(db.User, int(kwargs['id'])).get().to_dict() self.response.write(json.dumps(out)) if check: q = db.User.query(ndb.GenericProperty('password') == pWord) #q = db.User.filter(ndb.GenericProperty('username') == uName) result = q.get(keys_only=True) self.response.write(result.id()) #otherwise, if no id is specified, all users will be listed else: q = db.User.query(ndb.GenericProperty('password') == pWord) #q = db.User.query(ndb.GenericProperty('username') == uName) result = q.get(keys_only=True) out = ndb.Key(db.User, result.id()).get().to_dict() self.response.write(json.dumps(out))
def filter_queryset(self): """ Default queryset filter. List all model properties and compare to query params. All intersected attributes are applied ad filters on the queryset """ if not self.query_params: return self.queryset _model = get_model_class_from_query(self.queryset) fields = get_model_fields(_model) queryset = self.get_queryset() for field_name, field_class in fields: if field_name in self.query_params: filter_value = self.query_params[field_name] if field_class == KeyProperty: _key = get_key_from_urlsafe(filter_value) queryset = queryset.filter( ndb.GenericProperty(field_name) == _key) else: """ We use the property serializer to be able to make the filter with the good type. For instance if we want to filter an IntergerProperty, the self.query_params, will have only string values. We need to use the serializer to cast the param to the good type. """ filter_value = TYPE_MAPPING[field_class.__name__]( data=filter_value).data queryset = queryset.filter( ndb.GenericProperty(field_name) == filter_value) return queryset
def post(self): form_usu = cgi.escape(self.request.get('usuario')) form_clave = cgi.escape(self.request.get('password')) usuarios = Usuario.query().filter( ndb.GenericProperty('usuario') == form_usu).fetch(1) if usuarios: for element in usuarios: if element.usuario == form_usu: n_hash = MD5.new(form_clave).hexdigest() if element.clave == n_hash: val_template = {'url': '', 'nombre': ''} list_val = [] dict_permisos = {'permisos': []} permisos = Profile.query().filter( ndb.GenericProperty('usuario') == form_usu).fetch( 1) if permisos: for permiso in permisos: if permiso.operaciones == True: val_template = {'url': '', 'nombre': ''} val_template[ 'url'] = '/operaciones?usuario=' + form_usu val_template['nombre'] = 'operaciones' list_val.append(val_template) if permiso.reportes == True: val_template = {'url': '', 'nombre': ''} val_template[ 'url'] = '/reportes?usuario=' + form_usu val_template['nombre'] = 'reportes' list_val.append(val_template) if permiso.inventario == True: val_template = {'url': '', 'nombre': ''} val_template[ 'url'] = '/inventario?usuario=' + form_usu val_template['nombre'] = 'inventario' list_val.append(val_template) logging.info('lista: %s', list_val) dict_permisos['permisos'] = list_val template = jinja_environment.get_template( 'pagina0.html') self.response.out.write(template.render(dict_permisos)) #self.response.out.write(template.render(c)) else: self.printmensaje('index.html', 'Clave incorrecta') else: self.printmensaje('index.html', 'Usuario no existe') else: self.printmensaje('index.html', 'Usuario no existe')
def __order_query(order_json, query): for order in order_json: order_direction = order['direction'] order_field = order['field'] if order_direction == 'ASC': query = query.order(ndb.GenericProperty(order_field)) else: query = query.order(-ndb.GenericProperty(order_field)) return query
class MyAudit(ndb.Model): # when created_time=ndb.DateTimeProperty(auto_now_add=True) # by whome owner=ndb.KeyProperty(kind='Contact') # field name field_name=ndb.StringProperty(required=True) # old value old_value=ndb.GenericProperty() # new value new_value=ndb.GenericProperty()
def get(self): user_email = self.request.get('email').lower() logging.info("AddUserAsFriend "+user_email) user = User.query(ndb.GenericProperty('email_address') == user_email).get() if not user: user = User.query(ndb.GenericProperty('email_address') == user_email).get() if user.get_id() == self.user_id: self.response.out.write("EMAIL TO SELF") return if user: InviteInternal.add_invite(self.user_id, user.key.id()) self.response.out.write("FOUND") else: self.response.out.write("NOT FOUND")
class Stream(ndb.Model): """A main model for representing an individual Stream entry.""" author = ndb.StructuredProperty(Author) name = ndb.StringProperty() subscribers = ndb.GenericProperty(repeated=True) cover_url = ndb.StringProperty(indexed=False) tags = ndb.GenericProperty(repeated=True) view_count = ndb.IntegerProperty(indexed=False) last_date = ndb.DateProperty() photos = ndb.StructuredProperty(Photo, repeated=True) photos_original = ndb.StructuredProperty(Photo, repeated=True) photos2 = ndb.StructuredProperty(Photo, repeated=True) view_history = ndb.DateTimeProperty(repeated=True) history_len = ndb.ComputedProperty(lambda self: len(self.view_history)) create_time = ndb.DateTimeProperty(auto_now_add=True)
def _getSessionQuery(self, request, ancestor=None, required_fields=[]): """Return formatted query from the submitted filters.""" if ancestor: q = Session.query(ancestor=ancestor) else: q = Session.query() inequality_filter, filters = self._formatFilters( request.filters, SESSION_FIELDS, OPERATORS) # all required_fields must appear in filters field included_fields = [f['field'] for f in filters] missing_fields = [ rf for rf in required_fields if rf not in included_fields ] if missing_fields: raise endpoints.BadRequestException("Session '%s' field required" % "', '".join(missing_fields)) # If exists, sort on inequality filter first if inequality_filter: q = q.order(ndb.GenericProperty(inequality_filter)) q = q.order(Session.name) for filtr in filters: if filtr["field"] in ["duration"]: filtr["value"] = int(filtr["value"]) elif filtr["field"] in ["highlights"]: filtr["value"] = str(filtr["value"]).lower() == 'true' formatted_query = ndb.query.FilterNode(filtr["field"], filtr["operator"], filtr["value"]) q = q.filter(formatted_query) return q
def GetTestDescendantsAsync(test_key, has_rows=None, deprecated=None, keys_only=True): """Returns all the tests which are subtests of the test with the given key. Args: test_key: The key of the TestMetadata entity to get descendants of. has_rows: If set, filter the query for this value of has_rows. deprecated: If set, filter the query for this value of deprecated. Returns: A future for a list of keys of all descendants of the given test. """ test_parts = utils.TestPath(test_key).split('/') query_parts = [ ('master_name', test_parts[0]), ('bot_name', test_parts[1]), ('suite_name', test_parts[2]), ] for index, part in enumerate(test_parts[3:]): query_parts.append(('test_part%d_name' % (index + 1), part)) query = graph_data.TestMetadata.query() for part in query_parts: query = query.filter(ndb.GenericProperty(part[0]) == part[1]) if has_rows is not None: query = query.filter(graph_data.TestMetadata.has_rows == has_rows) if deprecated is not None: query = query.filter(graph_data.TestMetadata.deprecated == deprecated) futures = query.fetch_async(keys_only=keys_only) return futures
def get(self, source_short_name, id): if source_short_name == 'googleplus': # turned off in march 2019: https://github.com/snarfed/bridgy/issues/846 self.source = None self.response.status_int = 404 return super(UserHandler, self).get() cls = models.sources[source_short_name] self.source = cls.lookup(id) if not self.source: id = urllib.unquote(id).decode('utf-8') key = cls.query( ndb.OR(*[ ndb.GenericProperty(prop) == id for prop in 'domains', 'inferred_username', 'name', 'username' ])).get(keys_only=True) if key: return self.redirect(cls(key=key).bridgy_path(), permanent=True) if self.source and self.source.features: self.source.verify() self.source = self.preprocess_source(self.source) else: self.response.status_int = 404 super(UserHandler, self).get()
def get(self): dNow = datetime.now() sNM = dNow.month sNY = dNow.year sNum = (sNY - BASE_SESSION_YEAR) * 3 + mapYear(sNM) + 1 candidate_query = Feedback.query( ndb.GenericProperty('cycle') == int(sNum)) candidates = candidate_query.fetch(keys_only=True) cycles = [] for c in candidates: if c.parent() not in cycles: cycles.append(c.parent()) candidates = cycles interviewer_query = Interviewer.query().order(Interviewer.last_name) interviewers = interviewer_query.fetch() template_values = { 'candidates': candidates, 'interviewers': interviewers, } template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(template_values))
def _getQuerySession(self, request): """Return formatted query from the submitted filters.""" q = Session.query() inequality_filter, filters = self._formatFilters(request.filters, type='Session') # If exists, sort on inequality filter first if not inequality_filter: q = q.order(Session.name) else: q = q.order(ndb.GenericProperty(inequality_filter)) q = q.order(Session.name) for filtr in filters: if filtr["field"] in ["duration"]: filtr["value"] = int(filtr["value"]) elif filtr["field"] in ["startTime"]: filtr["value"] = datetime.datetime.strptime( "1970-01-01" + ' ' + filtr['value'][:], "%Y-%m-%d %H:%M") formatted_query = ndb.query.FilterNode(filtr["field"], filtr["operator"], filtr["value"]) q = q.filter(formatted_query) return q
def post(self): usuario = cgi.escape(self.request.get('login_usuario')) password = cgi.escape(self.request.get('login_password')) if (len(usuario) > 0) and (len(password) > 0): usuarios = Usuario.query().filter(ndb.GenericProperty('usuario') == usuario).fetch(1) if usuarios: for element in usuarios: if element.usuario == usuario: n_hash = MD5.new(password).hexdigest() if element.clave == n_hash: if element.rol == 'rol02' or element.rol == 'rol03': #ojo al llenado al reves ckb1 = cgi.escape(self.request.get('ckb3')) ckb2 = cgi.escape(self.request.get('ckb2')) ckb3 = cgi.escape(self.request.get('ckb1')) valopciones = {'val1':str(ckb1), 'val2': str(ckb2), 'val3': str(ckb3) } opciones = self.setnewopciones(valopciones) if self.updateprofile(usuario, opciones): self.printmensaje('t_adm_respuesta_rol.html','Actualizacion correcta') else: self.printmensaje('t_adm_respuesta_rol.html','Existen problemas para actualizar los datos') else: self.printmensaje(destino_login,'su usuario no tiene permisos en esta seccion') else: self.printmensaje(destino_login,'clave incorrecta') else: self.printmensaje(destino_login,'usuario no registrado') else: self.printmensaje(destino_login,'usuario no existe')
def _getSessionQuery(self, request, ancestor=None): """Return formatted query from the submitted filter""" if not ancestor: q = Session.query() else: a_key = ancestor q = Session.query(ancestor=ndb.Key(urlsafe=a_key)) inequality_filter, filters = self._formatFilters(request.filters) # If exists, sort on inequality filter first if not inequality_filter: q = q.order(Session.name) else: q = q.order(ndb.GenericProperty(inequality_filter)) q = q.order(Session.name) for filtr in filters: if filtr["field"] in ["duration"]: filtr["value"] = int(filtr["value"]) formatted_query = ndb.query.FilterNode(filtr["field"], filtr["operator"], filtr["value"]) q = q.filter(formatted_query) return q
class Invitation(ndb.Model): invitor = ndb.KeyProperty(kind=Student) invitee = ndb.KeyProperty(kind=Student) assignment_number = ndb.IntegerProperty() active = ndb.GenericProperty() accepted = ndb.BooleanProperty(default=False) created = ndb.DateTimeProperty(auto_now_add=True)
def get(self, id, pair_no): ''' Returns the opaque pair ID for pair with pair nubmer pair_no in this Tournament. Args: id: Tournament ID for this tournament. pair_no: The pair number whose ID has to be looked up. ''' if not is_int(pair_no): SetErrorStatus( self.response, 404, "Invalid Pair Number", "Pair number must be an integer, was {}".format(pair_no)) return tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id) if not tourney: return if not CheckUserOwnsTournamentAndMaybeReturnStatus( self.response, users.get_current_user(), tourney): return player_pairs = PlayerPair._query( ndb.GenericProperty('pair_no') == int(pair_no), ancestor=tourney.key).fetch(1, projection=[PlayerPair.id]) if not player_pairs: SetErrorStatus( self.response, 404, "Invalid Id", "Pair pair number {} does not exist in this " + "tournament".format(pair_no)) return self.response.headers['Content-Type'] = 'application/json' self.response.set_status(200) self.response.out.write( json.dumps({'pair_id': player_pairs[0].id}, indent=2))
def search_for_members(order=True, keys_only=False, verified_only=False, pagelen=0, page=0, search=None): """ Return all member objects """ args = [] if verified_only: args = [ndb.GenericProperty('verified') == True] if search: args.append(ndb.StringProperty('local_email_address') == search) # print('\n\n{0}\n\n'.format(*args)) if order: member_query = Member.lquery(*args).order(Member.lower_name) else: member_query = Member.lquery(*args) if pagelen == 0: members = member_query.fetch(keys_only=keys_only) else: members = member_query.fetch(keys_only=keys_only, offset=page * pagelen, limit=pagelen) return members
def get_query(self, filters, order_by_field='name', websafe_conference_key=None): """Return formatted query from the submitted filters.""" if websafe_conference_key: query = self.kind.query( self.kind.websafeConferenceKey == websafe_conference_key) else: query = self.kind.query() inequality_filter, filters = self.format_filters(filters) # If exists, sort on inequality filter first if not inequality_filter: query = query.order(getattr(self.kind, order_by_field)) else: query = query.order(ndb.GenericProperty(inequality_filter)) query = query.order(getattr(self.kind, order_by_field)) for filtr in filters: if filtr["field"] in self.int_values: filtr["value"] = int(filtr["value"]) if filtr["field"] in self.date_values: filtr["value"] = datetime.strptime(filtr["value"], "%Y-%m-%d").date() formatted_query = ndb.query.FilterNode(filtr["field"], filtr["operator"], filtr["value"]) query = query.filter(formatted_query) return query
def admin_exists(cls): ''' Returns True if an admin user already exists in the DB, else False. ''' admin = User.query().filter( ndb.GenericProperty('is_admin') == 'YES').get() return admin is not None
def _getSessionQuery(self, request): """Return formatted query from the submitted filters.""" q = Session.query() inequality_filter, filters = self._formatFilters(request.filters, SESSION_FIELDS) # If exists, sort on inequality filter first if not inequality_filter: q = q.order(Session.name) else: q = q.order(ndb.GenericProperty(inequality_filter)) q = q.order(Session.name) for filtr in filters: if filtr["field"] == "startTime": testTime = datetime.strptime(filtr["value"], "%H:%M").time() # filtr["value"] = datetime.strptime(filtr["value"], "%H:%M").time() # formatted_query = ndb.query.FilterNode(Session.startTime._to_base_type, filtr["operator"], testTime) if filtr["operator"] == '=': q = q.filter(Session.startTime == testTime) elif filtr["operator"] == '>=': q = q.filter(Session.startTime >= testTime) elif filtr["operator"] == '<=': q = q.filter( ndb.AND(Session.startTime <= testTime), (Session.startTime >= datetime.strptime("00:00", "%H:%M").time())) else: raise endpoints.BadRequestException("Unsupported filter for time.") else: formatted_query = ndb.query.FilterNode(filtr["field"], filtr["operator"], filtr["value"]) q = q.filter(formatted_query) return q
def list_devices( cls, enrolled=True, keys_only=False, page_size=100, next_cursor=None, **kwargs): """Returns a list of devices using given filters. Args: enrolled: bool, set True if only active devices should be queried, else False if only inactive. keys_only: bool, set True if only device keys should be returned. page_size: int, the number of devices to query for. next_cursor: datastore_query.Cursor, set when next page of results need to be queried. **kwargs: in which each kwarg name is the name of a Device property by which to filter the query, and its value is the filter value (str, int, etc.). Returns: A tuple consisting of a list of device keys, Cursor, and a bool. """ # pylint: disable=g-explicit-bool-comparison query = cls.query(cls.enrolled == enrolled) # pylint: enable=g-explicit-bool-comparison for filters, filter_values in kwargs.items(): if not isinstance(filter_values, (list, tuple)): filter_values = (filter_values,) for value in filter_values: query = query.filter(ndb.GenericProperty(filters) == value) return query.fetch_page( keys_only=keys_only, page_size=page_size, start_cursor=next_cursor)
def get(self, **kwargs): uName = self.request.get('username', default_value=None) pWord = self.request.get('password', default_value=None) #Checks for JSON if 'application/json' not in self.request.accept: self.response.status = 406 self.response.status_message = "Error, this API requires JSON objects" return #Displays the contents of that score if an id is passed if 'id' in kwargs: out = ndb.Key(db.Score, int(kwargs['id'])).get().to_dict() self.response.write(json.dumps(out)) if uName: q = db.Score.query(ndb.GenericProperty('password') == pWord) #q = db.Score.query(ndb.GenericProperty('username') == uName) result = q.get(keys_only=True) self.response.write(result.id()) #Otherwise just displays the keys for all the scores else: q = db.Score.query() keys = q.fetch(keys_only=True) results = {'keys': [x.id() for x in keys]} self.response.write(json.dumps(results))
def post(self): term = self.request.get('content') valid_input = func.valid_input(term) # self.response.write("%s is a valid term: %s <br>" % (term, valid_input)) is_url = func.is_url(term) if is_url: # Write to see what crawl_page returns #self.response.write(func.crawl_page(term)) crawl = func.crawl_page(term) if crawl: url_term = WordLink() url_term.author = users.get_current_user() url_term.words = json.dumps(list(crawl)) url_term.link = term url_term.put() self.response.write("%s is saved successfully!" % term) else: self.response.write("The URL %s was not searchable. \ Try again!" % term) else: # It's a Query url_query = WordLink.query() url_words = url_query.filter( ndb.GenericProperty('author') == users.get_current_user()) #fetch() #self.response.write("%s <br><br>" % term) self.response.write("%s <br>" % term) for entry in url_words: for word in json.loads(entry.words): if word == str(term.lower().strip()): self.response.write("<a href=%s>%s</a><br>" % (entry.link, entry.link)) break
def list(cls, question_type=None, enabled=True, page_size=100, next_cursor=None, **kwargs): """List all questions with filters. Args: question_type: QuestionType, The type of question to be queried for. enabled: bool, True to retrieve enabled surveys. page_size: int, The number of answers to return. next_cursor: datastore_query.Cursor, Next page of results. **kwargs: each kwarg name is the name of a Question property by which to filter the query and its value (string, integer, boolean, etc.). Returns: A tuple of a list of Question instances, datastore_query.Cursor instance, and a boolean representing whether or not there are more to retrieve. """ query = cls.query( cls.enabled == enabled, # pylint: disable=g-explicit-bool-comparison ndb.AND(cls.question_type == question_type)) for filters, values in kwargs.items(): if not isinstance(values, (list, tuple)): values = (values, ) for value in values: query = query.filter(ndb.GenericProperty(filters) == value) return query.fetch_page(page_size=page_size, start_cursor=next_cursor)
def get_stats(cls, cursor_key=None, limit=None, year=None, topic=None, sort_by=None, **kw): if topic: return RoshReviewUserTopicStats.get_stats(topic, cursor_key=cursor_key, limit=limit, year=year, **kw) limit = limit if limit else 20 sort_by = sort_by if sort_by else 'performance' cursor = Cursor(urlsafe=cursor_key) if cursor_key else None q = cls.query() if year: q = q.filter(cls.year == year) q = q.order(-ndb.GenericProperty(sort_by)) stats, cursor, _ = q.fetch_page(limit, start_cursor=cursor, **kw) return stats, (cursor.urlsafe() if cursor else None),
def __get_only_fields(json): projections = [] if 'fields' in json: for field in json['fields']: projections.append((ndb.GenericProperty(field))) return projections
def clean_up_verified(): """ find members that have not been verified for some reason """ """ first, find people who have seen the welcome message but are not verified. Weird. """ # member_query = Member.query(ndb.AND(Member.seen_welcome == True, ndb.GenericProperty('verified')==False)) # # members = member_query.fetch() # logging.info('fixed verified for {0} members'.format(len(members))) # # for m in members: # m.verified = True # # ndb.put_multi(members) """ now, find any members who have made plans but are not verified. really weird. """ member_query = Member.query(ndb.GenericProperty('verified')==False) members = member_query.fetch() set = [] for m in members: plan_query = plan.Plan.lquery(plan.Plan.member==m.key) plans = plan_query.fetch(keys_only=True) if plans: m.verified=True set.append(m) logging.info("found an unverified with plans! {0}".format(m.name)) if set: ndb.put_multi(set)
def _getQuery(self, request): """Return formatted query from the submitted filters.""" q = Conference.query() inequality_filter, filters = self._formatFilters(request.filters) # If exists, sort on inequality filter first if not inequality_filter: q = q.order(Conference.name) else: q = q.order(ndb.GenericProperty(inequality_filter)) q = q.order(Conference.name) for filtr in filters: if filtr["field"] in ["month", "maxAttendees"]: filtr["value"] = int(filtr["value"]) formatted_query = ndb.query.FilterNode(filtr["field"], filtr["operator"], filtr["value"]) q = q.filter(formatted_query) return q
def generateQuery(filters): q = DBObject.query() for f in filters: parts = f.split('::') q = q.filter(ndb.GenericProperty(parts[0]) == parts[1]) #q = q.order(-DBObject.date) # We can't do this because only simple queries are allowed on Expandos return q