Esempio n. 1
0
def createSignal(request):

# Do something for authenticated users.

	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 = SignalForm(request.POST)

		# All validation rules pass

		if form.is_valid():

			# Aqui inserto

			newSignal = Signal()

			newSignal.name = form.cleaned_data['name']
			newSignal.description = form.cleaned_data['description']
			newSignal.signalTags = form.cleaned_data['signalTags']
			newSignal.tagProposal = form.cleaned_data['tagSuggest']
			#newSignal.private = form.cleaned_data['private']
			newSignal.private = False
			newSignal.owner = request.user

			# Actualización de karma..

			if extra != None:
				extra.karmaCounter += KARMA_PER_CREATION
				extra.save()

			newSignal.save()

			# Insercion en solr

			solrutils.index_signal(newSignal.id,newSignal.name,newSignal.description,newSignal.owner.username,datetime.datetime.now(),newSignal.signalTags)

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

	return render_to_response("createSignal.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)