예제 #1
0
def mobileAddPoint(request,signalId):

	mapId = settings.MAP_ID
	apiKey = settings.API_KEY
	mapNearZoom = settings.MAP_NEAR_ZOOM


	mySignal = get_object_or_404(Signal,pk=signalId)

	try:
		extra = UserExtra.objects.get(user=request.user)
	except:
		extra = None

	# If the form has been submitted...

	if request.method == 'POST':

		# A form bound to the POST data

		form = mobilePointForm(request.POST, request.FILES)

		# All validation rules pass

		if form.is_valid():
			valores = {}
			valores['lat'] = form.cleaned_data['lat']
			valores['long'] = form.cleaned_data['long']
			valores['texto'] = form.cleaned_data['texto']
			valores['signal'] = form.cleaned_data['signal']

			# Aqui inserto

			newPoint = DataPoint()

			newPoint.lat = form.cleaned_data['lat']
			newPoint.long = form.cleaned_data['long']
			newPoint.auxText = form.cleaned_data['texto']
			newPoint.user = request.user
			newPoint.signal_id = int(form.cleaned_data['signal'])
			newPoint.pointTags = form.cleaned_data['pointTags']

			#if request.FILES.get('fileUrl',default=None) is not None:
			if 'imageUrl' in request.FILES:
				newPoint.imageUrl = request.FILES['imageUrl']

			# Actualizo los contadores y karma de usuario y señal

			# Karma del usuario

			if extra != None:
				extra.karmaCounter += KARMA_PER_INPUT

				# Karma de la señal, de momento lo incremento con el TOTAL del karmaCounter del usuario

				newPoint.signal.karmaCounter += signalKarmaFromUser(extra.karmaCounter)

				extra.save()

			# numero de puntos de la señal

			newPoint.signal.numPoints += 1
			newPoint.signal.numPointsNow += 1

			# Guardo la señal y el punto...

			newPoint.signal.save()

			newPoint.save()

			# inserto el punto en el solr

			solrutils.index_point(newPoint.id,newPoint.signal.id,newPoint.signal.name,newPoint.user.username,newPoint.auxText,datetime.datetime.now(),newPoint.lat,newPoint.long,newPoint.pointTags)

		return render_to_response("thanksPoint.html",locals(),context_instance=RequestContext(request))
	else:
		form = mobilePointForm() # An unbound form

	return render_to_response("mobileAddPoint.html",locals(),context_instance=RequestContext(request))
def bulk_import(data):

    signals_solr = sunburnt.SolrInterface(SIGNALS_SOLR_URL)
    points_solr = sunburnt.SolrInterface(POINTS_SOLR_URL)

    for sig in data:
        error_flag=0
        print "VOY con señal : %s" % sig['name'] if "name" in sig.keys() else sys.exit(0)
        error_flag=0
        signal = Signal()
        signal.name=sig['name']
        signal.owner=User.objects.get(id=sig['user_id'])
        signal.description=sig['description'] if 'description' in sig.keys() else ""
        signal.tagProposal=sig['tagProposal'] if 'tagProposal' in sig.keys() else ""
        signal.private=sig['private'] if 'private' in sig.keys() else 0
        signal.token=sig['token'] if 'token' in sig.keys() else ""
        signal.timestamp=sig['timestamp'] if 'timestamp' in sig.keys() else datetime.datetime.now()

        signal.numPoints=0
        signal.numPointsNow=0
        signal.karmaCounter=0
        signal.votesDown=0
        signal.votesUp=0

        try:
            signal.save()

        except:
            print "Error insertando signal : %s" % sig['name']
            error_flag=1

        #INSERTO EN SOLR
        if error_flag==0:
            document = {}
            document['id'] = signal.id
            document['name'] = signal.name
            document['text'] = signal.description
            document['owner'] = signal.owner
            document['datetime'] = signal.timestamp

            document['tags'] = tags_regex.findall(document['text'])
            try:
                signals_solr.add(document)
                signals_solr.commit()
            except:
                print "Error indexando signal %s" % signal.name
                error_flag=2

        if error_flag==0:
            data_points_count=0
            dp_ids=list()
            data_points = sig['data_points'] if "data_points" in sig.keys() else list()
            for data_point in data_points:

                dp = DataPoint()

                #OBLIGATORIOS
                dp.signal=signal
                dp.user=User.objects.filter(id=data_point['user_id'])[0]
                dp.lat=data_point['lat']

                dp.long=data_point['long']
                dp.auxText=data_point['auxText']

                #OPCIONALES/RELLENABLES
                dp.imageUrl=data_point['imageUrl'] if 'imageUrl' in data_point.keys() else ""
                dp.timestamp=data_point['timestamp'] if 'timestamp' in data_point.keys() else datetime.datetime.now()
                dp.votesUp=0
                dp.votesDown=0
                dp.karmaCounter=0


                try:

                    dp.save()
                    data_points_count+=1
                    dp_ids.append(dp.id)
                except:
                    print "Error insertando datapoint: %s-%s-%s" % (dp.lat,dp.long,dp.timestamp)


            for dp_id in dp_ids:
                aux_dp=DataPoint.objects.get(id=dp_id)
                point_document={}
                point_document['id']=aux_dp.id
                point_document['user']=aux_dp.user
                #point_document['user_id']=""
                point_document['signal_id']=signal.id
                point_document['signal_name']=signal.name
                point_document['text']=aux_dp.auxText
                point_document['pos_geo']="%s,%s" % (aux_dp.lat,aux_dp.long)
                point_document['datetime']=aux_dp.timestamp

                try:
                    points_solr.add(point_document)
                    points_solr.commit()
                except:
                    print "Error indexando punto: %s-%s-%s" % (aux_dp.lat,aux_dp.long,aux_dp.timestamp)


            signal.numPoints=data_points_count
            signal.numPointsNow=data_points_count
            try:
                signal.save()
            except:
                print "Error actualizando contador de points en señal %s\nNumPoints:%s" % (signal.name,data_points_counter)