Esempio n. 1
0
	def get(self):
		timestamp = self.request.get('timestamp')
                horas = self.request.get('horas')
                self.response.headers['Content-Type'] = 'text/plain'
                if not timestamp or not horas or timestamp == None or horas == None or timestamp == '' or horas == '':
                        errordict = {'error': -1, 'message': 'Must specify variables in GET method (i.e. /changecontrol?timestamp=<YYYYMMDDHH24>&horas=<int>)'}
                        self.response.out.write(json.dumps(errordict))
                elif len(timestamp) != 10:
                        errordict = {'error': -2, 'message': 'timestamp must be 10 chars long: YYYYMMDDHH24'}
                        self.response.out.write(json.dumps(errordict))
                else:
                        try:
                                timestamp = datetime.strptime(timestamp,'%Y%m%d%H')
                                horas = int(horas)
                                timestampend = timestamp + timedelta(hours = horas)
                        except ValueError:
                                errordict = {'error': -2, 'message': 'Value Error. Timestamp must be YYYYMMDDHH24 and horas is an integer'}
                                self.response.out.write(json.dumps(errordict))
                        if horas > 24:
                                errordict = {'error': -2, 'message': 'Horas must be <= 24'}
                                self.response.out.write(json.dumps(errordict))
                        else:
                                #timestamp += timedelta(hours = 5)
                                #timestampend += timedelta(hours = 5)
				changes = ChangeControl.all().filter("FechaHora >=", timestamp).filter("FechaHora <=", timestampend)
				changeslist = []
				for change in changes:
					changesdict = {'fecha': str(change.FechaHora), 'id': change.Id, 'modelo': change.Kind, 'tipo': change.Status}
					changeslist.append(changesdict)
				self.response.out.write(json.dumps(changeslist))
Esempio n. 2
0
	def token(self):
		token = self.request.get('token')
		if token and str(token) == 'ZWJmbWV4LXB1YnIeCxISX0FoQWRtaW5Yc3JmVG9rZW5fIgZfWFNSRl8M':
			try:
				gminutes = self.request.get('minutes')
				ghours = self.request.get('hours')
				gdays = self.request.get('days')
				if not gminutes:
					gminutes = 0
				else:
					gminutes = int(gminutes)
				if not ghours:
					ghours = 0
				else:
					ghours = int(ghours)
				if not gdays:
					gdays = 0
				else:
					gdays = int(gdays)
			except ValueError:
				gminutes = 30
				ghours = 0
				gdays = 0
			time = datetime.now() - timedelta(days = gdays, hours = ghours, minutes = gminutes)
			self.response.headers['Content-Type'] = 'application/json'

			#self.response.out.write (str(time))
			changecontrol = ChangeControl.all().filter("FechaHora >=", time).filter("Kind =", 'Oferta').filter("Status IN", ["A","M"])
			nbadded = 0
			nbremoved = 0
			for cc in changecontrol:
				#self.response.out.write(cc.Id + '\n')
				if cc.Status != 'B':
					ofertas = Oferta.all().filter("IdOft =", cc.Id)
					for oferta in ofertas:
						if cc.Status == 'M':
							searchdata = SearchData.all().filter("Sid =", str(oferta.key()))
							for sd in searchdata:
								db.delete(sd)
								nbremoved += 1
						desc = oferta.Descripcion.replace('\n',' ').replace('\r',' ').replace('.',' ').replace(',',' ').split(' ')
						nombre = oferta.Oferta.replace('.',' ').replace(',',' ').split(' ')
						for palabra in desc:
							if len(palabra) > 3:
								newsd = SearchData()
								newsd.Enlinea = oferta.Enlinea
								newsd.FechaHora = datetime.now() - timedelta(hours = H)
								newsd.Field = 'Descripcion'
								newsd.IdCat = oferta.IdCat
								newsd.Kind = 'Oferta'
								newsd.Sid = str(oferta.key())
								newsd.Value = palabra.lower()
								newsd.put()
								nbadded += 1
						for palabra in nombre:
							if len(palabra) > 3:
		                                                newsd = SearchData()
       			                                        newsd.Enlinea = oferta.Enlinea
		                                                newsd.FechaHora = datetime.now() - timedelta(hours = H)
		                                                newsd.Field = 'Oferta'
		                                                newsd.IdCat = oferta.IdCat
		                                                newsd.Kind = 'Oferta'
		                                                newsd.Sid = str(oferta.key())
		                                                newsd.Value = palabra.lower()
		                                                newsd.put()
								nbadded += 1
						palabraclave = OfertaPalabra.all().filter("IdOft =", oferta.IdOft)
						for palabra in palabraclave.Palabra:
							if len(palabra) > 3:
								newsd = SearchData()
		                                                newsd.Enlinea = oferta.Enlinea
		                                                newsd.FechaHora = datetime.now() - timedelta(hours = H)
		                                                newsd.Field = 'OfertaPalabra'
		                                                newsd.IdCat = oferta.IdCat
		                                                newsd.Kind = 'Oferta'
		                                                newsd.Sid = str(oferta.key())
		                                                newsd.Value = palabra.Palabra.lower()
		                                                newsd.put()
								nbadded += 1

			logging.info("Finished updating. Added: %s. Removed: %s.", str(nbadded), str(nbremoved))
		else:
			logging.error('Wrong token given.')